jthaman / ciTools

An R Package for Quick Uncertainty Intervals
GNU General Public License v3.0
106 stars 9 forks source link

add_ci.lmer assumes the standard error of random effects is constant across groups #24

Open jthaman opened 6 years ago

jthaman commented 6 years ago

When we compute the SEs of random effects, we use

seRandom <- arm::se.ranef(fit)[[1]][,1] ## which results in a scalar. This is fine if all groups are the same size. If the groups are not the same size, we should match the correct random effect SE to the group. In add_ci.glmer, we do this:

ranef_name <- names(fit@cnms)[1] ## just one random effect for now
seRandom <- arm::se.ranef(fit)[[1]][,1] ## get all random effect SEs
seRandom_vec <- rep(NA, length(tb[[ranef_name]])) 

seRandom_df <- tibble( ## convert to data frame
    group = names(seRandom),
    seRandom = seRandom
)
names(seRandom_df)[names(seRandom_df) == 'group'] <- ranef_name ## get the group names so SEs can be matched
seRandom_vec <- dplyr::left_join(tb, seRandom_df, by = ranef_name)[["seRandom"]] ## match RE SEs to each obs in tb using ranef_name as the matching criterion

`

This code is ugly and I'm not proud of it, but I think we need to do something about this because the documentation does not state that add_ci.lmer can only be used if the group sizes are equal.