# Binomial model with different sizes

**URL:** <https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168>\
**Category:** Modeling\
**Tags:** fitting-issues\
**Created:** [October 10, 2017, 12:07pm UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168 "2017-10-10T12:07:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron1](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@Ron1](https://discourse.mc-stan.org/u/Ron1)\
**Post date:** [October 10, 2017, 12:07pm UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/1 "2017-10-10T12:07:38Z")

</div>

I am trying to fit a binomial model with Rstan. However, I keep getting the error message:

```
Error in stanc(file = file, model_code = model_code, model_name = model_name, : 
  c++ exception (unknown reason)

```

The model is a binomial model with logic link. I want to find alpha and beta.  
I suspect that the error came from that data is from binomial with different size. And I tried to correct it but failed. Here is my code:

```stan
data {vector[8] n;
  vector[8] x;
  vector[8] r;
}
parameters {
  real alpha;
  real beta;
}
model {
  vector[8] p;
  p = inv_logit(alpha + beta * x);    
  alpha ~ normal( 0.0, 100);
  beta ~ normal( 0.0, 100);
  r ~ binomial(n, p);
}

```

df \<- list(x = c(1.69, 1.72, 1.75, 1.78, 1.81, 1.83, 1.86, 1.88),  
n = c(59, 60, 62, 56, 63, 59, 62, 60),  
r = c(6, 13, 18, 28, 52, 53, 61, 60))

fit \<- stan(file =‘filename.stan’, data=df, iter=1000, chains=4)

It is a very simple model, And I think the syntax is correct. I tried to replace the model by a for loop since as I mentioned, each data has different size. But still get the same error.

---

<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:** [October 10, 2017, 1:15pm UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/2 "2017-10-10T13:15:45Z")

</div>

you need to declare the correct types when using the binomial:

```
data {
  int n[8];
  row_vector[8] x;
  int r[8];
}
parameters {
  real alpha;
  real beta;
}
model {
  row_vector[8] p;
  p = inv_logit(alpha + beta * x);
  alpha ~ normal( 0.0, 100);
  beta ~ normal( 0.0, 100);
  r ~ binomial(n, p);
}

```

---

<div class="post-metadata">

**Author:** ![Ron1](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@Ron1](https://discourse.mc-stan.org/u/Ron1)\
**Post date:** [October 11, 2017, 5:33am UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/3 "2017-10-11T05:33:55Z")

</div>

I tried. but it does not work. I also tried many combination of vector, row\_vector, and int, getting same error message.

btw, why it is

> int n[8];  
> int r[8];

---

<div class="post-metadata">

**Author:** ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Post date:** [October 11, 2017, 5:51am UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/4 "2017-10-11T05:51:48Z")

</div>

@Ron1, first thing to do is to reinstall RStan.

There’s a compiler message that’s hidden away by RStan. The `c++ exception (unknown reason)` should actually report what’s wrong with your code. Hopefully reinstalling will fix that issue.

---

<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:** [October 11, 2017, 6:52am UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/5 "2017-10-11T06:52:51Z")

</div>

The program i posted above works for me, so the problem has to be something else.  
If you use rstan, **make sure to set** `chains = 1` otherwise you won’t see error messages.

The size of real and int arrays is declared differently then the size of vectors and matrices.

When I started using stan, I found it very helpful to read section **II Stan Modeling Language** in the [Stan language manual](https://github.com/stan-dev/stan/releases/download/v2.16.0/stan-reference-2.16.0.pdf).

---

<div class="post-metadata">

**Author:** ![Ron1](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@Ron1](https://discourse.mc-stan.org/u/Ron1)\
**Post date:** [October 11, 2017, 8:13am UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/7 "2017-10-11T08:13:33Z")

</div>

Sorry to ask this. I follows each step of installation shown on Rstan Website. And still see same error message.  
Is there a way to check the installation is correct? for example, some command.

(update) I reinstalled Rstan, now the program runs!  
However, I tested by inserting a invalid command, still get the same message

> c++ exception (unknown reason)

I google it. Seems like someone also has this issue as well.

@Guido_Biele using

> chains = 1

does not return proper error message either.

---

<div class="post-metadata">

**Author:** ![Ron1](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@Ron1](https://discourse.mc-stan.org/u/Ron1)\
**Post date:** [October 11, 2017, 8:14am UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/8 "2017-10-11T08:14:44Z")

</div>

I did read the manual.  
However, I am still quite unsure about the array and vector.  
What is the difference between

> int n[8];  
> vector[8] n;  
> row\_vector[8] n;

To me, first one is an array which contains 8 integers. Second is a column vector of size 8, which can also be 8 integers; same for the last one, except it is of row form.

BTW, to this question, I tested your code, it does not run in my computer. I am guessing, it is not syntax error, but something to do with the version I installed. So I should reinstall it.

---

<div class="post-metadata">

**Author:** ![Ron1](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@Ron1](https://discourse.mc-stan.org/u/Ron1)\
**Post date:** [October 11, 2017, 8:55am UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/9 "2017-10-11T08:55:16Z")

</div>

I found another reason why it would not run. I used constrain for vector p initially:

> row\_vector\<lower=0,upper=1\>[8] p;

never thought it could be a problem. I run the code without constrain \<lower=0,upper=1\> and then it works.  
@Guido_Biele Is it because **p** is between 0 and 1 by our definition

> p = inv\_logit(alpha + beta \* x);

so it is unnecessary to include the constrain?

---

<div class="post-metadata">

**Author:** ![stijn](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stijn/32/105_2.png) [@stijn](https://discourse.mc-stan.org/u/stijn)\
**Post date:** [October 11, 2017, 9:55am UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/10 "2017-10-11T09:55:31Z")

</div>

> [@Ron1](#):
>
> I run the code without constrain \<lower=0,upper=1\> and then it works.

Stan does not allow constraints on parameters that are declared in the model block. That is why your version of the model was not running.

Installing `rstan` from source might help to bring the error messages back which would probably have helped you identify the problem quicker.

> <https://github.com/stan-dev/rstan/issues/405>
>
> \#### Summary:
> The syntax errors are no longer informative
> 
> \#### Description:
> …
> When I use the rstudio\_stanc() function or stan\_model() with a stan file with syntax errors, the only information I got was "c++ exception (unknown reason)".
> 
> \#### Reproducible Steps:
> The following stan file gives \`~/Desktop/test.stan is syntactically correct.\`
> 
> \`\`\`
> parameters{
> vector\[10\] alpha;
> }
> model{
> alpha ~ normal(0, 1);
> }
> \`\`\`
> while this one gives \`Error in stanc(model\_code = paste(program, collapse = "\\n"), model\_name = model\_cppname, : 
> c++ exception (unknown reason)\`
> 
> \`\`\`
> parameters{
> vector\[10\] alpha;
> }
> model{
> alpha ~ normal(0, 1)
> }
> \`\`\`
> The only difference is that I dropped the ; in the message block.
> 
> \#### Current Output:
> See above.
> 
> \#### Expected Output:
> I think in previous versions I got more informative error messages.
> 
> \#### RStan Version:
> 2.15.1
> 
> \#### R Version:
> 3.4.0 with Rstudio 1.0.143
> 
> \#### Operating System:
> OsX 10.12.4

---

<div class="post-metadata">

**Author:** ![Ron1](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@Ron1](https://discourse.mc-stan.org/u/Ron1)\
**Post date:** [October 11, 2017, 10:39am UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/11 "2017-10-11T10:39:00Z")

</div>

I tried to reinstall from source by the instructions on Rstan Website.

I got the following error message during installation

> ERROR: failed to lock directory ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library’ for modifying  
> Try removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/00LOCK-rstan’  
> Warning in install.packages :  
> installation of package ‘rstan’ had non-zero exit status

Despite that, the error message is informative now! Thanks.

---

<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:** [October 11, 2017, 12:38pm UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/12 "2017-10-11T12:38:58Z")

</div>

@Ron1

you wrote

> [@Ron1](#):
>
> To me, first one is an array which contains 8 integers. Second is a column vector of size 8, which can also be 8 integers; same for the last one, except it is of row form.

As far as i know only `int` arrays can contain integers.  
Of course you can in R have a vector with “integers” as part as the data you submit to the stan model. But if data is not declared as `int` in the data block, stan will consider it to be `real` (which happens to be rounded to integer values).

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [October 14, 2017, 9:18pm UTC](https://discourse.mc-stan.org/t/binomial-model-with-different-sizes/2168/13 "2017-10-14T21:18:36Z")

</div>

There’s a chapter on the differences between arrays and vectors and advice on when to use each.

Vector types only contain real values, but integers can be promoted to assigned to real variables. Real variables may not be assigned to integers.
