Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting R regression summary for publishable paper

Tags:

r

I have multiple regression models in R, which I want to summarize in a nice table format that could be included in the publication. I have all the results ready, but couldn't find a way to export them, and it wouldn't be efficient to do this by hand as I need about 20 tables.

So, one of my models is:

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

And I'm getting desired summary in R.

However, what I want for my paper is to have only the following in the table, the estimates with t-statistic in the brackets and also the significance codes (*,,etc.).

Is there a way to create any type of table which will include the above? Lyx, excel, word, .rft, anything really.

Even better, another model that I have is (with some variables different):

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

could I have summary of the two regressions combined in one table (where same variables would be on the same row, and others would produce empty cells)?

Thank you in advance, and I'll appreciated any attempt of help.

Here is a reproducible example:

 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 ***

This is the result in R. I want the result in a table to look like

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

Is this possible?

like image 642
Tazo Lezhava Avatar asked May 10 '15 04:05

Tazo Lezhava


People also ask

How do I save regression results in R?

save. image(file="mysession. RData") will save all of the objects in your current workspace to a file (which can be read back into R via load("mysession. RData") ).

How do you interpret an R in summary?

The model summary table reports the strength of the relationship between the model and the dependent variable. R, the multiple correlation coefficient, is the linear correlation between the observed and model-predicted values of the dependent variable. Its large value indicates a strong relationship.


2 Answers

The Broom package is very good for making regression tables nice for export. Results can then be exported to csv for tarting up with Excel or one can use Rmarkdown and the kable function from knitr to make Word documents (or latex).

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|

I should mention that I now use the excellent pixiedust for exporting regression results as it allows much finer control of the output, allowing the user to do more in R and less in any other package.

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
like image 160
r.bot Avatar answered Oct 14 '22 19:10

r.bot


For text table, try this:

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

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

results in...

===============================================
                        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

For multiple regression, just do

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

And, just for the sake of making the asked outcome.

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)

You can use xtable::xtable, Hmisc::latex, Gmisc::htmltable etc. once you have a text table. Someone posted a link in comments. :)

like image 24
KenM Avatar answered Oct 14 '22 19:10

KenM