R/rmse.r

Defines functions rmse

Documented in rmse

#' Calculates the Root Mean Squared Error (RMSE) as a cost function for
#' model optimization. Different cost functions can be specified
#' if they adhere to the same basic structure in their parameters.
#' Currently only RMSE is provided as a cost function,
#'
#' @param par a vector of parameter values, this is functions specific
#' @param data nested data structure with validation data as returned
#' by format_phenocam() or format_pep725(), or your own dataset adhering
#' to the same data structure.
#' @param model the model name to be used in optimizing the model
#' Most models are listed in Melaas et al. 2013, additional ones
#' will be added over time, preliminary list:
#' 1. SEQ1.3
#' @param ... extra arguments to pass to the function
#' @return the RMSE comparing observed and estimated values
#' @keywords phenology, model, optimization, cost function
#' @export
#' @examples
#'
#' # The cost function returns the rmse between the
#' # true values and those generated by the model given a
#' # parameterset par.
#' \dontrun{
#' cost_value = rmse(par, data, model="TTs")
#' }

rmse = function(par, data, model, ...) {

  # inset validity checks
  val = data$transition_dates
  out = do.call(model,list(data = data, par = par, ...))

  if (any(is.na(out))) {
    return(9999)
  } else {
    # return the RMSE between the validation data and
    # the output of the model
    return(sqrt(mean((val - out) ^ 2, na.rm = T)))
  }
}
khufkens/phenor documentation built on Dec. 5, 2017, 12:37 a.m.