R/fitCurve_skewedGaussian.R

Defines functions fitCurve_skewedGaussian

# fit skewedGaussian
fitCurve_skewedGaussian <- function(x, y, useGuess, params, curveModel) {
  fittedCurve <- list()

  # Guess parameters and bounds
  if (useGuess) {
    new_params <- skewedGaussian_guess(x, y)
  } else {
    new_params <- params
  }

  # ensure order of init params and bounds (init is a list, lower and
  # upper are ordered numeric vectors)
  init    <- list(amplitude = new_params$init_params$amplitude,
                  center = new_params$init_params$center,
                  sigma = new_params$init_params$sigma,
                  gamma = new_params$init_params$gamma)
  lower   <- unlist(c(new_params$lower_bounds["amplitude"],
                      new_params$lower_bounds["center"],
                      new_params$lower_bounds["sigma"],
                      new_params$lower_bounds["gamma"]))
  upper   <- unlist(c(new_params$upper_bounds["amplitude"],
                      new_params$upper_bounds["center"],
                      new_params$upper_bounds["sigma"],
                      new_params$upper_bounds["gamma"]))

  # perform fit
  resultFit <- minpack.lm::nls.lm(par = init,
                                  lower = lower,
                                  upper = upper,
                                  fn = skewedGaussian_minpack.lm_objectiveFun,
                                  observed = y, xx = x)

  # prepare output
  fittedCurve <- resultFit$par
  fittedCurve$fitStatus <- resultFit$info
  fittedCurve$curveModel <- curveModel
  class(fittedCurve) <- "peakPantheR_curveFit"

  return(fittedCurve)
}
zhengfj1994/MeTEA documentation built on June 29, 2021, 5:21 a.m.