From 49df8fe37ad3b55bb2d54a0c0d47ae74448c4307 Mon Sep 17 00:00:00 2001 From: nocia Date: Wed, 24 Jan 2024 12:15:24 +0000 Subject: [PATCH] write function without dplyr functions and change vignette title --- vignettes/CondMean_Inference.Rmd | 61 +++++---- vignettes/CondMean_Inference.html | 168 ++++++++++++------------- vignettes/CondMean_Inference.html.asis | 2 +- 3 files changed, 115 insertions(+), 116 deletions(-) diff --git a/vignettes/CondMean_Inference.Rmd b/vignettes/CondMean_Inference.Rmd index 5c83f711..54b18689 100644 --- a/vignettes/CondMean_Inference.Rmd +++ b/vignettes/CondMean_Inference.Rmd @@ -1,5 +1,5 @@ --- -title: "rbmi: Frequentist and information-anchored inference for reference-based conditional mean imputation" +title: "rbmi: Inference with Conditional Mean Imputation" author: Alessandro Noci, Marcel Wolbers, Craig Gower-Page output: bookdown::html_document2: @@ -14,7 +14,7 @@ linkcolor: blue pkgdown: as_is: true vignette: > - %\VignetteIndexEntry{rbmi: CondMean_Inference} + %\VignetteIndexEntry{rbmi: Inference with Conditional Mean Imputation} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- @@ -175,6 +175,16 @@ To simplify the implementation, we have written a function `get_delta_match_refB ```{r} +#' Get delta adjustment that matches reference-based imputation +#' +#' @param draws: A `draws` object created by `draws()`. +#' @param data_ice: `data.frame` containing the information about the intercurrent events and the imputation strategies. Must represent the desired imputation strategy and not the MAR-variant. +#' @param references: A named vector. Identifies the references to be used for reference-based imputation methods. +#' +#' @return +#' The function returns a list containing the imputation objects under both reference-based and MAR imputation, plus a `data.frame` which contains the delta-adjustment. +#' +#' @seealso `draws()`, `impute()`. get_delta_match_refBased <- function(draws, data_ice, references) { # Impute according to `data_ice` @@ -187,26 +197,18 @@ get_delta_match_refBased <- function(draws, data_ice, references) { vars <- imputeObj$data$vars # Access imputed dataset (index=1 for method_condmean(type = "jackknife")) - cmi <- extract_imputed_dfs(imputeObj, index = 1, idmap = TRUE)[[1]] %>% - dplyr::select(vars$subjid, vars$visit, vars$outcome) %>% - rename(y_imp = !!as.symbol(vars$outcome)) + cmi <- extract_imputed_dfs(imputeObj, index = 1, idmap = TRUE)[[1]] + idmap <- attributes(cmi)$idmap + cmi <- cmi[, c(vars$subjid, vars$visit, vars$outcome)] + colnames(cmi)[colnames(cmi) == vars$outcome] <- "y_imp" # Map back original patients id since rbmi re-code ids to ensure id uniqueness - cmi <- cmi %>% mutate( - !!vars$subjid := factor( - recode(!!as.symbol(vars$subjid), !!! attributes(cmi)$idmap), - levels = attributes(cmi)$idmap - ) - ) + + cmi[[vars$subjid]] <- idmap[match(cmi[[vars$subjid]], names(idmap))] # Derive conditional mean imputations under MAR - dat_ice_MAR <- data_ice %>% mutate( - !!vars$strategy := ifelse( - !!as.symbol(vars$strategy) %in% c("CIR","CR","JR"), - "MAR", - strategy - ) - ) + dat_ice_MAR <- data_ice + dat_ice_MAR[[vars$strategy]] <- "MAR" # Impute under MAR # Note that in this specific context, it is desirable that an update @@ -223,24 +225,21 @@ get_delta_match_refBased <- function(draws, data_ice, references) { update_strategy = dat_ice_MAR ) ) - # Take imputed outcome - cmi_MAR <- extract_imputed_dfs(imputeObj_MAR, index = 1, idmap = TRUE)[[1]] %>% - dplyr::select(vars$subjid, vars$visit, vars$outcome) %>% - rename(y_MAR = !!as.symbol(vars$outcome)) + + # Access imputed dataset (index=1 for method_condmean(type = "jackknife")) + cmi_MAR <- extract_imputed_dfs(imputeObj_MAR, index = 1, idmap = TRUE)[[1]] + idmap <- attributes(cmi_MAR)$idmap + cmi_MAR <- cmi_MAR[, c(vars$subjid, vars$visit, vars$outcome)] + colnames(cmi_MAR)[colnames(cmi_MAR) == vars$outcome] <- "y_MAR" # Map back original patients id since rbmi re-code ids to ensure id uniqueness - cmi_MAR <- cmi_MAR %>% mutate( - !!vars$subjid := factor( - recode(!!as.symbol(vars$subjid), !!! attributes(cmi)$idmap), - levels = attributes(cmi)$idmap - ) - ) + cmi_MAR[[vars$subjid]] <- idmap[match(cmi_MAR[[vars$subjid]], names(idmap))] # Derive delta adjustment "aligned with ref-based imputation", # i.e. difference between ref-based imputation and MAR imputation - delta_adjust <- full_join(cmi, cmi_MAR, by = c(vars$subjid, vars$visit)) %>% - mutate(delta = y_imp - y_MAR) - + delta_adjust <- merge(cmi, cmi_MAR, by = c(vars$subjid, vars$visit), all = TRUE) + delta_adjust$delta <- delta_adjust$y_imp - delta_adjust$y_MAR + ret_obj <- list( imputeObj = imputeObj, imputeObj_MAR = imputeObj_MAR, diff --git a/vignettes/CondMean_Inference.html b/vignettes/CondMean_Inference.html index 64100dfa..f187f32c 100644 --- a/vignettes/CondMean_Inference.html +++ b/vignettes/CondMean_Inference.html @@ -10,10 +10,10 @@ - + -rbmi: Frequentist and information-anchored inference for reference-based conditional mean imputation +rbmi: Inference with Conditional Mean Imputation