R/outliersMAD.R
changed.
Other files ignored by Codecov
man/outliersMAD.Rd
has changed.
docs/articles/how-to-use-this-package.html
has changed.
docs/reference/outliersMAD.html
has changed.
24 | 24 | #' @export |
|
25 | 25 | #' |
|
26 | 26 | #' @usage |
|
27 | - | #' outliersMAD(x, MADCutOff = 2.5, replaceOutliersWith = NA, |
|
27 | + | #' outliersMAD(x, MADCutOff = 3.0, replaceOutliersWith = NA, |
|
28 | 28 | #' showMADValues = FALSE, outlierIndices = FALSE, bConstant = 1.4826, digits = 2) |
|
29 | 29 | #' |
|
30 | 30 | #' @examples |
|
31 | - | #' example <- c(1, 3, 3, 6, 8, 10, 10, 1000) # 1000 is an outlier |
|
31 | + | #' example <- c(1, 3, 3, 6, 8, 10, 10, 1000, -1000) # 1000 is an outlier |
|
32 | 32 | #' outliersMAD(example) |
|
33 | 33 | #' outliersMAD(example, MADCutOff = 3.0) |
|
34 | 34 | #' outliersMAD(example, MADCutOff = 2.5, replaceOutliersWith = -999) |
|
35 | 35 | #' outliersMAD(example, MADCutOff = 1.5, outlierIndices = TRUE) |
|
36 | 36 | #' outliersMAD(example, MADCutOff = 1.5, showMADValues = TRUE) |
|
37 | - | outliersMAD <- function(x, MADCutOff = 2.5, replaceOutliersWith = NA, showMADValues = FALSE, outlierIndices = FALSE, bConstant = 1.4826, digits = 2) { |
|
37 | + | #' outliersMAD(example, MADCutOff = 1.5, showMADValues = TRUE, replaceOutliersWith = -88) |
|
38 | + | outliersMAD <- function(x, MADCutOff = 3.0, replaceOutliersWith = NA, showMADValues = FALSE, outlierIndices = FALSE, bConstant = 1.4826, digits = 2) { |
|
38 | 39 | # bConstant: usually, b = 1.4826, a constant linked to the assumption of normality of the data, disregarding the abnormality induced by out- liers (Rousseeuw & Croux, 1993). |
|
39 | 40 | ||
40 | 41 | # compute number of absolute MADs away for each value: formula: abs( ( x - median(x) ) )/ mad(x) |
|
41 | - | absMADAway <- abs((x - stats::median(x, na.rm = T))/stats::mad(x, constant = bConstant, na.rm = T)) |
|
42 | + | MADAway <- (x - stats::median(x, na.rm = T)) / stats::mad(x, constant = bConstant, na.rm = T) |
|
43 | + | absMADAway <- abs(MADAway) |
|
42 | 44 | # subset data that has absMADAway greater than the MADCutOff and replace them with replace |
|
43 | 45 | x[absMADAway > MADCutOff] <- replaceOutliersWith |
|
44 | 46 | outliers <- length(x[absMADAway > MADCutOff]) |
|
45 | 47 | if (showMADValues) { # if values == TRUE, return number of mads for each value |
|
46 | - | message("Showing absolute MAD from median for each value.") |
|
48 | + | message("Showing MAD from median for each value.") |
|
47 | 49 | message(paste0(outliers, " outliers detected.")) |
|
48 | - | return(round(absMADAway, digits)) |
|
50 | + | return(round(MADAway, digits)) |
|
49 | 51 | } else if (outlierIndices) { |
|
50 | 52 | message("Showing indices of outliers.") |
|
51 | - | return(which(is.na(x))) |
|
53 | + | if (is.na(replaceOutliersWith)) { |
|
54 | + | return(which(is.na(x))) |
|
55 | + | } else { |
|
56 | + | return(x[x == replaceOutliersWith]) |
|
57 | + | } |
|
58 | + | ||
52 | 59 | } else { |
|
53 | 60 | message(paste0(outliers, " outliers detected.")) |
|
54 | 61 | message(paste0("Outliers replaced with ", replaceOutliersWith)) |
Files | Coverage |
---|---|
R | 4.01% |
Project Totals (11 files) | 4.01% |