# Predicted Probabilies with Multilevel Logit Models

**URL:** <https://discourse.mc-stan.org/t/predicted-probabilies-with-multilevel-logit-models/26730>\
**Category:** Modeling\
**Created:** [March 9, 2022, 3:18pm UTC](https://discourse.mc-stan.org/t/predicted-probabilies-with-multilevel-logit-models/26730 "2022-03-09T15:18:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![junxu2018](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@junxu2018](https://discourse.mc-stan.org/u/junxu2018)\
**Post date:** [March 9, 2022, 3:18pm UTC](https://discourse.mc-stan.org/t/predicted-probabilies-with-multilevel-logit-models/26730/1 "2022-03-09T15:18:27Z")

</div>

Dear all Stan users,

Hope things are going well with you all. I am posting to ask about post-estimation analysis of a two-level logit model after running stan\_glmer. Below y is a binary response variable, x1-x4 are individual level covariates, and x5 is a level-2 predictor. The level-1 model is specified as  
y = x0 + x1 + x2 + x3 + x4 + e, wherein x0 is a unit column for intercept and e is the error term  
and the level-2 models include  
b0 = g00 + x5 + u00  
b2 = g20 + x5 + u20  
where g00 and g20 are unit columns at level-2 and u’s are level-2 error terms.

```nohighlight
stanmod01 <-stan_glmer(y ~ x1 + x2 + x3 + x4 + x5 + x5*x2
              + (1 + x2 | group), 
              data = mydata, family = binomial, seed = 123456789)
} 

```

So after I obtain the results, I want to get the predicted probability of y = 1 given a x vector. So first I create a x vector as follow,

```nohighlight
x.vector <- data.frame(x1 = 3.5, x2 = 0, x3 = 0, x4 = 0, x5 = 30, group = 23019)

```

Then I use the posterior\_predict to calculate the prediction,

```nohighlight
predP = posterior_predict(stanmod01, newdata=x.vector)

```

I can get predP. But what is in predP are one’s and zero’s, not predicted probabilities. So my first question is how I can get the generated posterior distribution of predicted probabilities given a x vector. My second question is if the ones and zeros are random draws based on the predicted probabilities. Thank you all!

Jun Xu, PhD  
Professor  
Department of Sociology  
Ball State University

---

<div class="post-metadata">

**Author:** ![junxu2018](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@junxu2018](https://discourse.mc-stan.org/u/junxu2018)\
**Post date:** [March 11, 2022, 1:42pm UTC](https://discourse.mc-stan.org/t/predicted-probabilies-with-multilevel-logit-models/26730/2 "2022-03-11T13:42:33Z")

</div>

I think I’ve figured out. It looks like I need to use the posterior\_linpred and posterior\_epred functions instead of the posterior\_predict function. Will update you all if there is any new finding. Thank you all!

Jun
