[英]multiple regression models results from a list in R
我在 R 中执行多重回归结果并将它们保存在环境中作为a list of ncol(data)
,以显示回归结果之一及其summary
我使用此命令summary(lm_results[[1]])
,它打印以下内容
Call:
lm(formula = fml, data = data)
Residuals:
Min 1Q Median 3Q Max
-4.1615 -0.9830 -0.3605 0.3508 4.5893
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.04464 0.91212 0.049 0.961506
X2 0.34424 0.08067 4.267 0.000464 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.975 on 18 degrees of freedom
Multiple R-squared: 0.5029, Adjusted R-squared: 0.4753
F-statistic: 18.21 on 1 and 18 DF, p-value: 0.0004637
我想在一个命令中打印所有回归结果,例如for(i in 1:ncol(data)) Regress[i] <- summary(lm_results[[i]])
并且还能够仅提取所有回归模型的所有 R-squared 或 Adj R-square 值(并将它们格式化为一个数据帧)。 我怎么能在 R 中做到这一点?
您可以尝试以下任何一种方法(我使用了一些模拟数据):
#Option 1
lapply(listofmodels,function(x)summary(x)[8])
输出:
$model1
$model1$r.squared
[1] 0.01382265
$model2
$model2$r.squared
[1] 0.9271098
或者:
#Option 2
lapply(listofmodels,function(x)summary(x)[['r.squared']])
输出:
$model1
[1] 0.01382265
$model2
[1] 0.9271098
使用的一些数据:
#Data
listofmodels <- list(model1=lm(iris$Sepal.Length~iris$Sepal.Width),model2=lm(iris$Petal.Width~iris$Petal.Length))
我们可以tidy
或glance
与模型输出broom
和提取相关成分
library(broom)
library(purrr)
map_dfr(listofmodels, tidy)
仅提取“r.squared”
map_dfr(listofmodels, ~ glance(.x) %>%
select(r.squared))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.