-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
531 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# .coveragerc to control coverage.py | ||
[run] | ||
omit = | ||
test/* | ||
versioneer.py | ||
survivalstan/_version.py | ||
test/* | ||
versioneer.py | ||
survivalstan/_version.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
functions { | ||
vector sqrt_vec(vector x) { | ||
vector[dims(x)[1]] res; | ||
|
||
for (m in 1:dims(x)[1]){ | ||
res[m] = sqrt(x[m]); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
vector bg_prior_lp(real r_global, vector r_local) { | ||
r_global ~ normal(0.0, 10.0); | ||
r_local ~ inv_chi_square(1.0); | ||
|
||
return r_global * sqrt_vec(r_local); | ||
} | ||
} | ||
data { | ||
// dimensions | ||
int<lower=0> N; // number of observations | ||
int<lower=1> M; // number of predictors | ||
|
||
// observations | ||
matrix[N, M] x; // predictors for observation n | ||
vector[N] y; // time for observation n | ||
vector[N] event; // event status (1:event, 0:censor) for obs n | ||
} | ||
parameters { | ||
real<lower=0> tau_s_raw; | ||
vector<lower=0>[M] tau_raw; | ||
vector[M] beta_raw; | ||
real alpha; | ||
} | ||
transformed parameters { | ||
vector[M] beta; | ||
vector[N] lp; | ||
|
||
beta = bg_prior_lp(tau_s_raw, tau_raw) .* beta_raw; | ||
for (n in 1:N) { | ||
lp[n] = exp(dot_product(x[n], beta)); | ||
} | ||
} | ||
model { | ||
// priors | ||
target += normal_lpdf(beta_raw | 0.0, 1.0); | ||
target += normal_lpdf(alpha | 0.0, 1.0); | ||
|
||
// likelihood | ||
for (n in 1:N) { | ||
if (event[n]==1) | ||
target += exponential_lpdf(y[n] | (lp[n] * alpha)); | ||
else | ||
target += exponential_lccdf(y[n] | (lp[n] * alpha)); | ||
} | ||
} | ||
generated quantities { | ||
vector[N] yhat_uncens; | ||
vector[N] log_lik; | ||
|
||
for (n in 1:N) { | ||
yhat_uncens[n] = exponential_rng((lp[n] * alpha)); | ||
if (event[n]==1) { | ||
log_lik[n] = exponential_log(y[n], (lp[n] * alpha)); | ||
} else { | ||
log_lik[n] = exponential_ccdf_log(y[n], (lp[n] * alpha)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* Variable naming: | ||
// dimensions | ||
N = total number of observations (length of data) | ||
S = number of sample ids | ||
T = max timepoint (number of timepoint ids) | ||
M = number of covariates | ||
// main data matrix (per observed timepoint*record) | ||
s = sample id for each obs | ||
t = timepoint id for each obs | ||
event = integer indicating if there was an event at time t for sample s | ||
x = matrix of real-valued covariates at time t for sample n [N, X] | ||
// timepoint-specific data (per timepoint, ordered by timepoint id) | ||
t_obs = observed time since origin for each timepoint id (end of period) | ||
t_dur = duration of each timepoint period (first diff of t_obs) | ||
*/ | ||
// Jacqueline Buros Novik <jackinovik@gmail.com> | ||
|
||
data { | ||
// dimensions | ||
int<lower=1> N; | ||
int<lower=1> S; | ||
int<lower=1> T; | ||
int<lower=0> M; | ||
|
||
// data matrix | ||
int<lower=1, upper=N> s[N]; // sample id | ||
int<lower=1, upper=T> t[N]; // timepoint id | ||
int<lower=0, upper=1> event[N]; // 1: event, 0:censor | ||
matrix[N, M] x; // explanatory vars | ||
|
||
// timepoint data | ||
vector<lower=0>[T] t_obs; | ||
vector<lower=0>[T] t_dur; | ||
} | ||
transformed data { | ||
vector[T] log_t_dur; // log-duration for each timepoint | ||
|
||
log_t_dur = log(t_obs); | ||
} | ||
parameters { | ||
vector[T] log_baseline_raw; // unstructured baseline hazard for each timepoint t | ||
vector[M] beta; // beta for each covariate | ||
real<lower=0> baseline_sigma; | ||
real log_baseline_mu; | ||
} | ||
transformed parameters { | ||
vector[N] log_hazard; | ||
vector[T] log_baseline; // unstructured baseline hazard for each timepoint t | ||
|
||
log_baseline = log_baseline_raw + log_t_dur; | ||
|
||
for (n in 1:N) { | ||
log_hazard[n] = log_baseline_mu + log_baseline[t[n]] + x[n,]*beta; | ||
} | ||
} | ||
model { | ||
beta ~ cauchy(0, 2); | ||
event ~ poisson_log(log_hazard); | ||
log_baseline_mu ~ normal(0, 1); | ||
baseline_sigma ~ normal(0, 1); | ||
log_baseline_raw ~ normal(0, baseline_sigma); | ||
} | ||
generated quantities { | ||
real log_lik[N]; | ||
//int yhat_uncens[N]; | ||
vector[T] baseline_raw; | ||
|
||
// compute raw baseline hazard, for summary/plotting | ||
baseline_raw = exp(log_baseline_raw); | ||
|
||
// prepare yhat_uncens & log_lik | ||
for (n in 1:N) { | ||
//yhat_uncens[n] = poisson_log_rng(log_hazard[n]); | ||
log_lik[n] = poisson_log_log(event[n], log_hazard[n]); | ||
} | ||
} |
Oops, something went wrong.