ITADN

`model.frame.mmrm_tmb()` applies `na.action` too early

#548ClosedNikKrieger 创建于 2025-11-19
bug
N
NikKriegercommented
Line 328 in `mmrm:::model.frame.mmrm_tmb()` applies the user-specified `na.action` too early, not allowing `model.frame.default()` to properly apply the `na.action`: https://github.com/openpharma/mmrm/blob/832ee0c698dca3952417697e12ea014dfab6c228/R/tmb-methods.R#L325-L331 This can cause errors if an environment variable is present: ``` r library(mmrm) env_FEV1_BL <- fev_data$FEV1_BL fit <- mmrm( FEV1 ~ env_FEV1_BL + SEX + ar1(as.ordered(VISITN) | USUBJID), fev_data ) model.frame(fit, data = fev_data) #> Error in model.frame.default(formula = lst_formula_and_data$formula, data = h_get_na_action(na.action)(lst_formula_and_data$data), : variable lengths differ (found for 'env_FEV1_BL') # This works fine because no environment variable is involved: fit_no_env_var <- mmrm( FEV1 ~ FEV1_BL + SEX + ar1(as.ordered(VISITN) | USUBJID), fev_data ) head(model.frame(fit_no_env_var, data = fev_data)) #> FEV1 FEV1_BL SEX USUBJID as.ordered(VISITN) #> 2 39.97105 25.27144 Female PT1 2 #> 4 20.48379 25.27144 Female PT1 4 #> 6 31.45522 45.02477 Male PT2 2 #> 7 36.87889 45.02477 Male PT2 3 #> 8 48.80809 45.02477 Male PT2 4 #> 10 35.98699 43.50070 Female PT3 2 ``` <sup>Created on 2025-11-19 with [reprex v2.1.1](https://reprex.tidyverse.org)</sup> The error occurs because `data`, which is preprocessed with `na.omit()` before being passed to `model.frame.default()`, has fewer observations than the environment variable `env_FEV1_BL`. `model.frame.default()` doesn't tolerate this. Unless there is a good reason to apply `na.action` to `data` before sending it on to `model.frame.default()`, line 328 should just be: ``` r data = lst_formula_and_data$data, ``` `model.frame.default()` has an `na.action` argument, so just let it do its thing. If this change is made, the only test that is violated is this one: https://github.com/openpharma/mmrm/blob/832ee0c698dca3952417697e12ea014dfab6c228/tests/testthat/test-tmb-methods.R#L695-L707 I don't totally understand the purpose of this test. All it seems to be doing is checking that `USUBJID` gets subjected to `na.action` in view of the response variable `FEV1` (which has missing values), even though the user is explicitly leaving `response_var` out of the `include` argument of `model.frame.mmrm_tmb()`. (Maybe there's a good reason for this behavior and this test sheds light on why `data` is preprocessed with `na.action`, but I'm not understanding.) ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.5.1 (2025-06-13) #> os Ubuntu 24.04.3 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate C.UTF-8 #> ctype C.UTF-8 #> tz America/New_York #> date 2025-11-19 #> pandoc 3.6.3 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/x86_64/ (via rmarkdown) #> quarto 1.7.32 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/quarto #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> backports 1.5.0 2024-05-23 [1] CRAN (R 4.5.1) #> cachem 1.1.0 2024-05-16 [1] CRAN (R 4.5.1) #> checkmate 2.3.3 2025-08-18 [1] CRAN (R 4.5.1) #> cli 3.6.5 2025-04-23 [1] CRAN (R 4.5.1) #> devtools 2.4.6 2025-10-03 [1] CRAN (R 4.5.1) #> digest 0.6.37 2024-08-19 [1] CRAN (R 4.5.1) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.5.1) #> evaluate 1.0.5 2025-08-27 [1] CRAN (R 4.5.1) #> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.5.1) #> fs 1.6.6 2025-04-12 [1] CRAN (R 4.5.1) #> generics 0.1.4 2025-05-09 [1] CRAN (R 4.5.1) #> glue 1.8.0 2024-09-30 [1] CRAN (R 4.5.1) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.5.1) #> knitr 1.50 2025-03-16 [1] CRAN (R 4.5.1) #> lattice 0.22-5 2023-10-24 [4] CRAN (R 4.3.3) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.5.1) #> magrittr 2.0.4 2025-09-12 [1] CRAN (R 4.5.1) #> Matrix 1.7-4 2025-08-28 [4] CRAN (R 4.5.1) #> memoise 2.0.1 2021-11-26 [1] CRAN (R 4.5.1) #> mmrm * 0.3.15.9001 2025-11-19 [1] Github (openpharma/mmrm@832ee0c) #> nlme 3.1-168 2025-03-31 [4] CRAN (R 4.4.3) #> pkgbuild 1.4.8 2025-05-26 [1] CRAN (R 4.5.1) #> pkgload 1.4.1 2025-09-23 [1] CRAN (R 4.5.1) #> purrr 1.1.0 2025-07-10 [1] CRAN (R 4.5.1) #> R6 2.6.1 2025-02-15 [1] CRAN (R 4.5.1) #> rbibutils 2.4 2025-11-07 [1] CRAN (R 4.5.1) #> Rcpp 1.1.0 2025-07-02 [1] CRAN (R 4.5.1) #> Rdpack 2.6.4 2025-04-09 [1] CRAN (R 4.5.1) #> remotes 2.5.0 2024-03-17 [1] CRAN (R 4.5.1) #> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.5.1) #> rlang 1.1.6.9000 2025-11-17 [1] local #> rmarkdown 2.30 2025-09-28 [1] CRAN (R 4.5.1) #> rstudioapi 0.17.1 2024-10-22 [1] CRAN (R 4.5.1) #> sessioninfo 1.2.3 2025-02-05 [1] CRAN (R 4.5.1) #> TMB 1.9.18 2025-10-13 [1] CRAN (R 4.5.1) #> usethis 3.2.1 2025-09-06 [1] CRAN (R 4.5.1) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.5.1) #> withr 3.0.2 2024-10-28 [1] CRAN (R 4.5.1) #> xfun 0.53 2025-08-19 [1] CRAN (R 4.5.1) #> yaml 2.3.10 2024-07-26 [1] CRAN (R 4.5.1) #> #> [1] /home/nik/R/x86_64-pc-linux-gnu-library/4.5 #> [2] /usr/local/lib/R/site-library #> [3] /usr/lib/R/site-library #> [4] /usr/lib/R/library #> * ── Packages attached to the search path. ```
关闭于 2025-11-24 0 条评论