将多列添加到我的 texreg 输出
Adding multicolumns to my texreg output
我想通过 texreg
制作一个 table 与分组 列 。我只能看到分组 行 (groups
) 的选项。
这是一个例子:
set.seed(01349)
DF <- data.frame(y = rnorm(100), x1A = rnorm(100), x2A = rnorm(100),
x1B = rnorm(100), x2B = rnorm(100))
regs <- lapply(paste0("x", 1:2, c("A", "A", "B", "B")), function(x)
lm(paste0("y ~ ", x), data = DF))
这是我能用普通 texreg
得到的最接近的值:
texreg(regs, custom.coef.names = c("Intercept", rep("x", 4)),
custom.model.names = c("1", "2", "1", "2"))
使用 LaTeX 输出:
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
& 1 & 2 & 1 & 2 \
\hline
Intercept & $-0.13$ & $-0.13$ & $-0.11$ & $-0.11$ \
& $(0.12)$ & $(0.12)$ & $(0.12)$ & $(0.12)$ \
x & [=12=].02$ & [=12=].07$ & [=12=].13$ & $-0.11$ \
& $(0.13)$ & $(0.12)$ & $(0.12)$ & $(0.13)$ \
\hline
R$^2$ & 0.00 & 0.00 & 0.01 & 0.01 \
Adj. R$^2$ & -0.01 & -0.01 & 0.00 & -0.00 \
Num. obs. & 100 & 100 & 100 & 100 \
RMSE & 1.18 & 1.17 & 1.17 & 1.17 \
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
我想要一个额外的行(用 %
注释突出显示):
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
%*************A HEADER LINE HERE*********************
& \multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} \ %
%****************************************************
& 1 & 2 & 1 & 2 \
\hline
Intercept & $-0.13$ & $-0.13$ & $-0.11$ & $-0.11$ \
& $(0.12)$ & $(0.12)$ & $(0.12)$ & $(0.12)$ \
x & [=13=].02$ & [=13=].07$ & [=13=].13$ & $-0.11$ \
& $(0.13)$ & $(0.12)$ & $(0.12)$ & $(0.13)$ \
\hline
R$^2$ & 0.00 & 0.00 & 0.01 & 0.01 \
Adj. R$^2$ & -0.01 & -0.01 & 0.00 & -0.00 \
Num. obs. & 100 & 100 & 100 & 100 \
RMSE & 1.18 & 1.17 & 1.17 & 1.17 \
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
我是不是遗漏了什么,或者没有内置的方法来做到这一点?
我的解决方法是:
x <- capture.output(texreg(
regs, custom.coef.names = c("Intercept", rep("x", 4)),
custom.model.names = c("1", "2", "1", "2")))
x[6] <- paste0("& \multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} \\ \n", x[6])
cat(x, sep = "\n")
但这显然有点像胶带。
您可以通过在控制台中键入代码来阅读 function
的代码,或者从包 texreg
的 github
website 中阅读代码。
texreg
function (l, file = NULL, single.row = FALSE, stars = c(0.001,
0.01, 0.05), custom.model.names = NULL,...
这是 texreg
的 table 4 个模型的输出:
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
& Model 1 & Model 2 & Model 3 & Model 4 \
我查看了代码行 469,table 的开头:
string <- paste0(string, "\begin{tabular}{", coldef,
"}", linesep)
然后我添加了一些我自己的修改:
string <- paste0(string, "\begin{tabular}{", coldef,
"}", linesep)
## Additions
string <- paste0(string, "\\[-1.8ex]\hline", linesep)
string <- paste0(string, "& \multicolumn{", length(l),
"}{c}{\textit{Dependent variable:}} \\", linesep)
string <- paste0(string, "\cline{2-",length(modnames), "}", linesep)
string <- paste0(string, "\\[-1.8ex] & \multicolumn{", length(l),
"}{c}{", dep.var, "} \\", linesep)
然后,用不同的名称保存函数,例如:
texreg2 <- function (l, file = NULL, single.row = FALSE, ...)
现在,该函数需要包中的内部函数,因此您需要将自定义函数附加到环境中包的命名空间。简单易行:
environment(texreg2) <- asNamespace('texreg')
现在您可以调用您的新函数了。我的添加包括三行和因变量的名称,类似于 stargazer
.
texreg2(out, dep.var = "Normalize Citation Score")
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\[-1.8ex]\hline
& \multicolumn{4}{c}{\textit{Dependent variable:}} \
\cline{2-5}
\[-1.8ex] & \multicolumn{4}{c}{Normalize Citation Score} \
\hline
& Model 1 & Model 2 & Model 3 & Model 4 \
最后,如果您不喜欢这种方法,您可以使用 regex
来操纵输出,查看这个 。
这可能晚了,但仍然有用。
texreg
(1.36.28) 的新版本刚刚在 GitHub 上发布(尚未在 CRAN 上发布,2020 年 5 月 22 日)。它添加了选项 custom.header
来创建回归组。
应用于您的示例,它创建:
library(texreg)
set.seed(01349)
DF <- data.frame(y = rnorm(100), x1A = rnorm(100), x2A = rnorm(100),
x1B = rnorm(100), x2B = rnorm(100))
regs <- lapply(paste0("x", 1:2, c("A", "A", "B", "B")), function(x)
lm(paste0("y ~ ", x), data = DF))
screenreg(
regs,
custom.header = list("A" = 1:2, "B" = 3:4),
custom.coef.names = c("Intercept", rep("x", 4)),
custom.model.names = c("1", "2", "1", "2"),
)
=============================================
A B
---------------- ----------------
1 2 1 2
---------------------------------------------
Intercept -0.13 -0.13 -0.11 -0.11
(0.12) (0.12) (0.12) (0.12)
x 0.02 0.07 0.13 -0.11
(0.13) (0.12) (0.12) (0.13)
---------------------------------------------
R^2 0.00 0.00 0.01 0.01
Adj. R^2 -0.01 -0.01 0.00 -0.00
Num. obs. 100 100 100 100
=============================================
*** p < 0.001; ** p < 0.01; * p < 0.05
我使用 screenreg()
来更轻松地显示输出,但它也适用于 texreg()
。
我想通过 texreg
制作一个 table 与分组 列 。我只能看到分组 行 (groups
) 的选项。
这是一个例子:
set.seed(01349)
DF <- data.frame(y = rnorm(100), x1A = rnorm(100), x2A = rnorm(100),
x1B = rnorm(100), x2B = rnorm(100))
regs <- lapply(paste0("x", 1:2, c("A", "A", "B", "B")), function(x)
lm(paste0("y ~ ", x), data = DF))
这是我能用普通 texreg
得到的最接近的值:
texreg(regs, custom.coef.names = c("Intercept", rep("x", 4)),
custom.model.names = c("1", "2", "1", "2"))
使用 LaTeX 输出:
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
& 1 & 2 & 1 & 2 \
\hline
Intercept & $-0.13$ & $-0.13$ & $-0.11$ & $-0.11$ \
& $(0.12)$ & $(0.12)$ & $(0.12)$ & $(0.12)$ \
x & [=12=].02$ & [=12=].07$ & [=12=].13$ & $-0.11$ \
& $(0.13)$ & $(0.12)$ & $(0.12)$ & $(0.13)$ \
\hline
R$^2$ & 0.00 & 0.00 & 0.01 & 0.01 \
Adj. R$^2$ & -0.01 & -0.01 & 0.00 & -0.00 \
Num. obs. & 100 & 100 & 100 & 100 \
RMSE & 1.18 & 1.17 & 1.17 & 1.17 \
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
我想要一个额外的行(用 %
注释突出显示):
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
%*************A HEADER LINE HERE*********************
& \multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} \ %
%****************************************************
& 1 & 2 & 1 & 2 \
\hline
Intercept & $-0.13$ & $-0.13$ & $-0.11$ & $-0.11$ \
& $(0.12)$ & $(0.12)$ & $(0.12)$ & $(0.12)$ \
x & [=13=].02$ & [=13=].07$ & [=13=].13$ & $-0.11$ \
& $(0.13)$ & $(0.12)$ & $(0.12)$ & $(0.13)$ \
\hline
R$^2$ & 0.00 & 0.00 & 0.01 & 0.01 \
Adj. R$^2$ & -0.01 & -0.01 & 0.00 & -0.00 \
Num. obs. & 100 & 100 & 100 & 100 \
RMSE & 1.18 & 1.17 & 1.17 & 1.17 \
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
我是不是遗漏了什么,或者没有内置的方法来做到这一点?
我的解决方法是:
x <- capture.output(texreg(
regs, custom.coef.names = c("Intercept", rep("x", 4)),
custom.model.names = c("1", "2", "1", "2")))
x[6] <- paste0("& \multicolumn{2}{c}{A} & \multicolumn{2}{c}{B} \\ \n", x[6])
cat(x, sep = "\n")
但这显然有点像胶带。
您可以通过在控制台中键入代码来阅读 function
的代码,或者从包 texreg
的 github
website 中阅读代码。
texreg
function (l, file = NULL, single.row = FALSE, stars = c(0.001,
0.01, 0.05), custom.model.names = NULL,...
这是 texreg
的 table 4 个模型的输出:
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
& Model 1 & Model 2 & Model 3 & Model 4 \
我查看了代码行 469,table 的开头:
string <- paste0(string, "\begin{tabular}{", coldef,
"}", linesep)
然后我添加了一些我自己的修改:
string <- paste0(string, "\begin{tabular}{", coldef,
"}", linesep)
## Additions
string <- paste0(string, "\\[-1.8ex]\hline", linesep)
string <- paste0(string, "& \multicolumn{", length(l),
"}{c}{\textit{Dependent variable:}} \\", linesep)
string <- paste0(string, "\cline{2-",length(modnames), "}", linesep)
string <- paste0(string, "\\[-1.8ex] & \multicolumn{", length(l),
"}{c}{", dep.var, "} \\", linesep)
然后,用不同的名称保存函数,例如:
texreg2 <- function (l, file = NULL, single.row = FALSE, ...)
现在,该函数需要包中的内部函数,因此您需要将自定义函数附加到环境中包的命名空间。简单易行:
environment(texreg2) <- asNamespace('texreg')
现在您可以调用您的新函数了。我的添加包括三行和因变量的名称,类似于 stargazer
.
texreg2(out, dep.var = "Normalize Citation Score")
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\[-1.8ex]\hline
& \multicolumn{4}{c}{\textit{Dependent variable:}} \
\cline{2-5}
\[-1.8ex] & \multicolumn{4}{c}{Normalize Citation Score} \
\hline
& Model 1 & Model 2 & Model 3 & Model 4 \
最后,如果您不喜欢这种方法,您可以使用 regex
来操纵输出,查看这个
这可能晚了,但仍然有用。
texreg
(1.36.28) 的新版本刚刚在 GitHub 上发布(尚未在 CRAN 上发布,2020 年 5 月 22 日)。它添加了选项 custom.header
来创建回归组。
应用于您的示例,它创建:
library(texreg)
set.seed(01349)
DF <- data.frame(y = rnorm(100), x1A = rnorm(100), x2A = rnorm(100),
x1B = rnorm(100), x2B = rnorm(100))
regs <- lapply(paste0("x", 1:2, c("A", "A", "B", "B")), function(x)
lm(paste0("y ~ ", x), data = DF))
screenreg(
regs,
custom.header = list("A" = 1:2, "B" = 3:4),
custom.coef.names = c("Intercept", rep("x", 4)),
custom.model.names = c("1", "2", "1", "2"),
)
=============================================
A B
---------------- ----------------
1 2 1 2
---------------------------------------------
Intercept -0.13 -0.13 -0.11 -0.11
(0.12) (0.12) (0.12) (0.12)
x 0.02 0.07 0.13 -0.11
(0.13) (0.12) (0.12) (0.13)
---------------------------------------------
R^2 0.00 0.00 0.01 0.01
Adj. R^2 -0.01 -0.01 0.00 -0.00
Num. obs. 100 100 100 100
=============================================
*** p < 0.001; ** p < 0.01; * p < 0.05
我使用 screenreg()
来更轻松地显示输出,但它也适用于 texreg()
。