使用R比较回归模型

4

是否有一种工具可用于R,以生成出版准备的回归表格?我正在撰写一篇课程论文,在其中需要比较几个回归模型,如果我能够将它们嵌套在一个单独的表格中,就像这个,来自estout Stata包,那将非常高兴。

我已经检查过xtable,但无法达到相同的结果。任何提示都将不胜感激。

这是我的想法:

Multiple Nested Regressions


嗯...xtable是一个不错的选择。你用它试过什么代码了吗? - David Robinson
1
我之前在交叉验证网站上问过类似的问题,那里有一些很好的答案:http://stats.stackexchange.com/questions/6856/aggregating-results-from-linear-model-runs-r - Chase
1
Hmisc软件包中的“latex”函数是另一个选项,它允许更精细地控制输出。 - joran
2
“publication-ready” 是指哪种出版物?您希望得到可以拖放到Word文档中的内容吗?我们都喜欢LaTeX... - Spacedman
@ David Robinson - 我找不到使用 xtable 嵌套模型的方法。现在我可以发布图片了,希望我的目标会更清晰;@ Chase 和 @ joran,非常感谢你们的帮助。现在我需要想办法嵌套这些模型。@ Spacedman,我没有特定的期刊,只是一篇学期论文。我也在使用Latex,并希望找到一个将表格直接导出到Latex的函数,而不是手动组装模型系数。 - Rafael Magalhães
3个回答

3

3
您可能需要使用'memisc'包中的mtable函数。它有关联的LaTeX输出参数:
==========================================================================
                                              Model 1   Model 2   Model 3 
--------------------------------------------------------------------------
Constant                                     30.628***  6.360*** 28.566***
                                             (7.409)   (1.252)   (7.355)  
Percentage of population under 15            -0.471**            -0.461** 
                                             (0.147)             (0.145)  
Percentage of population over 75             -1.934              -1.691   
                                             (1.041)             (1.084)  
Real per-capita disposable income                       0.001    -0.000   
                                                       (0.001)   (0.001)  
Growth rate of real per-capita disp. income             0.529*    0.410*  
                                                       (0.210)   (0.196)  
--------------------------------------------------------------------------
sigma                                          3.931     4.189     3.803  
R-squared                                      0.262     0.162     0.338  
F                                              8.332     4.528     5.756  
p                                              0.001     0.016     0.001  
N                                             50        50        50      
==========================================================================

这是您得到的LaTeX代码:

texfile123 <- "mtable123.tex"
write.mtable(mtable123,forLaTeX=TRUE,file=texfile123)
file.show(texfile123)
#------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Calls:
% Model 1:  lm(formula = sr ~ pop15 + pop75, data = LifeCycleSavings) 
% Model 2:  lm(formula = sr ~ dpi + ddpi, data = LifeCycleSavings) 
% Model 3:  lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{tabular}{lcD{.}{.}{7}cD{.}{.}{7}cD{.}{.}{7}}
\toprule
&&\multicolumn{1}{c}{Model 1} && \multicolumn{1}{c}{Model 2} && \multicolumn{1}{c}{Model 3}\\
\midrule
Constant                                    &  & 30.628^{***} &&  6.360^{***} && 28.566^{***}\\
                                            &  &  (7.409)     &&  (1.252)     &&  (7.355)    \\
Percentage of population under 15           &  & -0.471^{**}  &&              && -0.461^{**} \\
                                            &  &  (0.147)     &&              &&  (0.145)    \\
Percentage of population over 75            &  &  -1.934      &&              &&  -1.691     \\
                                            &  &  (1.041)     &&              &&  (1.084)    \\
Real per-capita disposable income           &  &              &&   0.001      &&  -0.000     \\
                                            &  &              &&  (0.001)     &&  (0.001)    \\
Growth rate of real per-capita disp. income &  &              &&  0.529^{*}   &&  0.410^{*}  \\
                                            &  &              &&  (0.210)     &&  (0.196)    \\
\midrule
sigma                                       &  &     3.931    &&     4.189    &&     3.803   \\
R-squared                                   &  &     0.262    &&     0.162    &&     0.338   \\
F                                           &  &     8.332    &&     4.528    &&     5.756   \\
p                                           &  &     0.001    &&     0.016    &&     0.001   \\
N                                           &  &    50        &&    50        &&    50       \\
\bottomrule
\end{tabular}

1

xtable 可以做到这一点,但有点像黑客。

取两个线性模型,命名为 lm.x 和 lm.y。

如果您使用以下代码:

myregtables <- rbind(xtable(summary(lm.x)), xtable(summary(lm.y)))

xtable 将生成一个包含两个回归模型的表格。 如果在 LaTeX 中添加 \hline(或者可能是两个),那么它应该看起来不错。 您仍然只有一个标签和标题用于这两个模型。 正如我所说,这是一个有点像黑客的解决方案。


谢谢,里奇。由于这是一种相对常见的呈现数据的方式,我认为一定有更简单的方法来完成它。 - Rafael Magalhães
1
@RafaelMagalhães,编写代码并不难(我最近查看了xtable的内部结构,它们非常清晰)。但这不是我会使用的东西,所以可能需要其他人来完成。 - richiemorrisroe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接