【问题标题】:Comparing regression models with R将回归模型与 R 进行比较
【发布时间】:2012-05-15 14:27:24
【问题描述】:

R 中是否有可用的工具来生成可供发布的回归表?我正在写一篇课程论文,我需要在其中比较几个回归模型,如果我能将它们嵌套在 this one 这样的单个表中,我会非常高兴,来自 estout Stata 包。

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

这是我的想法:

【问题讨论】:

  • 嗯... xtable 是一个不错的选择。你用它试过什么代码?
  • 不久前我在交叉验证上问了一个类似的问题,那里有一些很好的答案:stats.stackexchange.com/questions/6856/…
  • Hmisc 包中的latex 函数是另一个选项,可以更精细地控制输出。
  • “准备出版”什么出版物?您是否期望可以将某些内容拖放到 Word 文档中?我们都喜欢 LaTeX...
  • @David Robinson - 我找不到使用xtable 将模型嵌套在表格内的方法。现在我可以发布图片了,我希望我的目标会变得更加清晰; @Chase 和 @joran 看起来很有帮助,谢谢。现在我需要想办法嵌套模型。 @Spacedman 我没有任何特别的期刊,它只是一篇学期论文。我也在使用 Latex,希望找到一个函数将表格直接导出到 Latex 中,而不是使用模型中的系数手动组装它。

标签: r stata


【解决方案1】:

你可能想要'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}

【讨论】:

    【解决方案2】:

    R wikibook 有一些关于 R 中生产质量输出的良好资源。

    我认为 wikibook 中列出的来自 Paul Johnson 的这个函数正是您正在寻找的:

    http://pj.freefaculty.org/R/WorkingExamples/outreg-worked.R

    我编辑了该函数以供我自己使用以使用 booktabs 格式并允许具有额外属性的模型:

    http://chandlerlutz.com/R/outregBkTabs.r

    【讨论】:

      【解决方案3】:

      xtable 可以做到这一点,但它有点像 hack。

      采用两个线性模型,分别命名为 lm.x 和 lm.y。

      如果你使用下面的代码:

      myregtables &lt;- rbind(xtable(summary(lm.x)), xtable(summary(lm.y)))

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

      【讨论】:

      • 谢谢,里奇。由于这是一种比较常见的数据呈现方式,我想应该有一种更简单的方法。
      • @RafaelMagalhães 编写代码并不难(我最近查看了 xtable 内部结构,它们非常清楚)。虽然它不是我会使用的东西,所以它可能是别人的工作。
      猜你喜欢
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      相关资源
      最近更新 更多