Skip to content

Error in rebuilding AD object with data argument from global environment #2

@janolefi

Description

@janolefi

Some RTMB users have become used to having a list including all data and potential hyperparameters, unpacked by getAll(), in their custom likelihood function. This was shown in many RTMB examples.

Sampling with SparseNUTS fails in this case because rebuilding of the AD function fails. Presumably, this is because the list (called dat below) is only available in the global environment, not inside sample_snuts().

The simple code below generates this error for me:

library(RTMB)
library(SparseNUTS)

data(ChickWeight)

nll <- function(par) {
  getAll(par, dat)
  mu <- beta0 + beta1 * x
  -sum(dnorm(y, mu, sdeps, log=TRUE))
}

par <- list(beta0 = mean(ChickWeight$weight), beta1 = 0, sdeps = 1)

# list with data and often hyperparameters
dat <- list(y = ChickWeight$weight, x = ChickWeight$Time)

# MakeADFun pulls dat from the global environment, so this works fine
obj <- MakeADFun(nll, par)
opt <- nlminb(obj$par, obj$fn, obj$gr)

fit <- sample_snuts(obj)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions