# [R-sig-ME] Predicted probabilites with CIs for multilevel logistic regression with prior weights

Mollie Brooks mo|||eebrook@ @end|ng |rom gm@||@com
Mon Jun 10 19:03:36 CEST 2019

```
> On 10Jun 2019, at 17:33, <d.luedecke using uke.de> <d.luedecke using uke.de> wrote:
>
>> mixed models in R do correctly not account for sampling weights
>
> Should be: mixed models in R do *currently* not account for sampling weights

I’m still trying to get a handle of the different definitions of "weights" but I believe we implemented sampling weights in glmmTMB. We do this by weighting the log-likelihood contribution of each observation. I think this is different from prior weights if you mean Bayesian priors. There has been some discussion of the different implementations of "weights" in different R functions (link below) and we still need to update the documentation for glmmTMB
https://github.com/glmmTMB/glmmTMB/issues/285

Here’s a binomial example:

library(glmmTMB)
set.seed(123)
n=100
dat=data.frame(trials=rpois(n, lambda=50), rownum=1:n)
dat\$success=rbinom(n, dat\$trials, prob=.3)
dat\$rep=sample(1:5, size=n, replace=TRUE) #each observation is repeated 1 to 5 times
rows=rep(dat\$rownum, each=1, times=dat\$rep)
dat_disaggregated=dat[rows, ]

summary(glmmTMB(cbind(success, trials-success)~1, weights=rep, dat, family=binomial))
summary(glmmTMB(cbind(success, trials-success)~1, dat_disaggregated, family=binomial))

and it works with non-integer weights

summary(glmmTMB(cbind(success, trials-success)~1, weights=rep/5, dat, family=binomial))

cheers,
Mollie

>
> -----Ursprüngliche Nachricht-----
> Von: R-sig-mixed-models <r-sig-mixed-models-bounces using r-project.org> Im
> Auftrag von d.luedecke using uke.de
> Gesendet: Montag, 10. Juni 2019 17:31
> An: 'Sam Crawley' <sam_crawley using warpmail.net>;
> r-sig-mixed-models using r-project.org
> Betreff: Re: [R-sig-ME] Predicted probabilites with CIs for multilevel
> logistic regression with prior weights
>
> Hi Sam,
>
> you could the "ggeffects" package
> (https://strengejacke.github.io/ggeffects/), and there is also an example
> for a logistic mixed effects model
> (https://strengejacke.github.io/ggeffects/articles/practical_logisticmixedmo
>
> For binomial models, using weights often results in the following warning:
> #> non-integer #successes in a binomial glm!
>
> However, CIs for the predicted probabilities can be calculated nevertheless
> (at least in my quick example). Note that afaik, mixed models in R do
> correctly not account for sampling weights. However, Thomas Lumley, author
> of the survey-package, works on a survey-function for mixed models
> (https://github.com/tslumley/svylme), probably the GitHub version is quite
> stable (haven't tested yet).
>
> An alternative would be the "scale_weights()" function from the
> sjstats-package
> (https://strengejacke.github.io/sjstats/articles/mixedmodels-statistics.html
> #rescale-model-weights-for-complex-samples ), which rescales sampling
> weights so they can be used as "weights" for the mixed models function you
> have in R (lme4, lme, ...).
>
> Based on that function, I have a small example that demonstrates how to
> compute predicted probabilities for mixed models with (sampling) weights
> (ignore the warnings, this is just for demonstration purposes):
>
> library(lme4)
> library(sjstats) # for scale_weights() and sample data
> library(ggeffects) # for ggpredict()
>
> data(nhanes_sample)
> set.seed(123)
> nhanes_sample\$bin <- rbinom(nrow(nhanes_sample), 1, prob = .3)
> nhanes_sample <- scale_weights(nhanes_sample, SDMVSTRA, WTINT2YR)
>
> m <- glmer(
>  bin ~ factor(RIAGENDR) * age + factor(RIDRETH1) + (1 | SDMVPSU),
>  family = binomial(),
>  data = nhanes_sample,
>  weights = svywght_a
> )
>
> ggpredict(m, c("age", "RIAGENDR")) %>% plot()
>
>
> Best
> Daniel
>
> -----Ursprüngliche Nachricht-----
> Von: R-sig-mixed-models <r-sig-mixed-models-bounces using r-project.org> Im
> Auftrag von Sam Crawley
> Gesendet: Montag, 10. Juni 2019 10:36
> An: r-sig-mixed-models using r-project.org
> Betreff: [R-sig-ME] Predicted probabilites with CIs for multilevel logistic
> regression with prior weights
>
> Hello all,
>
> I am doing a multilevel binomial logistic regression using lme4, and the
> survey data I am using requires weights to be used. I would like to
> calculate various predicted probabilities with confidence intervals based on
> the estimated model. The predict function obviously doesn't give me standard
> errors, and the recommended method to get these is to use the bootMer
> function.
>
> However, in my case, the weights provided are not integers, and the bootMer
> function exits with an error if the weights are not integers (I raised a
> GitHub issue about this, and was pointed to this list:
> https://github.com/lme4/lme4/issues/524 ).
>
> So my question is, what is the best way to calculate the predicted
> probabilities (with confidence intervals) in my case?
>
> I would appreciate any help you can give me, and I'm happy to provide more
> details if required.
>
> Thanks,
> Sam Crawley.
>
> _______________________________________________
> R-sig-mixed-models using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>
> --
>
> _____________________________________________________________________
>
> Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen
> Rechts; Gerichtsstand: Hamburg | www.uke.de
> Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr.
> Uwe Koch-Gromus, Joachim Prölß, Marya Verdel
> _____________________________________________________________________
>
> SAVE PAPER - THINK BEFORE PRINTING
>
> _______________________________________________
> R-sig-mixed-models using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>
> --
>
> _____________________________________________________________________
>
> Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de
> Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Prölß, Marya Verdel
> _____________________________________________________________________
>
> SAVE PAPER - THINK BEFORE PRINTING
>
> _______________________________________________
> R-sig-mixed-models using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models

[[alternative HTML version deleted]]

```