gpt4 book ai didi

r - 将多列添加到我的 texreg 输出

转载 作者:行者123 更新时间:2023-12-04 04:40:35 26 4
gpt4 key购买 nike

我想通过 texreg 制作一个包含分组列的表格.我只能看到分组行的选项( 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 & $0.02$ & $0.07$ & $0.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 & $0.02$ & $0.07$ & $0.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")

但这显然有点像胶带一样。

最佳答案

这可能晚了,但仍然有用。
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()也。

关于r - 将多列添加到我的 texreg 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com