0

这是上一个问题的后续问题:Extracting information from R objects and importing it to a modelssummary table

我想知道如何将 F 统计量和相关的星号粘合到一个带有和的modelsummary表中。glance_custom.lm_robustgof_map

这里有更多细节。

根据 Vincent 非常有用的建议和建议,我使用该modelsummary软件包制作了下面的模型汇总表,其中包含手动选择的一些模型的 F 统计量和相关 p 值。

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(
    "ftestF" = ftest[["F"]][2], 
    "ftestP" = sprintf("(%.3f)", ftest[["Pr(>F)"]][2]), 
    "ftestStars" = if (ftest[["Pr(>F)"]][2] <= 0.01) {
      "***"
    } else if (ftest[["Pr(>F)"]][2] <= 0.05) {
      "**"
    } else if (ftest[["Pr(>F)"]][2] <= 0.1) {
      "*"
    } else {
      NULL
    }
    )
  return(out)
}

gm <- tribble(
  ~raw, ~clean, ~fmt,
  "ftestF", "$H_0: \\beta_{\\rm{am}} = 0, \\ \\beta_{\\rm{vs}} = 0$", 3,
  "ftestP", "XXXP", 0,
  "ftestStars", "XXXStars",  0,
  "adj.r.squared", "$\\bar{R}^2$", 3,
  "nobs", "Sample size", 0
)

modelsummary(
  list(mod1, mod2, mod3, mod4), escape = FALSE, 
  estimate = "{estimate}{stars}", 
  stars = c('***' = .01, '**' = .05, '*' = .1), 
  gof_map = gm
  )

型号汇总表

这非常接近我想要的。但是,我想在 F 统计量旁边而不是在单独的行中显示 F 检验的显着性星,就像在回归系数旁边显示显着性星一样。我试着粘进去,"ftestF"但没有成功。我将不胜感激任何建议。"ftestStars"gm

我还想消除行标题XXXPXXXStars(我想XXXStars一旦我在上一段中的问题得到解决就会消失)。替换"XXXP"""(or " ") 确实使行标题消失,但 p 值 (0.000) 和星星移动到了意想不到的位置。我也将不胜感激任何建议。

4

0 回答 0