I performed multiple regression results in R and save them in the environment as a list of ncol(data)
, to display one of the regression results and it's summary
i use this command summary(lm_results[[1]])
, which prints the following
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
I want to print all the regression results in one command like for(i in 1:ncol(data)) Regress[i] <- summary(lm_results[[i]])
and also to be able to extract only all R-squared or Adj R-square values of all regression models (and format them in one dataframe) . How can i do that in R?
You can try any of these approaches (I have used some simulated data):
#Option 1
lapply(listofmodels,function(x)summary(x)[8])
Output:
$model1
$model1$r.squared
[1] 0.01382265
$model2
$model2$r.squared
[1] 0.9271098
Or:
#Option 2
lapply(listofmodels,function(x)summary(x)[['r.squared']])
Output:
$model1
[1] 0.01382265
$model2
[1] 0.9271098
Some data used:
#Data
listofmodels <- list(model1=lm(iris$Sepal.Length~iris$Sepal.Width),model2=lm(iris$Petal.Width~iris$Petal.Length))
We could tidy
or glance
the model output with broom
and extract the relevant component
library(broom)
library(purrr)
map_dfr(listofmodels, tidy)
To extract only the 'r.squared'
map_dfr(listofmodels, ~ glance(.x) %>%
select(r.squared))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.