how to add non-interaction curves?

34 views
Skip to first unread message

Vinícius Rodrigues

unread,
Dec 31, 2017, 10:49:38 AM12/31/17
to ggplot2
Hi,

I'm using GLM and I want to fit the curves from my model (m3). There is no interaction between the X-variables in the model, but the lines interact in the plot. What I'm doing wrong?

The example:

library(ggplot2)

varY
<-c(0,10,20,30,40,50,20,30,43,28,5,32,23,1)

varX1
<-c(0,11,22,31,44,50,24,34,12,45,5,1,5,2)

varX2
<-c("A","B","A","B","C","C","A","B","C","A","B","C","C","B")

data
<-data.frame(varY,varX1,varX2)

theme_set
(theme_bw())

m3
<- glm(
  varY
~ varX1 + varX2,
  data
= data
)

pre
<-cbind(data,predict(m3,interval="confidence"))

gg
<- ggplot(pre, aes(x = varX1, y = varY, group = varX2, color = factor(varX2))) +
  geom_point
() + geom_smooth(method=glm, se=FALSE) + #geom_line()+
  labs
(subtitle="Unknow data", title="Example plot", y="Categorical Y-Var", x="Categorical X-Var1") +
  scale_x_continuous
(limits=c(0,55))

gg

Many thanks

Roman Luštrik

unread,
Dec 31, 2017, 10:57:32 AM12/31/17
to Vinícius Rodrigues, ggplot2
It's not entirely clear to me what you mean. Your model doesn't include an interaction term at all, so you're not testing for any interaction.

Even if you had, the interaction term is statistically not significantly different from zero.

Call:
glm(formula = varY ~ varX1 * varX2, data = data)

Deviance Residuals: 
   Min      1Q  Median      3Q     Max  
-8.694  -2.444   0.283   2.081   8.930  

Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept)    2.8086     4.6681   0.602  0.56406   
varX1          0.6238     0.1681   3.711  0.00595 **
varX2B        -3.0175     6.0341  -0.500  0.63048   
varX2C        27.1888     5.8639   4.637  0.00167 **
varX1:varX2B   0.3045     0.2460   1.238  0.25089   
varX1:varX2C  -0.2844     0.2048  -1.389  0.20229   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for gaussian family taken to be 28.67115)

    Null deviance: 3138.86  on 13  degrees of freedom
Residual deviance:  229.37  on  8  degrees of freedom
AIC: 92.878


If you enable the confidence intervals (geom_smooth(method=glm, se=TRUE)), it should become apparent why the interaction term is non-significant.

Cheers,
Roman


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

Vinícius Rodrigues

unread,
Dec 31, 2017, 1:28:20 PM12/31/17
to ggplot2
Hi, thanks.

There is no interaction because in my model (m3 above). My model is y ~ x1 + x2, not y ~ x1 * x2. So, the lines should be parallels.

My doubt is not in the statistical stuff, but in ggplot. Even with my model without interaction, the plot is giving me interaction, the lines should be paralles. What I'm doing wrong?

Ben Bolker

unread,
Dec 31, 2017, 1:46:29 PM12/31/17
to ggp...@googlegroups.com

This is too complex a model specification for geom_smooth() to handle.
geom_smooth() fits models *separately* to each of the data groups.

interval="confidence" only works for lm, so that's what I've used
here. If you really want a non-Gaussian glm, you'll have to work a bit
harder.

If you want to restrict the predicted lines to the ranges of the data
for each group (as geom_smooth does by default), that's also a bit more
work.


m3 <- lm(
varY ~ varX1 + varX2,
data = data
)

pframe <- expand.grid(varX1=0:50,varX2=unique(varX2))
pp <- predict(m3,newdata=pframe,interval="confidence")
colnames(pp) <- c("varY","lwr","upr")
pre <- cbind(pframe,pp)

gg <- ggplot(data, aes(x = varX1, y = varY, color = varX2)) +
geom_point() +
geom_line(data=pre)+
geom_ribbon(data=pre,
color = NA, aes(fill=varX2, ymin=lwr, ymax=upr), alpha=0.4)
> --
> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
> <https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_hadley_devtools_wiki_Reproducibility&d=DwMFaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=HuvR6XLa8uI_fyvTlxHV7g&m=ZOBri4LA2FODTnCnP8n3RSOOPb_MdGIlqw0YAU_RcIM&s=sdjXpblOP3T92tSJT69DlgddPoECma1-wrBQK-m9xH8&e=>
>  
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
> <https://urldefense.proofpoint.com/v2/url?u=http-3A__groups.google.com_group_ggplot2&d=DwMFaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=HuvR6XLa8uI_fyvTlxHV7g&m=ZOBri4LA2FODTnCnP8n3RSOOPb_MdGIlqw0YAU_RcIM&s=VVfpM3ZrYjX3T7unhAvjCKCjL6i7K7v6yQrJZ4b9UkU&e=>
>
> ---
> You received this message because you are subscribed to the Google
> Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ggplot2+u...@googlegroups.com
> <mailto:ggplot2+u...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout
> <https://urldefense.proofpoint.com/v2/url?u=https-3A__groups.google.com_d_optout&d=DwMFaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=HuvR6XLa8uI_fyvTlxHV7g&m=ZOBri4LA2FODTnCnP8n3RSOOPb_MdGIlqw0YAU_RcIM&s=YnHAzvx0Hhy-Pax7gmHXIrm37KcjFXBKX_4nGyBLJKM&e=>.

Alexander Stevens

unread,
Dec 31, 2017, 4:41:54 PM12/31/17
to Ben Bolker, ggp...@googlegroups.com
Hi.

You aren’t doing anything wrong. The slopes are not, statistically speaking,  significantly different. The plot simply provides the best fit of the model to each group. The lines cross because the slopes differ (albeit, not significantly).

Alex
 
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages