Home > Blockchain >  R: Plotting Logistic Regression in Ggplot2
R: Plotting Logistic Regression in Ggplot2

Time:01-27

I am working with the R programming language.

I have the following data frame:

library(ggplot2)

library(tidyverse)
set.seed(123)

my_data1 = data.frame(Weight =  rnorm(500,100,100), asthma = sample(c(0,1), prob = c(0.7,0.3), replace=TRUE, size= 500))
my_data2 = data.frame(Weight = rnorm(500, 200, 50),  asthma = sample(c(0,1), prob = c(0.3,0.7), replace=TRUE, size= 500))
my_data = rbind(my_data1, my_data2)

I fit a logistic regression model to this data:

# fit the logistic regression model
model <- glm(asthma ~ Weight, data = my_data, family = binomial())

Then, I created a data frame which contains predicted values and the corresponding confidence intervals for different values of weights:

# create a data frame with the predicted values and confidence intervals
preds <- data.frame(Weight = seq(min(my_data$Weight), max(my_data$Weight), length.out = 100))
preds$pred <- predict(model, preds, type = "response")
preds$upper <- predict(model, preds, type = "response", se.fit = TRUE)$fit   1.96 * predict(model, preds, type = "response", se.fit = TRUE)$se.fit
preds$lower <- predict(model, preds, type = "response", se.fit = TRUE)$fit - 1.96 * predict(model, preds, type = "response", se.fit = TRUE)$se.fit

Finally, I tried to plot everything:

# plot the data and the model
ggplot(my_data, aes(x = Weight, y = asthma))  
  geom_point(alpha = 0.5)  
  geom_line(data = preds, aes(x = Weight, y = pred), color = "red")  
  geom_ribbon(data = preds, aes(x = Weight, ymin = lower, ymax = upper), alpha = 0.2)  
  ggtitle("Logistic Regression Model with Confidence Bands")

But this gives me the following error:

Error in `geom_ribbon()`:
! Problem while computing aesthetics.
i Error occurred in the 3rd layer.
Caused by error in `FUN()`:
! object 'asthma' not found
Run `rlang::last_error()` to see where the error occurred.

Does anyone know what I am doing wrong?

I can clearly see that a variable called "asthma" is within the data - furthermore, the following line of code runs without error:

ggplot(my_data, aes(x = Weight, y = asthma))  
    geom_point(alpha = 0.5)  
    geom_line(data = preds, aes(x = Weight, y = pred), color = "red") 

This being said, why is my code not running?

Thanks!

CodePudding user response:

You could use inherit.aes=FALSE in geoms using preds.

library(tidyverse)
set.seed(123)

my_data1 = data.frame(Weight =  rnorm(500,100,100), asthma = sample(c(0,1), prob = c(0.7,0.3), replace=TRUE, size= 500))
my_data2 = data.frame(Weight = rnorm(500, 200, 50),  asthma = sample(c(0,1), prob = c(0.3,0.7), replace=TRUE, size= 500))
my_data = rbind(my_data1, my_data2)

model <- glm(asthma ~ Weight, data = my_data, family = binomial())
preds <- data.frame(Weight = seq(min(my_data$Weight), max(my_data$Weight), length.out = 100))
preds$pred <- predict(model, preds, type = "response")
preds$upper <- predict(model, preds, type = "response", se.fit = TRUE)$fit   1.96 * predict(model, preds, type = "response", se.fit = TRUE)$se.fit
preds$lower <- predict(model, preds, type = "response", se.fit = TRUE)$fit - 1.96 * predict(model, preds, type = "response", se.fit = TRUE)$se.fit

ggplot(my_data, aes(x = Weight, y = asthma))  
  geom_point(alpha = 0.5)  
  geom_line(data = preds, aes(x = Weight, y = pred), color = "red", inherit.aes = FALSE)  
  geom_ribbon(data = preds, aes(x = Weight, ymin = lower, ymax = upper), alpha = 0.2, inherit.aes = FALSE)  
  ggtitle("Logistic Regression Model with Confidence Bands")

Created on 2023-01-26 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related