Skip to content

Commit

Permalink
write function without dplyr functions and change vignette title
Browse files Browse the repository at this point in the history
  • Loading branch information
nociale committed Jan 24, 2024
1 parent 6903b52 commit 49df8fe
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 116 deletions.
61 changes: 30 additions & 31 deletions vignettes/CondMean_Inference.Rmd
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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}
---
Expand Down Expand Up @@ -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`
Expand All @@ -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
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 49df8fe

Please sign in to comment.