ifelse statements in nimbleFunctions

463 views
Skip to first unread message

Ben Padilla

unread,
Sep 6, 2022, 1:13:09 PM9/6/22
to nimble-users
Hi all, 

I am back yet again hoping to learn from this great group of folks here. I am working to translate and adapt some R functions I wrote to nimble. My R functions are built with multiple conditional ifelse statements where there are threshold values needed for reproduction or survival, for example. the ifelse() function doesnt work with nimble, so I've been trying to code it appropriately, but I'm having trouble. I've looked through the existing conversations here in the google group and did my best to adapt accordingly, but I'm still running in to issues. 

The section of R code I am trying to replicate is here in red, with the corresponding section of the nimbleFunction in red below. Errors from printErrors() are included as well. 
Thanks a lot
Ben

# if Zs is below min threshold she doesnt breed, or if available Zr:Z is less than threshold she does not breed
  u<-ifelse (zs.values <= zs.threshold.k, 0, ifelse (Zr_o/z.tot<=breeding.zr.threshold, 0, 1))


R.nimb <- nimbleFunction(run = function(zr.v=double(1),zs.v=double(1),zs.k=double(0),c.slope=double(0),v.eng=double(0),steep=double(0),fat.thresh=double(0),sburn=double(0),sbs=double(0),sbw=double(0),m.ana=double(0),m.cat=double(0),zr.br=double(0),neomort=double(0),v.biom=double(0),warb=double(1),ost=double(1),aF=double(0),season=double(0)){
  returnType(double(1))
  ztot<-zr.v+zs.v
  maxC <- C.nimb(zr.v,zs.v,c.slope,steep,fat.thresh,v.biom,ost,aF,season)
  burned <- burn.nimb(z,sburn,sbs,sbw,season)
  if(season==2) eOut<-burned + (((63.24*(warb*0.85))/0.98 + (((0.05*70*4.187*0.85^0.75)/0.85)*warb)*200) * 0.001)
  if(season==1) eOut<-burned
  dE <- v.eng*(maxC*6)-eOut
  zr.o <- zr.v+(1/m.ana)*dE
  #create placeholder u to be filled with 0 and 1
  u <- zr.o
  for(i in 1:length(zr.o)){
    u[i]<- 0
    if(zs.v[i] <= zs.k) u[i] <- 0
    if((zr.o[i]/ztot[i]) <= zr.br) u[i] <- 0
    if((zr.o[i]/ztot[i]) >= zr.br) u[i] <- 1
  }

  u <- (1-neomort)*u
  u <- u/2
  return(u)
})
compileNimble(R.nimb)

> printErrors()
P_17_rcFun_R_GlobalEnv21.cpp:73:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]
}
^
P_17_rcFun_R_GlobalEnv21.cpp:210:29: error: use of undeclared identifier 'z'
burned = rcFun_R_GlobalEnv9(z, ARG8_sburn_, ARG9_sbs_, ARG10_sbw_, ARG19_season_);
                            ^
1 warning and 1 error generated.
make: *** [P_17_rcFun_R_GlobalEnv21.o] Error 1

Daniel Turek

unread,
Sep 6, 2022, 1:32:02 PM9/6/22
to Ben Padilla, nimble-users
Ben, I think the error that's being printed is coming from the first argument to the burn.nimb function, "z":

  burned <- burn.nimb(z,sburn,sbs,sbw,season)

which doesn't seem to be an argument, or declared anywhere.

Does that possibly fix things up?

Daniel




--
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/dff55e20-921c-4ac3-81dc-8445344fc06fn%40googlegroups.com.

Ben Padilla

unread,
Sep 6, 2022, 1:54:56 PM9/6/22
to db...@williams.edu, nimble-users

Ah! That did it. What I’d called ztot in this function I’d called z in the burn.nimb function and didn’t realize the mistake. I was so focused on the bigger issue I thought was going on that I didn’t notice this little detail. Thanks a lot Dan.

 

Ben

 

-- 

Benjamin Padilla Ph.D
Postdoctoral Research Associate

University of Calgary

Twitter: @bpdilla

Web: www.benpadilla.weebly.com

Reply all
Reply to author
Forward
0 new messages