【问题标题】:Combing regression output for by() into a single table将 by() 的回归输出合并到一个表中
【发布时间】:2021-06-11 22:38:07
【问题描述】:

我是 R、编码和 Stack Overflow 的新手:如果这是一个基本问题,请提前道歉。我正在尝试将变量“性别”的 3 个级别的回归输出组合到一个汇总表中,该汇总表保留了列中的所有信息以及值(残差、r2、调整后的 r2、F 统计量、 p 值)列在每个输出的底部。有人知道可行的方法吗?

这是我的输出当前的样子:

library(tidyverse)
Final_Frame.df <- read_csv("indirect.csv")

my.fun <- function(Final_Frame2.df){summary(lm(Product_Use~Mean_social_combined +
  Mean_traditional_time+
  Mean_Passive_Use_Updated+
  Mean_Active_Use_Updated, data=Final_Frame.df))}

by(Final_Frame.df, list(Final_Frame.df$Gender), my.fun)

输出

Call:
lm(formula = Product_Use ~ Mean_social_combined + Mean_traditional_time + 
    Mean_Passive_Use_Updated + Mean_Active_Use_Updated, data = Final_Frame.df)

Residuals:
    Min      1Q  Median      3Q     Max 
-26.592  -8.178  -3.936   6.228  62.258 

Coefficients:
                         Estimate Std. Error t value Pr(>|t|)    
(Intercept)               -0.5814     1.9664  -0.296 0.767612    
Mean_social_combined       2.4961     1.1797   2.116 0.034906 *  
Mean_traditional_time      1.0399     0.7416   1.402 0.161567    
Mean_Passive_Use_Updated   2.8230     0.8308   3.398 0.000739 ***
Mean_Active_Use_Updated    2.7562     1.7421   1.582 0.114329    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 12.07 on 451 degrees of freedom
  (18 observations deleted due to missingness)
Multiple R-squared:  0.1517,    Adjusted R-squared:  0.1442 
F-statistic: 20.17 on 4 and 451 DF,  p-value: 2.703e-15

--------------------------------------------------------------------------------------------- 
: 2

Call:
lm(formula = Product_Use ~ Mean_social_combined + Mean_traditional_time + 
    Mean_Passive_Use_Updated + Mean_Active_Use_Updated, data = Final_Frame.df)

Residuals:
    Min      1Q  Median      3Q     Max 
-26.592  -8.178  -3.936   6.228  62.258 

Coefficients:
                         Estimate Std. Error t value Pr(>|t|)    
(Intercept)               -0.5814     1.9664  -0.296 0.767612    
Mean_social_combined       2.4961     1.1797   2.116 0.034906 *  
Mean_traditional_time      1.0399     0.7416   1.402 0.161567    
Mean_Passive_Use_Updated   2.8230     0.8308   3.398 0.000739 ***
Mean_Active_Use_Updated    2.7562     1.7421   1.582 0.114329    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 12.07 on 451 degrees of freedom
  (18 observations deleted due to missingness)
Multiple R-squared:  0.1517,    Adjusted R-squared:  0.1442 
F-statistic: 20.17 on 4 and 451 DF,  p-value: 2.703e-15

--------------------------------------------------------------------------------------------- 
: 3

Call:
lm(formula = Product_Use ~ Mean_social_combined + Mean_traditional_time + 
    Mean_Passive_Use_Updated + Mean_Active_Use_Updated, data = Final_Frame.df)

Residuals:
    Min      1Q  Median      3Q     Max 
-26.592  -8.178  -3.936   6.228  62.258 

Coefficients:
                         Estimate Std. Error t value Pr(>|t|)    
(Intercept)               -0.5814     1.9664  -0.296 0.767612    
Mean_social_combined       2.4961     1.1797   2.116 0.034906 *  
Mean_traditional_time      1.0399     0.7416   1.402 0.161567    
Mean_Passive_Use_Updated   2.8230     0.8308   3.398 0.000739 ***
Mean_Active_Use_Updated    2.7562     1.7421   1.582 0.114329    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 12.07 on 451 degrees of freedom
  (18 observations deleted due to missingness)
Multiple R-squared:  0.1517,    Adjusted R-squared:  0.1442 
F-statistic: 20.17 on 4 and 451 DF,  p-value: 2.703e-15

【问题讨论】:

  • 看看ddply ,类似:ddply( Final_Frame.df, 'Gender', function(d) { create_the_data_frame_you_need() } ) # 在函数体中,您可以访问d,这是您的每个唯一值的数据的子集 Gender
  • 为了让我们帮助您,请编辑您的问题以包含reproducible example。例如,要生成最小数据集,您可以使用head()subset() 或索引。然后使用dput() 给我们一些可以立即放入R 的东西。另外,请确保您知道该怎么做when someone answers your question。更多信息可以在 StackOverflow 的help center 找到。谢谢!

标签: r merge output concatenation linear-regression


【解决方案1】:

1) broom 这将使用 broom 包中的 tidy and glance 生成系数数据框和另一个统计数据:

library(broom)
library(dplyr)

mtcars %>%
  group_by(cyl) %>%
  group_modify(~ tidy(lm(mpg ~ disp + hp, .))) %>%
  ungroup

mtcars %>%
  group_by(cyl) %>%
  group_modify(~ glance(lm(mpg ~ disp + hp, .))) %>%
  ungroup

2) 组合模型 虽然不等同,但可以创建单个模型。它确实会产生相同的系数。

summary(lm(mpg ~ factor(cyl)/(disp + hp) + 0, mtcars))

3) nlme 这也提供了一些相同的信息。 nlme 是 R 自带的,所以不需要安装,只需要使用如下库加载即可。

library(nlme)
summary(lmList(mpg ~ disp + hp | cyl, mtcars, pool = FALSE))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多