2

为什么 stargazer 没有在下表中输出标准错误和星星?

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

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

只是为了轻微的背景,由于分析的性质,我需要分别保存每个系数。我无法将每个模型保存为模型对象。

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

现在我正在使用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 
-------------------
4

1 回答 1

3

根据我在此处发布的类似答案,我建议使用xtable.

从您的帖子中,除了提到的要求(即下面的 SE 和系数旁边的重要性星)之外,我并不完全确定所需的表格格式。一般的想法是用 R 中的 sesired 维度构建一个矩阵或数据框,然后使用xtable. 可以使用file选项来保存此表print(有关详细信息,请参阅链接的帖子)。然后使用\input.

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')

我将它与threeparttableLaTeX 中的包一起使用来美化它。确保您阅读了对象print方法的可用选项xtable。它非常灵活,可以让您省略列名和行名等。

在 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}
于 2015-10-18T13:58:10.723 回答