Exporting R regression summary for publishable paper

前端 未结 2 1887
再見小時候
再見小時候 2020-12-14 22:48

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\

2条回答
  •  清歌不尽
    2020-12-14 23:23

    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
    

提交回复
热议问题