-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added example of log-logistic distribution - Added examples of how to calculate AIC / BIC / LOO - Added examples of how to calculate survival quantities + plots - Added brms examples
- Loading branch information
Showing
9 changed files
with
458 additions
and
206 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
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,157 @@ | ||
|
||
|
||
|
||
library(dplyr) | ||
library(flexsurv) | ||
library(survival) | ||
library(cmdstanr) | ||
library(posterior) | ||
library(bayesplot) | ||
library(here) | ||
|
||
|
||
dat <- flexsurv::bc |> | ||
as_tibble() |> | ||
mutate(arm = "A", study = "S", pt = sprintf("pt-%05d", 1:n())) | ||
|
||
|
||
# | ||
# | ||
# Have specified no covariates in this models so that they are comparable | ||
# JMpost uses a PH model where as the others use AFT thus adding covariates | ||
# would result in different models. Without covariates they are just fitting | ||
# the base distribution which should be identical. | ||
# | ||
# | ||
|
||
|
||
|
||
|
||
################################ | ||
# | ||
# Flexsurv parametric Regression | ||
# | ||
|
||
|
||
mod_flex <- flexsurvreg( | ||
Surv(recyrs, censrec) ~ 1, | ||
data = dat, | ||
dist = "llogis" | ||
) | ||
mod_flex | ||
|
||
logLik(mod_flex) | ||
AIC(mod_flex) | ||
BIC(mod_flex) | ||
|
||
|
||
################################ | ||
# | ||
# Bayesian Weibull Regression | ||
# | ||
|
||
mod <- cmdstan_model( | ||
stan_file = here("design/examples/loglogistic.stan"), | ||
exe_file = here("design/examples/models/loglogistic") | ||
) | ||
|
||
design_mat <- model.matrix(~ 1, data = dat) | ||
|
||
|
||
stan_data <- list( | ||
n = nrow(dat), | ||
design = design_mat, | ||
p = ncol(design_mat), | ||
times = dat$recyrs, | ||
event_fl = dat$censrec | ||
) | ||
fit <- mod$sample( | ||
data = stan_data, | ||
chains = 2, | ||
parallel_chains = 2, | ||
refresh = 200, | ||
iter_warmup = 1000, | ||
iter_sampling = 1500 | ||
) | ||
vars <- c( | ||
"beta_design", | ||
"alpha_0", | ||
"beta_0" | ||
) | ||
fit$summary(vars) | ||
|
||
|
||
# Log Likelihood | ||
log_lik <- fit$draws("log_lik", format = "draws_matrix") |> | ||
apply(1, sum) |> | ||
mean() | ||
log_lik | ||
|
||
# AIC | ||
k <- 2 | ||
-2 * log_lik + k * (stan_data$p + 1) # +1 for the scale parameter | ||
|
||
# BIC | ||
((stan_data$p + 1) * log(stan_data$n)) + (-2 * log_lik) | ||
|
||
|
||
|
||
################################ | ||
# | ||
# JMpost | ||
# | ||
|
||
devtools::load_all() | ||
# library(jmpost) | ||
|
||
jm <- JointModel( | ||
survival = SurvivalLogLogistic() | ||
) | ||
|
||
jdat <- DataJoint( | ||
subject = DataSubject( | ||
data = dat, | ||
subject = "pt", | ||
arm = "arm", | ||
study = "study" | ||
), | ||
survival = DataSurvival( | ||
data = dat, | ||
formula = Surv(recyrs, censrec) ~ 1 | ||
) | ||
) | ||
|
||
mp <- sampleStanModel( | ||
jm, | ||
data = jdat, | ||
iter_warmup = 1000, | ||
iter_sampling = 1500, | ||
chains = 2, | ||
parallel_chains = 2 | ||
) | ||
|
||
vars <- c( | ||
"sm_logl_lambda", | ||
"sm_logl_p" | ||
) | ||
|
||
x <- mp@results$summary(vars) | ||
|
||
c( | ||
"scale" = 1 / x$mean[1], | ||
"shape" = x$mean[2] | ||
) | ||
|
||
|
||
# Log Likelihood | ||
log_lik <- mp@results$draws("log_lik", format = "draws_matrix") |> | ||
apply(1, sum) |> | ||
mean() | ||
log_lik | ||
|
||
# AIC | ||
k <- 2 | ||
-2 * log_lik + k * 2 | ||
|
||
# BIC | ||
(2 * log(nrow(dat))) + (-2 * log_lik) |
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,51 @@ | ||
|
||
|
||
|
||
data { | ||
int<lower=1> n; // Number of subjects | ||
int<lower=0> p; // Number of covariates (including intercept) | ||
vector<lower=0>[n] times; // Event|Censor times | ||
array[n] int<lower=0, upper=1> event_fl; // 1=event 0=censor | ||
matrix[n, p] design; // Design matrix | ||
} | ||
|
||
transformed data { | ||
// Assuming that the first term is an intercept column which | ||
// will conflict with the alpha_0 term so remove it | ||
matrix[n, p-1] design_reduced; | ||
if (p > 1 ) { | ||
design_reduced = design[, 2:p]; | ||
}} | ||
|
||
parameters { | ||
vector[p-1] beta_design; | ||
real<lower=0> alpha_0; | ||
real<lower=0> beta_0; | ||
} | ||
|
||
transformed parameters { | ||
vector[n] alpha; | ||
if (p == 1) { | ||
alpha = rep_vector(alpha_0, n); | ||
} else { | ||
alpha = alpha_0 .* exp(design_reduced * beta_design); | ||
} | ||
|
||
// Likelihood | ||
vector[n] log_lik; | ||
for (i in 1:n) { | ||
if (event_fl[i] == 1) { | ||
log_lik[i] = loglogistic_lpdf(times[i] | alpha[i], beta_0); | ||
} else { | ||
log_lik[i] = log(1 - loglogistic_cdf(times[i] | alpha[i], beta_0)); | ||
} | ||
} | ||
} | ||
|
||
model { | ||
// Priors | ||
beta_design ~ normal(0, 3); | ||
alpha_0 ~ lognormal(log(2), 1); | ||
beta_0 ~ lognormal(log(2), 1); | ||
target += sum(log_lik); | ||
} |
Oops, something went wrong.