将 by() 的回归输出组合成单个 table

Combing regression output for by() into a single table

我是 R、编码和 Stack Overflow 的新手:如果这是一个基本问题,请提前致歉。我正在尝试将变量“性别”的 3 个级别的回归输出合并到一个摘要 table 中,该摘要保留列中的所有信息以及值(残差,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

1) broom 这将使用 broom 包中的 tidy 和 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))