dear readers---I struggled with how to do nice fixed-effects regressions in large economic samples for a while. Eventually, I realized that nlme is not really what I needed (too complex), and all I really wanted is the plm package. so, I thought I would share a quick example.
################ sample code to show fixed-effects models in R # create a sample panel data set with firms and years set.seed(0) fm= as.factor( c(rep("A", 5), rep("B", 5), rep("C", 5), rep("D", 5) )) yr= as.factor( rep( c(1985,1986,1987,1988,1989), 4)) d= data.frame( fm, yr, y=rnorm(length(yr)), x=rnorm(length(yr))) # first, the non-specific way. slow. lots of memory. solid. no panel-data expertise needed print(summary(lm( y ~ x + as.factor(fm) -1, data=d))) print(summary(lm( y ~ x + as.factor(yr) -1, data=d))) print(summary(lm( y ~ x + as.factor(yr) + as.factor(fm) -1, data=d))) # second, the specific plm way. fast. additional functionality library(plm); ## also, there is an an excellent vignette("plm", package = "plm") pd= pdata.frame( d, index=c("fm", "yr") ) # perhaps try the "drop.index=TRUE" argument and look at your output print(summary(plm( y ~ x, data=pd, model="within", effect="individual" ))) ### effect="individual" is the default --- this is firm-fixed effects print(summary(plm( y ~ x, data=pd, model="within", effect="time" ))) ### this is year-fixed effects print(summary(plm( y ~ x, data=pd, model="within", effect="twoways" ))) ### this is both (I have not yet verified that the plm regressions avoid computations of the factors [i.e., that they do not build an X'X matrix that includes the number of fixed effects, but work through averaging], but I presume that they do. this is of course useful for very large panel data sets with many thousands of fixed effects.) and, thanks, Yves and Giovanni for writing plm(). /iaw ---- Ivo Welch (ivo.we...@brown.edu, ivo.we...@gmail.com) ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.