R/fppest.R

Defines functions fppest

Documented in fppest

#' Wrapper function for use in estimation of simulation models
#' 
#' Return varies depending on output
#' 
#' @param par named or unnamed vector of untransformed parameters.
#' @param X an xts object of data
#' @param mdl data.frame describing the model
#' @param output one of param, hydro or error
#' @param parNames used to name par if not NULL [default]
#' 
#' @keywords FloorForT
#' @export
#' @examples
#' # Not Run
#' # ModelBuild()
fppest <- function(par,X,mdl,output=c("param","hydro","error"),parNames=NULL){

    ############ make a parameter structure ############
    param <- list()

    ## model definition
    mdl['dt'] <- unique( diff( as.numeric(index(X)) ) )
    param$mdl <- mdl

    ## add names to parameter vector if required
    if(!is.null(parNames)){
        names(par) <- parNames
    }
        
    ## untransformed parameters
    param$raw <- par

    ## transformed parameters
    param$trans <- ftrans(par,mdl)

    ## output if the parameters if required
    if(output=="param"){
        return(param)
    }

    ## call the model
    yhat <- fpp(param,X)

    ## output if hydro
    if(output=="hydro"){
        return(yhat)
    }

    ## compute errors
    t0 <- ftinit(X,param$mdl)
    t0 <- which(index(X)==t0)
    idx <- which(is.finite(X[,'output']))
    idx <- idx[idx > t0]
    e <- yhat[idx,'sim'] - X[idx,'output']
    
    idx <- which(!is.finite(e))
    e[idx] <- 2e34

    return(e)
}
waternumbers/FloodForT documentation built on Nov. 5, 2019, 12:07 p.m.