Regression tables in Markdown format (for flexible use in R Markdown v2)

后端 未结 6 1508
感动是毒
感动是毒 2020-12-13 00:49

The new version of R Markdown is based on pandoc, so you can easyly change the output format.

My Problem is to get markdown formated tables from e.g. regression mode

相关标签:
6条回答
  • 2020-12-13 01:24

    My above comment in more details:

    1. Define a few models for reproducible example:

      lm0 <- lm(hp ~ wt, mtcars)
      lm1 <- lm(qsec ~ hp, mtcars)
      lm2 <- lm(qsec ~ wt, mtcars)
      
    2. Create a comparative table from those:

      require(memisc)
      mtable123 <- mtable('Model 1' = lm0,
                  'Model 2' = lm1,
                  'Model 3' = lm2,
                  summary.stats = c('R-squared','F','p','N'))
      
    3. Render markdown table with a simple call to pander:

      pander(mtable123)
      
    4. Enjoy the result:

      --------------------------------------------------
           &nbsp;        Model 1    Model 2    Model 3  
      ----------------- ---------- ---------- ----------
       **(Intercept)**   -1.821\   20.556***\ 18.875***\
                         (32.325)   (0.542)    (1.103)  
      
           **wt**       46.160***\     \       -0.319\  
                         (9.625)               (0.328)  
      
           **hp**           \      -0.018***\     \     
                                    (0.003)             
      
        **R-squared**     0.434      0.502      0.031   
      
            **F**         22.999     30.190     0.945   
      
            **p**         0.000      0.000      0.339   
      
            **N**           32         32         32    
      --------------------------------------------------
      

    Thanks for Roman Tsegelskyi for implementing this nice feature in GSoC 2014.

    0 讨论(0)
  • 2020-12-13 01:32

    The huxtable package can now print nicely formatted regression table. See the documentaton https://hughjonesd.github.io/huxtable/huxreg.html

    0 讨论(0)
  • 2020-12-13 01:46

    Just generate the HTML or LATEX tables. All you have to do is to just add results='asis' to the code chunk. It will leave the output as it is.

    For example this code using xtable works for me.

    ```{r,results='asis'}
    x<-rnorm(100)
    y<-rnorm(100)
    lm <- lm(y~x)
    library(xtable)
    print(xtable(summary(lm)),type='html')
    ```
    
    0 讨论(0)
  • 2020-12-13 01:47

    Another way to use sjPlot (great library for easy presentation of regression output) is to use the no.output feature:

         library(sjmisc)
         library(sjPlot)
         library(magrittr)
    
         lm(qsec ~ wt, mtcars) %>% 
           sjt.lm(no.output = TRUE, show.se = TRUE) %>% 
           return() %>% .[["knitr"]] %>% asis_output
    
    0 讨论(0)
  • 2020-12-13 01:50

    Here what I did some hours ago:

    1. Some data:

      ```{r}
      lm1 <- lm(qsec ~ hp, mtcars)
      lm2 <- lm(qsec ~ wt, mtcars)
      ```
      
    2. We use the package sjPlot

      ```{r}
      library(sjPlot)
      tab_model(lm1,lm2, file="output.html")# You have to save the table in html format.
      ```
      

      The next part needs to be outside the chunk in markdown:

    htmltools::includeHTML("output.html")

    0 讨论(0)
  • 2020-12-13 01:50

    I'm updating this with an example that uses the popular (and newer) knitr and kableExtra packages.

    library(knitr)
    library(xtable)
    
    lm(hp ~ wt, mtcars) %>%
    summary() %>%
    xtable() %>%
    kable()
    

    Now you can access all the cool table-formatting features available in Hao Zhu's kableExtra package.

    0 讨论(0)
提交回复
热议问题