Using integrate_1d
:
functions{
real cumulative_exposure(real x, // Function argument
real xc, // Complement of function argument
// on the domain (defined later)
real[] theta, // parameters
real[] x_r, // data (real)
int[] x_i) { // data (integer)
real Beta = theta[1];
real eta = theta[2];
real w2_1 = theta[3];
real w2_2 = theta[4];
return exp(Beta*(eta + w2_1 + w2_2*log(x)));
}
}
data {
int<lower=1> n; // Sample size
int<lower=1> n_cens; // Right censored sample size
int<lower=1> n_obs; // Failed unit sample size
int<lower=1> N; // Total number of times considered (all times for all units)
real<lower=0> tt[N]; //All times for all units
real y_tt[N]; //All of the use-rates
int w_ind[N]; //Indices for w
real<lower=0> tt_fail[n_obs]; //Failure times for units that failed
int w_ind_fail[n_obs]; //Indices for w for units that failed
int w_ind_cens[n_cens];
real<lower=0> tt_cens[n_cens];
}
transformed data {
real x_r[0];
}
parameters {
real <lower = 0> mu0;
real <lower = 0> sigma0;
real <lower = 0> sig;
real <lower = 0> sig1;
real <lower = 0> sig2;
real <lower = -1, upper = 1>rho;
real eta;
real Beta;
matrix[2, n] w;
}
model {
//cumulative exposure for failed units and for censored units
vector[n_obs] u_obs;
vector[n_cens] u_cens;
//Defining covariance matrix, cholesky decomposition and mean and variance of mixed effects model
matrix[2,2] Sigma;
real Mu[N];
vector[n_obs] Mu_DFD;
matrix[2,2] L;
matrix[2, n] w2;
//Covariance matrix
Sigma[1,1] = sig1^2;
Sigma[1,2] = rho*sig1*sig2;
Sigma[2,1] = rho*sig1*sig2;
Sigma[2,2] = sig2^2;
//Cholesky decomposition
L = cholesky_decompose(Sigma);
w2 = L*w;
//Parameters used in mixed effects model
for(i in 1:N){
Mu[i] = eta + w2[1,w_ind[i]] + w2[2,w_ind[i]]*log(tt[i]);
}
//Calculating mu_DFD
for(i in 1:n_obs){
Mu_DFD[i] = eta + w2[1,w_ind_fail[i]] + w2[2,w_ind_fail[i]]*log(tt_fail[i]);
u_obs[i] = integrate_1d(cumulative_exposure, 0.01, tt_fail[i],
{Beta, eta, w2[1,w_ind_fail[i]], w2[2,w_ind_fail[i]]},
x_r, {w_ind_fail[i]}, 1e-8);
}
for(i in 1:n_cens){
u_cens[i] = integrate_1d(cumulative_exposure, 0.01, tt_cens[i],
{Beta, eta, w2[1,w_ind_cens[i]], w2[2,w_ind_cens[i]]},
x_r, {w_ind_cens[i]}, 1e-8);
}
//Likelihood
target += weibull_lpdf(u_obs| 1/sigma0, exp(mu0));
target += Beta*Mu_DFD;
target += weibull_lccdf(u_cens|1/sigma0, exp(mu0));
target += normal_lpdf(y_tt|Mu, sig);
// Prior:
Beta ~ normal(0, 10);
mu0 ~ normal(0, 10);
sigma0 ~ normal(0, 10);
eta ~ normal(0, 10);
to_vector(w) ~ normal(0, 1);
sig ~ normal(0, 10);
sig1 ~ normal(0, 10);
sig2 ~ normal(0, 10);
rho ~ normal(0, 10);
}
Using closed form:
data {
int<lower=1> n; // Sample size
int<lower=1> n_cens; // Right censored sample size
int<lower=1> n_obs; // Failed unit sample size
int<lower=1> N; // Total number of times considered (all times for all units)
real<lower=0> tt[N]; //All times for all units
real y_tt[N]; //All of the use-rates
int w_ind[N]; //Indices for w
real<lower=0> tt_fail[n_obs]; //Failure times for units that failed
int w_ind_fail[n_obs]; //Indices for w for units that failed
int w_ind_cens[n_cens];
real<lower=0> tt_cens[n_cens];
}
parameters {
real <lower = 0> mu0;
real <lower = 0> sigma0;
real <lower = 0> sig;
real <lower = 0> sig1;
real <lower = 0> sig2;
real <lower = -1, upper = 1>rho;
real eta;
real Beta;
matrix[2, n] w;
}
model {
//cumulative exposure for failed units and for censored units
vector[n_obs] u_obs;
vector[n_cens] u_cens;
//Defining covariance matrix, cholesky decomposition and mean and variance of mixed effects model
matrix[2,2] Sigma;
real Mu[N];
vector[n_obs] Mu_DFD;
matrix[2,2] L;
matrix[2, n] w2;
//Covariance matrix
Sigma[1,1] = sig1^2;
Sigma[1,2] = rho*sig1*sig2;
Sigma[2,1] = rho*sig1*sig2;
Sigma[2,2] = sig2^2;
//Cholesky decomposition
L = cholesky_decompose(Sigma);
w2 = L*w;
//Parameters used in mixed effects model
for(i in 1:N){
Mu[i] = eta + w2[1,w_ind[i]] + w2[2,w_ind[i]]*log(tt[i]);
}
//Calculating mu_DFD
for(i in 1:n_obs){
Mu_DFD[i] = eta + w2[1,w_ind_fail[i]] + w2[2,w_ind_fail[i]]*log(tt_fail[i]);
u_obs[i] = exp(Beta*(eta + w2[1,w_ind_fail[i]]))/(Beta*w2[2,w_ind_fail[i]] + 1)*(tt_fail[i]^(Beta*w2[2,w_ind_fail[i]] + 1) - 1);
}
for(i in 1:n_cens){
u_cens[i] = exp(Beta*(eta + w2[1,w_ind_cens[i]]))/(Beta*w2[2,w_ind_cens[i]] + 1)*(tt_cens[i]^(Beta*w2[2,w_ind_cens[i]] + 1) - 1);
}
//Likelihood
target += weibull_lpdf(u_obs| 1/sigma0, exp(mu0));
target += Beta*Mu_DFD;
target += weibull_lccdf(u_cens|1/sigma0, exp(mu0));
target += normal_lpdf(y_tt|Mu, sig);
// Prior:
Beta ~ normal(0, 10);
mu0 ~ normal(0, 10);
sigma0 ~ normal(0, 10);
eta ~ normal(0, 10);
to_vector(w) ~ normal(0, 1);
sig ~ normal(0, 10);
sig1 ~ normal(0, 10);
sig2 ~ normal(0, 10);
rho ~ normal(0, 10);
}