I need to fit a parametric model(s) to the PFS curve in the present dataset (E.g. exponential/Weibull distribution)
dput(head(data2, 10))
structure(list(Time = c(0, 2.99065, 5.98131, 8.97196, 11.9626,
14.9533, 17.9439, 20.9346, 23.9252, 26.9159), PFS = c(1, 0.851852,
0.793651, 0.671958, 0.613757, 0.486772, 0.391534, 0.322751, 0.285714,
0.285714)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
I know that there is a need to choose the following function:
runPSM <- function(data,
time_var, event_var, weight_var = "",
model.type = c("Separate", "Common shape", "Independent shape"),
distr = c('exp',
'weibull',
'gompertz',
'lnorm',
'llogis',
'gengamma',
'gamma',
'genf'
),
strata_var,
int_name,
ref_name){
#test that a legitimate value for model type has been provided
assertthat::assert_that(
all(model.type %in% c('Common shape', 'Independent shape', 'Separate', 'One arm')),
msg = "Only the following model types are supported are supported: 'Common shape', 'Independent shape', 'Separate', 'One arm' "
)
# store the passed information
config = list(data = data,
time_var = time_var,
event_var = event_var,
weight_var = weight_var,
model.type = model.type,
distr = distr,
int_name = int_name)
# these parameters are optional depending on the model
# fit so only store if used
if(!missing(strata_var)){
config$strata_var = strata_var
}
if(!missing(ref_name)){
config$ref_name = ref_name
}
#For models with common shape, calculate the location
#parameter for the treatment arm for each distribution
models <- list()
model_summary <- tibble::tibble()
parameters_vector <- numeric()
if('Common shape' %in% model.type){
output1 <- run_common_shape(data, time_var, event_var, weight_var, distr,strata_var, int_name, ref_name)
models <- output1$models
model_summary <- output1$model_summary
parameters_vector <- output1$parameters_vector
}
# For separate models split the data by treatment and for 2 separate models
# for each distribution
if('Separate' %in% model.type){
output2 <- run_separate(data, time_var, event_var, weight_var, distr, strata_var, int_name, ref_name)
models <- c(models, output2$models)
model_summary <- dplyr::bind_rows(model_summary, output2$model_summary)
parameters_vector <- c(parameters_vector, output2$parameters_vector)
}
#For models with independent shape calculate the scale and shape parameters for the
#treatment arm for each distribution
if('Independent shape' %in% model.type){
output3 <- run_independent_shape(data, time_var, event_var, weight_var, distr, strata_var, int_name, ref_name)
models <- c(models, output3$models)
model_summary <- dplyr::bind_rows(model_summary, output3$model_summary)
parameters_vector <- c(parameters_vector, output3$parameters_vector)
}
if('One arm' %in% model.type){
output4 <- run_one_arm(data, time_var, event_var, weight_var, distr, int_name)
models <- c(models, output4$models)
model_summary <- dplyr::bind_rows(model_summary, output4$model_summary)
parameters_vector <- c(parameters_vector, output4$parameters_vector)
}
# fix no visible binding issues
mdl1 <- mdl2 <- mdl3 <- ARM <- Intervention_name <- NULL
Status <- AIC <- BIC <- flexsurvfit <- Strata <- Model <- Dist <- NULL
# standardise the model summary output to match other outputs
s1 <- strsplit(model_summary$Dist, split = ".", fixed = TRUE)
Reference_name <- NA
temp_model_summary <- model_summary %>%
dplyr::transmute(
mdl1 = sapply(s1, function(x){x[1]}),
mdl2 = sapply(s1, function(x){x[2]}),
mdl3 = sapply(s1, function(x){x[3]}),
flexsurvfit = Dist,
model.type = mdl1,
distr = ifelse(mdl1 == "onearm", mdl3, mdl2),
ARM = ifelse(mdl1 == "sep", mdl3, ifelse(mdl1 == "onearm", mdl2, NA)),
Strata = ifelse(!is.na(ARM),
ifelse(ARM == "int", "Intervention", "Reference"),
NA),
Intervention_name,
Reference_name = ifelse(mdl1 == "onearm", NA, Reference_name),
Status,
AIC,
BIC
)
# define factors for models
Model.levels <- c("Kaplan Meier",
"Common shape", "Independent shape",
"Separate - Reference", "Separate - Intervention",
"One arm - Intervention")
Dist.orig <- c("exp", "weibull", "lnorm", "gamma", "gengamma",
"genf", "llogis", "gompertz", "Kaplan Meier")
Dist.levels <- c("Exponential", "Weibull", "Log Normal", "Gamma", "Generalized Gamma",
"Generalized F", "Log Logistic", "Gompertz", "Kaplan Meier")
final_model_summary <- temp_model_summary %>%
dplyr::transmute(
flexsurvfit,
Model = ifelse(model.type == "indshp","Independent shape",
ifelse(model.type == "comshp", "Common shape",
paste0(ifelse(model.type == "sep", "Separate", "One arm"), " - ", Strata))),
ModelF = factor(Model, levels = Model.levels, ordered = TRUE),
Dist = as.character(factor(distr, levels=Dist.orig, labels = Dist.levels)),
DistF = factor(Dist, levels = Dist.levels, ordered = TRUE),
distr,
Intervention_name,
Reference_name,
Status,
AIC,
BIC
)
# combine the outputs
output <- list(models = models,
model_summary = final_model_summary,
parameters_vector = parameters_vector,
config = config
)
return(output)
}
For some reason I cannot figure out the cause of, the outcome is an error
psm_PFS_all <- runPSM(data2,
time_var= "Time",
event_var= "data2$PFS",
c("Separate", "Common shape", "Independent shape"),
distr = c('exp',
'weibull',
'gompertz',
'lnorm',
'llogis',
'gengamma',
'gamma',
'genf'),
strata_var = "",
int_name="A",
ref_name = "B")
Error in run_common_shape(data, time_var, event_var, weight_var, distr, :
could not find function "run_common_shape"
Could you please help me out to figure out where this problem depends on?
The function is at the following link: https://rdrr.io/github/iain-t-bennett-roche/flexsurvPlus/src/R/runPSM.R
The tutorial I am sticking to here: https://iain-t-bennett-roche.github.io/flexsurvPlus/articles/Fitting_models_in_R.html