Skip to content

Instantly share code, notes, and snippets.

@trinker
Created February 1, 2019 12:21
Show Gist options
  • Save trinker/2a1ccb318fa13771ffac6f368a45f77a to your computer and use it in GitHub Desktop.
Save trinker/2a1ccb318fa13771ffac6f368a45f77a to your computer and use it in GitHub Desktop.
Class demo scaling techniques
min_max <- function(x, ...) {
m <- min(x, na.rm = TRUE)
(x - m)/(max(x, na.rm = TRUE) - m)
}
standardization <- function(x, ...) {
if (length(stats::na.omit(unique(x))) > 1) {
scale(x)
} else {
rep(0, length(x))
}
}
mean_normalization <- function(x, ...) {
(x - mean(x, na.rm = TRUE))/(max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
}
lapply(list(
normal = rnorm(1000, 10, 20),
binary = rbinom(n=1000, size=1, prob=0.05),
chisq_df3 = rchisq(1000, df = 3),
chisq_df1 = rchisq(1000, df = 1),
uniform = runif(10000)
), function(x){
par(mfrow = c(2, 2), ask = TRUE)
hist(x, main= 'unscaled')
hist(min_max(x), main= 'min max')
hist(standardization(x), main='standardization')
hist(mean_normalization(x), main='mean normalization')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment