user17088076
user17088076

Reputation:

How can I create a ggplot with a regression line based on the predicted values of a glm?

I am creating a ggplot and I would like to add in a regression line, I have tried using geom_smooth. But the trouble now is that I would like to add this line using the predicted values of my response variable which I retrieved from the predict() function.

I was just wondering if anyone knows how I could do this? I have tried adding:

geom_smooth(predicted.values ~ data$predictor1 + data$predictor2)

But this doesn't seem to be working. It returns:

Warning messages: 1: Removed 2 rows containing non-finite values (stat_smooth). 2: Computation failed in stat_smooth(): variable lengths differ (found for 'data$predictor1') 3: Removed 2 rows containing missing values (geom_point).

Thank you for the help!

Upvotes: 1

Views: 2571

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174478

If you want to add a regression line from a glm, you can do it directly with geom_smooth, provided that you supply a list of appropriate arguments to the method.args parameter.

For example, suppose I have the following count data and wish to carry out a Poisson regression using glm:

set.seed(1)

df <- data.frame(x = 1:100,
                 y = rpois(100, seq(1, 5, length.out = 100)))

Then my model would look like this:

model <- glm(y ~ x, data = df, family = poisson)

And if I want to plot my data with a regression line, I can do:

library(ggplot2)

ggplot(df, aes(x, y)) + 
  geom_point() +
  geom_smooth(method = "glm", method.args = list(family = poisson),
              fill = "deepskyblue4", color = "deepskyblue4", linetype = 2) 

enter image description here

If instead you want to use the output of predict directly, you could do something like:

predicted.values <- predict(model, type = "response")

ggplot(df, aes(x, y)) + 
  geom_point() +
  geom_line(aes(y = predicted.values), linetype = 2)

enter image description here

Upvotes: 2

Related Questions