`emmeans` support fails for MMRMs fitted to data with only one (post baseline) visit
bug
It is really great that the ANCOVA edge case (MMRM with only 1 post baseline visit) is supported by `mmrm` because
it simplifies code significantly when I don’t have to condition on
the number of post baseline visits. Ufortunately, it appears that
`emmeans` support fails for this case.
Please see the following example for details and let me know if you require additional information.
Get packages:
``` r
library(data.table)
library(mmrm)
library(emmeans)
```
Get data for first visit only:
``` r
dt <- copy(fev_data)
setDT(dt)
mydata <- dt[AVISIT == "VIS1"] |> droplevels()
mydata[, .N, AVISIT]
#> AVISIT N
#> <fctr> <int>
#> 1: VIS1 200
```
Fit ANCOVA model using `mmrm` (MMRM edge case):
``` r
fm2 <- mmrm(FEV1 ~ FEV1_BL + ARMCD + us(AVISIT | USUBJID),
data = mydata)
```
We can fit the model using `lm` to confirm that this is simply an
ANCOVA:
``` r
fm2_lm <- lm(FEV1 ~ FEV1_BL + ARMCD, data = mydata)
all.equal(coef(fm2_lm), coef(fm2))
#> [1] TRUE
all.equal(logLik(fm2), logLik(fm2_lm, REML=TRUE), check.attributes = FALSE)
#> [1] TRUE
```
Unfortunately the `emmeans` support does not extend to the ANCOVA edge
case:
``` r
em <- try(emmeans(fm2, ~ ARMCD)) # Error related to factor levels
#> Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
#> contrasts can be applied only to factors with 2 or more levels
```
This is what would have hoped for:
``` r
emmeans(fm2_lm, ~ ARMCD)
#> ARMCD emmean SE df lower.CL upper.CL
#> PBO 32.4 0.794 131 30.8 33.9
#> TRT 36.9 0.806 131 35.3 38.5
#>
#> Confidence level used: 0.95
```
Session-info:
``` r
sessionInfo()
#> R version 4.5.2 (2025-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 11 x64 (build 22631)
#>
#> Matrix products: default
#> LAPACK version 3.12.1
#>
#> locale:
#> [1] LC_COLLATE=Danish_Denmark.utf8 LC_CTYPE=Danish_Denmark.utf8
#> [3] LC_MONETARY=Danish_Denmark.utf8 LC_NUMERIC=C
#> [5] LC_TIME=Danish_Denmark.utf8
#>
#> time zone: Europe/Copenhagen
#> tzcode source: internal
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] emmeans_2.0.2 mmrm_0.3.17 data.table_1.18.2.1
#>
#> loaded via a namespace (and not attached):
#> [1] Matrix_1.7-4 compiler_4.5.2 Rcpp_1.1.1 splines_4.5.2
#> [5] yaml_2.3.12 fastmap_1.2.0 lattice_0.22-7 coda_0.19-4.1
#> [9] TH.data_1.1-5 generics_0.1.4 knitr_1.51 rbibutils_2.4
#> [13] MASS_7.3-65 backports_1.5.0 checkmate_2.3.3 rprojroot_2.1.1
#> [17] TMB_1.9.19 rlang_1.1.7 multcomp_1.4-29 xfun_0.55
#> [21] otel_0.2.0 estimability_1.5.1 cli_3.6.5 Rdpack_2.6.4
#> [25] digest_0.6.39 grid_4.5.2 rstudioapi_0.17.1 mvtnorm_1.3-3
#> [29] xtable_1.8-4 sandwich_3.1-1 nlme_3.1-168 evaluate_1.0.5
#> [33] codetools_0.2-20 zoo_1.8-15 survival_3.8-3 rmarkdown_2.30
#> [37] tools_4.5.2 htmltools_0.5.9
```
关闭于 2026-06-18 3 条评论