0

I try to add my (multiple) linear regression lines on my ggplot. I have two dummies (morning-evening) for the morning.dummy. The plot is correct but it gives me an error when I want to add the regression lines. Here is the code:

regression_1 <- lm(weight  ~ morning.dummy + dayNumber + (morning.dummy*dayNumber) + 
                     I(dayNumber^2) + (I(dayNumber^2)*morning.dummy),
                   data=weight_data)
summary(regression_1)
#plot
plot2 <- ggplot(data=weight_data,aes(x=dayNumber, y=weight, color=morning.dummy)) +
          geom_point()+
          stat_smooth(method = "lm", formula = weight  ~ morning.dummy + dayNumber + (morning.dummy*dayNumber) +I(dayNumber^2) + (I(dayNumber^2)*morning.dummy), size = 1) +
          labs(y = "Weight in kg", x = "Day Number of weight measurment", subtitle = "Day 0 = 3 October 2010")
plot2

This is the error:

Error in grid.Call.graphics(C_setviewport, vp, TRUE) : 
  non-finite location and/or size for viewport
In addition: Warning message:
Computation failed in `stat_smooth()`:
object 'dayNumber' not found 

Does someone have an idea where I do something wrong?

Sarah.d
  • 123
  • 1
  • 2
  • 12
  • 1
    You can only plot regression models with one DV in ggplot2. However, `stat_smooth(method = "lm", formula = weight ~ poly(daynumber, degree = 2), size = 1)` should give the same prediction as your model. – Roland Dec 14 '17 at 14:16
  • Thank you, I tried it bu still get the error 'dayNumber not found. But it is one of my variable's name in my dataset.. What can be the problem here then? – Sarah.d Dec 14 '17 at 14:18
  • 1
    Yes, I forgot. It has to be `stat_smooth(method = "lm", formula = y ~ poly(x, degree = 2), size = 1)`. See here: https://stackoverflow.com/a/25031125/1412059 – Roland Dec 14 '17 at 14:21
  • Roland, why would modeling the relationship of dayNumber on weight as quadratic yield the same results as the model including morning.dummy and the interaction effect? – tifu Dec 14 '17 at 14:22
  • 1
    @tifu Yes, if there is an interaction with each polynomial coefficient as in this example, predictions from `y ~ poly(x1, degree = 2) * x2` should be the same as predictions from separate polynomial fits for each factor level as ggplot2 would fit. But of course, one could also easily calculate predictions outside ggplot2 and pass these to the ggplot. – Roland Dec 14 '17 at 14:24
  • Right, I missed that in OPs model a polynomial of dayNumber was included. – tifu Dec 14 '17 at 14:28

1 Answers1

0

I hope this still help you:

library('ggplot2')

# create test data
weight <- runif(40) * 10
morning <- weight * runif(length(weight))
weight_data <- data.frame(weight, morning)

# test data
ression_1 <- lm( weight ~ poly(morning, degree = 2))
plot(morning, weight)
points(x=morning, y=regression_1$fitted.values, col='red')

#plot in ggplot2
plot2 <- ggplot(data=weight_data, aes(y=weight, x=morning))+
  geom_point()+
  stat_smooth(method = "lm", formula = y ~ poly(x, degree = 2), size = 1)+
  labs(y = "Weight in kg", x = "Day Number of weight measurment", subtitle = "Day 0 = 3 October 2010")

plot2
and-bri
  • 1,563
  • 2
  • 19
  • 34