# How can i get the WAIC and AIC using rstan for censored

How can i get the WAIC and AIC using rstan for censored
for this model??
R code>

``````functions{
//defined survival
vector log_s(vector t, real shape, vector scale){
vector[num_elements(t)] log_s;
for(i in 1:num_elements(t)){
log_s[i]=log((shape*exp(-t[i] / scale[i]))/(shape+(1-shape)*(1-exp(-t[i] / scale[i]))));
}
return log_s;
}

//define log_ft
vector log_ft(vector t, real shape, vector scale){
vector[num_elements(t)] log_ft;
for(i in 1:num_elements(t)){
log_ft[i]=log((shape/scale[i]*exp(-t[i] / scale[i]))/(shape+(1-shape)*(1-exp(-t[i] / scale[i])))^2);
}
return log_ft;
}
//define log hazard
vector log_h(vector t, real shape, vector scale){
vector[num_elements(t)] log_h;
vector[num_elements(t)] logft;
vector[num_elements(t)] logs;
logft=log_ft(t,shape,scale);
logs=log_s(t,shape,scale);
log_h=logft-logs;
return log_h;
}

//define the sampling distribution
real surv_MOEXP_lpdf(vector t, vector d, real shape, vector scale){
vector[num_elements(t)] log_lik;
real prob;
log_lik=d .* log_h(t,shape,scale)+log_s(t,shape,scale);
prob=sum(log_lik);
return prob;
}
}
//data block

data {
int N;                            // number of observations
vector<lower=0>[N] y;             // observed times
vector<lower=0,upper=1>[N] censor;//censoring indicator (1=observed, 0=censored)
int M;                            // number of covariates
matrix[N, M] x;            // matrix of covariates (with n rows and H columns)
}

parameters {
vector[M] beta; // Coefficients in the linear predictor (including intercept)
real<lower=0> shape;    // shape parameter

}

transformed parameters {
vector[N] linpred;
vector[N] scale;
vector[N] log_lik;
linpred = x*beta;
for (i in 1:N) {
scale[i] = exp(linpred[i]);
}
for (n in 1:N){
log_lik[n] <- surv_MOEXP_lpdf(y[n]|censor[n], shape, x[n]*scale);
}

}

model {
shape ~ cauchy(0,25);
beta ~ normal(0,1000);
y ~ surv_MOEXP(censor, shape, scale);
}
generated quantities{
real dev;
dev=0;
dev=dev + (-2)*surv_MOEXP_lpdf(y|censor,shape,scale);

}
``````

[edit: escaped code and auto-indented]

Hi @ABUJARAD, can you edit your post to show the code intended for easier reading? You can use three backticks ` to start and end a code block like this

``````functions{
//defined survival
vector log_s(vector t, real shape, vector scale) {
vector[num_elements(t)] log_s;
...
``````

It will be then easier to answer any model questions.

You can’t get AIC, as AIC uses maximum likelihood and you have priors. You could compute WAIC, but probably don’t want to, see http://link.springer.com/article/10.1007/s11222-016-9696-4 (preprint https://arxiv.org/abs/1507.04544).

Also, you should indent code for readability. I edited the original post and auto-indented the code using our emacs mode.

Thanks Bob

You can use this `log_lik` with `loo` package. It would be faster to compute it in generated quantities.

Thank you for your interest in the subject and reply. But I want to find WAIC when i have censored data.

You can use `loo` package and instructions in Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC (preprint) to compute waic. But if you care about your analysis, you have been now warned that you are more likely to get non-sensical results with waic than with loo as discussed in the article linked above.