# Help with misspecified model

**URL:** <https://discourse.mc-stan.org/t/help-with-misspecified-model/17875>\
**Category:** Modeling\
**Tags:** fitting-issues, specification\
**Created:** [September 5, 2020, 7:22am UTC](https://discourse.mc-stan.org/t/help-with-misspecified-model/17875 "2020-09-05T07:22:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oxyaks](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@oxyaks](https://discourse.mc-stan.org/u/oxyaks)\
**Post date:** [September 5, 2020, 7:22am UTC](https://discourse.mc-stan.org/t/help-with-misspecified-model/17875/1 "2020-09-05T07:22:43Z")

</div>

* * *

data {  
int\<lower=0\> N;//Number of observations  
int\<lower=1\> J;//Number of predictors with random slope  
int\<lower=1\> K;//Number of predictors which are fixed effects  
int\<lower=1\> L;//Number of customers/groups  
int\<lower=0,upper=1\> y[N];//Binary response variable  
int\<lower=1,upper=L\> ll[N];//Number of observations in groups  
matrix[N,K] x1;  
matrix[N,J] x2;  
}  
transformed data {  
vector[J] ones = rep\_vector(1, J);  
}  
parameters {  
row\_vector[J] rbeta\_mu; //mean of distribution of beta parameters  
row\_vector\<lower=0\>[J] rbeta\_sigma; //variance of distribution of beta parameters  
row\_vector[J] beta\_raw[L]; //group-specific parameters beta  
vector[K] beta;  
}  
transformed parameters {  
matrix[L,J] rbeta;  
for (l in 1:L)  
rbeta[l] = rbeta\_mu + rbeta\_sigma .\* beta\_raw[l]; // coefficients on x  
}  
model {  
vector[N] p;  
rbeta\_mu ~ normal(0,5);  
rbeta\_sigma ~ inv\_gamma(1,1);  
beta~normal(0,5);  
for (l in 1:L)  
beta\_raw[l] ~ std\_normal();

p = x1 \* beta + (x2 .\* rbeta[ll]) \* ones; // Multiplication by vector of ones as a row-wise summation of matrix  
y~bernoulli\_logit§;  
}

* * *

I am trying to fit a fairly simple multitlevel model. However I feel the model might be misspecified since when i tried to run the model using cmdstanR i get lot of errors.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/0/0b120734f76fee968ca75247294a6f18fa211eb4.png)

I am a newbie to Bayesian statistics. So I am looking for help from the community to determine what is the mistake.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Guido\_Biele](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/guido_biele/32/3414_2.png) [@Guido\_Biele](https://discourse.mc-stan.org/u/Guido_Biele)\
**Post date:** [September 10, 2020, 9:12am UTC](https://discourse.mc-stan.org/t/help-with-misspecified-model/17875/2 "2020-09-10T09:12:29Z")

</div>

Welcome to the Stan forum!

the _warning_ says that the value of the probability parameter is nan, and the relevant section of your model reads

```
y~bernoulli_logit§;

```

this should be changed to

```
y~bernoulli_logit(p);

```

In case your model already correctly uses `y~bernoulli_logit(p);`: The warning message is not a concern if it comes up at the beginning. (i.e. nor further warnings later during warm-up and especially during sampling)

Also, your priors look fairly wide (assuming the values in `x1` and `x2` are not very small).  
I would recommend to

- start with narrower priors (e.g. normal(0,2) throughout, even for `rbeta_sigma`)
- do a prior predictive check, i.e. comment out `// y~bernoulli_logit(p);`, run the model, and look at the distribution of the parameter `p` values.

Lastly, if you put  
"``` Stan  
[your stan model]  
"````

around your Stan model, it will be easier to read for others

---

<div class="post-metadata">

**Author:** ![oxyaks](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@oxyaks](https://discourse.mc-stan.org/u/oxyaks)\
**Post date:** [September 10, 2020, 9:15am UTC](https://discourse.mc-stan.org/t/help-with-misspecified-model/17875/3 "2020-09-10T09:15:25Z")

</div>

Hi @Guido_Biele, It actually does use it as y~bernoulli\_logit( p ). I don’t why it appeared here as so.

I will try prior predictive check and narrower priors. Thank you !
