15

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?

Tazo Lezhava
  • 151
  • 1
  • 1
  • 4
  • 1
    It would help if you included a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with some sample input data and desired output. Be clear what package the `flem()` function domes form. Is LaTeX output acceptable? – MrFlick May 10 '15 at 04:38
  • Unfortunately I can't post images as I'm new. But I think the answer to a general regression would suffice. So summary(fittedmodel) returns the estimates, st. error, etc. but I want to only export the estimates, t-statistic in brackets, and the significance in terms of # of stars. I'm using package 'lfe' but for the sake of simplicity let's assume it's a regular lm regression. I'm not familiar with Latex, but isn't it similar to lyx? – Tazo Lezhava May 10 '15 at 04:55
  • LyX is a frontend to LaTeX. You can use LaTeX markup in it. – Max Candocia May 10 '15 at 05:44
  • If you want to make one table, is it really worth the effort to customize the default-ish behavior, and wouldn't it be easier if you just made the table by hand? – Roman Luštrik May 10 '15 at 05:47
  • ok, thanks. So do you know how to export this to LaTeX? – Tazo Lezhava May 10 '15 at 05:47
  • well, not really because I have more than 20 regressions, each of them has on average 7-8 variables, and for each variable I need to have estimate, (t-statistic) and significance, so more than 400 inputs :). I don't want one table, I want to put groups of 2 or 3 together in one table, but if anyone can help me to export separate tables, that would also help since I could combine tables by hand. – Tazo Lezhava May 10 '15 at 05:49
  • if anyone can help me how to access the significance codes for each covariate, I can even write my own package for exporting. I just don't know how to get those stars. – Tazo Lezhava May 10 '15 at 05:53
  • First, you probably like `stargazer' package. And, if that doesn't solve your problem, you can access $coefficients from summary.lm object. You can write your function to make a result table. Since you don't post a reproducible example, I can't answer your question exactly. Is it sufficient to have a *relevant* answer (e.g. using demo from lm help document)? – KenM May 10 '15 at 07:05
  • http://stackoverflow.com/questions/5465314/tools-for-making-latex-tables-in-r – kristang May 10 '15 at 09:10
  • @TazoLezhava For regression, you may use `a<-summary(felm2); MVA<-cbind(a[["conf.int"]], a[['coefficients']][,'Pr(>|z|)'] ); write.csv(MVA, file="MVA.csv" )` – Mohamed Rahouma Jul 03 '20 at 16:41

2 Answers2

17

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
r.bot
  • 5,309
  • 1
  • 34
  • 45
8

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. :)

KenM
  • 2,756
  • 1
  • 13
  • 13