error on nimble function

262 views
Skip to first unread message

Rebecca Taylor

unread,
Apr 23, 2024, 3:30:00 PM4/23/24
to nimble-users

I just wrote my first ever nimble function. It works in R, but I need to have it compiled because of run-time. Therefore, I put it as a nimble function within my nimble code, but it produces the following error. I am at a loss to understand what is going on. I will reprint the nimble code below the error message. Thanks! Rebecca

> Rmodel <- nimbleModel(code=code, constants=consts, data=data, calculate=T, check=T, inits=I1)

Defining model

Error in replaceConstantsRecurse(x, constEnv, constNames) :

  Error, hit end

> traceback()

14: stop("Error, hit end")

13: replaceConstantsRecurse(x, constEnv, constNames)

12: FUN(X[[i]], ...)

11: lapply(code[-1], function(x) replaceConstantsRecurse(x, constEnv,

        constNames))

10: replaceConstantsRecurse(x, constEnv, constNames)

9: FUN(X[[i]], ...)

8: lapply(code[-1], function(x) replaceConstantsRecurse(x, constEnv,

       constNames))

7: replaceConstantsRecurse(x, constEnv, constNames)

6: FUN(X[[i]], ...)

5: lapply(code[-c(1, 2)], function(x) replaceConstantsRecurse(x,

       constEnv, constNames))

4: replaceConstantsRecurse(declInfo[[i]]$code, constantsEnv, constantsNamesList)

3: replaceAllConstants()

2: md$setupModel(code = code, constants = constants, dimensions = dimensions,

       inits = inits, data = data, userEnv = userEnv, debug = debug)

1: nimbleModel(code = code, constants = consts, data = data, calculate = T,

       check = T, inits = I1)

 ------------------------------------------------------------

code <- nimbleCode({

gpd.inversion <- nimbleFunction(

    run = function(thetaGP=double(0),lambdaGP=double(0)) {

      thetaGP <- thetaGP

      lambdaGP <- lambdaGP

      wGP <- exp(-lambdaGP)

      mysGP <- exp(-thetaGP)

      mypGP <- mysGP

      xGP <- 0

      uGP <- runif(1)

      while (uGP>mysGP){

        xGP <- xGP+1

        mycGP <- thetaGP-lambdaGP+lambdaGP*xGP

        mypGP <- wGP*mycGP*(1+lambdaGP/mycGP)^(xGP-1)*mypGP*xGP^(-1)

        mysGP <- mysGP+mypGP}

      return(xGP)

      returnType(integer(0))

      })

 

# ALL OF THE CODE FOR THE PRIORS, LIKELIHOOD, ETC. GOES HERE 

# FUNCTION CALL IS IN LOOP BELOW

  for(k in 1:nsites){

    for(i in 1:nSmpl[k]){

      for (j in 1:g[i,k]){    

        msgZ <- gpd.inversion(thetaGP=meanZ[i,j,k],lambdaGP=delta[i,k])

    }

  }

}) # END model

Perry de Valpine

unread,
Apr 23, 2024, 3:39:17 PM4/23/24
to Rebecca Taylor, nimble-users
Hi Rebecca,
Thanks for the question. Please try creating gpd.inversion outside of the nimbleCode call. You can still use it in model code. When you compile the model, nimble will automatically compile gpd.inversion as well. If you would like to be able to use the compiled version of gpd.inversion directly (e.g. for testing), you can also compile it separately from the model.
I hope that makes sense and gets you going.
Perry


--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/d064f857-ac5d-44fa-9fd7-76cee7289712n%40googlegroups.com.

Taylor, Rebecca L

unread,
Apr 23, 2024, 3:41:40 PM4/23/24
to Perry de Valpine, nimble-users
Thank you, Perry!!  I now see my confusion. I will try that momentarily. Rebecca

Rebecca Taylor
Research Statistician
Alaska Science Center
U. S. Geological Survey


Pronouns: She/her
Please note I sometimes send and answer email outside normal work hours. 
This helps me with my schedule but is not intended to pressure others to do the same

From: Perry de Valpine <pdeva...@berkeley.edu>
Sent: Tuesday, April 23, 2024 11:39 AM
To: Taylor, Rebecca L <rebecc...@usgs.gov>
Cc: nimble-users <nimble...@googlegroups.com>
Subject: [EXTERNAL] Re: error on nimble function
 

 

 This email has been received from outside of DOI - Use caution before clicking on links, opening attachments, or responding.  

Taylor, Rebecca L

unread,
Apr 25, 2024, 6:33:52 PM4/25/24
to Perry de Valpine, nimble-users

Hi Perry,

I’m afraid I am still unable to run a nimble function. I can create the following as a regular R function and call it from R either with or without compiling it first. But when I try to create it as a nimbleFunction (now outside of my primary nimble code), it “hangs” (gives no visible response) when I try to call it from nimble. If I use calculate=T, check=T in the nimbleModel statement, it hangs then. If I set those arguments equal to FALSE, then it hangs on the runMCMC command. I have vastly reduced the data set, so even if the function call takes a long time to evaluate, I don’t think it should take more than a couple of minutes for the nimbleModel statement to complete. (I’ve waited like 20 minutes.) I suspect I am missing something obvious. Here is my code:

gpd.inversion <- nimbleFunction(

  run = function(thetaGP=double(0),lambdaGP=double(0)) {

    thetaGP <- thetaGP

    lambdaGP <- lambdaGP

    wGP <- exp(-lambdaGP)

    mysGP <- exp(-thetaGP)

    mypGP <- mysGP

    xGP <- 0

    uGP <- runif(1)

    while (uGP>mysGP){

      xGP <- xGP+1

      mycGP <- thetaGP-lambdaGP+lambdaGP*xGP

      mypGP <- wGP*mycGP*(1+lambdaGP/mycGP)^(xGP-1)*mypGP*xGP^(-1)

      mysGP <- mysGP+mypGP}

    return(xGP)

    returnType(integer(0))

  })

assign('gpd.inversion', gpd.inversion, envir = .GlobalEnv)

 

code <- nimbleCode({

 

# LOTS OF CODE BEFORE FUNCTION CALL

 

  for(k in 1:nsites){

    for(i in 1:nSmpl[k]){  

      for (j in 1:g[i,k]){    

        msgZ[i,j,k] <- gpd.cmp(thetaGP=meanZ[i,j,k],lambdaGP=delta[i,k])

      }

    }

  }

})


Rebecca Taylor
Research Statistician
Alaska Science Center
U. S. Geological Survey


Pronouns: She/her
Please note I sometimes send and answer email outside normal work hours. 
This helps me with my schedule but is not intended to pressure others to do the same

From: Perry de Valpine <pdeva...@berkeley.edu>
Sent: Tuesday, April 23, 2024 11:39 AM
To: Taylor, Rebecca L <rebecc...@usgs.gov>
Cc: nimble-users <nimble...@googlegroups.com>
Subject: [EXTERNAL] Re: error on nimble function
 

 

 This email has been received from outside of DOI - Use caution before clicking on links, opening attachments, or responding.  



Perry de Valpine

unread,
Apr 25, 2024, 7:10:26 PM4/25/24
to Taylor, Rebecca L, nimble-users
Hi Rebecca,
Can you give a fully reproducible example with specific values? The following works fine for me:
c_gpd.inversion <- compileNimble(gpd.inversion)
set.seed(4) # just happened to be an interesting choice
gpd.inversion(1.6, 1.2)
# 38
set.seed(4)
c_gpd.inversion(1.6, 1.2)
# 38 again
So it seems to work and compile fine.
Are there potentially any values for inputs or uGP for which the while loop could get stuck forever?
Also it is curious to have a deterministic node that actually involves a stochastic draw (uGP). Please understand the calculation will only be invoked when necessary, which is based on assuming it is deterministic.
I think a good reproducible example would use set.seed and have just a single line of model code with the gpd.inversion called (I assume that is the same as gpd.cmp).
The first two lines of gpd.inversion seem like they could be removed.
Perry

Taylor, Rebecca L

unread,
Apr 25, 2024, 8:11:10 PM4/25/24
to Perry de Valpine, nimble-users
Thanks Perry. Let me work with it a bit more and then send you something reproductible if need be. I don't think the problem is the function, but rather something in how I am compiling it or calling it....It is a generalized poisson random number generator which I need to obtain the posterior predictive distribution. Maybe I should have looked at how to write a distribution instead of a function. It shouldn't get stuck in the while loop as long as my priors are correct—I could error trap the incoming values. Thanks again. Rebecca


Rebecca Taylor
Research Statistician
Alaska Science Center
U. S. Geological Survey


Pronouns: She/her
Please note I sometimes send and answer email outside normal work hours. 
This helps me with my schedule but is not intended to pressure others to do the same

From: 'Perry de Valpine' via nimble-users <nimble...@googlegroups.com>
Sent: Thursday, April 25, 2024 3:10 PM

To: Taylor, Rebecca L <rebecc...@usgs.gov>
Cc: nimble-users <nimble...@googlegroups.com>
Subject: Re: [EXTERNAL] Re: error on nimble function
 
You received this message because you are subscribed to a topic in the Google Groups "nimble-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nimble-users/OIipa32WyWY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/CABeTmqjiCa2pLSGug8YQqH%3Dgpz93QCKkcTnyf3jB2cr0OQx7oA%40mail.gmail.com.

Taylor, Rebecca L

unread,
Apr 25, 2024, 8:32:16 PM4/25/24
to Perry de Valpine, nimble-users
Hi again Perry,

I excised the guts of gpd.inversion and just stuck them in my nimble code instead of calling it as a function, and I got the following error: 

Error in processBUGScode(recurseCode, nextContextID, lineNumber = lineNumber,  :  Error: while not allowed in BUGS code in while (uGP > mysGP) {

 Could that be the problem—that I'm not supposed to use a while loop?
I also got the following warning (which you alluded to). 

  [Warning] Model includes deterministic assignment using '<-' of the result of a density ('d') or simulation ('r') calculation. This is likely not what you intended in: uGP <- runif(1).


I think this should be okay since it is a posterior predictive node, but maybe Nimble needs to know that...


Thanks again.


Rebecca Taylor
Research Statistician
Alaska Science Center
U. S. Geological Survey


Pronouns: She/her
Please note I sometimes send and answer email outside normal work hours. 
This helps me with my schedule but is not intended to pressure others to do the same

From: 'Perry de Valpine' via nimble-users <nimble...@googlegroups.com>
Sent: Thursday, April 25, 2024 3:10 PM
To: Taylor, Rebecca L <rebecc...@usgs.gov>
Cc: nimble-users <nimble...@googlegroups.com>
Subject: Re: [EXTERNAL] Re: error on nimble function
 
You received this message because you are subscribed to a topic in the Google Groups "nimble-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nimble-users/OIipa32WyWY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/CABeTmqjiCa2pLSGug8YQqH%3Dgpz93QCKkcTnyf3jB2cr0OQx7oA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages