Below is my stan code for fitting a truncated Weibull. I would like to output the log-likelihood for use in calculating information criteria. I thought that to do this for a truncated distribution, I need to specify the truncation “longhand” in the generated quantities block, but my model will not compile as written. Please help me debug this code!
Stan code
data {
int<lower=0> N;
vector<lower=0>[N] x;
real<lower=0> UL; // Upper truncation limit
real<lower=0> LL; // Lower truncation limit
}
parameters {
// Weibull density
real<lower=0> m;
real<lower=0> n;
}
model {
// Priors: Weibull density
m ~ lognormal(1, 1);
n ~ lognormal(1, 1);
// Likelihood: Weibull density
for (i in 1:N) x[i] ~ weibull(m, n) T[LL,UL];
}
generated quantities {
// Log likelihood
vector[N] log_lik;
for (i in 1:N) {
log_lik[i] = weibull_lpdf(x[i] | m, n);
if (x[i] < LL || x[i] > UL) {
log_lik[i] += negative_infinity();
} else {
log_lik[i] += log_diff_exp(weibull_lcdf(UL | m, n), weibull_lcdf(LL | m, n));
}
}
}
Parser error log
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
error in 'model3004df43e7a_testweib' at line 31, column 22
-------------------------------------------------
29: log_lik[i] = weibull_lpdf(x[i] | m, n);
30: if (x[i] < LL || x[i] > UL) {
31: log_lik[i] += negative_infinity();
^
32: } else {
-------------------------------------------------
PARSER EXPECTED: <expression>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model 'testweib' due to the above error.