将big.mark =“,”添加到texreg输出

zyfwsgd6  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(129)

如何将big.mark =“,”添加到texreg输出的每一列的观察数或聚类数或组数等?它们都是或看起来是整数,但每一行都可以通过第一列中的条目来标识(在LaTeX中以&分隔)。例如,对于观测数,该行的第一列包含Num. obs.
下面是一个可重现的示例:

# data sim
library(texreg)
set.seed(1)
x <- rnorm(1000)
z <- rnorm(1000)
y <- 5 + 2*x - z + rnorm(1000,0,40)/100
# regression
m1 <- lm(y~x)
summary(m1)
m2 <- lm(y~z)
summary(m2)
m3 <- lm(y~x+z)
summary(m3)
texreg(list(m1,m2,m2),
       caption = "Add big.marks, please!",
       label = NULL,
       model.names=c("M1","M2","M3"))

其生成输出:

\begin{table}
\begin{center}
\begin{tabular}{l c c c}
\hline
 & Model 1 & Model 2 & Model 3 \\
\hline
(Intercept) & $5.02^{***}$ & $4.98^{***}$  & $4.98^{***}$  \\
            & $(0.04)$     & $(0.07)$      & $(0.07)$      \\
x           & $2.01^{***}$ &               &               \\
            & $(0.03)$     &               &               \\
z           &              & $-0.98^{***}$ & $-0.98^{***}$ \\
            &              & $(0.06)$      & $(0.06)$      \\
\hline
R$^2$       & $0.78$       & $0.19$        & $0.19$        \\
Adj. R$^2$  & $0.78$       & $0.18$        & $0.18$        \\
Num. obs.   & $1000$       & $1000$        & $1000$        \\
\hline
\multicolumn{4}{l}{\scriptsize{$^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}}
\end{tabular}
\caption{Add big.marks, please!}
\label{}
\end{center}
\end{table}

我想在输出中添加big.marks,比如说,添加到以Num. obs.开头的行(或任何其他包含大整数的行),这样看起来就像

\begin{table}
\begin{center}
\begin{tabular}{l c c c}
\hline
 & Model 1 & Model 2 & Model 3 \\
\hline
(Intercept) & $5.02^{***}$ & $4.98^{***}$  & $4.98^{***}$  \\
            & $(0.04)$     & $(0.07)$      & $(0.07)$      \\
x           & $2.01^{***}$ &               &               \\
            & $(0.03)$     &               &               \\
z           &              & $-0.98^{***}$ & $-0.98^{***}$ \\
            &              & $(0.06)$      & $(0.06)$      \\
\hline
R$^2$       & $0.78$       & $0.19$        & $0.19$        \\
Adj. R$^2$  & $0.78$       & $0.18$        & $0.18$        \\
Num. obs.   & $1,000$      & $1,000$       & $1,000$       \\
\hline
\multicolumn{4}{l}{\scriptsize{$^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}}
\end{tabular}
\caption{Added big.marks, thank you!}
\label{}
\end{center}
\end{table}
dwbf0jvd

dwbf0jvd1#

我不熟悉这个包,但是快速检查docs并没有指示添加大标记的参数。然而,texreg()的输出是一个字符向量,所以我们可以将这个Java regex用于R来添加大标记:

tbl  <- texreg(list(m1,m2,m2),
       caption = "Add big.marks, please!",
       label = NULL,
       model.names=c("M1","M2","M3")) 

# Add big mark
tbl_big  <- gsub("(\\d)(?=(\\d{3}))", "\\1,", tbl, perl = TRUE)

# Remove extra space after the dollar so we keep alignment of ampersands
tbl_big  <- gsub("(\\$\\d+,\\d+\\$)\\s", "\\1", tbl_big)

这产生tbl_big

\begin{table}
\begin{center}
\begin{tabular}{l c c c}
\hline
 & Model 1 & Model 2 & Model 3 \\
\hline
(Intercept) & $5.02^{***}$ & $4.98^{***}$  & $4.98^{***}$  \\
            & $(0.04)$     & $(0.07)$      & $(0.07)$      \\
x           & $2.01^{***}$ &               &               \\
            & $(0.03)$     &               &               \\
z           &              & $-0.98^{***}$ & $-0.98^{***}$ \\
            &              & $(0.06)$      & $(0.06)$      \\
\hline
R$^2$       & $0.78$       & $0.19$        & $0.19$        \\
Adj. R$^2$  & $0.78$       & $0.18$        & $0.18$        \\
Num. obs.   & $1,000$      & $1,000$       & $1,000$       \\
\hline
\multicolumn{4}{l}{\scriptsize{$^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}}
\end{tabular}
\caption{Add big.marks, please!}
\label{}
\end{center}
\end{table}

相关问题