R/withinMargin.R

Defines functions withinMargin

Documented in withinMargin

#' Am I wihthin a given margin window?
#' 
#' Provided `given` and `to_be_tested_whether_within` numeric vectors, test whether elements of `to_be_tested_whether_within` are within provided `margin` up and down the elements of `given`
#' 
#' @param given Numeric vector acting as given target
#' @param to_be_tested_whether_within Numeric vector to be tested whether is within a given window defined by `given` (defines mid-point of the window) and `margin`
#' @param margin A numeric of percentage in decimal form defining the upper and lower boundary of the window. E.g. for 0.1 use 0.05. Defults to 0.1 
#' @export  

withinMargin = function(given,to_be_tested_whether_within,margin=0.1){
  # given: Numeric vector acting as given target (e.g. predicted value)
  # to_be_tested_whether_within: Numeric vector to be tested whether is within a given window defined by `given` (defines mid-point of the window) and `margin`
  # margin: A numeric of percentage in decimal form defining the upper and lower boundary of the window. E.g. for 0.1 use 0.05. Defults to 0.1 
  stopifnot(!(any(is.na(given)) | any(is.na(to_be_tested_whether_within))))
  lower = 1-margin
  upper = 1+margin
  relative = to_be_tested_whether_within/given
  out = sapply(X = relative, FUN = function(x){
    if((x < lower) | (x > upper)){
      return(F)
    }
    else{
      return(T)
    }
  })
  out
}
msxakk89/dat documentation built on Aug. 3, 2020, 6:39 p.m.