首页 文章

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

提问于
浏览
0

我试图找出为什么我的 lm() 估计值与 geom_smooth 的相同数据和公式不同 . 具体来说,我的分组变量"cat" level 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 回答

  • 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
    

    所以我可以看到,这两件事是一致的 .

相关问题