openpharma / mmrm

Mixed Models for Repeated Measures (MMRM) in R.
https://openpharma.github.io/mmrm/
Other
101 stars 17 forks source link

Type III tests fail for character covariates #426

Closed florianstijven closed 4 months ago

florianstijven commented 4 months ago

Summary

mmrm() automatically converts character variables to factors when they are added to the formula. This seems to work fine. However, when calling the type III tests using car::Anova(), an error is raised when a character variable was present in the formula. This error is not raised when the character variable is converted to a factor before running the mixed model.

I am not sure whether this is a bug in mmrm or car. It appears that, while mmrm() automatically converts character variables to factors, this conversion is not taken into account by car::Anova().

In the minimally reproducible example below, I first generate data where the trt variable is a character variable. mmrm() works fine, but car::Anova() raises an error for the type III tests. Next, I change trt to a factor variable and repeat everything. No errors are raised now.

library(mmrm)
# Generate data set.
set.seed(1)
data_example = data.frame(
  y = rnorm(100),
  week = factor(rep(c("1", "2", "3", "4"), 25)),
  # Treatment is a character variable.
  trt = c(rep("active", 50), rep("placebo", 50)),
  id = factor(rep(1:25, each = 4))
)
mmrm_fitted = mmrm(
  formula = y ~ week*trt + us(week | id),
  data = data_example
)
# Type III tests return error.
car::Anova(mmrm_fitted, type = "III")
# Change trt variable from character to factor and repeat same analysis.
data_example$trt = factor(data_example$trt)
mmrm_fitted = mmrm(
  formula = y ~ week*trt + us(week | id),
  data = data_example
)
summary(mmrm_fitted)
car::Anova(mmrm_fitted, type = "II")
# Type III now works.
car::Anova(mmrm_fitted, type = "III")

sessionInfo()

R session info


R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=Dutch_Belgium.utf8  LC_CTYPE=Dutch_Belgium.utf8    LC_MONETARY=Dutch_Belgium.utf8
[4] LC_NUMERIC=C                   LC_TIME=Dutch_Belgium.utf8    

time zone: Europe/Brussels
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] mmrm_0.3.10

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5         nlme_3.1-162        cli_3.6.2           rlang_1.1.3         stringi_1.8.3      
 [6] car_3.1-2           renv_1.0.2          generics_0.1.3      glue_1.7.0          backports_1.4.1    
[11] TMB_1.9.10          fansi_1.0.6         grid_4.3.1          abind_1.4-5         carData_3.0-5      
[16] tibble_3.2.1        lifecycle_1.0.4     BiocManager_1.30.22 stringr_1.5.1       compiler_4.3.1     
[21] Rcpp_1.0.12         pkgconfig_2.0.3     rstudioapi_0.15.0   lattice_0.21-8      utf8_1.2.4         
[26] pillar_1.9.0        Rdpack_2.6          parallel_4.3.1      rbibutils_2.2.16    magrittr_2.0.3     
[31] checkmate_2.3.1     Matrix_1.6-1.1      tools_4.3.1