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

Different geom_smooth and lm() estimates in R: forgot to add base slope and interaction slope together

我想弄清楚为什么我的 lm() 估计与相同数据和公式的 geom_smooth 不同。具体来说,我的分组变量 "cat" 级别 5 的斜率在 lm() 输出中为 >0,但在 geom_smooth 中为 <0(因此该图似乎没有反映摘要 table).

这里是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() 的摘要 table。谢谢!

我觉得一切都很好。 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

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