Skip to content
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

Fix warnings being suppressed #432

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion R/longData.R
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ longDataConstructor <- R6::R6Class(

dat_ice <- sort_by(dat_ice, c(self$vars$subjid))

has_nonMAR_to_MAR <- FALSE

for (subject in dat_ice[[self$vars$subjid]]) {

dat_ice_pt <- dat_ice[dat_ice[[self$vars$subjid]] == subject, ]
Expand All @@ -408,7 +410,6 @@ longDataConstructor <- R6::R6Class(

new_strategy <- dat_ice_pt[[self$vars$strategy]]

has_nonMAR_to_MAR <- FALSE
if (!update) {
visit <- dat_ice_pt[[self$vars$visit]]
self$ice_visit_index[[subject]] <- which(self$visits == visit)
Expand Down
61 changes: 61 additions & 0 deletions tests/testthat/test-longData.R
Original file line number Diff line number Diff line change
Expand Up @@ -1063,3 +1063,64 @@ test_that("get_data() uses na.rm and nmar.rm correctly", {
)

})


test_that("Warnings/errors are thrown when strategies are incorrectly updated", {
vars <- set_vars(
outcome = "out",
group = "group",
strategy = "strat",
subjid = "pt",
visit = "vis",
covariates = c("age")
)

dat <- tibble(
pt = factor(c("A", "A", "A", "B", "B", "B", "C", "C", "C"), levels = c("A", "B", "C")),
vis = factor(c("V1", "V2", "V3", "V1", "V2", "V3", "V1", "V2", "V3"), levels = c("V1", "V2", "V3")),
out = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
group = factor(c("T", "T", "T", "C", "C", "C", "T", "T", "T"), levels = c("C", "T")),
age = rnorm(9)
)
dat_ice <- tibble(
pt = factor(c("A", "B", "C"), levels = c("A", "B", "C")),
vis = factor( c("V2", "V2", "V2"), levels = c("V1", "V2", "V3")),
strat = c("JR", "MAR", "JR")
)
longdata <- longDataConstructor$new(dat, vars)
longdata$set_strategies(dat_ice)

# Error if updating MAR -> Non-Mar
ld2 <- longdata$clone()
dat_ice_upd <- tibble(
pt = factor(c("A", "B", "C"), levels = c("A", "B", "C")),
strat = c("JR", "JR", "JR")
)
expect_error(
ld2$update_strategies(dat_ice_upd),
regexp = "Updating strategies from MAR to non-MAR is invalid"
)

# Warning if updating Non-MAR -> MAR
ld2 <- longdata$clone()
dat_ice_upd <- tibble(
pt = factor(c("A", "B", "C"), levels = c("A", "B", "C")),
strat = c("JR", "MAR", "MAR")
)
expect_warning(
ld2$update_strategies(dat_ice_upd),
regexp = "Updating strategies from non-MAR to MAR.*You are advised to re-run `draws\\(\\)`"
)

# Same as above but catches niche bug where the warning would be supressed
# if a correct imputation came after an incorrect
ld2 <- longdata$clone()
dat_ice_upd <- tibble(
pt = factor(c("A", "B", "C"), levels = c("A", "B", "C")),
strat = c("MAR", "MAR", "JR")
)
expect_warning(
ld2$update_strategies(dat_ice_upd),
regexp = "Updating strategies from non-MAR to MAR.*You are advised to re-run `draws\\(\\)`"
)
})
Loading