| score | R Documentation |
Implements classical methods for data scaling: range, z-score normalization, location and location-scale normalization, as well as data thesholding through the simplest form of ReLU rectifier. Missing values are removed in all cases.
score(x, how, filter = NULL, ...)
x |
numeric vector, length > 1. Variable to be scaled or filtered |
how |
symbol. Choices are range, stdev or relu |
filter |
character, length 1. Default NULL. Choices "positive", "negative". Requires |
... |
list reserved for User input of paired values, statistic or otherwise. The list uses individual
ellipsis arguments therefore, order of values needs be respected at all times e.g. when |
Normalization (scaling) can be applied locally on subsets of x when User inputs the values in the ... list.
Otherwise, the scaling is global i.e. it is applied to x as a whole. No assumptions regarding the underlying distribution
of x are made.
how != relu. When ... is empty, the function uses the sample statistics of x e.g. the mean, range
or standard deviation. Otherwise, it uses values inputted by User case in which, location-scale normalization
requires how = stdev and the ... list filled as follows: the min(x) or max(x), or any other
value first and sd(x) > 0 or any other positive value second. In particular, location normalization works similarly
but with the second value = 1. Other location types, e.g. x/max(x), are obtainable.
NOTE: when ... is populated with custom values, all other arguments should be present and named (see Examples).
how = relu. This option acts as numeric thresholding locally as well as globally. It stands for rectified
linear unit and involves no statistics. It applies to numeric types that have ordering property (double,
integer). On return, all x attributes are dropped. When filter = 'positive', all negative values are set to zero
while positive values remain unchanged. Alternatively, when filter = 'negative', all negative values remain unchanged
while all other values are set to zero. The "negative" option was added for symmetry.
Numeric. When missing ... and how != relu, scaled values using x own sample statistics.
Otherwise, scaling is based on values inputted by User. When how = relu, x >= 0 or x <= 0, depending
on filter setting.
Ancillary Statistic for location and location-scale distributions
if (interactive()) {
# 1. ReLU thresholding
x = { set.seed(223); sort(runif(10, -3, 3)) }
y = score(x, relu, 'positive'); y
z = score(x, relu, 'negative'); z
# 1.1 ReLU Plot
olp = par(no.readonly = TRUE)
par(list(mar = c(1,1,1,1), mgp = c(0,0,0), tcl = -0.01, pty = 's'))
plot(x, y, type = 'l', col = 'steelblue', lwd = 2 ,
xlim = c(min(x), max(x)), ylim = c(min(x), max(x))
, ylab = expression(ReLU(x)), xaxs = 'i', yaxs = 'i', axes = FALSE, cex.lab = 0.7)
axis(1, pos = 0, cex.axis = 0.6) ; axis(2, pos = 0, cex.axis = 0.6)
points(x, z, type = 'l', col = 'orangered', lwd = 2)
legend('topleft', legend = c('positive', 'negative'),
col = c('steelblue', 'orangered'), pch = 'l', lwd = 2, cex = 0.6, bty = 'n')
par(olp)
# 2. Location and location-scale
# 2.1 Location (e.g. "x - max(x)")
x = 1:10
M = max(x)
std = 1
a = score(x, stdev, NULL, M, std); a
# 2.2 Location (e.g. "x/max(x)")
m = 0 # the mean
M = max(x) # or any value
b = score(x, range, NULL, m, M); b
# 2.3 Location-scale (e.g. "(x - max(x))/sd(x)")
M = max(x) # or any value
std = sd(x) # or any value > 0
c = score(x, stdev, NULL, M, std); c
# m, M and std above can be replaced with any values decided by User
# 3. Classical normalization
# 3.1 Range
d = score(x, range); d
# 3.2 z-score
e = score(x, stdev); e
# 4. Local vs. global z-score normalization
data(mtcars)
x = mtcars$wt
m = mean(x)
std = sd(x)
ll = split(x, f = as.factor(mtcars$cyl)) # partitioned x
# 4.1 Local scaling
aa = lapply(ll, score, stdev, NULL, m, std) # filled ... list
na = unlist(aa, FALSE, FALSE)
# 4.2 Global scaling
nb = score(x, stdev)
# 4.3 Local as well as global hold
identical(sort(na), sort(nb)) # TRUE
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.