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

add_cif assumes causes with numeric labels? #233

Open
fabian-s opened this issue Jul 12, 2023 · 0 comments
Open

add_cif assumes causes with numeric labels? #233

fabian-s opened this issue Jul 12, 2023 · 0 comments

Comments

@fabian-s
Copy link
Collaborator

In the reprex below, everything works fine for cause= 1/2; but not cause= Death/Discharge:

library(pammtools)
#> 
#> Attaching package: 'pammtools'
#> The following object is masked from 'package:stats':
#> 
#>     filter
data(sir.adm, package = "mvna")
ped <- as_ped(sir.adm, Surv(time, status)~ pneu, combine = TRUE) |>
  mutate(cause = factor(cause, labels = c("Death", "Discharge"))) #!!
pam <- pamm(ped_status ~ s(tend, by = cause) + cause*pneu, data = ped)
summary(pam)
#> 
#> Family: poisson 
#> Link function: log 
#> 
#> Formula:
#> ped_status ~ s(tend, by = cause) + cause * pneu
#> 
#> Parametric coefficients:
#>                     Estimate Std. Error z value Pr(>|z|)    
#> (Intercept)         -2.60477    0.04316 -60.358  < 2e-16 ***
#> causeDischarge      -2.35417    0.14266 -16.502  < 2e-16 ***
#> pneu                -1.07039    0.13009  -8.228  < 2e-16 ***
#> causeDischarge:pneu  0.93058    0.29722   3.131  0.00174 ** 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Approximate significance of smooth terms:
#>                         edf Ref.df Chi.sq  p-value    
#> s(tend):causeDeath     8.52  8.906 31.101 0.000671 ***
#> s(tend):causeDischarge 4.08  4.990  9.453 0.095237 .  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> R-sq.(adj) =  0.0329   Deviance explained = 14.3%
#> UBRE = -0.78348  Scale est. = 1         n = 19338

ndf <- ped |> make_newdata(tend  = unique(tend), pneu  = unique(pneu), cause = unique(cause))
ndf <- ndf |>
  group_by(cause, pneu) |> # important!
  add_cif(pam)
#> Warning in predict.gam(object, .df, type = "lpmatrix"): factor levels 1 not in
#> original fit
#> Error in `map()`:
#> ℹ In index: 1.
#> ℹ With name: 1.
#> Caused by error in `map()`:
#> ℹ In index: 1.
#> Caused by error in `X[, pstart[i] - 1 + 1:object$nsdf[i]] <- Xp`:
#> ! replacement has length zero
#> Backtrace:
#>      ▆
#>   1. ├─pammtools::add_cif(group_by(ndf, cause, pneu), pam)
#>   2. ├─pammtools:::add_cif.default(group_by(ndf, cause, pneu), pam)
#>   3. │ └─purrr::map_dfr(...)
#>   4. │   └─purrr::map(.x, .f, ...)
#>   5. │     └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>   6. │       ├─purrr:::with_indexed_errors(...)
#>   7. │       │ └─base::withCallingHandlers(...)
#>   8. │       ├─purrr:::call_with_cleanup(...)
#>   9. │       └─pammtools (local) .f(.x[[i]], ...)
#>  10. │         ├─pammtools:::get_cif(...)
#>  11. │         └─pammtools:::get_cif.default(...)
#>  12. │           └─purrr::map(...)
#>  13. │             └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>  14. │               ├─purrr:::with_indexed_errors(...)
#>  15. │               │ └─base::withCallingHandlers(...)
#>  16. │               ├─purrr:::call_with_cleanup(...)
#>  17. │               └─pammtools (local) .f(.x[[i]], ...)
#>  18. │                 ├─stats::predict(object, .df, type = "lpmatrix")
#>  19. │                 └─mgcv::predict.gam(object, .df, type = "lpmatrix")
#>  20. └─base::.handleSimpleError(...)
#>  21.   └─purrr (local) h(simpleError(msg, call))
#>  22.     └─cli::cli_abort(...)
#>  23.       └─rlang::abort(...)

Created on 2023-07-12 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.2 Patched (2022-11-10 r83330)
#>  os       Linux Mint 20
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en_US
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Berlin
#>  date     2023-07-12
#>  pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date (UTC) lib source
#>  backports      1.4.1      2021-12-13 [1] CRAN (R 4.2.2)
#>  checkmate      2.2.0      2023-04-27 [1] CRAN (R 4.2.2)
#>  cli            3.6.1      2023-03-23 [1] CRAN (R 4.2.2)
#>  codetools      0.2-18     2020-11-04 [4] CRAN (R 4.0.3)
#>  colorspace     2.1-0      2023-01-23 [1] CRAN (R 4.2.2)
#>  data.table     1.14.8     2023-02-17 [1] CRAN (R 4.2.2)
#>  digest         0.6.33     2023-07-07 [1] CRAN (R 4.2.2)
#>  dplyr          1.1.2      2023-04-20 [1] CRAN (R 4.2.2)
#>  evaluate       0.21       2023-05-05 [1] CRAN (R 4.2.2)
#>  fansi          1.0.4      2023-01-22 [1] CRAN (R 4.2.2)
#>  fastmap        1.1.1      2023-02-24 [1] CRAN (R 4.2.2)
#>  foreach        1.5.2      2022-02-02 [1] CRAN (R 4.2.2)
#>  Formula        1.2-5      2023-02-24 [1] CRAN (R 4.2.2)
#>  fs             1.6.2      2023-04-25 [1] CRAN (R 4.2.2)
#>  future         1.33.0     2023-07-01 [1] CRAN (R 4.2.2)
#>  future.apply   1.11.0     2023-05-21 [1] CRAN (R 4.2.2)
#>  generics       0.1.3      2022-07-05 [1] CRAN (R 4.2.2)
#>  ggplot2        3.4.2      2023-04-03 [1] CRAN (R 4.2.2)
#>  globals        0.16.2     2022-11-21 [1] CRAN (R 4.2.2)
#>  glue           1.6.2      2022-02-24 [1] CRAN (R 4.2.2)
#>  gtable         0.3.3      2023-03-21 [1] CRAN (R 4.2.2)
#>  htmltools      0.5.5      2023-03-23 [1] CRAN (R 4.2.2)
#>  iterators      1.0.14     2022-02-05 [1] CRAN (R 4.2.2)
#>  knitr          1.43       2023-05-25 [1] CRAN (R 4.2.2)
#>  lattice        0.20-44    2021-05-02 [4] CRAN (R 4.1.0)
#>  lava           1.7.2.1    2023-02-27 [1] CRAN (R 4.2.2)
#>  lazyeval       0.2.2      2019-03-15 [1] CRAN (R 4.2.2)
#>  lifecycle      1.0.3      2022-10-07 [1] CRAN (R 4.2.2)
#>  listenv        0.9.0      2022-12-16 [1] CRAN (R 4.2.2)
#>  magrittr       2.0.3      2022-03-30 [1] CRAN (R 4.2.2)
#>  Matrix         1.6-0      2023-07-08 [1] CRAN (R 4.2.2)
#>  mgcv           1.8-42     2023-03-02 [1] CRAN (R 4.2.2)
#>  munsell        0.5.0      2018-06-12 [1] CRAN (R 4.2.2)
#>  mvtnorm        1.2-2      2023-06-08 [1] CRAN (R 4.2.2)
#>  nlme           3.1-152    2021-02-04 [4] CRAN (R 4.0.3)
#>  numDeriv       2016.8-1.1 2019-06-06 [1] CRAN (R 4.2.2)
#>  pammtools    * 0.5.92     2023-07-12 [1] Github (adibender/pammtools@94e7ea1)
#>  parallelly     1.36.0     2023-05-26 [1] CRAN (R 4.2.2)
#>  pec            2023.04.12 2023-04-11 [1] CRAN (R 4.2.2)
#>  pillar         1.9.0      2023-03-22 [1] CRAN (R 4.2.2)
#>  pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.2.2)
#>  prodlim        2023.03.31 2023-04-02 [1] CRAN (R 4.2.2)
#>  purrr          1.0.1      2023-01-10 [1] CRAN (R 4.2.2)
#>  R.cache        0.16.0     2022-07-21 [1] CRAN (R 4.2.2)
#>  R.methodsS3    1.8.2      2022-06-13 [1] CRAN (R 4.2.2)
#>  R.oo           1.25.0     2022-06-12 [1] CRAN (R 4.2.2)
#>  R.utils        2.12.2     2022-11-11 [1] CRAN (R 4.2.2)
#>  R6             2.5.1      2021-08-19 [1] CRAN (R 4.2.2)
#>  Rcpp           1.0.11     2023-07-06 [1] CRAN (R 4.2.2)
#>  reprex         2.0.2      2022-08-17 [1] CRAN (R 4.2.2)
#>  rlang          1.1.1      2023-04-28 [1] CRAN (R 4.2.2)
#>  rmarkdown      2.23       2023-07-01 [1] CRAN (R 4.2.2)
#>  rstudioapi     0.15.0     2023-07-07 [1] CRAN (R 4.2.2)
#>  scales         1.2.1      2022-08-20 [1] CRAN (R 4.2.2)
#>  sessioninfo    1.2.2      2021-12-06 [1] CRAN (R 4.2.2)
#>  styler         1.8.1      2022-11-07 [1] CRAN (R 4.2.2)
#>  survival       3.2-11     2021-04-26 [4] CRAN (R 4.0.5)
#>  tibble         3.2.1      2023-03-20 [1] CRAN (R 4.2.2)
#>  tidyr          1.3.0      2023-01-24 [1] CRAN (R 4.2.2)
#>  tidyselect     1.2.0      2022-10-10 [1] CRAN (R 4.2.2)
#>  timereg        2.0.5      2023-01-17 [1] CRAN (R 4.2.2)
#>  utf8           1.2.3      2023-01-31 [1] CRAN (R 4.2.2)
#>  vctrs          0.6.3      2023-06-14 [1] CRAN (R 4.2.2)
#>  withr          2.5.0      2022-03-03 [1] CRAN (R 4.2.2)
#>  xfun           0.39       2023-04-20 [1] CRAN (R 4.2.2)
#>  yaml           2.3.7      2023-01-23 [1] CRAN (R 4.2.2)
#> 
#>  [1] /home/fabians/R/x86_64-pc-linux-gnu-library/4.2
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Digging deeper, the error is in get_cif:

pammtools/R/add-functions.R

Lines 724 to 730 in 94e7ea1

hazards <- map(
causes_model,
~ {
.df <- mutate(newdata, cause = .x) %>%
arrange(.data[[time_var]], .by_group = TRUE)
X <- predict(object, .df, type = "lpmatrix")
apply(sim_coef_mat, 1, function(z) exp(X %*% z))

where causes_model from the attr_ped$risks-slot of the fitted PAMM used for map is a factor with levels 1, 2 but causein newdata has the text labels

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant