Skip to content

Commit

Permalink
Enhanced Comparison Examples
Browse files Browse the repository at this point in the history
- 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
gowerc authored Feb 22, 2024
1 parent ca04ed1 commit dcce0f8
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 206 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ S3method(extractVariableNames,DataSurvival)
S3method(generateQuantities,JointModelSamples)
S3method(getParameters,Link)
S3method(getParameters,LinkComponent)
S3method(getParameters,StanModel)
S3method(getParameters,default)
S3method(initialValues,JointModel)
S3method(initialValues,Link)
S3method(initialValues,LinkComponent)
Expand Down
1 change: 1 addition & 0 deletions R/StanModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ as.list.StanModel <- function(x, ...) {
# getParameters-StanModel ----

#' @rdname getParameters
#' @export
getParameters.StanModel <- function(object) object@parameters


Expand Down
1 change: 1 addition & 0 deletions R/defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ NULL


#' @rdname getParameters
#' @export
getParameters.default <- function(object) {
if (missing(object) || is.null(object)) {
return(NULL)
Expand Down
157 changes: 157 additions & 0 deletions design/examples/loglogistic.R
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)
51 changes: 51 additions & 0 deletions design/examples/loglogistic.stan
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);
}
Loading

0 comments on commit dcce0f8

Please sign in to comment.