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

Cecilia Carmo cecilia.carmo at ua.pt
Mon Aug 10 00:45:10 CEST 2009

```Hi R-helpers.

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?