R中的不同geom_smooth和lm()估计:忘记将基坡和交互斜率加在一起

时间:2017-07-16 19:49:57

标签: r ggplot2 lm

我试图弄清楚为什么我的lm()估算值与geom_smooth的相同数据和公式不同。特别是我的分组变量" cat"级别5在lm()输出中> 0,但在geom_smooth中小于0(因此该图似乎不反映摘要表)。

以下是the data。 (比提供行为相似的示例数据更容易。)

模型:summary(lm(data=df, y~x*cat))

请注意x:cat5的斜率为> 0。

情节:

library(ggplot2)
plt <- ggplot(df, aes(x=x, y=y, group=cat)) +
    geom_smooth(method="lm", show.legend=FALSE) +
    facet_wrap(~cat, nrow=1) +
    geom_point(aes(color=color)

获取geom_smooth估算值(关注@Pedro Aphalo的答案here):

library(ggpmisc)     
my.formula <- y~x
plt + stat_poly_eq(formula = my.formula, 
            aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
            parse = TRUE)

注意方面5中的斜率<0。 lm()geom_smooth使用不同的平方和或其他内容吗?我在论文中报告哪个版本?如果可能的话,我想让两人达成一致,这样我就可以使用geom_smooth的情节和论文中lm()的汇总表。谢谢!

1 个答案:

答案 0 :(得分:3)

这一切对我来说都是正确的。 cat5的摘要行是:

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  1.932248   0.053131  36.368  < 2e-16 ***
x           -0.006651   0.001962  -3.389 0.000721 ***
...
cat5        -1.080554   0.075138 -14.381  < 2e-16 ***
...
x:cat5       0.005602   0.002775   2.019 0.043720 *  

这意味着cat5的斜率是x的整体斜率加上x:cat5相互作用的斜率:

> -0.006651+0.005602
[1] -0.001049

在图中我看到-0.00105

截距显示为0.852,即

> 1.932248+(-1.080554)
[1] 0.851694

据我所知,这两件事是一致的。

相关问题