Home > Mobile >  Cannot make a logistic curve in R
Cannot make a logistic curve in R

Time:08-31

I am trying to make a logistic curve in R but the line does not appear in the plot. My data are:

dput(los1)
structure(list(X1 = c("5.51688462301445", "2.55660506920185", 
"4.17130300764484", "15.0032350113684", "0.0672790807684578", 
"0", "10.7646529229551", "1.6819770192119", "4.44041933071867", 
"2.69116323073877", "0", "0.740069888453036", "1.54741885767498", 
"0.201837242305373", "1.81653518074882", "6.12239634993057", 
"3.49851219996026", "22.4039338958996", "0.538232646147662", 
"0.134558161536916", "1.2783025346007", "1.6819770192119", "16.9543283536541", 
"60.0129400454734", "9.62090854989083", "0.470953565379205", 
"33.7740985457708", "6.8624662383836", "0", "0", "4.50769841148758", 
"62.6368241954438", "264.137671097005", "14.5995605267576", "0", 
"0", "0", "6.12239634993057", "10.1591411960385", "22.9421665420477", 
"0.470953565379205", "2.28748874612802", "13.8594906383046", 
"11.0337692460289", "18.6363053728655", "27.2480277112295", "0.0672790807684578", 
"0.470953565379205", "0", "0"), X2 = c("No", "No", "Yes", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "Yes", "Yes", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No")), row.names = c(NA, 50L), class = "data.frame")

and the code I use for the curve is:

los1 %>%
  mutate(prob = ifelse(X2 == "Yes", 1, 0)) %>%
  ggplot(aes(X1, prob))  
  geom_point(alpha = 0.2)  
  geom_smooth(method = "lm", se=FALSE, method.args = list(family = "binomial"))  
  labs(
    title = "Logistic Regression Model", 
    x = "Plasma Glucose Concentration",
    y = "Probability of being diabete-pos"
  )

Any idea how to make the curve?

CodePudding user response:

los1 <- structure(list(X1 = c("5.51688462301445", "2.55660506920185", 
  "4.17130300764484", "15.0032350113684", "0.0672790807684578", 
  "0", "10.7646529229551", "1.6819770192119", "4.44041933071867", 
  "2.69116323073877", "0", "0.740069888453036", "1.54741885767498", 
  "0.201837242305373", "1.81653518074882", "6.12239634993057", 
  "3.49851219996026", "22.4039338958996", "0.538232646147662", 
  "0.134558161536916", "1.2783025346007", "1.6819770192119", "16.9543283536541", 
  "60.0129400454734", "9.62090854989083", "0.470953565379205", 
  "33.7740985457708", "6.8624662383836", "0", "0", "4.50769841148758", 
  "62.6368241954438", "264.137671097005", "14.5995605267576", "0", 
  "0", "0", "6.12239634993057", "10.1591411960385", "22.9421665420477", 
  "0.470953565379205", "2.28748874612802", "13.8594906383046", 
  "11.0337692460289", "18.6363053728655", "27.2480277112295", "0.0672790807684578", 
  "0.470953565379205", "0", "0"), X2 = c("No", "No", "Yes", "No", 
  "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
  "No", "No", "No", "No", "No", "No", "No", "No", "Yes", "Yes", 
  "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
  "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
  "No", "No", "No")), row.names = c(NA, 50L), class = "data.frame")
str(los1)
los1$X1 <- as.numeric(los1$X1)
los1$Y <- ifelse(los1$X2 == "Yes", 1, 0)

library(ggplot2)

los1 |> 
  ggplot(aes(X1, Y))  
  geom_point(alpha = 0.2)  
  geom_smooth(method = "glm", se=FALSE, method.args = list(family = "binomial"))  
  labs(
    title = "Logistic Regression Model", 
    x = "Plasma Glucose Concentration",
    y = "Probability of being diabete-pos"
  )

enter image description here

CodePudding user response:

Another option using stat_smooth with "glm" method and "X1" converted to numeric like this:

library(tidyverse)
los1 %>%
  mutate(prob = ifelse(X2 == "Yes", 1, 0)) %>%
  mutate(X1 = as.numeric(X1)) %>%
  ggplot(aes(X1, prob))  
  geom_point(alpha = 0.2)  
  stat_smooth(method="glm", color="green", se=FALSE, method.args = list(family=binomial))  
  labs(
    title = "Logistic Regression Model", 
    x = "Plasma Glucose Concentration",
    y = "Probability of being diabete-pos"
  )
#> `geom_smooth()` using formula 'y ~ x'

Created on 2022-08-30 with reprex v2.0.2

  • Related