【问题标题】:Extracting Coefficients, Std Errors, R2 etc from multiple regressions从多重回归中提取系数、标准误差、R2 等
【发布时间】:2018-05-15 19:40:29
【问题描述】:

我有以下回归模型;

models <- lapply(1:25, function(x) lm(Y_df[,x] ~ X1))

Y_df 数据框中的 25 列上运行 25 次回归。

其中一个输出可以显示为;

models[15] # Gives me the coefficients for model 15

Call:
lm(formula = Y_df[, x] ~ X1)

Coefficients:
(Intercept)         X1 
  0.1296812    1.0585835  

我可以将其存储在单独的 df 中。我遇到的问题是关于 Std。错误、R2、残差等。

我想将这些也存储到单独的数据框中。

我可以运行单个回归并提取摘要,就像正常的 R 回归输出一样。

ls_1 <- summary(models[[1]])
ls_1
ls_1$sigma

但是我希望直接从运行 25 次回归的代码行中获取值。

此代码有效

> (models[[15]]$coefficients)
  (Intercept)          X1 
-0.3643446787  1.0789369642

但是;这段代码没有。

> (models[[15]]$sigma)
NULL

我尝试了各种不同的组合来尝试提取这些结果,但没有成功。

以下内容完全符合我的要求。我曾希望有一种方法可以将单词 coef 替换为 Std ErrorR2 等,但这不起作用。

models <- lapply(1:25, function(x) lm(Y_df[,x] ~ X1))
# extract just coefficients
coefficients <- sapply(Y_df, coef)

理想情况下,我想存储上述模型中的Std Error

【问题讨论】:

  • 非常简单。您需要计算摘要才能获得额外的统计信息。
  • 不是重复的。我知道如何单独提取系数、Rsquared 等,但对于这个特定任务,我想提取 25 个回归的值并将它们存储在 df 中
  • 在你的 sapply 中使用summary(不要忘记simplify = FALSE),你应该可以使用sapply-方式获取摘要统计信息。
  • 嗨,Ben 我可以接受,我发现大多数搜索答案的新用户在阅读所有 cmets 之前很久就会检查答案。关于如何获得 lm 结果的相关但未链接的答案过多,IV 或 DV 的数量有所不同,实际上可以通过循环、应用系列或 purrr。由于某些细微差别,它们可能不是直接重复的,但它们都是相关的

标签: r linear-regression


【解决方案1】:

如果一个模型被命名为 mod,你可以用与系数相同的方式得到所有的残差:

mod$residuals

还有提取系数和残差的函数:

coef(mod)
resid(mod)

其他输出,你可以通过summary提取:

summary(mod)$coef[,"Std. Error"]  # standard errors
summary(mod)$r.squared            # r squared
summary(mod)$adj.r.squared        # adjusted r squared

因此,您可以为每个模型创建一个包含这些结果的列表:

outputList <- lapply(models, function(x){
  coefs <- coef(mod)
  stdErr <- summary(mod)$coef[,"Std. Error"]
  rsq <- summary(mod)$r.squared
  rsq_adj <- summary(mod)$adj.r.squared
  rsd <- resid(mod)
  list(coefs = coefs, 
       stdErr = stdErr, 
       rsq = rsq, 
       rsq_adj = rsq_adj, 
       rsd = rsd)
})

然后,您可以通过 outputList$mod1$rsq 访问 mod1 的 rsq,例如。

或者您可以为每个创建单独的数据框:

library(tidyverse)

# coefficients
coefs <- lapply(models, coef) %>%
  do.call(rbind, .) %>%
  as.data.frame() %>% # convert from matrix to dataframe
  rownames_to_column("model") # add original model name as a column in the dataframe

# standard errors
stdErr <- lapply(models, function(x){
  summary(mod)$coef[,"Std. Error"]
}) %>%
  do.call(rbind, .) %>%
  as.data.frame() %>% 
  rownames_to_column("model") 

# r squareds
rsq <- sapply(models, function(x){
  summary(mod)$r.squared
}) %>%
  as.data.frame() %>% 
  rownames_to_column("model")

# adjusted r squareds
rsq_adj <- sapply(models, function(x){
  summary(mod)$adj.r.squared
})%>%
  as.data.frame() %>% 
  rownames_to_column("model")

# residuals
rsd <- lapply(models, resid) %>%
  do.call(rbind, .) %>%
  as.data.frame() %>% 
  rownames_to_column("model") 

值得注意的是,如果您在 RStudio 中并将摘要分配给某个对象(即temp &lt;- summary(mod)),您可以键入对象的名称,然后键入“$”和所有其他对象的下拉列表可以从摘要中提取出来。

【讨论】:

  • 感谢您的评论,非常有用。
猜你喜欢
  • 2012-10-14
  • 2020-12-25
  • 2018-07-07
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多