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

betareg default residuals are heavy, which may cause an error to allocate vector due to its high size. This can be solved by changing the type of residuals in the summary call as explained here.

However, when presenting regression tables with stargazer, the type of residuals can't be explicitly set.

Is there any way to make (large) betareg objects work in stargazer?

The potential solutions that I can think of, but don't know how to implement, are:

  • Indicating the residual type in the original betareg call (type = "pearson" (or any other type) doesn't work).
  • Explicitly indicating the argument that stargazer should include when calling summary on the betareg object.
  • Any other?

Example:

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

You could take stargazer::.stargazer.wrap to task and change how the model summary is calculated, but it's about 8k lines long.

As a workaround, it's probably much easier to first compute a smaller regression that can be digested by the table-generating function, then replace the coefficients and statistics before compiling.

I show you the method using the texreg package, it may also be possible with 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

Screen:

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)      & $0.008$       \\
# & $(0.007)$     \\
# x                & $-0.011$      \\
# & $(0.012)$     \\
# Precision: (phi) & $1.999^{***}$ \\
# & $(0.008)$     \\
# \hline
# Pseudo R$^2$     & $0.000$       \\
# Log Likelihood   & $2.137$       \\
# Num. obs.        & $1000$        \\
# \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}

For getting HTML code, use:

texreg::htmlreg

Data:

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