簡體   English   中英

R-> TeX表包:手動輸入數據后,輸出中缺少SE和星號

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

下表為何不顯示標准差和星號?

我如何才能讓觀星者(或其他表格包裝)在系數下方的括號中顯示標准誤差,並在系數旁邊顯示顯着性星星?

正如您在底部看到的那樣,現在它僅輸出系數。

僅出於輕微的背景,由於分析的性質,我需要分別保存每個系數。 我無法將每個模型另存為模型對象。

對於每個模型,我都有十二個系數,標准誤差和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 
-------------------

基於我在這里發布的類似答案,我建議使用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中的threeparttable軟件包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}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM