Skip to content

Commit

Permalink
fix anova error (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkliming authored Feb 19, 2024
1 parent 5f16cdc commit ac3b6d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Bug Fixes

- Previously if a secondary optimizer fails `mmrm` will fail. This is fixed now.
- Previously character covariate variable will make `Anova` fail. This is fixed now.

# mmrm 0.3.9

Expand Down
6 changes: 4 additions & 2 deletions R/interop-car.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ h_get_contrast <- function(object, effect, type = c("II", "III", "2", "3"), tol
assert_subset(effect, colnames(fcts))
idx <- which(effect == colnames(fcts))
cols <- which(asg == idx)
data <- component(object, "full_frame")
data <- model.frame(object)
var_numeric <- vapply(data, is.numeric, FUN.VALUE = TRUE)
coef_rows <- length(cols)
l_mx <- matrix(0, nrow = coef_rows, ncol = length(asg))
Expand All @@ -64,7 +64,9 @@ h_get_contrast <- function(object, effect, type = c("II", "III", "2", "3"), tol
},
"3" = ,
"III" = {
additional_levels <- vapply(data[additional_vars], function(x) nlevels(x), FUN.VALUE = 1L)
additional_levels <- vapply(data[additional_vars], function(x) {
if (is.factor(x)) nlevels(x) else length(unique(x))
}, FUN.VALUE = 1L)
t_levels <- prod(additional_levels)
l_mx[, cols] / t_levels
}
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-car.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ test_that("Anova works as expected", {
)
})

## Anova with character covariates

test_that("Anova works if covariate are character", {
skip_if_not_installed("car")
fev2 <- fev_data
fev2$ARMCD <- as.character(fev2$ARMCD)
fit <- mmrm(FEV1 ~ ARMCD * AVISIT + ar1(AVISIT | USUBJID), data = fev2)
fit2 <- mmrm(FEV1 ~ ARMCD * AVISIT + ar1(AVISIT | USUBJID), data = fev_data)
expect_identical(
Anova(fit, "III"),
Anova(fit2, "III")
)
expect_identical(
Anova(fit, "II"),
Anova(fit2, "II")
)
})

# Anova Integration Test ----

test_that("Anova give similar results to SAS", {
Expand Down

0 comments on commit ac3b6d8

Please sign in to comment.