Skip to content

tmbprofile() for non-scalars? #413

@wlandau

Description

@wlandau

I'm extracting likelihood profiles, and I've noticed that tmbprofile() struggles if the parameter is a vector or matrix. This seems to be because the function relies on parameter names from the $env$last.par.best field of the fitted model object, and non-scalar parameters span multiple elements with duplicate names.

library(RTMB)
data(ChickWeight)
parameters <- list(
  mua = 0,
  sd = c(1, 1),
  mub = 0,
  sdeps = 1,
  a = rep(0, 50),
  b = rep(0, 50)
)
objective <- function(parms) {
  getAll(ChickWeight, parms, warn = FALSE)
  weight <- OBS(weight)
  nll <- 0
  nll <- nll - sum(dnorm(a, mean = mua, sd = sd[1], log = TRUE))
  nll <- nll - sum(dnorm(b, mean = mub, sd = sd[2], log = TRUE))
  predWeight <- a[Chick] * Time + b[Chick]
  nll <- nll - sum(dnorm(weight, predWeight, sd = sdeps, log = TRUE))
  ADREPORT(predWeight)
  nll
}
model <- RTMB::MakeADFun(
  objective,
  parameters,
  random = c("a", "b"),
  silent = TRUE
)
optimization <- nlminb(model$par, model$fn, model$gr)
TMB::tmbprofile(model, name = "sd", trace = FALSE)
#> Error in TMB::tmbprofile(model, name = "sd", trace = FALSE): 'name' is not unique

head(names(model$env$last.par.best))
#> [1] "mua"   "sd"    "sd"    "mub"   "sdeps" "a"

names(model$env$last.par.best) <- make.unique(names(
  model$env$last.par.best
))

head(names(model$env$last.par.best))
#> [1] "mua"   "sd"    "sd.1"  "mub"   "sdeps" "a"

profile <- TMB::tmbprofile(model, name = "sd.1", trace = FALSE)

Created on 2025-11-07 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.0 (2025-04-11)
#>  os       macOS 26.0.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  C.UTF-8
#>  ctype    C.UTF-8
#>  tz       America/Indiana/Indianapolis
#>  date     2025-11-07
#>  pandoc   3.6.3 @ /Applications/Positron.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#>  quarto   1.8.24 @ /Applications/quarto/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.5   2025-04-23 [1] CRAN (R 4.5.0)
#>  codetools     0.2-20  2024-03-31 [1] CRAN (R 4.5.0)
#>  digest        0.6.37  2024-08-19 [1] CRAN (R 4.5.0)
#>  evaluate      1.0.5   2025-08-27 [2] CRAN (R 4.5.0)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.5.0)
#>  fs            1.6.6   2025-04-12 [2] CRAN (R 4.5.0)
#>  glue          1.8.0   2024-09-30 [1] CRAN (R 4.5.0)
#>  htmltools     0.5.8.1 2024-04-04 [2] CRAN (R 4.5.0)
#>  knitr         1.50    2025-03-16 [1] CRAN (R 4.5.0)
#>  lattice       0.22-7  2025-04-02 [2] CRAN (R 4.5.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.5.0)
#>  MASS          7.3-65  2025-02-28 [2] CRAN (R 4.5.0)
#>  Matrix        1.7-4   2025-08-28 [2] CRAN (R 4.5.0)
#>  Rcpp          1.1.0   2025-07-02 [2] CRAN (R 4.5.0)
#>  reprex        2.1.1   2024-07-06 [2] CRAN (R 4.5.0)
#>  rlang         1.1.6   2025-04-11 [1] CRAN (R 4.5.0)
#>  rmarkdown     2.29    2024-11-04 [2] CRAN (R 4.5.0)
#>  RTMB        * 1.8     2025-10-14 [1] CRAN (R 4.5.0)
#>  sessioninfo   1.2.3   2025-02-05 [2] CRAN (R 4.5.0)
#>  TMB           1.9.18  2025-10-13 [1] CRAN (R 4.5.0)
#>  withr         3.0.2   2024-10-28 [1] CRAN (R 4.5.0)
#>  xfun          0.53    2025-08-19 [2] CRAN (R 4.5.0)
#>  yaml          2.3.10  2024-07-26 [2] CRAN (R 4.5.0)
#> 
#>  [1] /Users/C240390/Library/R/arm64/4.5/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library
#>  * ── Packages attached to the search path.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions