[R] "fixed effects" transformation

Eduardo Leoni e.leoni at gmail.com
Thu Aug 24 04:55:51 CEST 2006


Hi -

I am doing an analysis using panel data methods, particularly what
economists call "fixed effects". It can easily be done in R through
the inclusion of factors in an lm formula. However, when the number of
groups is excessive (in my case 2000+) it is much more efficient to
demean the data by panel.

I created this function following Farnsworth
(http://cran.r-project.org/doc/contrib/Farnsworth-EconometricsInR.pdf)


demean <- function(x,index) {
  for (i in unique(index)) {
    for (j in 1:ncol(x)) {
      x.now <- x[index==i,j]
      x[index==i,j] <- x.now-mean(x.now,na.rm=TRUE)
    }
  }
  x
}

it is obvious that there must be a much much more efficient way to do
this, though. Any recommendations?

thanks,

-eduardo



More information about the R-help mailing list