if (FALSE) {
library(dplyr)
# Example 1: Incorporating Expert Opinion on Survival Probabilities Using MLE
# Define expert opinion as a normal distribution centered at 0.1 with sd 0.05
param_expert_example1 <- list()
param_expert_example1[[1]] <- data.frame(
dist = "norm",
wi = 1, # Ensure weights sum to 1 across experts
param1 = 0.1,
param2 = 0.05,
param3 = NA
)
# Time point at which expert opinion is provided
timepoint_expert <- 14 # For example, 14 months
# Prepare the data
# Assume 'data' is your dataset containing 'time' and 'censored' variables
data2 <- data %>%
rename(status = censored) %>%
mutate(
time2 = ifelse(time > 10, 10, time),
status2 = ifelse(time > 10, 0, status)
)
# Fit the survival models using MLE, incorporating expert opinion
example1 <- fit.models.expert(
formula = Surv(time2, status2) ~ 1,
data = data2,
distr = c("wei", "gom"), # Weibull and Gompertz distributions
method = "mle",
opinion_type = "survival",
times_expert = timepoint_expert,
param_expert = param_expert_example1
)
# Plot the fitted survival curves along with the Kaplan-Meier estimate
plot(example1, add.km = TRUE, t = 0:20)
# Compare models using Akaike Information Criterion (AIC)
model.fit.plot(example1, type = "aic")
# Example 2: Incorporating Expert Opinion Using Bayesian Estimation
# Pre-compile Stan models (ideally after installing the package)
# This step can be time-consuming but only needs to be done once per session
compiled_models_saved <- compile_stan()
# Fit the survival models using Bayesian estimation with expert opinion
example1_bayes <- fit.models.expert(
formula = Surv(time2, status2) ~ 1,
data = data2,
distr = c("wei", "gom"),
method = "bayes",
opinion_type = "survival",
times_expert = timepoint_expert,
param_expert = param_expert_example1,
iter = 2000, # Set to a high number for convergence (e.g., 2000 or more)
compile_mods = compiled_models_saved
)
# Summarize the Bayesian model results
summary(example1_bayes)
# Plot the Bayesian fitted survival curves
plot(example1_bayes, add.km = TRUE, t = 0:20)
}
Run the code above in your browser using DataLab