如何使用反引号从 rmakrdown 文档中的回归输出中引用估计值?

How to refer to an estimate value from the regression output in rmakrdown document using back quote?

我知道如何在我的 rmarkdown 文档中引用来自 r stat 分析的数值,例如,将 r round(x.peaks[,2],3)[1] 放在 反引号 之间而无需更新每当值发生变化时。但我想知道是否有办法对回归输出的估计系数值做同样的事情。因此,例如,我想使用反引号将截距 -0.32958(请参阅 table)放在我的 rmarkdown 文档中,而不必在每次根据数据集生成不同的输出时键入或更新。

lm(formula = log(p_share) ~ xxx, data = df2)

Residuals:
     Min       1Q   Median       3Q      Max 
-3.04165 -0.37272 -0.00279  0.48895  1.16493 

Coefficients: (2 not defined because of singularities)
                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)        -0.32958    0.05525  -5.965 1.62e-08 ***
xxx             0.03313    0.11835   0.280     0.78    
  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.6103 on 154 degrees of freedom
  (2 observations deleted due to missingness)
Multiple R-squared:  0.0005087, Adjusted R-squared:  -0.005981 
F-statistic: 0.07838 on 1 and 154 DF,  p-value: 0.7799

你应该使用包 broom.

例子:

title: "Untitled"
header-includes:    
- \usepackage[utf8]{inputenc}
output:
  pdf_document:
    latex_engine: xelatex
---

```{r}

library(broom)

#our data
df = data.frame(x=rnorm(10),y=rnorm(10))

#our model
model <- lm(y ~ x, data=df)

t_tab <- tidy(model)

t_tab
```

Your coef is `r round(t_tab$estimate[1],3)`