GGPLOT confidence interval too narrow to see or not plotted at all

I want to plot my model regressions with confidence intervals.
I have used ggplot but it only shows the confidence intervals for one of my two categories. Could it be that the range of standard error for one category is too small relative to the other and that's why ggplot only displays confidence intervals for one?
When I plot the two categories separately then I get confidence intervals but when I merge them into one plot in ggplot then only one of them is displayed with confidence intervals.

I want to plot the linear regression of the how food consumption rate changes with increasing temperature (temp) and compare this between two day types .

This is my model: m1 <- glmmPQL(totbeakfuls ~ scale(temp)*day.type + offset(log(duration/60)), random=~1|birdid, family=quasipoisson(link="log"),data = focaldata)

Then I use ggeffects to produce predictions of my model, taking to account that it is an interaction model between day type and temperature.

dat<- ggeffects::ggpredict(m1, terms = c("temp", "day.type"), type = "re")

then I use ggplot to those predictions

ggplot(data = focaldata, aes(x = temp, y = consumption)) + geom_point(aes(color = day.type)) + geom_smooth(data= dat, method = "lm", aes(x = x, y= predicted, fill= group, color=day.type), se = TRUE)

Even through I specify "se = TRUE" It only plots the confidence intervals for one of the day type categories.

Welcome to the forum!

It is OK to cross-post here and other sites, but we do ask that you link to any other posts. See FAQ: Is it OK if I cross-post?.

Here is a link to your question on Stack Overflow: https://stackoverflow.com/questions/56855258/ggplot-confidence-intervals-too-narrow-to-see

For help on making a reproducible example, see our FAQ here: FAQ: What's a reproducible example (`reprex`) and how do I do one?

As I said in a comment on Stack Overflow, you need to calculate your CI from your model. Right now you are having geom_smooth() calculate the standard error based on a linear model fit to your predictions with lm(). Ideally ggpredict() will calculate the CI for you (I haven't checked the documentation), which should make things a little more straightforward.

I show a basic example of adding CI calculated from the model with geom_ribbon() in this section of a blog post, which may help demonstrate the concept.

1 Like

Just to follow up, I looked at the "Examples" section of the documentation for ggpredict() and I think the closest example to what you are trying to do in terms of plotting is this:

data(efc)
fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)

library(ggplot2)
library(ggeffects)
mydf <- ggpredict(fit, terms = "c12hour")
ggplot(mydf, aes(x, predicted)) +
  geom_line() +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .1)

That being said, I haven't installed ggeffects and so I don't know if you get CI from glmmPQL objects by default or not.

Hey thank you for your reply. Your suggestion does work however the predicted confidence intervals look really weird. The predictions are all less than 0 which is correct because I'm predicting a rate. But then my confidence intervals go up to 150 max. So I'm trying to just find different code for confidence intervals for glmpql models to see if they all predict confidence intervals in the same range. But your suggestion works. Finding code that works for Glmmpql models is really difficult from my experience.

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