Error from checkConsistency function

9 views
Skip to first unread message

Gary Nelson

unread,
Jun 17, 2026, 7:28:56 AM (11 days ago) Jun 17
to TMB Users
Hello All,
I've created a simple state-space model (see below) that estimates correctly.  But when I use the checkConsistency function,  I get the error message :

Error in x$reverse.update(value, j) : Implicit simulation failed. Cannot update operands of '(x - mean)[1L]' when both are missing

Would anyone know why this occurs? Any hint would be appreciated.

Thanks,
Gary Nelson

#Create Data
len <- 500
u1 <- numeric(len + 1)
psd <- 0.1 #process error
for(i in 1:len) u1[i+1] <- u1[i] + rnorm(1, 0, psd)

series <- numeric(len)
osd<- 0.1 #observation error
series<- u1[2:(len+1)] + rnorm(len, 0, osd)


#Setup information
datar<-data.frame(series=series)
parameters<-list(logpe = log(0.1), logoe = log(0.1),
                 u=rep(0, length(series)+1))
map <- list(u=as.factor(c(NA,1:length(series))))


#Objective function
estimate<-function(parms){
  RTMB::getAll(datar,parms,warn=FALSE)
  series<-RTMB::OBS(series)
  psd<-exp(logpe)
  osd<-exp(logoe)
  nll<-0
  for(i in 2:length(u)){
    nll <-nll-RTMB::dnorm(u[i], u[i-1], psd, TRUE)
  }
  for(i in 1:length(series)){
    nll <- nll-RTMB::dnorm(series[i],u[i+1], osd, TRUE)
  }
  ADREPORT(psd)
  ADREPORT(osd)
  nll
}

#Fit model
model <- RTMB::MakeADFun(estimate,parameters,map=map,random="u")
fit<-try(nlminb(model$par, model$fn, model$gr,control=nlminb.control),silent=T)

RTMB::checkConsistency(model)

Kasper Kristensen

unread,
Jun 17, 2026, 8:00:40 AM (11 days ago) Jun 17
to TMB Users
First, I suggest you run traceback() after an error. That'll point you to the code line causing the error:

 RTMB::dnorm(u[i], u[i - 1], psd, TRUE)

What the error message says is that it can't simulate u[i] without knowing the value of u[i-1].
That's because there's no specification on how to draw the initial state. Replacing 'nll<-0' by say 'nll <- -dnorm(u[1],log=TRUE)' will make the simulation work.

Note that mapping of random effects is known to cause trouble, so better avoid that. If you really want a deterministic initial state, then make your 'u' one element shorter, and adapt the code.
Reply all
Reply to author
Forward
0 new messages