forked from opensafely/post-covid-cvd-methods
-
Notifications
You must be signed in to change notification settings - Fork 0
Across MI #2
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
Open
RaspberryEmma
wants to merge
17
commits into
main
Choose a base branch
from
across-MI
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Across MI #2
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f1f7dcc
partial implementation
RaspberryEmma 2eafbe4
complete across MI implementation, posthoc removed
RaspberryEmma fb7a73f
Delete analysis/post_hoc_vars directory
RaspberryEmma e4505c2
no longer remove dates from dataframe
RaspberryEmma c496389
Added 1/M weights to all models except cox_ipw
RaspberryEmma c3df6b8
hotfix dataframes
RaspberryEmma 468b6ef
refactor: removed magic number for imp datasets
RaspberryEmma 15cb61e
custom weights for cox_ipw reusable action
RaspberryEmma 0ea453b
fully dynamic number of imputed datasets
RaspberryEmma 706ec19
refactored generate_weights function
RaspberryEmma 4d01292
table1, table2 output changes
RaspberryEmma d3aa1c3
hotfixes: bmi handling and gsub bug
RaspberryEmma f734c60
convert_terms_to_vars fix
RaspberryEmma aa480ba
new action to generate Nelson-Aalen plots
RaspberryEmma b75042c
table of missingness values
RaspberryEmma 24a8952
robust SE in cox models
RaspberryEmma 4846359
nelson aalen SEs and updated plots
RaspberryEmma 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| check_covariates <- function(df, covariate_threshold, strata) { | ||
| library(magrittr) | ||
|
|
||
| # Identify non-numeric covariates to remove ---------------------------------- | ||
| print("Identify non-numeric covariates to remove") | ||
|
|
||
| covariate_removed <- NULL | ||
|
|
||
| for (i in unique(c(colnames(df)[grepl("cov_", colnames(df))], strata))) { | ||
| # Consider non-numeric covariates ------------------------------------------ | ||
|
|
||
| if (grepl("cov_bin_", i) || grepl("cov_cat_", i)) { | ||
| print(paste0("Covariate: ", i)) | ||
|
|
||
| # Calculate frequency for each level ------------------------------------- | ||
| print("Calculate frequency for each level") | ||
|
|
||
| tmp <- unique(df[ | ||
| df$exposure_status == 1 & df$outcome_status == 1, | ||
| c("patient_id", i) | ||
| ]) | ||
| freq <- data.frame(table(tmp[, i])) | ||
|
|
||
| print(freq) | ||
|
|
||
| # Add covariates to removal list if they fall below covariate threshold -- | ||
|
|
||
| if ( | ||
| nrow(freq[ | ||
| freq$Freq <= covariate_threshold & freq$Var1 != "Missing", | ||
| ]) > | ||
| 0 | ||
| ) { | ||
| print("Add covariate to removal list") | ||
| covariate_removed <- c(covariate_removed, i) | ||
| } | ||
|
|
||
| # Add covariates to removal list if the covariate has a single level ----- | ||
|
|
||
| if (nrow(freq) == 1) { | ||
| print("Add covariate to removal list") | ||
| covariate_removed <- c(covariate_removed, i) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Collapse special case covariates ------------------------------------------- | ||
|
|
||
| covariate_collapsed <- NULL | ||
|
|
||
| ## Region | ||
|
|
||
| if ("cov_cat_region" %in% covariate_removed) { | ||
| print("Collapsing region as special case") | ||
|
|
||
| df <- df %>% | ||
| dplyr::mutate( | ||
| cov_cat_region = dplyr::case_when( | ||
| cov_cat_region == "North East" ~ "Northern England", | ||
| cov_cat_region == "North West" ~ "Northern England", | ||
| cov_cat_region == "Yorkshire and The Humber" ~ "Northern England", | ||
| cov_cat_region == "London" ~ "Southern England", | ||
| cov_cat_region == "South East" ~ "Southern England", | ||
| cov_cat_region == "East Midlands" ~ "Midlands", | ||
| cov_cat_region == "West Midlands" ~ "Midlands", | ||
| cov_cat_region == "South West" ~ "Southern England", | ||
| cov_cat_region == "East" ~ "Southern England" | ||
| ) | ||
| ) | ||
|
|
||
| df$cov_cat_region <- factor(df$cov_cat_region) | ||
| df$cov_cat_region <- relevel(df$cov_cat_region, ref = "Southern England") | ||
|
|
||
| covariate_removed <- setdiff(covariate_removed, "cov_cat_region") | ||
| covariate_collapsed <- c(covariate_collapsed, "cov_cat_region") | ||
| } | ||
|
|
||
| ## Deprivation | ||
|
|
||
| if ("cov_cat_deprivation" %in% covariate_removed) { | ||
| print("Collapsing deprivation as special case") | ||
|
|
||
| df <- df %>% | ||
| dplyr::mutate( | ||
| cov_cat_deprivation = dplyr::case_when( | ||
| cov_cat_deprivation == "1-2 (most deprived)" ~ "1-4", | ||
| cov_cat_deprivation == "3-4" ~ "1-4", | ||
| cov_cat_deprivation == "5-6" ~ "5-6", | ||
| cov_cat_deprivation == "7-8" ~ "7-10", | ||
| cov_cat_deprivation == "9-10 (least deprived)" ~ "7-10" | ||
| ) | ||
| ) | ||
|
|
||
| df$cov_cat_deprivation <- ordered( | ||
| df$cov_cat_deprivation, | ||
| levels = c("1-4", "5-6", "7-10") | ||
| ) | ||
|
|
||
| covariate_removed <- setdiff(covariate_removed, "cov_cat_deprivation") | ||
| covariate_collapsed <- c(covariate_collapsed, "cov_cat_deprivation") | ||
| } | ||
|
|
||
| # Smoking status ------------------------------------------------------------- | ||
|
|
||
| if ("cov_cat_smoking_status" %in% covariate_removed) { | ||
| print("Collapsing smoking status as special case") | ||
|
|
||
| df <- df %>% | ||
| dplyr::mutate( | ||
| cov_cat_smoking_status = dplyr::case_when( | ||
| cov_cat_smoking_status == "Never smoker" ~ "Never smoker", | ||
| cov_cat_smoking_status == "Ever smoker" ~ "Ever smoker", | ||
| cov_cat_smoking_status == "Current smoker" ~ "Ever smoker", | ||
| cov_cat_smoking_status == "Missing" ~ "Missing" | ||
| ) | ||
| ) | ||
|
|
||
| df$cov_cat_smoking_status <- ordered( | ||
| df$cov_cat_smoking_status, | ||
| levels = c("Never smoker", "Ever smoker", "Missing") | ||
| ) | ||
|
|
||
| covariate_removed <- setdiff(covariate_removed, "cov_cat_smoking_status") | ||
| } | ||
|
|
||
| # Check special case collapsed covariates ------------------------------------ | ||
|
|
||
| for (i in c( | ||
| "cov_cat_deprivation", | ||
| "cov_cat_smoking_status", | ||
| "cov_cat_region" | ||
| )) { | ||
| if (i %in% colnames(df)) { | ||
| print(paste0("Rechecking covariate: ", i)) | ||
|
|
||
| # Calculate frequency for each level among exposed with outcome ---------- | ||
| print("Calculate frequency for each level among exposed with outcome") | ||
|
|
||
| tmp <- unique(df[ | ||
| df$exposure_status == 1 & df$outcome_status == 1, | ||
| c("patient_id", i) | ||
| ]) | ||
| freq <- data.frame(table(tmp[, i])) | ||
|
|
||
| print(freq) | ||
|
|
||
| # Add covariates to removal list if they fall below covariate threshold -- | ||
|
|
||
| if ( | ||
| nrow(freq[ | ||
| freq$Freq <= covariate_threshold & freq$Var1 != "Missing", | ||
| ]) > | ||
| 0 | ||
| ) { | ||
| print("Add covariate to removal list") | ||
| covariate_removed <- c(covariate_removed, i) | ||
| covariate_collapsed <- setdiff(covariate_collapsed, i) | ||
| } | ||
|
|
||
| # Add covariates to removal list if the covariate has a single level ----- | ||
|
|
||
| if (nrow(freq) == 1) { | ||
| print("Add covariate to removal list") | ||
| covariate_removed <- c(covariate_removed, i) | ||
| covariate_collapsed <- setdiff(covariate_collapsed, i) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Check strata variables meet covariate threshold ---------------------------- | ||
| print("Check strata variables meet covariate threshold") | ||
|
|
||
| strata_warning <- "" | ||
|
|
||
| if (length(intersect(covariate_removed, strata)) > 0) { | ||
| strata_warning <- paste0( | ||
| intersect(covariate_removed, strata), | ||
| collapse = ";" | ||
| ) | ||
| for (i in intersect(covariate_removed, strata)) { | ||
| tmp <- unique(df[ | ||
| df$exposure_status == 1 & df$outcome_status == 1, | ||
| c("patient_id", i) | ||
| ]) | ||
| freq <- data.frame(table(tmp[, i])) | ||
| print(paste0( | ||
| "Warning: strata variable '", | ||
| i, | ||
| "' does not meet covariate threshold" | ||
| )) | ||
| print(freq) | ||
| } | ||
| } | ||
|
|
||
| # Remove covariates ---------------------------------------------------------- | ||
| print("Remove covariates") | ||
|
|
||
| df <- df[, !(colnames(df) %in% setdiff(covariate_removed, strata))] | ||
|
|
||
| # Return data and list of removed covariates --------------------------------- | ||
|
|
||
| output <- list( | ||
| df = df, | ||
| covariate_removed = setdiff(covariate_removed, strata), | ||
| covariate_collapsed = covariate_collapsed, | ||
| strata_warning = strata_warning | ||
| ) | ||
|
|
||
| return(output) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updates to add function instead of absolute number