为什么观星者不会在下表中输出标准误差和星标?
如何让stargazer(或其他表格包)在系数下面的括号中显示标准误差,并在系数旁边显示重要星?
正如您在底部所看到的,现在它只输出系数。
仅仅因为分析的性质,我需要分别保存每个系数。我无法将每个模型保存为模型对象。
对于每个模型,我有12个系数,标准误差和p值。然后我使用se=
和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
-------------------
答案 0 :(得分:2)
根据我发布here的类似答案,我建议使用xtable
从头开始构建。
从你的帖子中我不完全确定表格的理想格式,除了提到的要求(即下面的SE和系数旁边的重要星)。一般的想法是在R中构建具有sesired维度的矩阵或数据框,然后使用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中的包threeparttable
一起使用来美化它。请务必阅读print
xtable
方法的\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}
方法的可用选项。它非常灵活,可以省略列名和行名等。
在LaTeX中,我经常使用某种东西
testOnly