Binomial model with different sizes

fitting-issues

#1

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:

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.


#2

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);
}

#3

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];


#4

@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.


#5

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.


#7

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.


#8

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.


#9

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?


#10

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


#11

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.


#12

@Ron1

you wrote

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).


#13

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.