R->TeX Table 包:手动输入数据后,输出中缺少 SE 和星号

R->TeX Table Package: after inputting data manually, SEs and stars are missing in output

为什么 stargazer 不输出下面的标准误差和星星 table?

如何让 stargazer(或其他 table 包)在系数下方的括号中显示标准误差,并在系数旁边显示重要的星星?

正如你在底部看到的,现在它只输出系数。

仅供参考,由于分析的性质,我需要单独保存每个系数。我无法将每个模型保存为模型对象。

对于每个模型,我有十二个系数、标准误差和 p 值。然后,我使用 se=p= 命令将这些值输入 stargazer,但我显然犯了一个错误。

现在我正在使用 stargazer() 但我很乐意接受使用任何 R->TeX 包(例如 xtable())的答案。

set.seed(961)

# Two models, twelve variables each. 

# Create empty matrices to store results below 
coefs <- matrix(NA, nrow = 12, ncol = 2)
ses <- matrix(NA, nrow = 12, ncol = 2)
p_values <- matrix(NA, nrow = 12, ncol = 2)

colnames(coefs) <- c("Model 1", "Model 2")
rownames(coefs) <- c("V1",  "V2",  "V3",  "V4",  "V5",  "V6",  "V7",  "V8",  "V9",  "V10", "V11", "V12")

colnames(ses) <- c("Model 1", "Model 2")
rownames(ses) <- c("V1",  "V2",  "V3",  "V4",  "V5",  "V6",  "V7",  "V8",  "V9",  "V10", "V11", "V12")

colnames(p_values) <- c("Model 1", "Model 2")
rownames(p_values) <- c("V1",  "V2",  "V3",  "V4",  "V5",  "V6",  "V7",  "V8",  "V9",  "V10", "V11", "V12")

for(i in 1:2){
coefs[, i] <- rnorm(12, 0, 5)  # Random coefficients
ses[, i] <- coefs[, i]*seq(0.1, 1.2, by = 0.1)  #Define standard error for coef
z <- coefs[, i] / ses[, i]  # Calculate Z-score for each coef
p_values[, i] <- 2*pnorm(-abs(z))  # Calculate p-value for each coef
}

stargazer(coefs, se = ses, p = p_values)


===================
    Model 1 Model 2
-------------------
V1  -0.500   0.054 
V2   7.667  -8.738 
V3   0.631   2.266 
V4  -4.003   3.759 
V5  -4.608  -8.939 
V6  -7.241   0.893 
V7   6.799  13.984 
V8  -5.981   3.577 
V9   3.041  10.789 
V10 -6.941  -1.109 
V11  0.776  -5.073 
V12  2.277   8.667 
-------------------

基于我 posted here 的类似回答,我建议使用 xtable.

从头开始​​构建它

根据您的 post,除了提到的要求(即下面的 SE 和系数旁边的重要星号)之外,我不完全确定 table 的所需格式。总体思路是在 R 中构建具有所需维度的矩阵或数据框,然后使用 xtable 检索骨架 table。此 table 可以使用 file 选项保存到 print(有关详细信息,请参阅链接的 post)。然后使用 \input.

将该文件输入到您的最终 LaTeX 文档中
set.seed(961)

# Two models, twelve variables each. 

# Create empty matrices to store results below 
coefs <- matrix(NA, nrow = 12, ncol = 2)
ses <- matrix(NA, nrow = 12, ncol = 2)
p_values <- matrix(NA, nrow = 12, ncol = 2)

colnames(coefs) <- c("Model 1", "Model 2")
rownames(coefs) <- c("V1",  "V2",  "V3",  "V4",  "V5",  "V6",  "V7",  "V8",  "V9",  "V10", "V11",     "V12")

colnames(ses) <- c("Model 1", "Model 2")
rownames(ses) <- c("V1",  "V2",  "V3",  "V4",  "V5",  "V6",  "V7",  "V8",  "V9",  "V10", "V11",     "V12")

colnames(p_values) <- c("Model 1", "Model 2")
rownames(p_values) <- c("V1",  "V2",  "V3",  "V4",  "V5",  "V6",  "V7",  "V8",  "V9",  "V10",     "V11", "V12")

for(i in 1:2){
    coefs[, i] <- rnorm(12, 0, 5)  # Random coefficients
    ses[, i] <- coefs[, i]*seq(0.1, 1.2, by = 0.1)  #Define standard error for coef
    z <- coefs[, i] / ses[, i]  # Calculate Z-score for each coef
    p_values[, i] <- 2*pnorm(-abs(z))  # Calculate p-value for each coef
}

### generate coefficients, se, t-stat and p values

star.wars <- function(x){
    out <- ifelse(x <= 0.1, ifelse(x <= 0.05, ifelse(x <= 0.01, "***", "**"), '*'), "")
    out
}

stars.coef <- apply(p_values, 2, function(x) sapply(x, star.wars))
coefs_w_stars <- paste(sprintf("%.4f",coefs), stars.coef, sep="")
ses_w_pars <-paste("(", sprintf("%.4f", ses), ")", sep="")



df_model = matrix(c(coefs_w_stars, ses_w_pars), ncol = 2)

colnames(df_model) <- c("Coef.", "Std. error")
tbl <- xtable(t(df_model))
print(tbl, only.contents=TRUE, include.rownames=T, 
      include.colnames=T, floating=F,
      hline.after=NULL,
      file = 'text.tex')

我将它与 LaTeX 中的包 threeparttable 一起使用来美化它。请务必阅读 xtable 对象的 print 方法的可用选项。它非常灵活,允许您省略列名和行名等。

在 LaTeX 中我经常使用这样的东西

\begin{table}[t]
\centering
\begin{threeparttable}
\captionabove{Regression results.}
\begin{tabular}{lccc}
      \toprule
      & <Insert your variable names here> \
      \midrule
      \input{test}
      \bottomrule
   \end{tabular}
\label{tab:summary}
\end{threeparttable}
\end{table}