R->TeX 表包 : after inputting data manually, SE 和星号在输出中丢失

标签 r latex regression xtable stargazer

为什么 stargazer 不输出下表中的标准错误和星星?

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

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

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

对于每个模型,我有 12 个系数、标准误差和 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 
-------------------

最佳答案

基于我发布的类似答案here ,我建议使用 xtable 从头开始​​构建它。

从您的帖子中,除了提到的要求(即下面的 SE 和系数旁边的重要性星星)之外,我不完全确定表格所需的格式。总体思路是在 R 中构建具有所需维度的矩阵或数据框,然后使用 xtable 检索骨架表。可以使用 file 选项保存此表以print(有关详细信息,请参阅链接的帖子)。然后使用 \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 中的包 thirdparttable 一起使用来美化它。确保您阅读了 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}

关于R->TeX 表包 : after inputting data manually, SE 和星号在输出中丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32964665/

相关文章:

r - 在 ggplot 上添加回归线

R filter() 处理 NA

r - 使用 tryCatch 保存引发警告的对象

latex - 如何以 UTF8 编译 LaTeX?

python - python中的多元(多项式)最佳拟合曲线?

R:我怎样才能拟合一个对系数有约束的回归?

r - 有效使用 R data.table 和 unique()

r - 拆分和删除分组变量

python - 使用 python Popen 和 pandoc 解析 html 时不需要的新行?

r - Dynamic Sweave 文档