Hello,I’m trying to fit the following beta regression in Stan :
Y_{i}\sim beta(p_{i},q_{i})
p_{i}=((1-\mu_{i})\mu_{i}^{2}-\mu_{i}\sigma^{2})/\sigma^{2}
q_{i}=(1-\mu_{i})(\mu_{i}-\mu_{i}^{2}-\sigma^{2})/\sigma^{2}
\mu_{i}=w_{1}x_{i,1}+...w_{k}x_{i,k}+w_{k+1}z_{i}
and make inference for weights w.
Where w\sim Dirichlet (1,...1)
, \sigma^{2}\sim Uniform(0,min((1-\mu)\mu))
and z\sim Beta(0.5,0.5)
So in order to do that I implement the following code:
data{
int<lower=0> N;
int<lower=0> K;
vector[K] C;
vector<lower=0,upper=1>[N] Y;
matrix<lower=0,upper=1>[N,K] X;
}
parameters{
vector<lower=0.0,upper=1.0>[K+1] W;
}
transformed parameters {
vector<lower=0, upper=1>[N] mu;
vector<lower=0>[N] p;
vector<lower=0>[N] q;
real<lower=0,upper=1> Z;
real<lower=0> sig;
for (i in 1:N) {
mu[i] = X[i,]*W[1:K]+W[K+1]*Z;
p[i] = ((1-mu[i])*mu[i]*mu[i]-mu[i]*sig*sig)/(sig*sig);
q[i] = (1-mu[i])*(mu[i]-mu[i]*mu[i]-sig*sig)/(sig*sig);
}
}
model{
target += beta_lpdf(Z|0.5,0.5);
target += uniform_lpdf(sig|0,min((1-mu).*mu));
target += dirichlet_lpdf(W|C);
target += beta_lpdf(Y|p,q);
}
However, I get the following message multiple times
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: model31f066875_0b43718f5170bcc3da95f3b8a5114180_namespace::write_array: mu[1] is nan, but must be greater than or equal to 0 (in 'model31f066875_0b43718f5170bcc3da95f3b8a5114180' at line 14)
I’ve read that in order to solve these problems Rejecting initial value
,Error evaluating the log probability at the initial value.
you have to change the constraints of the variables.I made some attempts but I still get the same message.
The way that the model is constructed it seems straightforward , thus I don’t know what I’m doing wrong.