Displaying degrees of freedom in stargazer table
Asked Answered
M

1

3

When constructing documents with Sweave and R, I make use of the stargazer library for tables.

When using stargazer, is there a mechanism to display the degrees of freedom associated with the residual deviance for a model constructed with glm?

Minimal code:

library(stargazer)
set.seed(1234)
data <- data.frame(x=1:10)
data$y <- data$x + rnorm(10, 0, 0.2)
model <- glm(y~x, data=data, family=gaussian)
summary(model)
stargazer(model,title="A test", align=T,label="Tab:test",style="all2")

Resultant stargazer table will have Observations, Log Likelihood, AIC, Residual Deviance and Null Deviance but no d.f. I can work out d.f. but would have thought this could be displayed directly. Also see:

https://sites.google.com/site/marekhlavac/stargazer

Update #1:

Thank you Marek for your response. For the benefit of others that encounter this, here is the process that lets you form the work around:

  1. Obtain version 4.0 (not 4.5 - I'll come back to this) from http://cran.r-project.org/src/contrib/Archive/stargazer/
  2. Within the package directory structure under R, edit "stargazer-internal.R" as per the instructions in the answer below.
  3. Ensure that the library is not loaded in your R session
  4. Ensure that you have removed any existing stargazer lib
  5. Install the edited version of the stargazer package.
  6. Reload the library in R and compile as per usual.

Here are the commands:

detach("package:stargazer", unload=TRUE)
remove.packages("stargazer")

From the command line:

R CMD INSTALL -l <path to library directory> stargazer

Finally (assuming you have a few models at hand),

library(stargazer)
stargazer(model6,model7,model8, title="Logistic model summary",align=T,label="Tab:logmod1",  font.size="footnotesize", style="all2")

Result:

% Table created by stargazer v.4.0 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Tue, Sep 24, 2013 - 17:17:17
% Requires LaTeX packages: dcolumn 
\begin{table}[!htbp] \centering 
  \caption{Logistic model summary} 
  \label{Tab:logmod1} 
\footnotesize 
\begin{tabular}{@{\extracolsep{5pt}}lD{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} } 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{3}{c}{\textit{Dependent variable:}} \\ 
\cline{2-4} 
\\[-1.8ex] & \multicolumn{3}{c}{whalesighted} \\ 
\\[-1.8ex] & \multicolumn{1}{c}{\textit{logistic}} & \multicolumn{1}{c}{\textit{probit}} & \multicolumn{1}{c}{\textit{glm: binomial}} \\ 
 & \multicolumn{1}{c}{\textit{}} & \multicolumn{1}{c}{\textit{}} & \multicolumn{1}{c}{\textit{link = cloglog}} \\ 
\\[-1.8ex] & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)}\\ 
\hline \\[-1.8ex] 
 visibility & 0.392^{***} & 0.226^{***} & 0.216^{***} \\ 
  & (0.051) & (0.027) & (0.026) \\ 
  Constant & -1.251^{***} & -0.745^{***} & -1.149^{***} \\ 
  & (0.246) & (0.144) & (0.182) \\ 
 \hline \\[-1.8ex] 
Observations & \multicolumn{1}{c}{232} & \multicolumn{1}{c}{232} & \multicolumn{1}{c}{232} \\ 
Log Likelihood & \multicolumn{1}{c}{-110.485} & \multicolumn{1}{c}{-110.888} & \multicolumn{1}{c}{-112.694} \\ 
Akaike Inf. Crit. & \multicolumn{1}{c}{224.970} & \multicolumn{1}{c}{225.775} & \multicolumn{1}{c}{229.388} \\ 
Residual Deviance (df = 230) & \multicolumn{1}{c}{220.970} & \multicolumn{1}{c}{221.775} & \multicolumn{1}{c}{225.388} \\ 
Null Deviance (df = 231) & \multicolumn{1}{c}{310.759} & \multicolumn{1}{c}{310.759} & \multicolumn{1}{c}{310.759} \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{3}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\normalsize 
\end{tabular} 
\end{table} 

Returning to the error I get when I implement the workaround based on the 4.5 code. I actually get the same error when I install from the mac binary (version 4.5.1) (http://cran.r-project.org/web/packages/stargazer/index.html) and simply try to use stargazer, see below.

> install.packages("stargazer")
trying URL 'http://cran.ms.unimelb.edu.au/bin/macosx/contrib/3.0/stargazer_4.5.1.tgz'
Content type 'application/x-tar' length 332917 bytes (325 Kb)
opened URL
==================================================
downloaded 325 Kb

> stargazer(model6,model7,model8,      
+           title="Logistic model summary",
+           align=T,
+           label="Tab:logmod1", 
+           font.size="footnotesize",
+           style="all2")
Error in `rownames<-`(`*tmp*`, value = "visibility") : 
  length of 'dimnames' [1] not equal to array extent

Marek, for your reference I will email the results traceback() to you. Cheers.

Marlonmarlow answered 21/9, 2013 at 3:32 Comment(2)
I'm not a user of stargazer, but I will often "take a shot" at a problem if there is minimal code offered.Poulenc
Thanks for the detailed follow-up. I will examine your issue, and - if necessary - fix in the next release. Note that, without implementing the workaround, 4.5.1 works without any difficulty (just does not default to degrees of freedom output for null and residual deviance).Braiding
B
5

Stargazer author here. It looks like the package's default is not to output degrees of freedom for residual and null deviance. I will consider changing the default in a future release.

As a quick fix for now, you might wish to use the source package (from CRAN), and modify the function .adjust.settings.style in stargazer-internal.R to contain the following:

   if (style == "all") {
      .format.table.parts <<- c("=!","dependent variable label","dependent variables","models","columns","numbers","-","coefficients","-","omit","-","additional","N","R-squared","adjusted R-squared","max R-squared","log likelihood","sigma2","theta(se)*(p)", "SER(df)","F statistic(df)*(p)","chi2(df)*(p)","Wald(df)*(p)","LR(df)*(p)","logrank(df)*(p)","AIC","BIC","UBRE","rho(se)*(p)","Mills(se)*(p)","residual deviance(df)*","null deviance(df)*","=!","notes")  
      .format.coefficient.table.parts <<- c("variable name","coefficient*","standard error","t-stat","p-value")  
    }

    else if (style == "all2") {
      .format.table.parts <<- c("=!","dependent variable label","dependent variables","models","columns","numbers","-","coefficients","-","omit","-","additional","N","R-squared","adjusted R-squared","max R-squared","log likelihood","sigma2","theta(se)*(p)", "SER(df)","F statistic(df)*(p)","chi2(df)*(p)","Wald(df)*(p)","LR(df)*(p)","logrank(df)*(p)","AIC","BIC","UBRE","rho(se)*(p)","Mills(se)*(p)","residual deviance(df)*","null deviance(df)*","=!","notes")  
      .format.coefficient.table.parts <<- c("variable name","coefficient*","standard error")  
    }

Note that the only change here is that I added "(df)*" to "residual deviance" and "null deviance".

Braiding answered 24/9, 2013 at 3:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.