R,线性回归的自动循环,在不同的 DV 上使用相同的 IV 来存储系数
R, automated loop of linear regressions using same IVs on different DVs to store coefficients
Mtcars 有 11 个变量的 32 个观测值。假设 "mpg"、"drat"、"qsec" 是感兴趣的因变量。假设 "cyl" 和 "hp" 是模型类型 1 的自变量,"disp" 是模型类型 2 的自变量。我想自动化一些回归,但不能执行步骤 (2 ) 以下。
我想做什么?
在我的实际数据框上,我感兴趣的因变量比自变量多得多。
我想 运行 "lm" 或 "glm" 以下各项:
- mpg~cyl+hp,
- drat~cyl+hp,
- qsec~cyl+hp,
- mpg~disp,
- drat~disp,以及
- qsec~disp
这似乎是我目前最大的问题。我想制作一个新的数据框(估计),其中包含系数估计和 Pr(>|t| ), 例如(假设已填写),
IV DV mpg.Est mpg.Pr drat.Est drat.Pr qsec.Est qsec.Pr
cyl -2.26 0.00 .. .. .. ..
hp -0.02 0.21 .. .. .. ..
disp -0.04 0.00 .. .. .. ..
然后我想将列附加到 "estimates" 以描述每个 IV(cyl、hp、disp)的 Pr 值,例如(假设已填写),
IV stat mean.Pr median.Pr min.Pr max.Pr
cyl 0.03 0.02 0.00 0.18
hp .. .. .. ..
disp .. .. .. ..
尝试次数
##### Step (1)
## Make the formulae
## For scale, it would be great to use varlists here:
## dvvarlist <- c("mpg", "drat", "qsec")
## ivvarlist <- c("cyl + hp", "disp")
models <- lapply(paste(c("mpg", "mpg", "drat", "drat", "qsec", "qsec"),
c("cyl + hp", "disp"), sep = "~"), formula)
## Run the regressions
res.models <- lapply(models, FUN = function(x)
{summary(lm(formula = x, data = mtcars))})
##### Step (2)
## Spot the coefficients
coefficients(res.models[[1]])
## How to automate grab coefficients from all models?
## How to automate place coefficients in proper location in new dataframe?
##### Step (3)
## Append columns to "estimates"
## For scale, could again use dvvarlist <- c("mpg", "drat", "qsec")
estimates$mean.Pr <- rowMeans(estimates[ , c("mpg.Est", "drat.Est", "qsec.Est")])
相关链接?
- Linear Regression loop for each independent variable individually against dependent
- Loop linear regression and saving ALL coefficients
使用基数 R:
data("mtcars")
y=c("mpg","drat","qsec")
x=c("cyl+hp","disp")
A=Map(function(i,j)
summary(lm(as.formula(paste0(i,"~",j)),data=mtcars))$coef[,c(1,4)],
rep(y,each=length(x)),x)
B=do.call(cbind.data.frame,
tapply(A,rep(y,each=length(x)),
function(s){a=do.call(rbind,s);a[row.names(a)!="(Intercept)",]}))
B
drat.Estimate drat.Pr(>|t|) mpg.Estimate mpg.Pr(>|t|) qsec.Estimate qsec.Pr(>|t|)
cyl -0.318242238 5.528430e-05 -2.26469360 4.803752e-04 -0.005485698 0.981671077
hp 0.003401029 6.262861e-02 -0.01912170 2.125285e-01 -0.018339365 0.005865329
disp -0.003063904 5.282022e-06 -0.04121512 9.380327e-10 -0.006253039 0.013144036
我还不清楚第三步需要什么。我希望你能进一步详细说明。虽然我查看了您的代码,但您似乎在寻找系数的平均值、系数的中值等。我不知道您是否也在寻找概率的平均值、最大值等,但我只是计算它们以备不时之需:
C=split(data.frame(t(B)),rep(c("Estimate","Pr(>|t|)"),length(y)))
D=lapply(C,function(f)
matrix(mapply(function(i,j) i(j),
rep(c(mean,median,min,max),each=length(f)),f),length(f)))
cbind(B,do.call(cbind.data.frame,lapply(D,`colnames<-`,c("mean","median","min","max"))))
drat.Estimate drat.Pr(>|t|) mpg.Estimate mpg.Pr(>|t|) qsec.Estimate qsec.Pr(>|t|) Estimate.mean
cyl -0.318242238 5.528430e-05 -2.26469360 4.803752e-04 -0.005485698 0.981671077 -0.86280718
hp 0.003401029 6.262861e-02 -0.01912170 2.125285e-01 -0.018339365 0.005865329 -0.01135334
disp -0.003063904 5.282022e-06 -0.04121512 9.380327e-10 -0.006253039 0.013144036 -0.01684402
Estimate.median Estimate.min Estimate.max Pr(>|t|).mean Pr(>|t|).median Pr(>|t|).min Pr(>|t|).max
cyl -0.318242238 -2.26469360 -0.005485698 0.327402245 4.803752e-04 5.528430e-05 0.98167108
hp -0.018339365 -0.01912170 0.003401029 0.093674136 6.262861e-02 5.865329e-03 0.21252847
disp -0.006253039 -0.04121512 -0.003063904 0.004383106 5.282022e-06 9.380327e-10 0.01314404
我相信您可以将其转换为在一个屏幕上查看,而不是滚动 left/right。
如果这有助于让我们知道。谢谢
Mtcars 有 11 个变量的 32 个观测值。假设 "mpg"、"drat"、"qsec" 是感兴趣的因变量。假设 "cyl" 和 "hp" 是模型类型 1 的自变量,"disp" 是模型类型 2 的自变量。我想自动化一些回归,但不能执行步骤 (2 ) 以下。
我想做什么?
在我的实际数据框上,我感兴趣的因变量比自变量多得多。
我想 运行 "lm" 或 "glm" 以下各项:
- mpg~cyl+hp,
- drat~cyl+hp,
- qsec~cyl+hp,
- mpg~disp,
- drat~disp,以及
- qsec~disp
这似乎是我目前最大的问题。我想制作一个新的数据框(估计),其中包含系数估计和 Pr(>|t| ), 例如(假设已填写),
IV DV mpg.Est mpg.Pr drat.Est drat.Pr qsec.Est qsec.Pr cyl -2.26 0.00 .. .. .. .. hp -0.02 0.21 .. .. .. .. disp -0.04 0.00 .. .. .. ..
然后我想将列附加到 "estimates" 以描述每个 IV(cyl、hp、disp)的 Pr 值,例如(假设已填写),
IV stat mean.Pr median.Pr min.Pr max.Pr cyl 0.03 0.02 0.00 0.18 hp .. .. .. .. disp .. .. .. ..
尝试次数
##### Step (1)
## Make the formulae
## For scale, it would be great to use varlists here:
## dvvarlist <- c("mpg", "drat", "qsec")
## ivvarlist <- c("cyl + hp", "disp")
models <- lapply(paste(c("mpg", "mpg", "drat", "drat", "qsec", "qsec"),
c("cyl + hp", "disp"), sep = "~"), formula)
## Run the regressions
res.models <- lapply(models, FUN = function(x)
{summary(lm(formula = x, data = mtcars))})
##### Step (2)
## Spot the coefficients
coefficients(res.models[[1]])
## How to automate grab coefficients from all models?
## How to automate place coefficients in proper location in new dataframe?
##### Step (3)
## Append columns to "estimates"
## For scale, could again use dvvarlist <- c("mpg", "drat", "qsec")
estimates$mean.Pr <- rowMeans(estimates[ , c("mpg.Est", "drat.Est", "qsec.Est")])
相关链接?
- Linear Regression loop for each independent variable individually against dependent
- Loop linear regression and saving ALL coefficients
使用基数 R:
data("mtcars")
y=c("mpg","drat","qsec")
x=c("cyl+hp","disp")
A=Map(function(i,j)
summary(lm(as.formula(paste0(i,"~",j)),data=mtcars))$coef[,c(1,4)],
rep(y,each=length(x)),x)
B=do.call(cbind.data.frame,
tapply(A,rep(y,each=length(x)),
function(s){a=do.call(rbind,s);a[row.names(a)!="(Intercept)",]}))
B
drat.Estimate drat.Pr(>|t|) mpg.Estimate mpg.Pr(>|t|) qsec.Estimate qsec.Pr(>|t|)
cyl -0.318242238 5.528430e-05 -2.26469360 4.803752e-04 -0.005485698 0.981671077
hp 0.003401029 6.262861e-02 -0.01912170 2.125285e-01 -0.018339365 0.005865329
disp -0.003063904 5.282022e-06 -0.04121512 9.380327e-10 -0.006253039 0.013144036
我还不清楚第三步需要什么。我希望你能进一步详细说明。虽然我查看了您的代码,但您似乎在寻找系数的平均值、系数的中值等。我不知道您是否也在寻找概率的平均值、最大值等,但我只是计算它们以备不时之需:
C=split(data.frame(t(B)),rep(c("Estimate","Pr(>|t|)"),length(y)))
D=lapply(C,function(f)
matrix(mapply(function(i,j) i(j),
rep(c(mean,median,min,max),each=length(f)),f),length(f)))
cbind(B,do.call(cbind.data.frame,lapply(D,`colnames<-`,c("mean","median","min","max"))))
drat.Estimate drat.Pr(>|t|) mpg.Estimate mpg.Pr(>|t|) qsec.Estimate qsec.Pr(>|t|) Estimate.mean
cyl -0.318242238 5.528430e-05 -2.26469360 4.803752e-04 -0.005485698 0.981671077 -0.86280718
hp 0.003401029 6.262861e-02 -0.01912170 2.125285e-01 -0.018339365 0.005865329 -0.01135334
disp -0.003063904 5.282022e-06 -0.04121512 9.380327e-10 -0.006253039 0.013144036 -0.01684402
Estimate.median Estimate.min Estimate.max Pr(>|t|).mean Pr(>|t|).median Pr(>|t|).min Pr(>|t|).max
cyl -0.318242238 -2.26469360 -0.005485698 0.327402245 4.803752e-04 5.528430e-05 0.98167108
hp -0.018339365 -0.01912170 0.003401029 0.093674136 6.262861e-02 5.865329e-03 0.21252847
disp -0.006253039 -0.04121512 -0.003063904 0.004383106 5.282022e-06 9.380327e-10 0.01314404
我相信您可以将其转换为在一个屏幕上查看,而不是滚动 left/right。 如果这有助于让我们知道。谢谢