Dear Stan community,
I have a problem fitting the bernoulli_logit model. I am trying to combine the linear predictors and inputting into a longitudinal binary model using bernoulli_logit. I have two matrices X (predictor) and Y (outcome), where the row represented subjects and column represented the visits. In the Y matrix, some cells are “NA” because for some subjects, they have missing outcomes at some visits, for others their outcome are 0/1.
I searched and found that the Y should be integer type using bernoulli_logit(). I am not sure how to indicate the matrix to be integer, or does the missing data affect?
Please see code below:
// Binary long model for PA+
data {
// Define variables in data
// Number of observations (an integer)
int<lower=0> M;
// Number of c parameters
int<lower=0> p;
// Number of timepoints
int k[M];
// Covariates
int N;
matrix[M, N] X;
// Count outcome
matrix[M, N] Y;
}
parameters {
// Define parameters to estimate
real c0;
real c[p];
real sig[p];
real cp;
real s[M];
real u[M];
real ga;
}
transformed parameters {
//
matrix[M, N] p2;
for(i in 1:M){
for(j in 1:k[i]){
p2[i,j] <- c0 + c[1] * (X[i,j]-cp) + c[2] * (X[i,j]-cp) * step(X[i,j]-cp) + ga*s[i] + u[i];
}
}
}
model {
// Prior part of Bayesian inference
c0 ~ normal(0,10000);
c[1] ~ normal(0,10000);
c[2] ~ normal(0,10000);
sig[1] ~ uniform(0,100);
sig[2] ~ uniform(0,100);
for(i in 1:M){
u[i] ~ normal(0,sig[1]);
s[i] ~ normal(0,sig[2]);
}
ga ~ normal(0,10000);
cp ~ uniform(0.07,21.45);
// Likelihood part of Bayesian inference
Y ~ bernoulli_logit(p2);
}
I got the error below:
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
0
Semantic error in 'string', line 43, column 1 to column 25:
Ill-typed arguments to '~' statement. No distribution 'bernoulli_logit' was found with the correct signature.
After I change the matrix to vector using to_vector(), the error becomes,
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Info: assignment operator <- deprecated in the Stan language; use = instead.
No matches for:
vector ~ bernoulli_logit(vector)
Available argument signatures for bernoulli_logit:
int ~ bernoulli_logit(real)
int ~ bernoulli_logit(real[ ])
int ~ bernoulli_logit(vector)
int ~ bernoulli_logit(row_vector)
int[ ] ~ bernoulli_logit(real)
int[ ] ~ bernoulli_logit(real[ ])
int[ ] ~ bernoulli_logit(vector)
int[ ] ~ bernoulli_logit(row_vector)
Real return type required for probability function.
error in 'model1ea0b972b7c_50a1805149e3235b326959a4870349ac' at line 56, column 50
-------------------------------------------------
54:
55: // Likelihood part of Bayesian inference
56: to_vector(Y) ~ bernoulli_logit(to_vector(p2));
^
57: }
-------------------------------------------------
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model '50a1805149e3235b326959a4870349ac' due to the above error.
Thank you for your help!