2

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 
-------------------
Dr. Beeblebrox
  • 838
  • 2
  • 13
  • 30
  • The documentation for stargazer says those arguments need to be lists, not matrices. – Josh Oct 12 '15 at 10:24
  • @Josh I tried playing around with lists, but did not get it to work. Can you can figure it out with the code example? – Dr. Beeblebrox Oct 12 '15 at 12:56
  • Do you need to use Stargazer? Why not use something more generic like `xtable()`? – Thomas Oct 13 '15 at 16:07
  • @Thomas Good question. I would be happy to use any table package. It does not need to be Stargazer. – Dr. Beeblebrox Oct 13 '15 at 17:54
  • The problem is you still need a model object for stargazer to work. If you change your call to `stargazer(coef = coefs, se = ses, p = p_values)` you'll get an error, as the model isn't there. For now it is failing silently, as it thinks `coefs` is the model object. – jeremycg Oct 14 '15 at 00:27
  • @jeremycg Unfortunately, not possible here. I'm running something unusual and I do not want to go into it here, but like I said in the question, I cannot save each model as a model object. – Dr. Beeblebrox Oct 14 '15 at 06:35
  • 1
    as @jeremycg wrote, you have to supply a model object. Otherwise you'd have to trick stargazer() by re-creating dummy model objects from your values. I don't think it's worth it. I'd be very interested knowing why you can't or don't want to save your model objects. You could at leat trim them by deleting the huge components. – Karl Forner Oct 15 '15 at 14:52
  • @KarlForner I cannot supply a model because every coefficient comes from a different model. My columns are polynomial specifications. My rows represent variation in a model parameter. I am showing how the key estimand varies across polynomial specifications and this other parameter. – Dr. Beeblebrox Oct 15 '15 at 15:52
  • in this case you should not use stargazer. You could look at its output on a model ,and replace the actual values by yours using a sprintf-based approach for instance/ – Karl Forner Oct 15 '15 at 15:58
  • @KarlForner I am not wedded to stargazer. I am afraid that a "sprintf-based approach" is beyond me. I have no knowledge of C. – Dr. Beeblebrox Oct 16 '15 at 05:51

1 Answers1

3

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}
Community
  • 1
  • 1
pfifas
  • 592
  • 4
  • 11
  • Thanks @pfifas. I'm not familiar with `sprintf()` (other than reading the help file). What does `sprintf()` offer in this case? For example, what's the difference between using your line `paste(sprintf("%.4f",coefs), stars.coef, sep="")` and the following `paste(round(coefs, 4), stars.coef, sep="")`? – Dr. Beeblebrox Oct 19 '15 at 11:51
  • When you run your code, do you see a 12x2 table (or 24x2 if you count standard errors rows), like i present in my code format in my code? – Dr. Beeblebrox Oct 19 '15 at 12:22
  • I think in this case it delivers exactly the same result. I use `sprintf` with my tables, because I find its handling more intuitive and get desired results with fewer errors on the way (see for a brief overview over the alternatives: http://www.r-bloggers.com/paste-paste0-and-sprintf/). It also has some neat options that may become handy (see: http://stackoverflow.com/questions/23654521/collapsing-character-vectors-with-sprintf-instead-of-paste) that `paste` does not have. While in your case it returns the same, I think it's a just good to be aware of it. – pfifas Oct 19 '15 at 13:36