easystats / modelbased

:chart_with_upwards_trend: Estimate effects, contrasts and means based on statistical models
https://easystats.github.io/modelbased/
GNU General Public License v3.0
232 stars 19 forks source link

estimate_means() : option for prediction interval/include sigma #146

Open bwiernik opened 2 years ago

bwiernik commented 2 years ago

I'd like to be able to make this plot, visualizing a discrete predictor, with estimated group means and prediction intervals. I'd also like to include densities.

library(see)
dat_mtcars <- transform(mtcars, am = factor(am))
m <- lm(mpg ~ am, data = dat_mtcars)
modelbased::estimate_means(m, at = "am") |> plot() + ggdist::stat_dist_halfeye(ggplot2::aes(x = am, dist = distributional::dist_normal(Mean, sqrt(SE^2 + sigma(m)^2)), fill = am), alpha = .5, width = .2) + see::theme_modern() + ggplot2::guides(fill = "none")

Created on 2021-09-08 by the reprex package (v2.0.1)

To accomplish this, we would need two changes:

  1. Add option to estimate_means() for prediction intervals
  2. Add option to include densities to estimate means / estimate_prediction family with discrete x
DominiqueMakowski commented 2 years ago

for means violins do work asshowninthedocs ^^:

m <- modelbased::estimate_means(lm(mpg ~ am, data = dplyr::mutate(mtcars, am = as.factor(am))))
#> We selected `at = c("am")`.
plot(m, show_data = c("violin", "boxplot", "jitter"))

plot(m, show_data = c("violin", "jitter"), 
     violin = list(aes = list(fill = "am")))

Created on 2021-09-09 by the reprex package (v2.0.1)

We don't support half violins though, but it can easily be added by adding its support to see::geom_from_list(). But then again, my thought is that default plotting is just that; default plotting for quick and convenient explorations. If people want better plots for publications and all, they should make them because we cannot cater for all preferences (and this is why we also can provide the RECIPE of the ggplot so that people can reproduce the plot and then tweak it to their wildest desires 💪).

Then about prediction intervals, if emmeans supports it then we do too via the kwargs, the question is wether to eventually expose the argument? If you meat by that tweaking only the dist aesthetic of ggdist::stat_dist_halfeye, then it falls back to supporting ggdist' geoms in see::geom_from_list()

mattansb commented 2 years ago

Didn't we write a whole function of getting prediction intervals?

DominiqueMakowski commented 2 years ago

yes for estimate_predicted that works, but estimate_means relies on emmeans

bwiernik commented 2 years ago

The solution here might be to resolve #145

strengejacke commented 2 years ago

There is a way to do this in emmeans, I think