R/Scaling_type.R

Defines functions st_dev autoscale paretoscale rangescale vastscale norm scale_data

Documented in scale_data

st_dev<-function(x){
  return(sqrt(sum((x-mean(x))^2/(length(x)-1))))
}

autoscale<-function(x){
  return((x-mean(x))/st_dev(x))
}

paretoscale<-function(x){
  return((x-mean(x))/sqrt(st_dev(x)))
}

rangescale<-function(x){
  return((x-mean(x))/(max(x)-min(x)))
}

vastscale<-function(x){
  return(((x-mean(x))/st_dev(x))*(mean(x)/st_dev(x)))
}

norm<-function(data){
  a<-rowSums(abs(data),na.rm = T)
  data1<-data/a
  ref<-apply(data1,2,median,na.rm=T)
  new<-t(t(data1)/ref)
  coe<-apply(new, 2, median,na.rm=T)
  return(data1/coe)
}

#' Title
#' @param x data
#' @param scale_type scaling type to be used by the user
#'
#' @export
#'
scale_data <- function(x, scale_type="none"){

  if(scale_type=="none")
    x

  else if(scale_type=="PQN")
    norm(x)

  else if(scale_type=="autoscale")
    autoscale(x)

  else if (scale_type=="paretoscale")
    paretoscale(x)

  else if (scale_type=="rangescale")
    rangescale(x)

  else if (scale_type=="vastscale")
    vastscale(x)

  else print("There are only four types of scaling present in this function.
             Please select one of the four types of scaling : Autoscale, Pr
             aretoscale, RangeScale or Vastscale")
}
GweeXianYao/metaboliteR documentation built on Jan. 21, 2020, 7:18 a.m.