Skip to content

Instantly share code, notes, and snippets.

@arne-cl
Created August 7, 2014 10:38
Show Gist options
  • Save arne-cl/a2c3eba6551574ae814e to your computer and use it in GitHub Desktop.
Save arne-cl/a2c3eba6551574ae814e to your computer and use it in GitHub Desktop.
find one outlier using Grubbs' test
# code adapted from Lukasz Komsta's grubbs.test
# outlier value ("o" in grubbs.test) is stored in $outlier_value
# row name of the outlier ("G" in grubbs.test) is stored in $outlier_rowname
find_one_outlier <- function (x, opposite = FALSE)
{
DNAME <- deparse(substitute(x))
x <- sort(x[complete.cases(x)])
n <- length(x)
if (xor(((x[n] - mean(x)) < (mean(x) - x[1])), opposite)) {
alt = paste("lowest value", x[1], "is an outlier")
o <- x[1]
d <- x[2:n]
}
else {
alt = paste("highest value", x[n], "is an outlier")
o <- x[n]
d <- x[1:(n - 1)]
}
g <- abs(o - mean(x))/sd(x)
u <- var(d)/var(x) * (n - 2)/(n - 1)
pval <- 1 - pgrubbs(g, n, type = 10)
method <- "Grubbs test for one outlier"
RVAL <- list(statistic = c(G = g, U = u), alternative = alt,
p.value = pval, method = method, data.name = DNAME,
outlier_value = o, outlier_rowname = g)
class(RVAL) <- "htest"
return(RVAL)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment