【问题标题】:creating Kable in R Markdown在 R Markdown 中创建 Kable
【发布时间】:2018-02-15 10:27:11
【问题描述】:

如何将以下模型构造放入 R markdown 中的 kable 中。

>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

所以我首先从 library(knitr) 然后 kable(modelTT, .....) 我不确定这是否正确。

【问题讨论】:

  • 我建议使用stargazer 包。省去了很多麻烦
  • 我已经安装了stargazer。而且我使用代码stargazer(modelTT),看不到输出在哪里。
  • 这个链接有帮助吗? stackoverflow.com/questions/45724432/…

标签: r r-markdown kable


【解决方案1】:

通过保存要打印的对象,然后使用内联 R 函数(相对于块)呈现表格,可以直接在 R Markdown 中使用 kable() 打印来自 R 函数的表格内容。

要从模型中打印回归系数,需要识别包含系数的特定对象并将其打印出来,而不是整个summary() 对象。我们将使用lm() 用一个简单的例子来说明。

summary(lm(...)) 的输出对象是 11 个对象的列表,其中一个对象称为 coefficientscoefficients 对象包含回归斜率、标准误差、t 和概率值。

以下代码可以保存为 Rmd 文件并与 knitr 编织以将这些项目打印为 kable() 表。

---
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)`

当我们将文档编织成 HTML 时,它会生成以下文档。

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多