R codes for logistic regression on survival as dependent and temperatures and hours as indepenent variables

I am beginner to R so do not create much codes my self.... even i saw a lot of tutorials on logistic regression but those were not fitting to my data set. There is always an error to do...
I want to creat hours as factors in same plot like in following example using logistic regression.
Screenshot 2022-01-09 16.16.31

I tried following codes but could not plot my predicted data.....

Temperature = c(20, 25, 30, 35, 40, 20, 25, 30, 35, 40, 20, 25, 30, 35, 40, 20, 25, 30, 35, 40, 20, 25, 30, 35, 40)
Hour = c(7, 7, 7, 7, 7, 12, 12, 12, 12, 12, 24, 24, 24, 24, 24, 35, 35, 35, 35, 35, 48, 48, 48, 48, 48)
survival: = c(18, 29, 32, 35, 35, 10, 24, 30, 35, 35, 0, 4, 25, 32, 35, 0, 0,19, 27, 35, 0, 0, 6, 21, 35)

library(ggplot2)
library(reshape2)

data <- read.csv(file.choose())
View(data)

Dataframe

data
str(data)
summary(data)

Modeling with generic function 'glm' in R

fit=glm(Surv~Temp + Hour + Temp * Hour, data = data, family = "binomial")

summary(fit)

newdata1 <- with(data, data.frame(Temp = mean(Temp), Hour = (c(7, 12, 24, 35, 48))))
head(newdata1)
tail(newdata1)

newdata1$HourP <- predict(fit, newdata = newdata1, type = "response")
newdata1

newdata2 <- with(data, data.frame(Temp = rep(seq(from = 200, to = 800, length.out = 100),
5), Hour =(rep(c(7, 12, 24, 35, 48), each = 100))))

newdata3 <- cbind(newdata2, predict(fit, newdata = newdata2, type = "link",
se = TRUE))

newdata3 <- within(newdata3, {
PredictedProb <- plogis(fit)
LL <- plogis(fit - (1.96 * se.fit))
UL <- plogis(fit + (1.96 * se.fit))
})

head(newdata3)

plot(newdata3)
ggplot(newdata3, aes(x = Temp, y = PredictedProb)) + geom_line(aes(colour = Hour), size = 1)

ggplot(newdata3, aes(x = Temp, y = PredictedProb)) + geom_ribbon(aes(ymin = LL,
ymax = UL, fill = Hour (c(7, 12, 24, 35, 48)), alpha = 0.2) +
geom_line(aes(colour = Hour), size = 1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.