Reputation: 131
How do I add big.mark = "," to, say, the every column's number of observations or the number of clusters or the number of groups, etc to a texreg output? They all are or seem to be integer numbers, but each line can be identified by the entry in the first column (delimited in LaTeX with an &
). For example, for the number of observations, the first column of that line contains Num. obs.
Here is a reproducible example:
# 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"))
which generates the output:
\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}
I would like to add big.marks to the output, say, to the line that starts with Num. obs.
(or any other line with large integer numbers) so it would look like
\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}
Upvotes: 2
Views: 80
Reputation: 20512
I'm not familiar with this package but a quick check of the docs doesn't indicate an argument for adding a big mark. However, the output of texreg()
is a character vector, so we can adapt this Java regex for R to add the big mark:
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)
This produces 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}
Upvotes: 1