R/GP_Lmom.R

Defines functions gp_Lmom

Documented in gp_Lmom

#' Fitting exponential distribution with L-moments
#' @param dat 
#' @param threshold 
#' 
#' @importFrom nsRFA par.genpar  
#' @importFrom nsRFA Lmoments
#' 
#' @return param Estimated parameters and standard error returned as a list($estimate, $se).
#' 
#' @export
#'
#' @examples estimate = gp_Lmom(test_data, threshold)
#' threshold was added as to the function because the function sometimes set the location parameter higher than threshold causing NA values in parameter estimation

gp_Lmom <- function(dat, threshold = NA) {

  param <- list(estimate = c(NA, NA, NA), se = c(NA, NA, NA))
  if (length(dat) >= 1) {
    if (is.na(threshold)){


      dat.Lmom <- Lmoments(dat)

      fail_safe <- failwith(NULL, par.genpar)
      fitted.param <- fail_safe(dat.Lmom[1], dat.Lmom[2], dat.Lmom[4])

      if (is.null(fitted.param) == TRUE) {
        print("Warning: the function par.genpar failed in gp_Lmom")
        invisible(param)
      } else {
        # Creating the returning list
        param$estimate <- c(fitted.param$xi, fitted.param$alfa, fitted.param$k)
        # Standard error is not yet implemented
        invisible(param)
      }
    }else {
      #put as a failsafe, the parameter estimation above sometimes gave location parameter lower than threshold
      dat2<-dat-threshold
      dat.Lmom <- Lmoments(dat2)
      t2 = dat.Lmom[2] / dat.Lmom[1]
      param$estimate[1] <- threshold
      param$estimate[3] <- 2-1/t2
      param$estimate[2] <- dat.Lmom[1]*(1/t2-1)
      invisible(param)
    }
  }

  else {
    print(paste("Warning: this station has less than ", 1," years of data. Use another method!",
                collapse = "", sep = ""))
    invisible(param)
  }
}
NVE/fitdistrib documentation built on May 7, 2019, 6:04 p.m.