【问题标题】:Save the result from "summary(lm)" to use in PowerBi保存“summary(lm)”的结果以在 PowerBi 中使用
【发布时间】:2020-07-12 01:12:34
【问题描述】:

是否可以以 PowerBI 中可用的格式保存摘要 (lm) 对象?

让我们说:

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 中显示的绘图对象。

【问题讨论】:

    标签: r ggplot2 lm


    【解决方案1】:

    This example 应该对你有用。在这里申请:

    plot(hwy ~ displ, data=mpg)
    abline(lm1)
    

    对于ggplot2 解决方案,这是可行的:

    ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_smooth(method='lm', formula=y~x)
    

    【讨论】:

    • 不,不会。因为我想添加超过 1 个预测器。我想要包含所有估计值、标准值等的汇总表。
    【解决方案2】:

    试试这样的:

    fit <- lm(hwy ~ displ, data = mpg)
    txt = capture.output(print(summary(fit)))
    
    plot(NULL,xlim=c(-1,1),ylim=c(-1,1),xaxt="n",yaxt="n",bty="n",xlab="",ylab="")
    text(x=0,y=0,paste(txt,collapse="\n"))
    

    您可能需要考虑使用 stringr::str_pad 来使文本更漂亮.. 但这应该会让您得到一些有用的东西。

    这就是你把它放在ggplot2上的方式:

    ggplot() + xlim(c(-1,1)) + ylim(c(-1,1)) + 
    geom_text(aes(x=0,y=0,label=paste(txt,collapse="\n"))) + 
        theme_void()
    

    【讨论】:

    • 非常酷。如果您愿意,可以左对齐hjust = 0
    • 不客气..其实是个很有趣的问题:)
    猜你喜欢
    • 1970-01-01
    • 2013-08-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多