Integrate_1d is not finding my integrand function. I am receving the error
variable "integrand" does not exist.
My Stan code is shown below.
functions {
real sev_logpdf(real y, real mu, real sigma){
real z;
z = (y - mu) / sigma;
return -log(sigma) + z - exp(z);
}
real sev_logccdf(real y, real mu, real sigma){
return -exp((y - mu) / sigma);
}
real sev_logcdf(real y, real mu, real sigma){
return log1m_exp(-exp((y - mu) / sigma));
}
real integrand(real x,
real xc,
real[] theta,
real[] x_r,
int[] x_i) {
//real mu1 = theta[1];
//real sigma1 = theta[2];
//real mu2 = theta[3];
//real sigma2 = theta[4];
real mu3 = theta[1];
real sigma3 = theta[2];
//real log_pi = theta[7];
real z = (1/sigma3)*x^(1/sigma3-1)*exp(-x^(1/sigma3)*exp(-mu3/sigma3)-mu3/sigma3);
return z;
}
}
data {
int N_obs;
int N_cens;
real endtime_obs[N_obs];
real endtime_cens[N_cens];
real starttime_obs[N_obs];
real starttime_cens[N_cens];
vector<lower=0, upper=1>[2] p; # quantiles to model
int N_obs_s5;
int N_cens_s5;
real endtime_obs_s5[N_obs_s5];
real endtime_cens_s5[N_cens_s5];
real starttime_obs_s5[N_obs_s5];
real starttime_cens_s5[N_cens_s5];
}
transformed data{
vector[2] z_corr;
for(i in 1:2)
z_corr[i] = log(-1.0 * log1m(p[i])); # used to get location(mu) from quantile(t_p)
}
parameters{
real log_tp1;
real log_tp2;
real<lower=0> sigma1;
real<lower=0, upper=1> sigma2;
real logit_pi;
real<lower = 0> mu3;
real<lower = 0> sigma3;
}
transformed parameters{
real mu1;
real mu2;
real log_pi;
mu1 = log_tp1 - (sigma1 * z_corr[1]); //change
mu2 = log_tp2 - (sigma2 * z_corr[2]);
log_pi = log_inv_logit(logit_pi);
}
model{
real tmp[2];
for(i in 1:N_obs){
tmp[1] = log_sum_exp(log_pi + sev_logpdf(endtime_obs[i], mu1, sigma1) +
sev_logccdf(endtime_obs[i], mu2, sigma2),
sev_logpdf( endtime_obs[i], mu2, sigma2) +
log1m_exp(log_pi + sev_logcdf(endtime_obs[i], mu1, sigma1)
)
);
tmp[2] = log1m_exp(log_pi + sev_logcdf(starttime_obs[i], mu1, sigma1)) +
sev_logccdf(starttime_obs[i], mu2, sigma2);
target += tmp[1] - tmp[2];
}
for(i in 1:N_cens){
tmp[1] = log1m_exp(log_pi + sev_logcdf(endtime_cens[i], mu1, sigma1)) +
sev_logccdf(endtime_cens[i], mu2, sigma2);
tmp[2] = log1m_exp(log_pi + sev_logcdf(starttime_cens[i], mu1, sigma1)) +
sev_logccdf(starttime_cens[i], mu2, sigma2);
target += tmp[1] - tmp[2];
}
for(i in 1:N_obs_s5){
tmp[1] = sev_logpdf(endtime_obs_s5[i], mu3, sigma3);
tmp[2] = sev_logccdf(starttime_obs_s5[i], mu3, sigma3);
target += tmp[1]- tmp[2];
}
for(i in 1:N_cens_s5){
tmp[1] = sev_logccdf(endtime_cens_s5[i], mu3, sigma3);
tmp[2] = sev_logccdf(starttime_cens_s5[i], mu3, sigma3);
target += tmp[1] - tmp[2];
}
//priors
log_tp1 ~ normal(8, 4);
log_tp2 ~ normal(10, 4);
sigma1 ~ lognormal(0, 2.5);
sigma2 ~ lognormal(0, 2.5);
sigma3 ~ lognormal(0, 2.5);
logit_pi ~ normal(-3,2);
mu3 ~ normal(0,10);
}
generated quantities {
real<lower = 0> Fs5;
Fs5 = integrate_1d(integrand,
0,
1000,
{mu3,sigma3},
x_r,
x_i,
1e-8);
}
Can anyone see the mistake that I am making?
PS the integral can be solved analytically. I am just checking how the function works.
Edit: I bet I have an outdated version of Stan that does not support integrate_1d.
Edit: I am using Stan version 2.18. My understanding is that integrate_1d
should work for this version of Stan.