Hi,
I am modelling time series data for a given number of subjects. some data points are censored.
I am currently computing the log-likelihood for each data point in the generated quantities block as a matrix and taking the sum outside Stan.
I am struggling to determine if this is the most efficient way to compute the log-likelihood? Any help would be appreciated.
Thanks
Below is part of my code
data {
int<lower=1> J; //number of subjects
int<lower=1> n_obs; //number of observed measurements
real<lower =0> y_hat[n_obs,J]; //observed measurements
int <lower =0, upper =1> is_censored[n_obs,J]; // Indicator matrix:
real L; // censoring point
int<lower=0, upper = 1> run_estimation; // a switch to evaluate the likelihood
}
```stan
model {
vector[N] mu = alpha + beta * x;
y ~ normal(mu, sigma);
}
parameters{
....
}
transformed parameters{
real y_new[n_obs,J]; //mean matrix
}
model{
if(run_estimation==1){
for (j in 1:J){
for (n in 1:n_obs){
if (is_censored[n,j] == 0){
y_hat[n,j] ~ lognormal(log(y_new[n,j]), std); //likelihood for uncensored observed data
} else {
target += lognormal_lcdf(L |log(y_new[n,j]), std); //likelihood of censored data
}
}
}
}
}
generated quantities {
real log_like[n_obs, J];
//compute log likelihood
if(run_estimation==1){
for (j in 1:J){
for (n in 1:n_obs){
if (is_censored[n,j] == 0){
log_like[n,j] = lognormal_lpdf(y_hat[n,j]|log(y_new[n,j]),std);//likelihood for uncensored observed data
} else {
log_like[n,j]= lognormal_lcdf(L |log(y_new[n,j]), std); //likelihood of censored data
}
}
}
}
}