R/deprecated/modelSim.R

Defines functions model.sim

#' Simulates the Models through types
#'
#' @param m.mtn model generated by model.motion
#' @param m.dns model generated by model.density
#' @param m.mnp model generated by model.manipulation
#' @param m.lng model generated by model.longNote
#' @param mtn.pow power factor for mtn
#' @param dns.pow power factor for dns
#' @param decay.ms stress decay per ms
#' @param decay.perc.s stress decay percentage per second
#'
#' Note that 1.0 means 100 percent decay per second
#' @param stress.init stress to start off with
#' @param bin.size the bins size for model smoothing
#'
#' The smaller the bin size the more suspectible the model will be to sudden
#' spikes.
#' @param sim.disable disables stress simulation. Improves performance
#' @importFrom magrittr %<>% %>%
#' @importFrom dplyr mutate
#' @importFrom rlang .data
#' @importFrom purrr reduce
#' @export

model.sim <- function(m.mtn, m.dns, m.mnp, m.lng,
                      mtn.pow, dns.pow,
                      decay.ms = 0.0,
                      decay.perc.s = 0.1,
                      stress.init = 0,
                      bin.size = 5000,
                      sim.disable = F){

  if (decay.perc.s > 1 | decay.perc.s < 0) {
    stop("decay.perc.s cannot be more than 1 and less than 0.")
  }

  # This joins all models
  model <- list(m.mtn, m.dns, m.mnp, m.lng) %>%
    purrr::reduce(dplyr::left_join, by = 'offsets') %>%
    dplyr::rename(mtn.vals = 2,
                  dns.vals = 3,
                  mnp.vals = 4,
                  lng.vals = 5) %>%
    dplyr::mutate(
      lng.vals = dplyr::if_else(is.na(.data$lng.vals), 1, .data$lng.vals),
      bins = (.data$offsets %/% bin.size) * bin.size,
      values =
        (((.data$mtn.vals * 10) + 1) ** mtn.pow +
        (.data$dns.vals + 1) ** dns.pow) * .data$mnp.vals * .data$lng.vals
      )

  if (!sim.disable){
    # Simulation is done before binning
    sim <- .cppSimulateKey(model$offsets,
                           model$values,
                           decay_ms = decay.ms,
                           decay_perc_s = decay.perc.s,
                           stress_init = stress.init)
  } else { sim = NULL }

  # Binning
  model %<>%
    dplyr::group_by(.data$bins) %>%
    dplyr::summarise(values = mean(.data$values))


  return(list("sim" = sim, "model" = model))
}
Eve-ning/vsrgtools documentation built on Oct. 30, 2019, 5:40 p.m.