从 R 对象中提取信息并将其导入模型摘要 table

Extracting information from R objects and importing it to a modelsummary table

我正在尝试使用 modelsummary 包制作统计模型摘要 table。

我运行 6个回归。我对其中两个进行了联合假设检验,其中包括某些变量(具体来说,educexper)作为解释变量。我想在 table 中包含这些测试的 F 统计数据,但无法从测试中提取信息并将其导入 table。

这里有更多详细信息。

(1) 我做了什么,第 1 部分:包、数据和分析

library(tidyverse)
library(wooldridge)   # Data
library(estimatr)     # Estimation
library(car)          # F test
library(modelsummary) # Table
library(kableExtra)   # Table

wage2 <- wage2 %>% 
  mutate(lwage = log(wage))

model1 <- lm_robust(lwage ~ educ, 
                    data = wage2, se_type = "HC1")
model2 <- lm_robust(lwage ~ exper, 
                    data = wage2, se_type = "HC1")
model3 <- lm_robust(lwage ~ educ + exper, 
                    data = wage2, se_type = "HC1")
F3 <- linearHypothesis(model3, test = "F", 
                       c("educ = 0", "exper = 0"))
model4 <- lm_robust(lwage ~ educ + tenure, 
                    data = wage2, se_type = "HC1")
model5 <- lm_robust(lwage ~ exper + tenure, 
                    data = wage2, se_type = "HC1")
model6 <- lm_robust(lwage ~ educ + exper + tenure, 
                    data = wage2, se_type = "HC1")
F6 <- linearHypothesis(model6, test = "F", 
                       c("educ = 0", "exper = 0"))

联合假设检验结果如下

F3
## Linear hypothesis test
## 
## Hypothesis:
## educ = 0
## exper = 0
## 
## Model 1: restricted model
## Model 2: lwage ~ educ + exper
## 
##   Res.Df Df      F    Pr(>F)    
## 1    934                        
## 2    932  2 67.715 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
F6
## Linear hypothesis test
## 
## Hypothesis:
## educ = 0
## exper = 0
## 
## Model 1: restricted model
## Model 2: lwage ~ educ + exper + tenure
## 
##   Res.Df Df      F    Pr(>F)    
## 1    933                        
## 2    931  2 63.592 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(2) 我做了什么,第 2 部分:Table

regs <- list()
regs[['Model 1']] <- model1
regs[['Model 2']] <- model2
regs[['Model 3']] <- model3
regs[['Model 4']] <- model4
regs[['Model 5']] <- model5
regs[['Model 6']] <- model6

gm <- tribble(
  ~raw, ~clean, ~fmt,
  "adj.r.squared", "$\bar{R}^2$", 3,
  "nobs", "Sample size", 0
  )

rows <- tribble(
  ~term, ~'Model 1', ~'Model 2', ~'Model 3', 
  ~'Model 4', ~'Model 5', ~'Model 6', 
  '$F$ statstics', '', '', '', '', '', '',
  '$H_0: \beta_{\rm{educ}} = 0, \beta_{\rm{exper}} = 0$', 
  '', '', '67.715***', '', '', '63.592***',
  '', '', '', '(0.000)', '', '', '(0.000)'
)
attr(rows, 'position') <- c(9:11)

table <- msummary(regs, 
         estimate = "{estimate}{stars}",
         gof_map = gm, 
         notes = list("*** p < 0.01, ** p < 0.05, * p < 0.1. 
                      Heteroskedasticity-consistent 
                      standard errors in brackets. 
                      P values in brackets for F statistics."),
         add_rows = rows) %>% 
  row_spec(c(1, 3, 5, 7, 9, 10), 
           extra_css = "border-bottom: hidden") 
table

上面的代码给了我一个很好的table。 (不幸的是,我似乎无法 post 因为我没有赢得足够的声望。)

(3) 我想知道的

上面我手动输入了rows中的F统计量、stars、p-values得到了我想要的table,但是我想看看有没有一种从 F3F6 中提取信息并将其导入到 table.

的方法

如果我没理解错的话,您想从模型中导出数据。直接喊出来:

F3$F

F3$`Pr(>F)`

如果我没理解错的话,你想要:

  1. 在 table 的底部添加两行:F 统计量和关联的 p 值
  2. 这些行应该只添加到您手动选择的某些模型中

实现此目的的最简单方法是使用您在示例中使用的 add_rows 参数。你的做法是正确的!

如果您想要更通用和自动化的策略,you can define a glance_custom.lm_robust method.

你的例子很复杂,所以我冒昧地将它简化为 Minimal Reproducible Example.你会注意到我创建并检查了一个新属性以确定哪些模型将在其中进行线性假设检验table.

(注意:回答你的问题让我发现了一个小错误,它混淆了 table 底部的行顺序。这个错误现在已经修复了。下面粘贴的 table 是使用modelsummary 0.9.2.9000开发版制作,调用安装即可:remotes::install_github("vincentarelbundock/modelsummary") )

library(modelsummary)
library(estimatr)
library(tibble)
library(car)

mod1 <- lm_robust(mpg ~ am + vs, mtcars)
mod2 <- lm_robust(mpg ~ am + vs + hp, mtcars)
mod3 <- lm_robust(mpg ~ am + vs + wt, mtcars)
mod4 <- lm_robust(mpg ~ am + vs + qsec, mtcars)

# models 2 and 4 will have a linear hypothesis test
attr(mod2, "FTEST") <- TRUE
attr(mod4, "FTEST") <- TRUE

glance_custom.lm_robust <- function(x) {
  # check if we should include the linear hypothesis test in this specific model
  if (!isTRUE(attr(x, "FTEST"))) return(NULL)

  # conduct linear hypothesis test
  ftest <- linearHypothesis(x, test = "F", c("am = 0", "vs = 0"))

  # return a 1-row tibble with each statistic in separate columns
  out <- tibble(
    "$H_0: \beta_{\rm{vs}} = 0, \beta_{\rm{am}} = 0$" = ftest[["F"]][2],
    "     " = sprintf("(%.3f)", ftest[["Pr(>F)"]][2]))
  return(out)
}

modelsummary(list(mod1, mod2, mod3, mod4), escape = FALSE)