Good morning all,
I am trying to write a model on rstan for my time to event data.
I use a Weibull model for my time of occurrence and a right censoring time.
In R, the calculation of my log pdf is as follows:
If you denote by T
the vector of times at which event occured, Tc
the censoring time (T has thus its first value 0, a certain number of non null times and as last value Tc
, lambda
and beta
my parameters (on which I put lognormal priors by the way) and , my logpdf is calculated as follows
N <- length(T)
init <- which(T==0)
cens <- which(T==Tc)
ind <- setdiff(1:N, append(init,cens))
hazard <- (beta/lambda)*(T/lambda)^(beta-1) #my hazard function
H <- (T/lambda)^beta #the primitve of the hazard function
logpdf <- rep(0,N)
logpdf[cens] <- -H[cens] + H[cens-1]
logpdf[ind] <- -H[ind] + H[ind-1] + log(hazard[ind])
This is an implementation of the theoretical pdf for such models defined as
p(y |\psi) = \exp\left\{-\int_{0}^{T_c}h(u, \lambda,\beta)\textrm{d}u \right\} \prod_{j=1}^{n-1} h(t_{j}, \lambda,\beta)
How would you proceed in writing such model?
Here is an initial version
model <- 'data {
int<lower=0> N_obs;// Number of observations
vector[N_obs] time; //obs
real<lower=0> lambda_pop;
real<lower=0> beta_pop;
real<lower=0> omega_lambda;
real<lower=0> omega_beta;
}
parameters {
vector<lower=0>[2] param;
}
model {
//Priors
param[1] ~ lognormal( lambda_pop , omega_lambda);
param[2] ~ lognormal( beta_pop , omega_beta);
time ~ weibull(param[1], param[2]);
}'