2

I have 100 groups with 40 observations each. I know there might be other appropriate models but i am currently only interested in the following.

reg<-lmList(Y ~ Intercept + a + b + c + d | grp,data=data, pool=F)

In order to save the residuals (from lmList) to my table I just do

data$residual <- residuals(reg)

Now I want to save the beta coefficients to the original table as well. Since each group only has a single set of coefficients it should be same for the group but different across groups.

  • coef <- coef(reg) gives me a list with the group coefficients. However R shows this as a list with only 5 columns (excluding the group names).
  • data$coef<-coef[,c(1) ] gives me the intercepts but I lose the group information.

I was thinking about creating a separate table with group identification so that I just merge the original table and the coefficient tables. But could not figure out how to get the group identification along with the coefficients.

If there is an easier way to do this please help.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
oblixram
  • 23
  • 3
  • 1
    reproducible example would help http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Bulat May 25 '16 at 22:16

1 Answers1

0

If fm1 is your fitted lmList object then I think simply making the row names into an additional column should do what you want:

library(nlme)
fm1 <- lmList(distance ~ age | Subject, Orthodont)
res <- data.frame(Subject=rownames(coef(fm1)),coef(fm1),check.names=FALSE)
rownames(res) <- NULL ## now redundant
head(res)
  Subject (Intercept)   age
1 M16           16.95 0.550
2 M05           13.65 0.850
...

Then you should be able to merge() (although merging with the Orthodont object seems problematic - I think this may be because it's a weird groupedData object ...)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453