簡體   English   中英

多元回歸模型來自 R 中的列表

[英]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))

我們可以tidyglance與模型輸出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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM