Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

r - Obtain standard errors of regression coefficients for an "mlm" object returned by `lm()`

I'd like to run 10 regressions against the same regressor, then pull all the standard errors without using a loop.

depVars <- as.matrix(data[,1:10]) # multiple dependent variables
regressor <- as.matrix([,11]) # independent variable
allModels <- lm(depVars ~ regressor) # multiple, single variable regressions

summary(allModels)[1] # Can "view" the standard error for 1st regression, but can't extract...

allModels is stored as an "mlm" object, which is really tough to work with. It'd be great if I could store a list of lm objects or a matrix with statistics of interest.

Again, the objective is to NOT use a loop. Here is a loop equivalent:

regressor <- as.matrix([,11]) # independent variable
for(i in 1:10) { 
  tempObject <- lm(data[,i] ~ regressor) # single regressions
  table1Data[i,1] <- summary(tempObject)$coefficients[2,2] # assign std error
  rm(tempObject)
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you put your data in long format it's very easy to get a bunch of regression results using lmList from the nlme or lme4 packages. The output is a list of regression results and the summary can give you a matrix of coefficients, just like you wanted.

library(lme4)

m <- lmList( y ~ x | group, data = dat)
summary(m)$coefficients

Those coefficients are in a simple 3 dimensional array so the standard errors are at [,2,2].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...