如何使用 stargazer 输出 gee 模型(不适合我)

how to output a gee model using stargazer (not working for me)

使用 stargazer 构建了一些我想展示的模型,但我发现我的代码出现以下错误:

Error in if (object.name$family$family == "gaussian") { : argument is of length zero

我的代码是:

library(drgee)
library(stargazer)

sat_mod01 <- gee(sat~GAD*Ratg + whtb + GRatio + Control_sec + Op_Ratio + Time_Cat + Yr, family = guassian, data = cd_df,
                 clusterid = "id", corstr = "AR-1")
mat_mod01 <- gee(mat~GAD*Ratg + whtb + GRatio + Control_sec + Op_Ratio + Time_Cat + Yr, family = guassian, data = cd_df,
                 clusterid = "id", corstr = "AR-1")
lat_mod01 <- gee(lat~GAD*Ratg + whtb + GRatio + Control_sec + Op_Ratio + Time_Cat + Yr, family = guassian, data = cd_df,
                 clusterid = "id", corstr = "AR-1")
ag_att_mod01 <- gee(ag_att~GAD*Ratg + whtb + GRatio + Control_sec + Op_Ratio + Time_Cat + Yr, family = guassian, 
            data = cd_df, clusterid = "id", corstr = "AR-1")
stargazer(sat_mod01, mat_mod01, lat_mod01, ag_att_mod01, title = "Regression Reults", align = T)

下面包含一个最小可重现数据集(数据敏感但具有重现变量,代码产生相同的错误。

structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), GAD = structure(c(2L, 2L, 1L, 
2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L), .Label = c("0", 
"1"), class = "factor"), whtb = structure(c(2L, 2L, 2L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 
2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L), .Label = c("0", 
"1"), class = "factor"), Yr = structure(c(4L, 5L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("2015", 
"2016", "2017", "2018", "2019", "2020", "2021"), class = "factor"), 
    GRatio = c(0.0523917995444191, 0.0523917995444191, 0.0523917995444191, 
    0.0523917995444191, 0.0523917995444191, 0.0523917995444191, 
    0.0523917995444191, 0.0523917995444191, 0.0523917995444191, 
    0.0523917995444191, 0.0523917995444191, 0.0523917995444191, 
    0.0523917995444191, 0.0411764705882353, 0.0411764705882353, 
    0.0411764705882353, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547, 0.0373467112597547, 
    0.0373467112597547, 0.0373467112597547), Time_Cat = structure(c(1L, 
    3L, 1L, 2L, 1L, 2L, 3L, 1L, 2L, 2L, 1L, 2L, 2L, 3L, 1L, 1L, 
    1L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 1L, 1L, 
    3L, 3L, 2L, 1L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L), .Label = c("Afternoon", "Evening", "Morning"
    ), class = "factor"), Control_sec = c(600, 180, 600, 600, 
    600, 1800, 1800, 1800, 1800, 900, 900, 900, 900, 600, 600, 
    60, 300, 600, 300, 300, 600, 600, 600, 300, 300, 300, 300, 
    300, 300, 300, 300, 300, 300, 300, 300, 300, 300, 300, 300, 
    300, 300, 180, 180, 300, 300, 300, 300, 300, 300, 300), Ratg = c(722, 
    506, 529, 623, 856, 618, 585, 578, 570, 553, 539, 533, 541, 
    267, 342, 462, 597, 594, 565, 558, 580, 676, 729, 744, 766, 
    854, 836, 859, 767, 803, 776, 762, 742, 821, 820, 822, 811, 
    803, 808, 783, 620, 729, 753, 732, 730, 612, 611, 653, 766, 
    759), sat = c(2.35, 1.82, 2.88, 1.82, 2.53, 1.82, 2.35, 2.35, 
    2.35, 2.53, 1.82, 1.82, 1.82, 1.82, 1.82, 2.71, 2, 2.53, 
    2.71, 2.7, 2, 2.35, 1.82, 1.82, 2.35, 1.64, 2.53, 2.53, 1.82, 
    0.76, 2.53, 2.53, 1.82, 2.7, 2.35, 1.82, 1.64, 2, 2.35, 2.17, 
    1.64, 1.64, 2, 1.64, 1.82, 2.35, 2.35, 1.82, 2, 1.47), mat = c(0.83, 
    0.87, 2.88, 1.3, 1.35, 0.76, 0.94, 2.53, NA, 1.23, 1.05, 
    2.7, 0.76, 2.46, 2.7, 0.58, 1.82, 3.24, 0.87, 3.06, 1.64, 
    1.11, 2.64, 2.99, 1.65, 0.94, 1.82, 1.64, 1.35, 1.23, 1, 
    0.76, 2.11, 2.18, 0.65, 1.65, 2.35, 2.17, 1.82, 1.29, 0.94, 
    0.94, 2.6, 1.64, 0.76, 2.71, 2.17, 2, 2.06, 2.35), lat = c(0.83, 
    0, 0, 0, 0.36, 0.87, 0.76, 0.29, 2.59, 4.24, 3.71, 0.82, 
    1.59, 2.12, 1.64, 0.58, 0.65, 3.24, 0.87, 2.35, 2.24, 1.29, 
    2.75, 2.17, 0, 0.29, 0.76, 0.87, 0.58, 0.65, 0.58, 1.88, 
    1.64, 0.47, 0.65, 2.71, 0.76, 2.17, 1.05, 1.29, 0.76, 0, 
    2.6, 0.87, 1.88, 2.99, 2.88, 1.05, 2.24, 0.58), ag_att = c(1.33666666666667, 
    0.896666666666667, 1.92, 1.04, 1.41333333333333, 1.15, 1.35, 
    1.72333333333333, NA, 2.66666666666667, 2.19333333333333, 
    1.78, 1.39, 2.13333333333333, 2.05333333333333, 1.29, 1.49, 
    3.00333333333333, 1.48333333333333, 2.70333333333333, 1.96, 
    1.58333333333333, 2.40333333333333, 2.32666666666667, 1.33333333333333, 
    0.956666666666667, 1.70333333333333, 1.68, 1.25, 0.88, 1.37, 
    1.72333333333333, 1.85666666666667, 1.78333333333333, 1.21666666666667, 
    2.06, 1.58333333333333, 2.11333333333333, 1.74, 1.58333333333333, 
    1.11333333333333, 0.86, 2.4, 1.38333333333333, 1.48666666666667, 
    2.68333333333333, 2.46666666666667, 1.62333333333333, 2.1, 
    1.46666666666667), Op_Ratio = c(928, 603, 507, 685, 1084, 
    459, 498, 592, 636, 656, 513, 555, 569, 138, 355, 479, 621, 
    1299, 660, 573, 644, 696, 840, 689, 830, 838, 867, 866, 775, 
    897, 740, 563, 609, 883, 800, 946, 757, 826, 832, 838, 625, 
    729, 711, 578, 671, 667, 573, 694, 769, 852)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

在没有 stargazer 的情况下,它们都 运行 正常并输出所需的结果。我已经彻底搜索过了,stargazer 的文档说它支持 gee,但还没有看到 gee 中的任何实现。请问有什么想法吗?

我能够重现您的问题,这似乎是 stargazer 中的错误。不幸的是,很难诊断该包中的实际问题,因为 it consists of a single function that is 7000 lines long! Also, I’m not sure we should expect a fix soon since stargazer has not been updated since 2018.

与此同时,您可能需要考虑替代回归 table 包。例如,很容易用 the modelsummary package 来总结你的模型(免责声明:我是作者,所以有偏见)。

modelsummary 支持超过一百种开箱即用的模型,但不幸的是不支持 drgee 包生产的模型。但是,通过定义两个简单的函数来添加对这些模型的支持非常容易:tidy.geeglance.geeClick here for details on the general strategy.

library(modelsummary)
library(drgee)

tidy.gee <- function(x, ...) {
  out <- data.frame(
    term = names(coef(x)),
    estimate = coef(x),
    std.error = sqrt(diag(vcov(x)))
  )
  return(out)
}

glance.gee <- function(x, ...) {
  out <- data.frame(
    nobs = x$gee.data$n.obs
  )
  return(out)
}

models <- list(
  "SAT" = gee(sat ~ GAD * Ratg + whtb + GRatio + Control_sec + Op_Ratio + Time_Cat + Yr,
              family = guassian, data = cd_df, clusterid = "id", corstr = "AR-1"),
  "MAT" = gee(mat ~ GAD * Ratg + whtb + GRatio + Control_sec + Op_Ratio + Time_Cat + Yr,
              family = guassian, data = cd_df, clusterid = "id", corstr = "AR-1"),
  "LAT" = gee(lat ~ GAD * Ratg + whtb + GRatio + Control_sec + Op_Ratio + Time_Cat + Yr,
              family = guassian, data = cd_df, clusterid = "id", corstr = "AR-1"),
  "AG ATT" = gee(ag_att ~ GAD * Ratg + whtb + GRatio + Control_sec + Op_Ratio + Time_Cat + Yr,
                 family = guassian, data = cd_df, clusterid = "id", corstr = "AR-1")
)

modelsummary(models)