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
--
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.
|
This email has been received from outside of DOI - Use caution before clicking on links, opening attachments, or responding. |
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])
}
}
}
})
|
This email has been received from outside of DOI - Use caution before clicking on links, opening attachments, or responding. |
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 think this should be okay since it is a posterior predictive node, but maybe Nimble needs to know that...
Thanks again.