I want to develop a binomial SBC by applying this code followed the instructions of Stan’s manual.
But the error i am getting is “Ill-typed arguments to ‘~’ statement. No distribution ‘bernoulli_logit’ was found with the correct signature.”
transformed data {
real mu_sim = normal_rng(1.5, 1);
real<lower=0> sigma_sim = lognormal_rng(0, 1);
int<lower=0> J = 10;
vector<lower=0, upper=1>[J] y_sim;
vector<lower=-2, upper=4>[J] theta_sim;
for (j in 1:J) {
theta_sim[j] = lognormal_rng(mu_sim, sigma_sim);
y_sim[j] = bernoulli_logit_rng(theta_sim[j]);
}
}
parameters {
real mu;
real<lower=0> sigma;
re<lower=-2, upper=4> theta;
}
model {
mu ~ normal(1.5, 1);
sigma ~ lognormal(0, 1);
theta ~ lognormal(mu,sigma);
y_sim ~ bernoulli_logit(theta);
}
I tried to vectorise the model by replacing the model section with
model {
mu ~ normal(1.5, 1);
sigma ~ lognormal(0, 1);
for (j in 1:10) {
theta[j] ~ lognormal(mu, sigma);
y_sim[j] ~ bernoulli(inv_logit(theta[j]));
}
}
But i am getting the same error "“Ill-typed arguments to ‘~’ statement. No distribution ‘bernoulli_logit’ was found with the correct signature”.
Can you help??
Thank you
1 Like
Hi, @nicksc79 and welcome to the Stan forums.
I’m afraid Stan’s deficient in not having a bernoulli_logit_rng
. We will get around to adding it eventually.
For now, you can replace calls bernoulli_logit_rng(x)
with bernoulli_rng(inv_logit(x))
. The _logit
in bernoulli_logit
indicates the argument is on the logit scale, so applying inv_logit
takes it back to the probability scale.
There are a few other issues with your model. First, interval constraints, like those on theta
can be problematic both computationally and statistically when probabability mass accumulates near the boundaries. So we usually recommend softer constraints. Here, there’s a bigger problem. The lognormal distribution is only defined for positive values, but you let theta go down to -2. Any values of theta
below zero will just cause rejections during sampling. Did you intend normal distribution rather than a lognormal here?
Given that theta
is univariate, the stacked lognormal priors are just going to add extra diffusion and not really do anything for you in the model and should probably just be replaced with a simpler prior on theta
.
2 Likes
Thank you a lot for your response.
Following your guidelines, I changed the code to
transformed data {
int<lower=0> J = 10;
vector<lower=0, upper=100>[J] theta_sim;
vector<lower=0, upper=1>[J] y_sim;
for (j in 1:J) {
theta_sim[j] = lognormal_rng(1, 1);
y_sim[j] = bernoulli_rng(inv_logit(theta_sim[j]));
}
}
parameters {
real<lower = 0, upper=100> theta;
}
model {
theta ~ lognormal(1,1);
y_sim ~ bernoulli_logit(theta);
}
but now the error message I am getting is
Semantic error in 'string', line 15, column 2 to column 33:
-------------------------------------------------
13: model {
14: theta ~ lognormal(1,1);
15: y_sim ~ bernoulli_logit(theta);
^
16: }
-------------------------------------------------
Ill-typed arguments supplied to function 'bernoulli_logit':
(vector, real)
Available signatures:
(array[] int, row_vector) => real
The first argument must be array[] int but got vector
(array[] int, vector) => real
The first argument must be array[] int but got vector
(array[] int, array[] real) => real
The first argument must be array[] int but got vector
(array[] int, real) => real
The first argument must be array[] int but got vector
(int, row_vector) => real
The first argument must be int but got vector
(Additional signatures omitted)
I wonder why since the theta value is a real.
Apologies for, I suppose my naive questions
bernoulli_logit
is defined only over intergers, but you have declared y_sim
to be a vector (of reals). Change your declaration of y_sim
to be an array of integers.
1 Like