Reputation: 1037
how can I put the following model construction into a kable in R markdown.
>modelTT <- glm(formula
,family=gaussian(link = "identity"), data=dataL_TT)
>regr_tab_NM(modelTT)
Estimate Pr(>|t|)
(Intercept) -2.6077 < 0.001
Days_diff_Eff_Subm_2 -0.0114 < 0.001
TR_BS_BROKER_ID_360_2 0.0344 < 0.001
TR_BS_BROKER_ID_360_M 0.8551 < 0.001
RURALPOP_P_CWR_2 -0.0083 < 0.001
RURALPOP_P_CWR_M -0.7106 < 0.001
TR_B_BROKER_ID_360 0.0241 < 0.001
TR_SCW_BROKER_ID_360 -0.0005 < 0.001
PIP_Flag 3.5838 < 0.001
TR_BS_BROKER_INDIVIDUAL_720_2 0.0357 < 0.001
TR_BS_BROKER_INDIVIDUAL_720_M -0.0780 < 0.001
Resolved_Conflictless5m 1.1547 < 0.001
Resolved_Conflict5mTo1d 1.5352 < 0.001
Resolved_Conflictafter1d 2.1279 < 0.001
Priority_2Other -1.1499 < 0.001
So first I started with library(knitr) then kable(modelTT, .....) I'm not sure if this is correct.
Upvotes: 0
Views: 1696
Reputation: 10875
One can print tabular content from R functions directly in R Markdown with kable()
by saving the object to be printed, and then using an inline R function (vs. a block) to render the table.
To print the regression coefficients from a model, one needs to identify the specific object that contains the coefficients and print it, rather than the entire summary()
object. We'll illustrate with a simple example using lm()
.
The output object from summary(lm(...))
is a list of 11 objects, one of which is called coefficients
. The coefficients
object contains the regression slopes, standard errors, t and probability values.
The following code can be saved as an Rmd file and knit with knitr
to print these items as a kable()
table.
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Stackoverflow question [48804938](https://stackoverflow.com/questions/48804938/creating-kable-in-r-markdown) asked how to take output from a linear model and render it with kable in R Markdown. We'll run a `lm()` with the `mtcars` data frame and print the coefficients table with kable.
```{r regression, echo = TRUE, warning = FALSE, eval = TRUE}
library(knitr)
fit <- lm(mpg ~ am + disp + wt, data = mtcars)
theStats <- summary(fit)
```
At this point we can print the coefficients table with `theStats$coefficients` as the argument to the `kable()` function.
`r kable(theStats$coefficients)`
When we knit the document to HTML, it produces the following document.
Upvotes: 1