根据我发布的类似答案 这里 ,我建议从头开始使用 xtable 。
xtable
从你的帖子我不完全确定表格的理想格式,除了提到的要求(即下面的SE和系数旁边的重要星)。一般的想法是在R中构建具有sesired维度的矩阵或数据框,然后使用获取骨架表 xtable 。该表可以使用保存 file 选项 print (有关详细信息,请参阅链接的帖子)。然后使用此文件将其输入到最终的LaTeX文档中 \input 。
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')
我和包一起使用它 threeparttable 在LaTeX中美化这个。确保您阅读了可用的选项 print 的方法 xtable 对象。它非常灵活,可以省略列名和行名等。
threeparttable
在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}