Skip to content

Instantly share code, notes, and snippets.

@joelgombin
Created February 14, 2014 14:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save joelgombin/9002444 to your computer and use it in GitHub Desktop.
Nested means discretisation (Scripter, 1970)
n_means <- function(x, n) {
if (!(log(n, base=2) %% 1 %in% 0)) {
stop("The number of classes is not a power of 2.")
}
index <- rep(1, length(x))
m <- c()
for (i in 1:log(n, base=2)) {
m <- sort(c(m, tapply(x, index, mean)))
index <- findInterval(x, m)
}
names(m) <- c()
return(list(borns = m, classes=index))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment