[R] Fixed Effects Estimations (in Panel Data)
    ivo welch 
    ivowel at gmail.com
       
    Tue May 25 00:24:00 CEST 2010
    
    
  
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.welch at brown.edu, ivo.welch at gmail.com)
    
    
More information about the R-help
mailing list