Skip to content

Commit 041b801

Browse files
550: Clarify error message when factor is required (#553)
* Clarify error message when factor is required * use mathjax for math rendering in `pkgdown` to fix rendering issue * remove duplicate assertion, update test
1 parent f67421a commit 041b801

4 files changed

Lines changed: 20 additions & 10 deletions

File tree

R/tmb.R

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,13 @@ h_mmrm_tmb_data <- function(
288288
n_visits <- max(subject_n_visits)
289289
} else {
290290
assert(identical(ncol(coordinates), 1L))
291-
assert_factor(coordinates[[1L]])
291+
if (!test_factor(coordinates[[1L]])) {
292+
stop(
293+
"Time point variable '",
294+
formula_parts$visit_var,
295+
"' must be a factor."
296+
)
297+
}
292298
coordinates_matrix <- as.matrix(as.integer(coordinates[[1L]]) - 1, ncol = 1)
293299
n_visits <- nlevels(coordinates[[1L]])
294300
assert_true(all(subject_n_visits <= n_visits))
@@ -618,14 +624,6 @@ fit_mmrm <- function(
618624
covariance <- h_reconcile_cov_struct(formula, covariance)
619625
formula_parts <- h_mmrm_tmb_formula_parts(formula, covariance)
620626

621-
if (
622-
!formula_parts$is_spatial && !is.factor(data[[formula_parts$visit_var]])
623-
) {
624-
stop(
625-
"Time variable must be a factor for non-spatial covariance structures"
626-
)
627-
}
628-
629627
assert_class(control, "mmrm_control")
630628
assert_list(control$optimizers, min.len = 1)
631629
assert_numeric(weights, any.missing = FALSE)

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ template:
55
bootstrap: 5
66
params:
77
ganalytics: UA-125641273-1
8+
math-rendering: "mathjax"
89

910
navbar:
1011
structure:

tests/testthat/test-fit.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,16 @@ test_that("divergent optimizer will not be marked as success", {
546546
)
547547
})
548548

549+
test_that("mmrm provides readable error message when visit variable is not a factor", {
550+
fev <- fev_data
551+
fev$AVISIT <- as.character(fev$AVISIT)
552+
expect_error(
553+
mmrm(FEV1 ~ AVISIT + us(AVISIT | USUBJID), data = fev),
554+
"Time point variable 'AVISIT' must be a factor.",
555+
fixed = TRUE
556+
)
557+
})
558+
549559
test_that("mmrm will use the provided contrast", {
550560
fev2 <- fev_data
551561
contrasts(fev2$ARMCD) <- contr.poly(n = 2)

tests/testthat/test-tmb.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2631,7 +2631,8 @@ test_that("fit_mmrm throws informative error for non-spatial cov with non-factor
26312631
data = tmp_data,
26322632
weights = rep(1, nrow(tmp_data))
26332633
),
2634-
"Time variable must be a factor for non-spatial covariance structures"
2634+
"Time point variable 'AVISIT' must be a factor",
2635+
fixed = TRUE
26352636
)
26362637
})
26372638

0 commit comments

Comments
 (0)