如何提高R中使用for循环获得的lm输出?
我做了一个for循环,以便使用相同的Y和不同的X来评估多个模型。 使用mtcars数据库,我创建了两个子集:如何提高R中使用for循环获得的lm输出?,r,for-loop,linear-regression,lm,R,For Loop,Linear Regression,Lm,我做了一个for循环,以便使用相同的Y和不同的X来评估多个模型。 使用mtcars数据库,我创建了两个子集: mcars1 <- subset(mtcars, select=c("mpg","hp","disp")) mcars2 <- subset(mtcars, select=c("mpg","disp")) 正如你所看到的,一切都很好。我只想知道我可以使用哪些函数来改进输出,例如: Model 1 Estimate Std. Error t
mcars1 <- subset(mtcars, select=c("mpg","hp","disp"))
mcars2 <- subset(mtcars, select=c("mpg","disp"))
正如你所看到的,一切都很好。我只想知道我可以使用哪些函数来改进输出,例如:
Model 1
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.73590425 1.331566129 23.082522 3.262507e-20***
hp -0.02484008 0.013385499 -1.855746 7.367905e-02***
disp -0.03034628 0.007404856 -4.098159 3.062678e-04***
Model 2
Estimate Std. Error t value Pr(>|t|)
(Intercept) 29.59985476 1.229719515 24.070411 3.576586e-21***
disp -0.04121512 0.004711833 -8.747152 9.380327e-10***
任何建议都将不胜感激
lapply(list(mcars1,mcars2),function(x)coef(summary(lm(x))))
[[1]]
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.73590425 1.331566129 23.082522 3.262507e-20
hp -0.02484008 0.013385499 -1.855746 7.367905e-02
disp -0.03034628 0.007404856 -4.098159 3.062678e-04
[[2]]
Estimate Std. Error t value Pr(>|t|)
(Intercept) 29.59985476 1.229719515 24.070411 3.576586e-21
disp -0.04121512 0.004711833 -8.747152 9.380327e-10
或者你可以:
lapply(list(mcars1,mcars2),function(x)coef(summary(lm(mpg~.,dat=x))))
是否将结果存储在列表中?e、 g.结果[[var]]
lapply(list(mcars1,mcars2),function(x)coef(summary(lm(x))))
[[1]]
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.73590425 1.331566129 23.082522 3.262507e-20
hp -0.02484008 0.013385499 -1.855746 7.367905e-02
disp -0.03034628 0.007404856 -4.098159 3.062678e-04
[[2]]
Estimate Std. Error t value Pr(>|t|)
(Intercept) 29.59985476 1.229719515 24.070411 3.576586e-21
disp -0.04121512 0.004711833 -8.747152 9.380327e-10
lapply(list(mcars1,mcars2),function(x)coef(summary(lm(mpg~.,dat=x))))