简体   繁体   English

使用回归 output 外部块和内部文本 R markdown

[英]Using regression output outside chunk and inside text in R markdown

I am doing repeated analysis for various regression techniques in a report I am doing.我正在对我正在做的报告中的各种回归技术进行重复分析。 I can do the regression fine, however I would like to get the regression output and put it into text.我可以很好地进行回归,但是我想获得回归 output 并将其放入文本中。 I make a reproducible example below:我在下面做了一个可重现的例子:

linearMod <- lm(dist ~ speed, data=cars)  # build linear regression model on full data
print(linearMod)

With the output being: output 为:

Call:
lm(formula = dist ~ speed, data = cars)

Coefficients:
(Intercept)        speed  
    -17.579        3.932  

How would I get this into some R markdown text.我怎样才能把它变成一些 R markdown 文本。 For example, I would want to write: The linear regression model found that dist=-17.579+3.932Speed, with speed being statistically significant比如我想写:线性回归model发现dist=-17.579+3.932Speed,速度统计显着

I realise I can do this manually by typing, but this becomes more difficult and cumbersome when there are many variables involved, which in my case there can be around 10, with multiple regression equations in the report.我意识到我可以通过键入手动执行此操作,但是当涉及许多变量时,这变得更加困难和繁琐,在我的情况下可能有大约 10 个,报告中有多个回归方程。

You can display any R variable/expression in Markdown using `r VariableName您可以使用 `r VariableName 在 Markdown 中显示任何 R 变量/表达式
On your example:在你的例子中:

The linear regression model found that dist=`r linearMod$coefficients[1]` + `r linearMod$coefficients[2]` Speed.

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM