3

在使用 R 构建文档时Sweave,我使用了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、残余偏差和空偏差但没有 df 我可以计算出 df 但会认为这可以直接显示。另见:

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

更新#1:

谢谢马雷克的回复。为了其他遇到这种情况的人的利益,这里是让您形成解决方法的过程:

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

以下是命令:

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() 通过电子邮件发送给您。干杯。

4

1 回答 1

4

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

作为现在的快速修复,您可能希望使用源包(来自 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)*”。

于 2013-09-24T03:29:13.053 回答