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 anova type 3 #460

Merged
merged 19 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Bug Fixes

- Previously `emmeans` will return `NA` for spatial covariance structure. This is fixed now.
- Previously `car::Anova` will give incorrect result if the interaction term is included and the order of the covariate of interest is not the first categorical variable. This is fixed now.
clarkliming marked this conversation as resolved.
Show resolved Hide resolved
- Previously, `mmrm` will ignore contrasts defined for covariates in the input data set. This is fixed now.

# mmrm 0.3.12
Expand Down
11 changes: 10 additions & 1 deletion R/interop-car.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,17 @@ h_get_contrast <- function(object, effect, type = c("II", "III", "2", "3"), tol
additional_levels <- vapply(data[additional_vars], function(x) {
if (is.factor(x)) nlevels(x) else length(unique(x))
}, FUN.VALUE = 1L)
prior_vars <- additional_vars[which(match(additional_vars, row.names(fcts)) < match(effect, row.names(fcts)))]
clarkliming marked this conversation as resolved.
Show resolved Hide resolved
prior_lvls <- vapply(data[prior_vars], function(x) {
if (is.factor(x)) nlevels(x) else length(unique(x))
}, FUN.VALUE = 1L)
prior_lvls <- prod(prior_lvls - 1L)
t_levels <- prod(additional_levels)
l_mx[, cols] / t_levels
current_lvls <- length(cols)
current_row_idx <- rep(rep(seq_len(current_lvls), each = prior_lvls), times = length(current_col) / prior_lvls / current_lvls)
mt <- matrix(0, nrow = current_lvls, ncol = length(current_col))
mt[cbind(current_row_idx, seq_len(length(current_row_idx)))] <- 1 / t_levels
mt
}
)
l_mx[, current_col] <- sub_mat
Expand Down
44 changes: 44 additions & 0 deletions tests/testthat/test-car.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,50 @@ test_that("h_get_contrast works as expected", {
h_get_contrast(get_mmrm_trans(), "ARMCD:AVISIT", "3"),
matrix(rep(rep(c(0, 1), 3), c(6, 1, 9, 1, 9, 1)), nrow = 3, byrow = TRUE)
)
expect_identical(
fit <-
h_get_contrast(get_mmrm_trans(), "ARMCD:AVISIT", "3"),
matrix(rep(rep(c(0, 1), 3), c(6, 1, 9, 1, 9, 1)), nrow = 3, byrow = TRUE)
)
})

test_that("h_get_contrast works even if the interaction term order changes", {
mod1 <- mmrm(
formula = FEV1 ~ RACE + AVISIT + RACE * AVISIT + FEV1_BL + us(AVISIT|USUBJID),
data = fev_data
)
ctr <- expect_silent(h_get_contrast(mod1, "AVISIT", "3"))
colnames(ctr) <- names(coef(mod1))
expect_identical(
names(ctr[1, ctr[1, ] != 0]),
sprintf(c("AVISITVIS%s","RACEBlack or African American:AVISITVIS%s","RACEWhite:AVISITVIS%s"), "2")
)
expect_identical(
names(ctr[2, ctr[3, ] != 0]),
sprintf(c("AVISITVIS%s","RACEBlack or African American:AVISITVIS%s","RACEWhite:AVISITVIS%s"), "3")
)
expect_identical(
names(ctr[3, ctr[3, ] != 0]),
sprintf(c("AVISITVIS%s","RACEBlack or African American:AVISITVIS%s","RACEWhite:AVISITVIS%s"), "4")
)
mod2 <- mmrm(
formula = FEV1 ~ AVISIT + RACE + AVISIT * RACE + FEV1_BL + us(AVISIT|USUBJID),
data = fev_data
)
ctr <- expect_silent(h_get_contrast(mod2, "AVISIT", "3"))
colnames(ctr) <- names(coef(mod2))
expect_identical(
names(ctr[1, ctr[1, ] != 0]),
sprintf(c("AVISITVIS%s","AVISITVIS%s:RACEBlack or African American","AVISITVIS%s:RACEWhite"), "2")
)
expect_identical(
names(ctr[2, ctr[2, ] != 0]),
sprintf(c("AVISITVIS%s","AVISITVIS%s:RACEBlack or African American","AVISITVIS%s:RACEWhite"), "3")
)
expect_identical(
names(ctr[3, ctr[3, ] != 0]),
sprintf(c("AVISITVIS%s","AVISITVIS%s:RACEBlack or African American","AVISITVIS%s:RACEWhite"), "4")
)
})

# Anova ----
Expand Down