Learn R Programming

RTMB (version 1.7)

OSA-residuals: Recursive quantile residuals

Description

OSA residuals are computed using the function oneStepPredict. For this to work, you need to mark the observation inside the objective function using the OBS function. Thereafter, residual calculation is as simple as oneStepPredict(obj). However, you probably want specify a method to use.

Usage

oneStepPredict(
  obj,
  observation.name = names(obj$env$obs)[1],
  data.term.indicator = "_RTMB_keep_",
  ...
)

# S3 method for osa [(x, ...)

# S3 method for osa length(x)

# S3 method for osa dim(x)

# S3 method for osa is.array(x)

# S3 method for osa is.matrix(x)

Value

data.frame with standardized residuals; Same as oneStepPredict.

Arguments

obj

TMB model object (output from MakeADFun)

observation.name

Auto detected - use the default

data.term.indicator

Auto detected - use the default

...

Passed to TMB::oneStepPredict - please carefully read the documentation, especially the method argument.

x

Object of class 'osa'

Functions

  • oneStepPredict(): Calculate the residuals. See documentation of TMB::oneStepPredict.

  • [: Subset observations marked for OSA calculation. This function makes sure that when you subset an observation of class "osa" such as obs <- new("osa", x=advector(matrix(1:10,2)), keep = cbind(rep(TRUE,10),FALSE,FALSE)) the 'keep' attribute will be adjusted accordingly obs[,1:2]

  • length(osa): Equivalent of length

  • dim(osa): Equivalent of dim

  • is.array(osa): Equivalent of is.array

  • is.matrix(osa): Equivalent of is.matrix

Examples

Run this code
set.seed(1)
rw <- cumsum(.5*rnorm(20))
obs <- rpois(20, lambda=exp(rw))
func <- function(p) {
  obs <- OBS(obs) ## Mark 'obs' for OSA calculation on request
  ans <- 0
  jump <- c(p$rw[1], diff(p$rw))
  ans <- ans - sum(dnorm(jump, sd=p$sd, log=TRUE))
  ans <- ans - sum(dpois(obs, lambda=exp(p$rw), log=TRUE))
  ans
}
obj <- MakeADFun(func,
                 parameters=list(rw=rep(0,20), sd=1),
                 random="rw")
nlminb(obj$par, obj$fn, obj$gr)
res <- oneStepPredict(obj,
                      method="oneStepGeneric",
                      discrete=TRUE,
                      range=c(0,Inf))$residual

Run the code above in your browser using DataLab