Skip to content

Commit 452ce6b

Browse files
authored
make h_get_minimal_fit_data() extract names from calls (#537)
1 parent 8f4375b commit 452ce6b

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

R/utils.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -769,14 +769,19 @@ h_fits_common_data <- function(fits) {
769769
#' @keywords internal
770770
h_get_minimal_fit_data <- function(fit) {
771771
assert_class(fit, "mmrm")
772-
predictors <-
773-
fit[["formula_parts"]][
774-
c("visit_var", "subject_var", "group_var", "model_var")
775-
]
776-
predictors <- unique(unlist(predictors, use.names = FALSE))
772+
covariance_vars <-
773+
fit[["formula_parts"]][c("visit_var", "subject_var", "group_var")]
774+
covariance_vars <- Filter(length, covariance_vars)
775+
covariance_vars <- lapply(covariance_vars, str2lang)
776+
covariance_vars <- lapply(covariance_vars, all.vars)
777+
covariance_vars <- unlist(covariance_vars, use.names = FALSE)
778+
predictors <- c(covariance_vars, fit[["formula_parts"]][["model_var"]])
779+
predictors <- unique(predictors)
777780
terms_attr <- attributes(terms(fit))
778-
response <- as.character(terms_attr$variables[[terms_attr$response + 1]])
779-
fit[["data"]][c(response, predictors)]
781+
response <- all.vars(terms_attr$variables[[terms_attr$response + 1]])
782+
cols <- c(response, predictors)
783+
cols <- intersect(cols, colnames(fit[["data"]]))
784+
fit[["data"]][cols]
780785
}
781786

782787

tests/testthat/helper-examples.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ get_mmrm_transformed <- function() {
2222
}
2323

2424
.mmrm_trans <- mmrm(
25-
FEV1 ~ log(FEV1_BL) + ARMCD * AVISIT + ar1(AVISIT | USUBJID),
25+
identity(FEV1) ~
26+
log(FEV1_BL) + ARMCD * AVISIT + ar1(as.ordered(AVISIT) | USUBJID),
2627
data = fev_data
2728
)
2829

tests/testthat/test-mmrm-methods.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ test_that("h_get_minimal_fit_data() grabs only colums used in model fitting", {
253253
h_get_minimal_fit_data(get_mmrm()),
254254
fev_data[c("FEV1", "AVISIT", "USUBJID", "RACE", "SEX", "ARMCD")]
255255
)
256+
257+
# Account for model terms that are calls instead of just symbols (e.g., log(x)
258+
# as opposed to just x)
259+
expect_equal(
260+
h_get_minimal_fit_data(get_mmrm_trans()),
261+
fev_data[c("FEV1", "AVISIT", "USUBJID", "FEV1_BL", "ARMCD")]
262+
)
256263
})
257264

258265
test_that("h_check_covar_nesting() ensures models have nested covariates", {

0 commit comments

Comments
 (0)