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:
- Obtain version 4.0 (not 4.5 - I'll come back to this) from http://cran.r-project.org/src/contrib/Archive/stargazer/
- Within the package directory structure under R, edit "stargazer-internal.R" as per the instructions in the answer below.
- Ensure that the library is not loaded in your R session
- Ensure that you have removed any existing stargazer lib
- Install the edited version of the stargazer package.
- 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.
stargazer
, but I will often "take a shot" at a problem if there is minimal code offered. – Poulenc