saudiwin / ordbetareg_pack

Repository for R package ordbetareg, used to fit continuous data with lower and upper bounds.
Other
17 stars 3 forks source link

pp_check_ordbetareg grouped discrete plot shows count of total data rows in every level of grouping variable #12

Closed eipi10 closed 1 year ago

eipi10 commented 1 year ago

Using the group argument in pp_check_ordbetareg should produce a discrete plot that compares the modeled counts in each level of the group variable to the actual counts in each level of the group variable. However, the plot shows the same total count of data rows for every level. Below is a reproducible example:

library(tidyverse)
library(brms)
library(ordbetareg)
library(patchwork)

data("pew")
model_data <- select(pew,therm,age="F_AGECAT_FINAL",
                     sex="F_SEX_FINAL",
                     income="F_INCOME_FINAL",
                     ideology="F_IDEO_FINAL",
                     race="F_RACETHN_RECRUITMENT",
                     education="F_EDUCCAT2_FINAL",
                     region="F_CREGION_FINAL",
                     approval="POL1DT_W28",
                     born_again="F_BORN_FINAL",
                     relig="F_RELIG_FINAL",
                     news="NEWS_PLATFORMA_W28") %>% 
  mutate_at(c("race","ideology","income","approval","sex","education","born_again","relig"), function(c) {
    factor(c, exclude=levels(c)[length(levels(c))])
  }) %>% 
  # need to make these ordered factors for BRMS
  mutate(education=ordered(education),
         income=ordered(income))

ord_fit_mean <- ordbetareg(formula=therm ~ mo(education)*mo(income) +
                             (1|region), 
                           data=model_data,
                           control=list(adapt_delta=0.8),
                           cores=1,chains=1,iter=500,
                           refresh=0)

pp_check_ordbeta(ord_fit_mean, group=ord_fit_mean$data$education) 

Rplot08

saudiwin commented 1 year ago

It looks like it's the background that's not changing with the group. Should be straightforward, I'll post again when I've pushed a fix.

eipi10 commented 1 year ago

Yes, exactly. For the discrete plot, the function is grouping the posterior simulations by group but isn't grouping the data used to fit the model. I was focused on the discrete plot when I posted this issue, but I also realized it's not doing any grouping at all on the continuous plot. Shouldn't there be six of those, one for each group level?

saudiwin commented 1 year ago

Currently it doesn't group for the continuous plot, but I can see if that's possible.

eipi10 commented 1 year ago

Thanks Robert!