Skip to content

placeholder r function for user-defined dist not found when using nimbleMCMC #1530

@paciorek

Description

@paciorek

Growing out of a user post 2025-02-15, it appears we have a lookup/scoping problem finding the placeholder r function when a user-defined function is used in a model in nimbleMCMC.

dmy_prior = nimbleFunction(
    name = 'dmy_prior',
  run = function(x=double(0),
                 log=integer(0)){
    returnType(double(0))
    logProb <- -2.5*log(x)
    if(log){
      return(logProb)
    }else{
      return(exp(logProb))
    }
  }
)

code = nimbleCode({
  # hierarchical model
    y ~ dbinom(theta, 5)
    theta ~ dbeta(alpha, beta)
  
  # prior
  alpha ~ dmy_prior()
  beta <- 1
})

out <- nimbleMCMC(code, data=list(y=0), inits=list(alpha=1,theta=0.5), niter=100)
Error: In sizeAssignAfterRecursing: 'rmy_prior' is not available or its output type is unknown. This may occur if a user-defined function name is the same as the name of a function in a package that `nimble` uses.
 This occurred for: model_alpha[1] <<- rmy_prior(1)
 This was part of the call:  {
  model_alpha[1] <<- rmy_prior(1)
}

It is fine if dmy_prior is first registered or if the user defines rmy_prior.

I suspect we are putting the placeholder in the wrong scope, but I haven't looked into it yet.

This might be related to recent additional checking we do to make sure that functions used in DSL code are legitimate functions/function names.

Metadata

Metadata

Assignees

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