在stargazer表中显示自由度

时间:2013-09-21 03:32:44

标签: r stargazer

使用Sweave和R构建文档时,我会使用stargazer库来表。

使用stargazer时,是否有机制显示与glm构建的模型的剩余偏差相关的自由度?

最小代码:

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")

结果观星表将具有观测,对数似然,AIC,剩余偏差和空偏差,但没有d.f.我可以解决d.f.但是会想到这可以直接显示出来。另见:

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

更新#1:

感谢Marek的回复。为了让遇到这种情况的其他人受益,以下是允许您形成工作的过程:

  1. http://cran.r-project.org/src/contrib/Archive/stargazer/
  2. 获取版本4.0(不是4.5 - 我会回到此处)
  3. 在R下的包目录结构中,根据以下答案中的说明编辑“stargazer-internal.R”。
  4. 确保您的R会话中未加载库
  5. 确保您已删除任何现有的stargazer lib
  6. 安装编辑版的stargazer包。
  7. 在R中重新加载库并按常规编译。
  8. 以下是命令:

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

    从命令行:

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

    最后(假设您手头有几个模型),

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

    结果:

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

    回到我基于4.5代码实现变通方法时得到的错误。我从mac二进制文件(版本4.5.1)(http://cran.r-project.org/web/packages/stargazer/index.html)安装时遇到同样的错误,只是尝试使用stargazer,见下文。

    > 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,我将把结果traceback()通过电子邮件发送给您。 欢呼声。

1 个答案:

答案 0 :(得分:2)

Stargazer作者在这里。看起来包的默认值不是输出残差和零偏差的自由度。我将考虑在将来的版本中更改默认值。

作为现在的快速修复,您可能希望使用源包(来自CRAN),并修改stargazer-internal.R中的函数.adjust.settings.style以包含以下内容:

   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")  
    }

请注意,这里唯一的变化是我将“(df)*”添加到“剩余偏差”和“零偏差”。