Skip to content

Commit f49f0f7

Browse files
committed
fix interaction
1 parent 4ca2d6a commit f49f0f7

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

R/trial_sequence.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,12 @@ setMethod(
514514
period = as_formula(trial_period_terms, add = collection),
515515
stabilised = as_formula(get_stabilised_weights_terms(object), add = collection)
516516
)
517-
adjustment <- unique(c(all.vars(formula_list$adjustment), all.vars(formula_list$stabilised)))
517+
treatment <- all.vars(formula_list$treatment)
518+
adjustment <- setdiff(
519+
unique(c(all.vars(formula_list$adjustment), all.vars(formula_list$stabilised))),
520+
treatment
521+
)
522+
518523
assert_names(
519524
adjustment,
520525
subset.of = colnames(object@data@data),
@@ -619,6 +624,7 @@ get_stabilised_weights_terms <- function(object) {
619624
stabilised_terms
620625
}
621626

627+
# Update outcome model (formulas generally and variables in stabilised models)
622628
update_outcome_formula <- function(object) {
623629
assert_class(object, "trial_sequence")
624630

@@ -638,7 +644,7 @@ update_outcome_formula <- function(object) {
638644
object@outcome_model@formula <- outcome_formula
639645

640646
object@outcome_model@adjustment_vars <- unique(
641-
c(all.vars(formula_list$adjustment), all.vars(formula_list$stabilised))
647+
c(object@outcome_model@adjustment_vars, all.vars(formula_list$stabilised))
642648
)
643649

644650
object

tests/testthat/test-trial_sequence.R

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,29 @@ test_that("stabilised weight terms are included in outcome model", {
500500
)
501501
})
502502

503-
# Expand
503+
504+
test_that("interaction terms work as expected", {
505+
result <- trial_sequence("PP") |>
506+
set_data(data_censored) |>
507+
set_outcome_model(adjustment_terms = ~ assigned_treatment * x2)
508+
509+
expect_equal(
510+
result@outcome_model@formula,
511+
outcome ~ assigned_treatment + x2 + followup_time + I(followup_time^2) +
512+
trial_period + I(trial_period^2) + assigned_treatment:x2,
513+
ignore_formula_env = TRUE
514+
)
515+
expect_equal(result@outcome_model@treatment_var, "assigned_treatment")
516+
expect_equal(result@outcome_model@adjustment_vars, "x2") # shouldn't include treatment
517+
expect_equal(
518+
result@outcome_model@adjustment_terms, # can include treatment
519+
~ assigned_treatment * x2,
520+
ignore_formula_env = TRUE
521+
)
522+
})
523+
524+
# Expand ---
525+
504526
test_that("weights are 1 when not calculated by calculate_weights", {
505527
trial_ex <- TrialEmulation::trial_example
506528

0 commit comments

Comments
 (0)