导出可发布论文的R回归摘要

时间:2015-05-10 04:34:28

标签: r

我在R中有多个回归模型,我想以一种可以包含在出版物中的漂亮表格格式进行总结。我已准备好所有结果,但无法找到导出它们的方法,手动执行此操作效率不高,因为我需要大约20个表格。

所以,我的一个模特是:

felm1=felm(ROA~BC+size+sizesq+age | stateyeard+industryyeard, data=data)

我在R中得到了理想的总结。

但是,我对论文的要求是在表格中只有以下内容,括号中的t统计量和重要性代码( *,等)的估计值。

有没有办法创建包含上述内容的任何类型的表? Lyx,excel,word,.rft,真的。

更好的是,我拥有的另一个模型(有些变量不同):

felm2=felm(ROA~BC+BCHHI+size+sizesq+age | stateyeard+industryyeard, data=data)

我可以在一个表中汇总两个回归(其中相同的变量将在同一行,其他变量会产生空单元格)吗?

提前感谢您,我将不胜感激任何帮助。

这是一个可重复的例子:

 x<-rnorm(1:20)

 y<-(1:20)/10+x

 summary(lm(y~x))



   Coefficients:
            Estimate Std. Error t value Pr(>|t|)    

(Itercept)  1.0539     0.1368   7.702 4.19e-07 ***

  x         1.0257     0.1156   8.869 5.48e-08 ***

这是R中的结果。我希望表中的结果看起来像

 (Itercept)  1.0539*** (7.702)
      X      1.0257*** (8.869)

这可能吗?

2 个答案:

答案 0 :(得分:10)

Broom包非常适合使回归表适合导出。然后可以将结果导出到csv以便用Excel进行调整,或者可以使用Rmarkdown和knitr中的kable函数来生成Word文档(或者乳胶)。

require(broom) # for tidy()
require(knitr) # for kable()

x<-rnorm(1:20)

y<-(1:20)/10+x

model <- lm(y~x)
out <- tidy(model)
out
        term estimate std.error statistic      p.value
1 (Intercept) 1.036583 0.1390777  7.453261 6.615701e-07
2           x 1.055189 0.1329951  7.934044 2.756835e-07

kable(out)


|term        | estimate| std.error| statistic| p.value|
|:-----------|--------:|---------:|---------:|-------:|
|(Intercept) | 1.036583| 0.1390777|  7.453261|   7e-07|
|x           | 1.055189| 0.1329951|  7.934044|   3e-07|

我应该提一下,我现在使用优秀的pixiedust导出回归结果,因为它允许更精细的输出控制,允许用户在R中做更多而在任何其他包中做得更少。

see the vignette on Cran

library(dplyr) # for pipe (%>%) command
library(pixiedust)

dust(model) %>% 
      sprinkle(cols = c("estimate", "std.error", "statistic"), round = 2) %>%
      sprinkle(cols = "p.value", fn = quote(pvalString(value))) %>% 
      sprinkle_colnames("Term", "Coefficient", "SE", "T-statistic", 
                        "P-value")

         Term Coefficient   SE T-statistic P-value
1 (Intercept)        1.08 0.14        7.44 < 0.001
2           x        0.93 0.14        6.65 < 0.001

答案 1 :(得分:3)

对于文本表,请尝试以下方法:

x<-rnorm(1:20)
y<-(1:20)/10+x
result <- lm(y~x)

library(stargazer)
stargazer(result, type = "text")

导致......

===============================================
                        Dependent variable:    
                    ---------------------------
                                 y             
-----------------------------------------------
x                            0.854***          
                              (0.108)          

Constant                     1.041***          
                              (0.130)          

-----------------------------------------------
Observations                    20             
R2                             0.777           
Adjusted R2                    0.765           
Residual Std. Error       0.579 (df = 18)      
F Statistic           62.680*** (df = 1; 18)   
===============================================
Note:               *p<0.1; **p<0.05; ***p<0.01

对于多元回归,只需执行

stargazer(result, result, type = "text")

而且,只是为了提出问题结果。

addStars <- function(coeffs) {
  fb <- format(coeffs[, 1], digits = 4)
  s <- cut(coeffs[, 4],
           breaks = c(-1, 0.01, 0.05, 0.1, 1),
           labels = c("***", "**", "*", ""))
  sb <- paste0(fb, s)
}
addPar <- function(coeffs) {
  se <- format(coeffs[, 2], digits = 3)
  pse <- paste0("(", se, ")")
}
textTable <- function(result){
  coeffs <- result$coefficients
  lab <- rownames(coeffs)
  sb <- addStars(coeffs)
  pse <- addPar(coeffs)
  out <- cbind(lab,sb, pse)
  colnames(out) <- NULL
  out
}
print(textTable(result), quote = FALSE)

一旦有了文本表,就可以使用xtable::xtableHmisc::latexGmisc::htmltable等。有人在评论中发布了一个链接。 :)

相关问题