Skip to content

Commit d4c2e06

Browse files
authored
Fix scoping for nimbleMCMC (#1554)
* Add missing newline in warning message. * Fix roxygen for buildMCMC. * Remove one of the refs to MCMCsuite/compareMCMCs as redundant. * Reword some text related to compareMCMCs in manual. * Fix scoping for user-def objects with nimbleMCMC (issue 1545).
1 parent af18244 commit d4c2e06

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

packages/nimble/R/MCMC_run.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ nimbleMCMC <- function(code,
313313
samples = TRUE,
314314
samplesAsCodaMCMC = FALSE,
315315
summary = FALSE,
316-
WAIC = FALSE) {
316+
WAIC = FALSE,
317+
userEnv = parent.frame()) {
317318
#### process 'code' argument, to accept a filename, or a function
318319
##if(is.character(code) || is.function(code)) {
319320
## if(is.function(code)) modelText <- mergeMultiLineStatements(deparse(body(code)))
@@ -327,7 +328,7 @@ nimbleMCMC <- function(code,
327328
if(missing(code) && missing(model)) stop('must provide either code or model argument')
328329
if(!samples && !summary && !WAIC) stop('no output specified, use samples = TRUE, summary = TRUE, or WAIC = TRUE')
329330
if(!missing(code) && inherits(code, 'modelBaseClass')) model <- code ## let's handle it, if model object is provided as un-named first argument to nimbleMCMC
330-
Rmodel <- mcmc_createModelObject(model, inits, nchains, setSeed, code, constants, data, dimensions, check)
331+
Rmodel <- mcmc_createModelObject(model, inits, nchains, setSeed, code, constants, data, dimensions, check, userEnv = userEnv)
331332
conf <- configureMCMC(Rmodel, monitors = monitors, thin = thin, enableWAIC = WAIC, print = FALSE)
332333
Rmcmc <- buildMCMC(conf)
333334
compiledList <- compileNimble(Rmodel, Rmcmc) ## only one compileNimble() call

packages/nimble/R/MCMC_utils.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ mcmc_checkWAICmonitors <- function(model, monitors, dataNodes) {
422422
}
423423

424424

425-
mcmc_createModelObject <- function(model, inits, nchains, setSeed, code, constants, data, dimensions, check, buildDerivs = FALSE) {
425+
mcmc_createModelObject <- function(model, inits, nchains, setSeed, code, constants, data, dimensions, check, buildDerivs = FALSE, userEnv) {
426426
## create the Rmodel object using arguments provided to nimbleMCMC
427427
if(missing(model)) { ## model object not provided
428428
if(!missing(inits)) {
@@ -434,8 +434,8 @@ mcmc_createModelObject <- function(model, inits, nchains, setSeed, code, constan
434434
} else if(is.list(inits) && (length(inits) > 0) && is.list(inits[[1]])) {
435435
theseInits <- inits[[1]]
436436
} else theseInits <- inits
437-
Rmodel <- nimbleModel(code, constants, data, theseInits, dimensions = dimensions, check = check, buildDerivs = buildDerivs) ## inits provided
438-
} else Rmodel <- nimbleModel(code, constants, data, dimensions = dimensions, check = check, buildDerivs = buildDerivs) ## inits not provided
437+
Rmodel <- nimbleModel(code, constants, data, theseInits, dimensions = dimensions, check = check, buildDerivs = buildDerivs, userEnv = userEnv) ## inits provided
438+
} else Rmodel <- nimbleModel(code, constants, data, dimensions = dimensions, check = check, buildDerivs = buildDerivs, userEnv = userEnv) ## inits not provided
439439
} else { ## model object provided
440440
if(!is.model(model)) stop('model argument must be a NIMBLE model object')
441441
Rmodel <- if(is.Rmodel(model)) model else model$Rmodel

0 commit comments

Comments
 (0)