[R] help with a loop (coefficients with lmList)

Gabor Grothendieck ggrothendieck at gmail.com
Mon Aug 10 01:05:42 CEST 2009


Your data has 2 points per regression for each year in industry 1 and
only one point per regression for the other industries so one would
expect many NAs:

> table(data[c("industry", "year")])
        year
industry 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
       1    2    2    2    2    2    2    2    2    2    2
       5    1    1    1    1    1    1    1    1    1    1
       7    1    1    1    1    1    1    1    1    1    1
       9    1    1    1    1    1    1    1    1    1    1


On Sun, Aug 9, 2009 at 6:45 PM, Cecilia Carmo<cecilia.carmo at ua.pt> wrote:
> Hi R-helpers.
>
> #I start with the reproducible example:
> firm<-c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10))
> year<-c(rep(1998:2007,5))
> industry<-c(rep(1,20),rep(5,10),rep(7,10),rep(9,10))
> X1<-rnorm(50)
> X2<-rnorm(50,mean=0.5,sd=0.1)
> Y<-rnorm(50,mean=0,sd=0.5)
> data<-data.frame(firm, industry,year,X1,X2,Y)
> data
>
> #I need to calculate for all the industries the following #coefficients and
> store it (lmList is from nlme package)
> b1 <- lmList(Y~X1+X2| year, na.action=na.omit, data, subset=industry==1)
> b2<-summary(b1)
> b3<-as.data.frame(b2$coefficients)
> b4<-round(b3,3)
> b4
>
> #Instead of changing the industry in the subset I want to do it at once, so
> I’ve thinking in doing  a loop. Before I created an array to store my
> coefficients:
>
> years<-c("1998","1999","2000","2001","2002","2003","2004","2005","2006","2007")
> industry<-sort(unique(data$industry))
> coef<-c("Estimate.(Intercept)","Std. Error.(Intercept)","t
> value.(Intercept)", "Pr(>|t|).(Intercept)", "Estimate.X1", "Std.
> Error.X1","t value.X1", "Pr(>|t|).X1", "Estimate.X2", "Std. Error.X2", "t
> value.X2", "Pr(>|t|).X2")
> coefs<-array("NaN",dim=c(10,12,4),dimnames=list(years,coef,industry))
> coefs
>
> #The loop that I’ve tried was:
> for (k in industry){
>        b1 <- lmList(Y~X1+X2| year,
> na.action=na.omit,data,subset=industry==k)
> b2<-summary(b1)
> b3<-as.data.frame(b2$coefficients)
> b4<-round(b3,3)
> coefs[ , , k]<-b4[ , ]
> }
>
> This doesn't work. The data in the reproducible example as many NaN but I
> think this is not the problem. The problem is the R language. Could anyone
> help me?
>
> Thanks in advance,
>
> Cecília (Universidade de Aveiro – Portugal)
>
> ______________________________________________
> R-help at 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.
>




More information about the R-help mailing list