[R] "fixed effects" transformation

From: Eduardo Leoni <e.leoni_at_gmail.com>
Date: Thu 24 Aug 2006 - 12:55:51 EST

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)

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



R-help@stat.math.ethz.ch 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. Received on Thu Aug 24 16:24:11 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 25 Aug 2006 - 02:23:12 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.