-
Notifications
You must be signed in to change notification settings - Fork 4
418-population-effects-implementation #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e5647c6
first try
gravesti 3ae1c6c
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
github-actions[bot] 2601b87
update on feedback
gravesti d0d8ffb
seeds
gravesti f93dc75
tidy up
gravesti 23e408a
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
github-actions[bot] 3689975
more comments
gravesti 362f362
fix case
gravesti b2e5f7b
fix lints and test
gravesti 247ca1f
missing comma
gravesti 5e1baa2
fix tests
gravesti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,83 +1,85 @@ | ||
| #' Calculate Population Hazard Ratios | ||
| #' | ||
| #' Calculates hazard ratios marginalised over subject specific random effects | ||
| #' Calculates hazard ratios marginalised over subject specific random effects using the | ||
| #' approach proposed by \insertCite{oudenhoven2020marginal}{jmpost}. | ||
| #' | ||
| #' @param object ([`JointModelSamples`]) \cr samples as drawn from a Joint Model. | ||
| #' @param HR_terms (`character`) \cr to include in the hazard ratio calculation. | ||
| #' By default this include the terms used in the survival model. | ||
| #' @param HR_formula (`formula`) \cr defines the terms to include in the hazard ratio calculation. | ||
| #' By default this uses the right side of the formula used in the survival model. | ||
| #' Set to `NULL` not include any terms | ||
| #' @param arm (`logical`) \cr calculate hazard ratio for `arm`. | ||
| #' @param baseline (`string`) \cr terms to model baseline hazard using variable `"time"`. | ||
| #' Default is a B-spline from [splines]: `"bs(time, df=10)"` | ||
| #' @param baseline (`formula`) \cr terms to model baseline hazard using variable `time`. | ||
| #' Default is a B-spline from [splines]: `~bs(time, df = 10)` | ||
| #' @param quantiles (`numeric`) \cr vector of two values in (0, 1) for calculating quantiles from log hazard ratio | ||
| #' distributions. | ||
| #' | ||
gravesti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #' @returns A matrix | ||
| #' @references \insertAllCited{} | ||
| #' | ||
| #' @returns A matrix | ||
| #' @export | ||
| #' @importFrom splines bs | ||
| populationHR <- function( | ||
| object, | ||
| HR_formula = object@data@survival@formula, | ||
| baseline = ~ bs(time, df = 10), | ||
| quantiles = c(0.025, 0.975) | ||
| ) { | ||
| assert_class(object, "JointModelSamples") | ||
| assert_formula(HR_formula) | ||
| assert_formula(baseline) | ||
| assert_numeric(quantiles, lower = 0, upper = 1, any.missing = FALSE, unique = TRUE) | ||
| if (!"time" %in% all.vars(baseline)) stop("baseline formula should include a time term.") | ||
|
|
||
| PopulationHR <- function(object, | ||
| HR_terms = attr(terms(object@data@survival@formula), "term.labels"), | ||
| arm = TRUE, | ||
| baseline = "bs(time, df=10)") { | ||
| # Extract the variable names used in the data | ||
gravesti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| subject_var <- object@data@subject@subject | ||
| arm_var <- object@data@subject@arm | ||
| long_time_var <- all.vars(delete.response(terms(object@data@longitudinal@formula))) | ||
| surv_time_var <- all.vars(object@data@survival@formula[[2]][[2]]) | ||
| surv_covs <- all.vars(delete.response(terms(object@data@survival@formula))) | ||
| surv_covs <- all.vars(delete.response(terms(HR_formula))) | ||
| if (!all(surv_covs %in% colnames(object@data@survival@data))) { | ||
| stop("All variables in HR_formula must be in survival data") | ||
| } | ||
|
|
||
| population_terms <- unique(c( | ||
| baseline, | ||
| if (arm) arm_var else NULL, | ||
| HR_terms | ||
| )) | ||
|
|
||
| marginal_formula <- reformulate(population_terms) | ||
| marginal_formula <- stats::reformulate(c( | ||
| attr(terms(baseline), "term.labels"), | ||
| attr(terms(HR_formula), "term.labels") | ||
| )) | ||
|
|
||
| # Get the survival quantities at the observed longitudinal times | ||
| # and survival event times for each patient: | ||
| # Get the survival quantities at the observed longitudinal and survival times for each patient: | ||
| times_df <- rbind( | ||
| object@data@longitudinal@data |> | ||
| select(subject = {{subject_var}}, time = {{long_time_var}}), | ||
| object@data@survival@data |> | ||
| select(subject = {{subject_var}}, time = {{surv_time_var}}) | ||
| ) |> | ||
| filter(time > 0) | ||
| stats::setNames(object@data@longitudinal@data[, c(subject_var, long_time_var)], c("subject", "time")), | ||
| stats::setNames(object@data@survival@data[, c(subject_var, surv_time_var)], c("subject", "time")) | ||
| ) | ||
| times_df <- times_df[order(times_df$subject, times_df$time), ] | ||
| times_df <- times_df[times_df$time > 0, ] | ||
| times_df <- times_df[!duplicated.data.frame(times_df), ] | ||
|
|
||
| # Generate samples of the log hazard log h_i(t) for each patient i at these time points t. | ||
| grid_spec <- split(times_df$time, times_df$subject) | ||
| logH <- SurvivalQuantities( | ||
| log_haz_samples <- SurvivalQuantities( | ||
| object, | ||
| grid = GridManual(grid_spec), | ||
| type = "loghaz" | ||
| )@quantities@quantities |> t() | ||
|
|
||
| # Construct \tilde{X} in paper's notation with one row per patient's time | ||
| W_df <- times_df |> | ||
| select(subject, time) |> | ||
| left_join( | ||
| object@data@subject@data |> | ||
| select(subject = {{subject_var}}, arm = {{arm_var}}), | ||
| by = "subject" | ||
| ) |> | ||
| left_join( | ||
| object@data@survival@data |> | ||
| select(subject = {{subject_var}}, {{surv_covs}}), | ||
| by = "subject" | ||
| ) | ||
|
|
||
| W_df <- dplyr::left_join( | ||
| times_df, | ||
| stats::setNames(object@data@survival@data[, c(subject_var, surv_covs)], c("subject", surv_covs)), | ||
| by = "subject" | ||
| ) | ||
| W_mat <- model.matrix(marginal_formula, W_df) | ||
|
|
||
| # As model matrix contains baseline and covariates, we don't need the intercept term | ||
| # but we want factor variables encoded relative to the intercept | ||
| W_mat <- W_mat[, colnames(W_mat) != "(Intercept)", drop = FALSE] | ||
|
|
||
| estimates <- lm.fit(x = W_mat, y = logH)$coefficients | ||
| estimates <- stats::lm.fit(x = W_mat, y = log_haz_samples)$coefficients | ||
| tidy_res <- apply(estimates, 1, function(x) { | ||
| c(mean = mean(x), | ||
| median = median(x), | ||
| quantile(x, c(0.025, 0.975)) | ||
| ) | ||
| }) |> t() | ||
| quantiles <- stats::quantile(x, probs = quantiles) | ||
| c(mean = mean(x), median = median(x), quantiles) | ||
| }) |> | ||
| t() | ||
|
|
||
| list(tidy_res, estimates) | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.