ITADN

Estimated means using the `emmeans` with the `counterfactuals` argument.

#560Openrunehaubo 创建于 2026-03-23
bugpriority
R
runehaubocommented
Dear MMRM authors. Thank you for making the `mmrm` package available to us. This is a much needed addition to the R landscape for the analysis of RCTs. I am fitting MMRMs and extracting estimated means using the `emmeans` package but run into errors that I cannot find a way around. Any help would be much appreciated. Rather than the conventional estimated means that pertain to the subjects actually randomized to each treatment arm, I want to compute the causally interpretable estimated means that pertain to all subjects had they all been randomized to each treatment arm. I usually obtain these using the `counterfactuals` argument to `emmeans` or `ref_grid` but either this is not supported by the `mmrm` package or I am doing something wrong. Any assistance would be highly appreciated! Please see the following example for details. Get packages: ``` r library(data.table) library(mmrm) library(emmeans) ``` Get data and fit MMRM: ``` r dt <- copy(fev_data) setDT(dt) dt #> USUBJID AVISIT ARMCD RACE SEX FEV1_BL FEV1 #> <fctr> <fctr> <fctr> <fctr> <fctr> <num> <num> #> 1: PT1 VIS1 TRT Black or African American Female 25.27144 NA #> 2: PT1 VIS2 TRT Black or African American Female 25.27144 39.97105 #> 3: PT1 VIS3 TRT Black or African American Female 25.27144 NA #> 4: PT1 VIS4 TRT Black or African American Female 25.27144 20.48379 #> 5: PT2 VIS1 PBO Asian Male 45.02477 NA #> --- #> 796: PT199 VIS4 TRT White Male 41.05401 NA #> 797: PT200 VIS1 PBO Black or African American Male 51.19101 35.70341 #> 798: PT200 VIS2 PBO Black or African American Male 51.19101 41.64454 #> 799: PT200 VIS3 PBO Black or African American Male 51.19101 NA #> 800: PT200 VIS4 PBO Black or African American Male 51.19101 54.25081 #> WEIGHT VISITN VISITN2 #> <num> <int> <num> #> 1: 0.6767231 1 -0.62645381 #> 2: 0.8006186 2 0.18364332 #> 3: 0.7087859 3 -0.83562861 #> 4: 0.8088997 4 1.59528080 #> 5: 0.4651848 1 0.32950777 #> --- #> 796: 0.7897474 4 -1.02939165 #> 797: 0.4894286 1 -0.01092569 #> 798: 0.4903211 2 -1.22499116 #> 799: 0.1676835 3 -2.59611139 #> 800: 0.4189191 4 1.16912259 fm <- mmrm(FEV1 ~ FEV1_BL + ARMCD * AVISIT + us(AVISIT | USUBJID), data = dt) ``` Get the traditional estimated means by treatment group and visit: ``` r em <- emmeans(fm, ~ ARMCD | AVISIT) as.data.frame(em) |> as.data.table() #> ARMCD AVISIT emmean SE df lower.CL upper.CL #> <fctr> <fctr> <num> <num> <num> <num> <num> #> 1: PBO VIS1 32.62622 0.7659343 143.6702 31.11226 34.14017 #> 2: TRT VIS1 37.29120 0.7800802 143.3083 35.74925 38.83315 #> 3: PBO VIS2 37.48339 0.6036548 148.5230 36.29053 38.67625 #> 4: TRT VIS2 41.85162 0.5989397 146.4279 40.66794 43.03530 #> 5: PBO VIS3 42.95839 0.5129039 131.0582 41.94374 43.97303 #> 6: TRT VIS3 46.52501 0.5662378 131.9368 45.40493 47.64508 #> 7: PBO VIS4 47.99745 1.2054966 134.7808 45.61332 50.38159 #> 8: TRT VIS4 53.00951 1.2084669 134.0161 50.61938 55.39965 ``` But we want the estimated means adjusted to the full baseline distribution (typically the FAS): ``` r em <- try(emmeans(fm, ~ ARMCD | AVISIT, counterfactuals = "ARMCD")) #> Error in emmeans(fm, ~ARMCD | AVISIT, counterfactuals = "ARMCD") : #> No variable named AVISIT in the reference grid ``` Surprisingly this fails with an error. The error message indicates that something about `AVISIT` is missing so we try this instead: ``` r em <- emmeans(fm, ~ ARMCD | AVISIT, counterfactuals = c("ARMCD", "AVISIT")) pr_em <- try(print(em)) #> Error in mmrm::df_md(dfargs$object, contrast = k) : #> Assertion on 'contrast' failed: Must have exactly 9 cols, but has 64 cols. ``` Now the `emmeans` object is constructed but cannot be printed. Taking a step deeper and building the reference grid first does not help: ``` r dt_no_response <- dt[, .(FEV1_BL, ARMCD, AVISIT, USUBJID)] rg <- ref_grid(fm, data=dt_no_response, counterfactuals = "ARMCD") rg@grid #> actual_ARMCD ARMCD .wgt. #> 1 PBO PBO 420 #> 2 TRT PBO 380 #> 3 PBO TRT 420 #> 4 TRT TRT 380 em <- try( emmeans(rg, ~ ARMCD | AVISIT) ) #> Error in emmeans(rg, ~ARMCD | AVISIT) : #> No variable named AVISIT in the reference grid # Modifying the counterfactuals argument: rg <- ref_grid(fm, data=dt_no_response, counterfactuals = c("ARMCD", "AVISIT")) em <- emmeans(rg, ~ ARMCD | AVISIT) pr_em <- try(print(em)) #> Error in mmrm::df_md(dfargs$object, contrast = k) : #> Assertion on 'contrast' failed: Must have exactly 9 cols, but has 64 cols. # Try using the original dataset: rg <- try(ref_grid(fm, data=dt, counterfactuals = "ARMCD")) #> Error in rep(1, nrow(data)) : ugyldigt 'times'-argument ``` 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 ```
2 条评论