# Example 1: Basic IQR method without groups
x <- c(1, 2, 3, 4, 100)
detect_outlier(x)
# IQR method with summary
detect_outlier(x, summary = TRUE)
# Z-score method with custom threshold
y <- c(-10, 1, 2, 3, 4, 5, 20)
detect_outlier(y, method = "zscore", z_threshold = 2.5)
# Handling missing values
z <- c(1, 2, NA, 4, 5, 100)
detect_outlier(z, method = "iqr", multiplier = 3)
# Example 2: IQR method with groups
x2 <- c(1, 2, 3, 100, 5, 6, 7, 200)
groups2 <- c("A", "A", "A", "A", "B", "B", "B", "B")
detect_outlier(x2, groups = groups2)
# Example 3: Z-score method with groups and summary
x3 <- c(-10, 1, 2, 20, 3, 4, 5, 50)
groups3 <- c("X", "X", "X", "X", "Y", "Y", "Y", "Y")
detect_outlier(x3, method = "zscore", z_threshold = 2, groups = groups3, summary = TRUE)
# Example 4: IQR method with groups and NA values
x4 <- c(1, 2, NA, 100, 5, 6, 7, 200,1000)
groups4 <- c("G1", "G1", "G1", "G1", "G2", "G2", "G2", "G2","G1")
detect_outlier(x4, groups = groups4)
# Error cases
if (FALSE) {
detect_outlier(c("a", "b")) # Non-numeric input
detect_outlier(c(1), groups = c("A")) # Insufficient data
detect_outlier(c(1, 2), groups = c("A")) # Mismatched group length
detect_outlier(c(1, NA, 3), na.rm = FALSE) # NA with na.rm = FALSE
}
Run the code above in your browser using DataLab