是否可以将摘要(lm)对象保存为PowerBI中可用的格式?
让我们说以下:
data <- mpg
lm <- lm(hwy ~ displ, data = mpg)
summary(lm)
输出:
Call:
lm(formula = hwy ~ displ, data = mpg)
Residuals:
Min 1Q Median 3Q Max
-7.1039 -2.1646 -0.2242 2.0589 15.0105
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 35.6977 0.7204 49.55 <2e-16 ***
displ -3.5306 0.1945 -18.15 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.836 on 232 degrees of freedom
Multiple R-squared: 0.5868, Adjusted R-squared: 0.585
F-statistic: 329.5 on 1 and 232 DF, p-value: < 2.2e-16
我会把这些信息保存为gglpot2对象或一般的图片,然后将其显示在Power BI中。这样我们就可以将其用作Power BI内部快速回归的模板。这是因为Power BI仅可以显示R代码,该代码会生成“图”而不是文本。
我尝试过:
textplot(capture.output(summary(lm)))
但是我首先遇到这个错误:
>install.packages('textplot')
Warning in install.packages :
package ‘textplot’ is not available (for R version 3.5.3)
不幸的是,Power BI不支持textplot()。
编辑:澄清,我不是想绘制回归线也不是平面。我正在寻找一种将“ summary(lm)”中的文本输出保存为可以在Power BI中显示的绘图对象的方法。
This example应该对您有用。在这里申请:
plot(hwy ~ displ, data=mpg)
abline(lm1)
对于ggplot2
解决方案,此方法有效:
ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_smooth(method='lm', formula=y~x)