truncation error in model defining

9 views
Skip to first unread message

Ben Padilla

unread,
Jul 26, 2022, 8:08:18 PM7/26/22
to nimble-users
Hello All, 
sorry for the simple question. I've searched through prior conversations on the subject of truncation and couldn't find anything to solve my problem. 
I am working on a model with truncation throughout, with all upper and lower bounds of truncation defined. When I try to define the model using nimbleModel() or run the code with nimbleMCMC straight from the model code I get the following error. 

Defining model
  [Note] Using 'ccr,csd,ndat' (given within 'constants') as data.
Error in getSymbolicParentNodesRecurse(code, constNames, indexNames, nimbleFunctionNames,  :
  R function 'T' has arguments that cannot be evaluated; either the function must be a nimbleFunction or values for the following inputs must be specified as constants in the model: character(0).


Thanks in advance for the help 

### code below here #######
ipm_epsi_mod <- nimbleCode( {
  # This is a demographic IPM
  # the model estimates population size, reproduction, and
  # survival for female caribou in
  # three age classes (calves, subadults, adults)
 
  # Priors
  ## Reproduction
  repr_mu ~ T(dnorm(log(ccr1/(1 - ccr1)), 10),-10,10)
 
  ## Survival
  sJuv_mu ~ T(dnorm(log(sC.mu/(1-sC.mu)), 10),-10,10)
  sAF_mu ~ T(dnorm(log(sF.mu/(1-sF.mu)), 10),-10,10)
 
  # Hyper-Priors and Linear Predictors
  ## Standard deviations for random effects
  R_tau <- pow(repr_sig, -2)
  repr_sig ~ dunif(0, 50)
  sA_tau <- pow(sA_sig, -2)
  sA_sig ~ dunif(0, 50)
  sJ_tau <- pow(sJ_sig, -2)
  sJ_sig ~ dunif(0, 50)
 
  ## Linear Predictors
  for(yr in 1:nYear){
    # Reproduction: Probability an Adult Cow Reproduces in each year
    repr_yr[yr] ~ T(dnorm(0, R_tau),-10,10)
    logit(Repr[yr]) <- repr_mu + repr_yr[yr]
   
    # Survival[year, age, sex]
    ## Calf Survival (constant over time)
    sJuv_yr[yr] ~ T(dnorm(0, sJ_tau),-10,10)
    logit(sJuv[yr]) <- sJuv_mu + sJuv_yr[yr]
   
    ## Adult and Sub-adult Survival Probability in each year
    ## Both males and females vary at the same annual rate (sA_yr)
    sA_yr[yr] ~ T(dnorm(0, sA_tau),-10,10)
    logit(sAf[yr]) <- sAM_mu + sA_yr[yr]
  }
 
 
  # Initial starting Population [year, age, sex]
  ## Calf and sub-adult starting pop at 0
  nReps[1] <- nJf[1]
  nJf[1] <- T(dnorm((nC1*ccr1), 0.0001),0,550000)
  nYf[1] <- 0
  ## Adult starting population
  nAf[1] ~ T(dnorm(nC1, 0.0001),0,550000)
 
  totC[1] <- nJf[1]
  totF[1] <- nJf[1] + nYf[1] + nAf[1]
  totN[1] <- totF[1] + (totF[1]*bcr1)
 
  # Process Model for Three Age-Classes and Two sexes
  ###################################################
 
  for(yr in 2:nYear){
    # Calves (reproduction)
    c.Mu[yr] <- nAf[yr]*Repr[yr]
    nYoung[yr] ~ dnorm(c.Mu[yr], 1/abs(c.Mu[yr]))
    nJf[yr] <- nYoung[yr]*0.5
   
    # Sub-adults (1-2yrs)
    nYf[yr] ~ dbin(sJuv[yr-1], nJf[yr-1])
   
    # Adults (≥2yrs)
    nAf[yr] ~ dbin(sAf[yr-1], (nYf[yr-1] + nAf[yr-1]))
   
    #  Calculate totals in each year
    totC[yr] <- nJf[yr]
    totF[yr] <- nJf[yr] + nYf[yr] + nAf[yr]
    totN[yr] <- totF[yr] + (totF[yr]*bcr1)
  }
 
  # Observation Models for Data Objects
  ######################################
 
  #  Breeding Cow Abundance (Aerial Surveys)
  for(i in 1:nndat){
    ndat[i,2] ~ T(dnorm(nAf[ndat[i,1]], ndat[i,4]),0,550000)
  }
 
  # Spring Calf:Cow Ratio (Aerial and Ground Surveys)
  for(i in 1:nccr){
    ccr[i,2] ~ T(dnorm(ccRat[ccr[i,1]], ccr[i,4]),0,1)
  }
 
  # Cow Survival (Telemetry)
  for(i in 1:ncsd){
    csd[i,2] ~ T(dnorm(sAf[csd[i,1]], csd[i,4]),0,1)
  }
 
  # Derive calf:cow and bull:cow ratios from abundance parameter estimates
  # Age ratio (ccRat), total young/females 1.5 and older
  for(yr in 1:nYear){
    ccRat[yr] <- (nJf[yr])/(nYf[yr] + nAf[yr] + 1)
  }
  #  Derive Population Growth Rate (lambda)
  #  arbitrary value of 1 is given to the first year
  lambdaF[1] <- 1
  for(yr in 2:nYear){
    lambdaF[yr] <- (totF[yr] + 1)/(totF[yr-1] + 1)
  }
  lambda[1] <- 1
  for(yr in 2:nYear){
    lambda[yr] <- (totN[yr] + 1)/(totN[yr-1] + 1)
  }
})

Chris Paciorek

unread,
Jul 27, 2022, 10:50:05 AM7/27/22
to Ben Padilla, nimble-users
Hi Ben, one of your truncation statements uses `<-` instead of `~`. This causes our warning system to think you're trying to use T() as a function rather than as a truncation symbol, hence the somewhat obscure message. (And ideally the system would point to the line in which the error is occurring. I'll file an issue report for us to try to improve the warning system in this regard.)

nJf[1] <- T(dnorm((nC1*ccr1), 0.0001),0,550000)

Side note, you have dnorm precisions of 10 in some cases. I assume that is what you want given you use the default precision parameterization throughout, but it did occur to me that you might have meant those values of 10 to be standard deviations.

-chris

--
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/356dcb7c-9662-4d33-8d08-cbe3a3a513fcn%40googlegroups.com.

Ben Padilla

unread,
Jul 27, 2022, 11:51:15 AM7/27/22
to paci...@stat.berkeley.edu, nimble-users

Hi Chris,

Thanks so much for catching that mistake. Sorry I didn’t notice it myself!

 

Thanks, too, for the thoughts on precision values. I always end up going back and forth on whether it is better to specify these as standard deviations or as precision (tau). I am typically starting from a JAGs script at some point, which helps to guide me as I code the model in Nimble. From my understanding nimble accepts either SD or precision, but is one preferred over the other?

Thanks again,

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