0

I need to add the confidence interval to my line plot. Usually I would do this in ggplot with geom_ribbon, however I had to calculate the predictions myself from the output of my regression model. Hence it ended up in a regular base plot. Is there any way to add a confidence interval for the values p1, p2, p3 and p4?

Or as an alternative, is there any way to create a ggplot with confidence intervals from this? The items P1 to P4 are "values" in my Rstudio global environment. I have added a picture of what the plot looks like now as a reference for if it is reproducible in ggplot.

deficit <- seq(from= 0, to= 300, by= 10)
xbeta <- deficit*(-0.005104)
logistic_cdf <-function(x){
  return(1/(1+exp(-x)))
}

p1 <- logistic_cdf(-1.2578 - xbeta) #none
p2 <- logistic_cdf(-0.5250 - xbeta) - logistic_cdf(-1.2578 - xbeta) #low
p3 <- logistic_cdf(0.6878 - xbeta) -logistic_cdf(-0.5250 - xbeta) #high
p4 <- 1 - logistic_cdf(0.6878 - xbeta) #very high

plot(deficit, p1, type='l', 
     ylab='Probabilty of nuisance level',
     xlab= "Precipitation deficit",
     main= "(b)",
     lwd=2.5, col= "#ffd47f",
     ylim = c(0.15, 0.55))
lines(deficit, p2, col='#fbfb7c', lwd=2.5)
lines(deficit, p3, col="#cfe7ef", lwd=2.5)
lines(deficit, p4, col='#75a5e9', lwd=2.5)
legend("topleft", title = "Nuisance levels", lty=1, lwd=2, col=c("#ffd47f", '#fbfb7c', "#cfe7ef", '#75a5e9'), 
       legend=c("No nuisance", "Low nuisance levels", "High nuisance levels", "Very high Nuisance levels"))

enter image description here

manro
  • 3,529
  • 2
  • 9
  • 22
Hartje
  • 13
  • 6
  • To calculate the confidence intervals you'll need a fitted model object, rather than just some estimated parameters. If you've got the estimates, they can be plotted using additional lines or a polygon. – Miff Aug 17 '22 at 09:18
  • Thanks, I see. I have calculated the values p1 to p4 using the estimates from the ordinal regression model. I used the formula "logistic_cdf(_estimate_- xbeta)" Do you happen to know how or have a link explaining how to use the estimates for confidence interval plotting? – Hartje Aug 17 '22 at 09:48
  • [This answer](https://stackoverflow.com/a/14424417/3379675) seems to be a good starting point. – Miff Aug 17 '22 at 09:58
  • @Miff Thank I will have a look! – Hartje Aug 17 '22 at 10:56

0 Answers0