Search code examples
rlatexregressionxtablestargazer

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


Why isn't stargazer outputting standard errors and stars in the below table?

How can I get stargazer (or another table package) to present the standard errors in parentheses below the coefficient, and present significance stars next to the coefficient?

As you can see at bottom, right now it only outputs the coefficients.

Just for slight background, because of the nature of the analysis, I need to save each coefficient separately. I cannot save each model as a model object.

For each model I have twelve coefficients, the standard errors, and the p-values. I then feed these values into stargazer with the se= and p= commands, but I am clearly making a mistake.

Right now I am using stargazer() but I would be happy to accept an answer using any R->TeX package (e.g., 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 
-------------------

Solution

  • Based on a similar answer I posted here, I suggest to build this from scratch using xtable.

    From your post I am not entirely sure about the desired format of the table apart from the requirements mentioned (i.e. SEs below and significance stars next to coefficients). The general idea is to build a matrix or data frame with the sesired dimensions in R and then retrieve a skeletal table using xtable. This table can be saved using the file option to print (see the linked post for details). This file is then input into your final LaTeX document using \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')
    

    I use this together with the package threeparttable in LaTeX to beautify this. Make sure your read the available options for the print method for xtable objects. It is quite flexible and lets you omit column and row names etc.

    In LaTeX I often use something as this

    \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}