问题描述
我想针对同一个回归变量运行10个回归,然后不使用循环提取所有标准错误.
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
存储为"mlm"对象,这确实很难使用.如果我可以存储一个lm
对象列表或一个包含感兴趣的统计信息的矩阵,那就太好了.
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)
}
推荐答案
如果以长格式存储数据,则可以很容易地使用lmList
从nlme或lme4包中获得大量回归结果.输出是回归结果的列表,摘要可以为您提供一个系数矩阵,就像您想要的一样.
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
这些系数位于简单的3维数组中,因此标准误差在[,2,2]
处.
Those coefficients are in a simple 3 dimensional array so the standard errors are at [,2,2]
.
这篇关于获得针对"mlm"的回归系数的标准误差. lm()返回的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!