Convert factors to numbers

November 29, 2007 · Posted in R bloggers · Comments Off 
If you have a vector of factors it is easy to get the factor level; however, I always forget how to extract the factor value. I ran into the answer here.

> x<-factor(c(round(rnorm(10),2),"A","B",NA))
> x
[1] 1.61 1.12 1.26 0.09 -0.13 0.16 -0.03 -0.1 0.09 -0.47
[11] A B
Levels: -0.03 0.09 -0.1 -0.13 0.16 -0.47 1.12 1.26 1.61 A B
> as.numeric(x)
[1] 9 7 8 2 4 5 1 3 2 6 10 11 NA
> as.numeric(levels(x)[x])
[1] 1.61 1.12 1.26 0.09 -0.13 0.16 -0.03 -0.10 0.09 -0.47
[11] NA NA NA
Warning message:
NAs introduced by coercion



**UPDATE**
And another method mentioned in the comments (that I like better):

> as.numeric(as.character(x))

Preparing plots for publication

November 15, 2007 · Posted in R bloggers · Comments Off 
The plotting capabilities of R are excellent; however, when I am preparing a figure for publication, I often need to combine multiple plots or add objects (e.g., arrows or text) to an existing plot. While this can be accomplished in R, my patience for tweaking layout parameters tends to run out quickly. I searched around and found a nice solution using open source software (an example with Matlab plots is described here).


1) Create the desired figure in R and export as an eps file:



library(ggplot2)
postscript("myfig.eps",horizontal=FALSE, paper="special",height=10,width=10)
qplot(x,y,data=mydat)
dev.off()


2) Install the necessary software on your computer, and then convert the image file to one more easily edited.


#install software
sudo aptitude install pstoedit tgif

#clean up the eps file so it is more easily read
eps2eps myfig.eps myfig2.eps

#convert the eps file to a tgif object
pstoedit -f tgif myfig2.eps myfig2.obj


3) Edit the new file myfig2.obj in tgif.

Linear panel data models in R: The PLM package

November 10, 2007 · Posted in R bloggers · Comments Off 

The plm package for R lets you run a number of common panel data models, including

  • The fixed effects (or within) estimator
  • The random effects GLS estimator

It also allows for general GLS estimation, as well as GMM estimation, and includes a feature for heteroscedasticity consistent covariance estimation.

It’s very easy to use, it simply requires that you use a special type of dataframe that specifies which variable is the individual and which variable is the cluster/group (this is done using the pdata.frame) command. Once this is done, you can estimate models using the plm command and its options.

See the documentation (PDF) for more.