R/remove_outliers.R

Defines functions remove_outliers

Documented in remove_outliers

#' Remove outliers
#'
#' @param nodedf A data.frame containing a "logFC" column
#' @param plots logical, should plots with outliers be shown?
#'
#' @return The input data.frame with outlying measurements removed
#' @export
#'
remove_outliers <- function(nodedf, plots = F) {
  library("outliers")

  #### detect and remove outliers (according to grubbs.test)###
  outliernew <- as.numeric(outliers::grubbs.test(x = nodedf$logFC)["p.value"])

  if (plots & log10(outliernew) < -3) {
    plot(nodedf$logFC, main = nodedf$nodeID)
  }
  while (log10(outliernew) < -3) {
    if (plots) {
      points(x = which(nodedf$logFC == outliers::outlier(nodedf$logFC)), y = outliers::outlier(nodedf$logFC), col = "red")
    }
    nodedf <- nodedf[-which(nodedf$logFC == outliers::outlier(nodedf$logFC)), ]
    outliernew <- as.numeric(outliers::grubbs.test(x = nodedf$logFC)["p.value"])
  }

  nodedf
}
anschue/toxprofileR documentation built on Nov. 2, 2019, 1:55 p.m.