r - 如何在 LaTeX 表格中仅显示四舍五入为整数的系数?

标签 r latex decimal stargazer texreg

使用 R 的 stargazer 在 LaTeX 中生成表格效果很好。 但我不知道如何将我的数字格式化为整数(this post 是相关的,但不同)。

由于数据规模,我希望系数和标准误差不显示小数。因此,我使用选项 digits=0。但是,此选项还会从模型拟合统计数据中删除小数(例如,R2 = 0 而不是 R2 = 0.42)。如何为表格的“顶部”和“底部”部分设置不同的舍入规则?

最佳答案

使用正则表达式,我们可以用它们的舍入值替换 LaTeX 输出 s 中的特定数字。

由于 texreg 包目前似乎比 stargazer 维护得更好,所以我将两者都包含在答案中。

虽然很难概括一个解决方案,因为回归结果可能总是不同的,但这里有一个想法。

(fit <- lm(mpg ~ hp + cyl, mtcars))
# Call:
#   lm(formula = mpg ~ hp + cyl, data = mtcars)
# 
# Coefficients:
# (Intercept)           hp          cyl  
#    36.90833     -0.01912     -2.26469  

s <- stargazer::stargazer(fit, header=FALSE, digit.separator="")

texreg 用户会做:

tmp <- tempfile()
texreg::texreg(fit, file=tmp)
s <- readLines(tmp)

为此,首先,我们要对 s 进行子集化以不包括 GOF。我们也不想替换括号内的 LaTeX 格式命令。

s.sub1 <- 1:(grep("Observations", s) - 1)  ## stargazer
s.sub1 <- 1:(grep("R$^2$", s, fixed=TRUE) - 1)  ## texreg
re <- regexpr("(\\d+\\.\\d+)(?![^[]*\\])", s[s.sub1], perl=TRUE)
toInt <- regmatches(s[s.sub1], re)
v.repl <- round(as.double(toInt), digits=0)

s.sub2 <- which(attr(re, "match.length")[s.sub1] > 1)

在我们获得替换 v.repl 之后,我们在第二步中使用 mapply 将小数替换为四舍五入为整数的值。

s[s.sub2] <- mapply(function(x, y) gsub("(\\d+\\.\\d+)(?![^[]*\\])", x, y, perl=TRUE),
    v.repl, s[s.sub2])

结果

观星者

# \begin{table}[!htbp] \centering 
#   \caption{} 
#   \label{} 
# \begin{tabular}{@{\extracolsep{5pt}}lc} 
# \\[-1.8ex]\hline 
# \hline \\[-1.8ex] 
#  & \multicolumn{1}{c}{\textit{Dependent variable:}} \\ 
# \cline{2-2} 
# \\[-1.8ex] & mpg \\ 
# \hline \\[-1.8ex] 
#  hp & $-$0 \\ 
#   & (0) \\ 
#   & \\ 
#  cyl & $-$2$^{***}$ \\ 
#   & (1) \\ 
#   & \\ 
#  Constant & 37$^{***}$ \\ 
#   & (2) \\ 
#   & \\ 
# \hline \\[-1.8ex] 
# Observations & 32 \\ 
# R$^{2}$ & 0.741 \\ 
# Adjusted R$^{2}$ & 0.723 \\ 
# Residual Std. Error & 3.173 (df = 29) \\ 
# F Statistic & 41.422$^{***}$ (df = 2; 29) \\ 
# \hline 
# \hline \\[-1.8ex] 
# \textit{Note:}  & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
# \end{tabular} 
# \end{table} 

enter image description here

正则表达式

# \begin{table}
# \begin{center}
# \begin{tabular}{l c}
# \hline
#  & Model 1 \\
# \hline
# (Intercept) & $37^{***}$ \\
#             & $(2)$      \\
# hp          & $-0$       \\
#             & $(0)$      \\
# cyl         & $-2^{***}$ \\
#             & $(1)$      \\
# \hline
# R$^2$       & $0.74$        \\
# Adj. R$^2$  & $0.72$        \\
# Num. obs.   & $32$          \\
# \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}

enter image description here

注意:要使系数对齐,您可能需要查看 LaTeX 的 siunitx 包。这里是starter在 tex.stackexchange 上。阅读here如何在 Rmarkdown 中包含 LaTeX 包。

关于r - 如何在 LaTeX 表格中仅显示四舍五入为整数的系数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62309834/

相关文章:

r - 访问 R Dataframe 值而不是 Tibble

r - 为 data.frame 和 tbl_df 子集行和列

r - 为 R 图中的每个因子水平分配颜色

c# - 如何使用 decimal.MaxValue 进行测试?

R 下载文件重定向

r - 如何使用stargazer按多个类别(组)创建汇总统计表?

vim - Mactex + Vimtex 实时预览延迟

用于对齐文本组的 Latex minipage

regex - 如何使用正则表达式验证带有可选百分比符号的小数?

Android edittext 仅输入货币格式