gpt4 book ai didi

r - 在 stargazer 表中显示自由度

转载 作者:行者123 更新时间:2023-12-04 02:45:36 24 4
gpt4 key购买 nike

在使用 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")

生成的 stargazer 表将包含观测值、对数似然、AIC、残差和零偏差,但没有 d.f。我可以锻炼 d.f.但本来以为这可以直接显示。另见:

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

更新 #1:

感谢 Marek 的回复。为了其他遇到此问题的人的利益,以下是让您形成解决方法的过程:

  1. http://cran.r-project.org/src/contrib/Archive/stargazer/ 获取版本 4.0(不是 4.5 - 我会回来讨论这个)
  2. 在 R 下的包目录结构中,按照以下答案中的说明编辑“stargazer-internal.R”。
  3. 确保库未加载到您的 R session 中
  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() 通过电子邮件发送给您。干杯。

最佳答案

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

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

关于r - 在 stargazer 表中显示自由度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18928608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com