簡體   English   中英

Exact latex output for R summary of lm function in Rmarkdown pdf output

[英]Exact latex output for R summary of lm function in Rmarkdown pdf output

當我們像這樣安裝線性 model 時,

x <- c(1.52, 1.6, 1.68, 1.75, 1.83)
y <- c(1.69, 1.74, 1.80, 1.93, 2.0)

fit <- lm(y ~ x)

summary(fit)

Output

Call:
lm(formula = y ~ x)

Residuals:
        1         2         3         4         5 
 0.021416 -0.012387 -0.036190  0.020482  0.006679 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept)  0.07633    0.19447   0.392  0.72093   
x            1.04754    0.11579   9.047  0.00285 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.0282 on 3 degrees of freedom
Multiple R-squared:  0.9646,    Adjusted R-squared:  0.9529 
F-statistic: 81.85 on 1 and 3 DF,  p-value: 0.002852

所以,我想要的是這個“精確”表的 latex output 用於Rmarkdown pdf Z78E6621F6398381DZ 文件。 我不希望生成的這樣的 output 會更改表或不包含該表中的某些信息。 正如我的意思,完全相同的 output 但在latex output 中。

一個示例 output 將類似於此圖像,但對於單次回歸 model並且還包含由summary(lm(y~x))生成的所有信息

在此處輸入圖像描述

注意:我使用過這樣的庫sjPlotstargazertexreg等。
目前我正在使用R version 4.1.2

使用echo=FALSE會產生您想要的結果。

---
output: pdf_document
---

```{r, echo=FALSE}
x <- c(1.52, 1.6, 1.68, 1.75, 1.83)
y <- c(1.69, 1.74, 1.80, 1.93, 2.0)

fit <- lm(y ~ x)

summary(fit)
```

在此處輸入圖像描述

我是制作表格的modelsummary的忠實粉絲

library(tidyverse) 
library(modelsummary)
library(broom)
library(kableExtra)


 df = tibble(x = c(1.52, 1.6, 1.68, 1.75, 1.83),
             y = 1.69, 1.74, 1.80, 1.93, 2.0 )

fit <- lm(y ~ x , data = df)

tidy(fit)


modelsummary(fit,
             output = "kableExtra")

這將產生一個非常基本的LaTeX表。 但是,如果您想按 dvs 對它們進行分組,您可以在估計模型的地方執行類似的操作,然后使用broom ,然后

data("iris")

iris = iris %>% 
  mutate(versicolor = ifelse(Species == "versicolor", 1, 0), 
         virginica = ifelse(Species == "virginica", 1, 0))


## pass it to tidy for each model
tidy(model1)

modelsummary(list( model1 , model2, model3, 
                        model4,  model5, model6),
             output = "kableExtra")  %>%
  add_header_above(c(" " = 1 , "Petal Length" = 3 , "Sepal Width" = 3))


這應該讓你接近你正在尋找的東西! 您可能需要查看kableExtra中的更多選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM