1

I'm trying to figure out why my lm() estimates are different than the geom_smooth for the same data and formula. Specifically the slope for my grouping variable "cat" level 5 is >0 in the lm() output, but <0 in the geom_smooth (and hence the plot does not appear to reflect the summary table).

Here are the data. (Easier than coming up with example data that behave similarly.)

The model: summary(lm(data=df, y~x*cat))

Notice the slope for x:cat5 is >0.

The plot:

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)

Get the geom_smooth estimates (following @Pedro Aphalo's answer here):

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

Notice the slope in facet 5 is <0. Are lm() and geom_smooth using a different sum of squares or something? Which version do I report in a paper? If possible, I'd like to get the two to agree so I can use the plot with geom_smooth and the summary table from lm() in the paper. Thanks!

BonnieM
  • 191
  • 1
  • 13
  • Can you add the output from `summary` for the data (and the model) so we can be sure we've read the data in correctly. – Spacedman Jul 16 '17 at 19:56
  • @Spacedman, your output below confirms you've read the data correctly. I'm not going to update the question with the output since you solved the question below. – BonnieM Jul 17 '17 at 00:47

1 Answers1

4

It all looks right to me. The summary lines for cat5 are:

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 *  

Which means the slope for cat5 is the overall slope for x plus the slope for the x:cat5 interaction:

> -0.006651+0.005602
[1] -0.001049

and on the plot I see -0.00105

The intercept is shown as 0.852 which is

> 1.932248+(-1.080554)
[1] 0.851694

So as far as I can see, the two things agree.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Oh my gosh, @Spacedman. (Self forehead slap!) Thank you for catching my simple mistake. I was forgetting to add the x slope to the interaction slope! Many thanks!!! – BonnieM Jul 17 '17 at 00:45