0

I am trying to reflect estimated uncertainty in my geom_density() plots of my coefficient (yes that is a lot of random effect coefficients) by adding in a grey SE line.

But I am struggling to connect or link the grey shaded area to my geom_density plot in ggplot in R. The location of the grey uncertainty interval is wrong.

I am also aware of this answer (Add shaded standard error curves to geom_density in ggplot2) but it does not work for me. I have tried changing where I declare my ymin and ymax variables and if I append to the variable name the dataset name or not. I have spent a full day on this problem now...

DF <- data.frame(x=rep(1,576))
DF$V <- rnorm(576,mean=2,sd=0.2)
DF$V_se <- rnorm(576,mean=0.2,sd=0.01)

ggplot(data = DF, aes(x = V, ymin = V - (2*V_se),ymax = V + (2*V_se))) +
  geom_ribbon(alpha=0.1, colour = NA)+
  geom_density()

The grey line prints in a downward sloping diagonal. Instead I would like the grey ribbon to print and follow along the density curve, to show the degree of uncertainty along the curve. (And I can see that this is a bit odd perhaps give that this is a kernel approximation of the data in the first pace.) See image link please

Thank you!

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • `geom_density` does a transformation of the data, `geom_ribbon` does not. Your standard errors aren't representing the uncertainty in the density. I think the answer you linked has the right idea; you calculate the quantiles from many simulated sets of coefficients. Are the random effects from a Bayesian or frequentist model? – Axeman Jul 30 '19 at 19:29
  • Thanks for this. Correct. The standard errors are from a Bayesian credible interval: these `random' (group-level) effects are from a Bayesian model. The standard errors are reflecting uncertainty in the coefficients. It's a model with many coefficients (576), so plotting the coefficients like a kernel density and seeing how these coefficients change across time, or between models, is very helpful to me. But maybe you are right: it doesn't make sense to compare a SE of a coefficient (geom_ribbon) with an approximated density of the coefficients (via geom_density). – Ilan Strauss Jul 31 '19 at 20:23
  • If they are based on posteriors, you can just sample from those posteriors directly, calculate a density for each, then calculate quantiles afterwards. I can post some code using `brms` if you're interested. – Axeman Jul 31 '19 at 20:25

0 Answers0