Stargazer 中的 Betareg(分配大小为 __ 的向量时出错)

Betareg in Stargazer (Error to allocate vector of size __)

betareg默认残差很重,由于其尺寸较大,可能会导致分配向量时出错。这可以通过更改 summary 调用中残差的 type 来解决,如 here.

所述

但是,当使用 stargazer 呈现回归表时,无法明确设置残差类型。

有没有办法让(大)betareg 对象在 stargazer 中工作?

我能想到但不知道如何实施的潜在解决方案是:

示例:

set.seed(1)
df <- data.frame(y=runif(100000), x=runif(100000))

library(betareg)
beta <- betareg(y ~ x, data=df)

library(stargazer)
stargazer(beta)
# Error: cannot allocate vector of size 74.5 Gb

您可以 stargazer::.stargazer.wrap 完成任务并更改模型摘要的计算方式,但它大约有 8000 行长。

作为一种解决方法,首先计算一个可以被 table 生成函数消化的更小的回归,然后在编译之前替换系数和统计数据可能会容易得多。

我给你展示了使用 texreg 包的方法,用 stargazer 也可能。

foo <- y ~ x
beta0 <- betareg::betareg(foo, data=df[sample(nrow(df), 1e3), ])  ## small version
beta <- betareg::betareg(foo, data=df)  ## actual version

(beta.sum <- betareg:::summary.betareg(beta, type='pearson')$coe)  ## actual summary
# $mean
#                 Estimate  Std. Error    z value  Pr(>|z|)
# (Intercept)  0.007933124 0.006967576  1.1385773 0.2548795
# x           -0.010845820 0.012080681 -0.8977822 0.3693017
# 
# $precision
#       Estimate  Std. Error z value Pr(>|z|)
# (phi) 1.999218 0.007501926 266.494        0

Table

屏幕:

texreg::screenreg(beta0,
                  digits=3,
                  override.coef=unlist(lapply(beta.sum, \(x) x[, 1])),
                  override.se=unlist(lapply(beta.sum, \(x) x[, 2])),
                  override.pvalues=unlist(lapply(beta.sum, \(x) x[, 4]))
)
# ==============================
#                   Model 1     
# ------------------------------
# (Intercept)          0.008    
#                     (0.007)   
# x                   -0.011    
#                     (0.012)   
# Precision: (phi)     1.999 ***
#                     (0.008)   
# ------------------------------
# Pseudo R^2           0.000    
# Log Likelihood       2.137    
# Num. obs.         1000        
# ==============================
# *** p < 0.001; ** p < 0.01; * p < 0.05

LaTeX:

texreg::texreg(beta0,
                  digits=3,
                  override.coef=unlist(lapply(beta.sum, \(x) x[, 1])),
                  override.se=unlist(lapply(beta.sum, \(x) x[, 2])),
                  override.pvalues=unlist(lapply(beta.sum, \(x) x[, 4]))
)
# \begin{table}
# \begin{center}
# \begin{tabular}{l c}
# \hline
# & Model 1 \
# \hline
# (Intercept)      & [=12=].008$       \
# & $(0.007)$     \
# x                & $-0.011$      \
# & $(0.012)$     \
# Precision: (phi) & .999^{***}$ \
# & $(0.008)$     \
# \hline
# Pseudo R$^2$     & [=12=].000$       \
# Log Likelihood   & .137$       \
# Num. obs.        & 00$        \
# \hline
# \multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}}
# \end{tabular}
# \caption{Statistical models}
# \label{table:coefficients}
# \end{center}
# \end{table}

要获取 HTML 代码,请使用:

texreg::htmlreg

数据:

set.seed(1)
n <- 1e5
df <- data.frame(y=runif(n), x=runif(n))

我最终解决这个问题的方法是修改stargazer函数。特别是,在 .stargazer.wrap 中,您可以找到 here 的代码,我添加了以下代码:

else if (class(object.name)[1] == "betareg") { #betareg
          .summary.object <<- summary(object.name, type = "person")

.add.model 内,即在这些代码行之后:

.add.model <-
      function(object.name, user.coef=NULL, user.se=NULL, user.t=NULL, user.p=NULL, auto.t=TRUE, auto.p=TRUE, user.ci.lb=NULL, user.ci.rb=NULL) {
        
        if (class(object.name)[1] == "Glm") {
          .summary.object <<- summary.glm(object.name)
        }

请注意,您需要使用新的 .stargazer.wrap 创建一个新的 stargazer 函数。我只是称它为 stargazer2:

stargazer2 <-
  function(..., type = "latex", title="", style="default", summary=NULL, out=NULL, out.header=FALSE,
           column.labels=NULL, column.separate = NULL, covariate.labels=NULL, dep.var.caption=NULL, 
           dep.var.labels=NULL, dep.var.labels.include=TRUE, align=FALSE, coef=NULL, se=NULL, t=NULL, 
           p=NULL,  t.auto=TRUE, p.auto=TRUE, ci=FALSE, ci.custom=NULL, ci.level=0.95, ci.separator=NULL, 
           add.lines=NULL, apply.coef=NULL, apply.se=NULL, apply.t=NULL, apply.p=NULL, apply.ci=NULL, 
           colnames = NULL, column.sep.width = "5pt", 
           decimal.mark=NULL, df=TRUE, digit.separate=NULL, digit.separator=NULL, digits=NULL, digits.extra=NULL, 
           flip=FALSE,
           float=TRUE, float.env="table", font.size=NULL, header=TRUE, initial.zero=NULL, intercept.bottom=TRUE, 
           intercept.top=FALSE, keep=NULL, keep.stat=NULL, label="", model.names=NULL, model.numbers=NULL, 
           multicolumn=TRUE, no.space=NULL, notes=NULL, notes.align=NULL, notes.append=TRUE, notes.label=NULL, 
           object.names=FALSE,
           omit=NULL, omit.labels=NULL, omit.stat=NULL, omit.summary.stat=NULL, omit.table.layout=NULL,
           omit.yes.no=c("Yes","No"), order=NULL, ord.intercepts=FALSE, 
           perl=FALSE, report=NULL, rownames = NULL,
           rq.se = "nid", selection.equation=FALSE, single.row=FALSE, star.char=NULL, 
           star.cutoffs=NULL, suppress.errors=FALSE, table.layout=NULL, table.placement = "!htbp", zero.component=FALSE, 
           summary.logical=TRUE, summary.stat=NULL, nobs=TRUE, mean.sd=TRUE, min.max=TRUE, median=FALSE, 
           iqr=FALSE) {
    
    save.warn.option <- getOption("warn") 
    options(warn=-1)
    return(.stargazer.wrap(..., type=type, title=title, style=style, summary=summary, out=out, out.header=out.header,
                           column.labels=column.labels, column.separate = column.separate,
                           covariate.labels=covariate.labels, dep.var.caption = dep.var.caption,
                           dep.var.labels=dep.var.labels, dep.var.labels.include=dep.var.labels.include,
                           align=align, coef=coef, se=se, t=t, p=p, t.auto=t.auto, p.auto=p.auto, 
                           ci=ci, ci.custom=ci.custom, ci.level=ci.level, ci.separator = ci.separator,
                           add.lines=add.lines, apply.coef=apply.coef, apply.se=apply.se, apply.t=apply.t, 
                           apply.p=apply.p, apply.ci=apply.ci, colnames=colnames,
                           column.sep.width=column.sep.width, decimal.mark=decimal.mark, df=df,
                           digit.separate=digit.separate, digit.separator=digit.separator, 
                           digits=digits, digits.extra=digits.extra, 
                           flip=flip,
                           float=float, float.env=float.env,
                           font.size=font.size, header=header, 
                           initial.zero=initial.zero, 
                           intercept.bottom=intercept.bottom, intercept.top=intercept.top, 
                           keep = keep, keep.stat = keep.stat,
                           label = label,
                           model.names=model.names, model.numbers=model.numbers,
                           multicolumn = multicolumn,
                           no.space=no.space, notes=notes, notes.align=notes.align, 
                           notes.append=notes.append, notes.label=notes.label, object.names=object.names,
                           omit=omit, omit.labels=omit.labels, omit.stat=omit.stat, omit.summary.stat=omit.summary.stat,
                           omit.table.layout=omit.table.layout,omit.yes.no=omit.yes.no, 
                           order=order, ord.intercepts=ord.intercepts, perl=perl, 
                           report=report, rownames=rownames, rq.se=rq.se, selection.equation=selection.equation,
                           single.row=single.row, star.char=star.char, 
                           star.cutoffs=star.cutoffs, suppress.errors=suppress.errors,
                           table.layout=table.layout,
                           table.placement = table.placement, zero.component=zero.component,
                           summary.logical = summary.logical,
                           summary.stat = summary.stat,
                           nobs=nobs, mean.sd=mean.sd, 
                           min.max=min.max, median=median, iqr=iqr, warn=save.warn.option))
  }