I made a multilevel model which looks like below. I was able to run it on a small dataset but now I am using a larger dataset and suddenly I get the following error messages:
Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: normal_lpdf: Location parameter is inf, but must be finite! (in 'model at line 120)
Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”
Line 120 is the following line: activations[i] ~ normal(mu[i], sigma_e0);
I also tried to change the initial values by setting init_r
to a different value, but without success.
Does anyone know what I am doing wrong?
data {
// Define variables in data
// Number of level-1 observations (an integer)
int<lower=0> Ni;
// Number of level-2 clusters
int<lower=0> Nj;
// Number of level-3 clusters
int<lower=0> Nk;
// Numer of models x countries
int<lower = 0> Njk;
// Number of fixed effect parameters
int<lower=0> p;
// Number of random effect parameters
//int<lower=0> q;
int<lower=0> Npars;
// Variables with fixed coefficient
matrix[Ni,p] fixedEffectVars;
// Cluster IDs
int<lower=1> modelid[Ni];
int<lower=1> countryid[Ni];
// Level 3 look up vector for level 2
int<lower=1> countryLookup[Npars];
int<lower=1> countryMonthLookup[Njk];
// Continuous outcome
int activations[Ni];
}
parameters {
// Define parameters to estimate
// Population intercept (a real number)
real beta_0;
// Fixed effects
vector[p] beta;
// Level-1 errors
real<lower=0> sigma_e0;
// Level-2 random effect
real u_0jk[Npars];
real<lower=0> sigma_u0jk;
// Level-3 random effect
real u_0k[Nk];
real<lower=0> sigma_u0k;
}
transformed parameters {
// Varying intercepts
real beta_0jk[Npars];
real beta_0k[Nk];
// Individual mean
real mu[Ni];
// Varying intercepts definition
// Level-3 (10 level-3 random intercepts)
for (k in 1:Nk) {
beta_0k[k] = beta_0 + u_0k[k];
}
// Level-2 (100 level-2 random intercepts)
for (j in 1:Npars) {
beta_0jk[j] = beta_0k[countryLookup[j]] + u_0jk[j];
}
// Individual mean
for (i in 1:Ni) {
mu[i] = beta_0jk[countryMonthLookup[i]] + fixedEffectVars[i,]*beta;
}
}
model {
// Prior part of Bayesian inference
// Flat prior for mu (no need to specify if non-informative)
// Random effects distribution
u_0k ~ normal(0, sigma_u0k);
u_0jk ~ normal(0, sigma_u0jk);
beta[1] ~ normal(-0.25, 1);
// Likelihood part of Bayesian inference
// Outcome model N(mu, sigma^2) (use SD rather than Var)
for (i in 1:Ni) {
activations[i] ~ normal(mu[i], sigma_e0);
}
}