This function transform the input data via z-standardisation. See the Details section below for further information.
Usage
zStandardisation(x, robust = FALSE, by = c("rows", "cols"), na.rm = TRUE)
Arguments
- x
a (named) numerical vector or a matrix features-by-samples.
- robust
logical, whether to compute a robust z-standardisation.
- by
character string, indicating whether to compute the standardisation by rows or columns of
x
. It is used whenx
is a matrix.- na.rm
logical, whether to remove
NA
values before the computation.
Details
The z-standardisation (or z-score normalisation) is a method for transforming the data so that it has a mean of zero and a standard deviation of one. It is computed as:
$$z(x) = \frac{x - \mu}{\sigma}$$
where \(\mu\) and \(\sigma\) are the mean and standard deviation of the population, respectively.
Since z-scores can be affected by unusually large or small data values, we can also compute a more robust modified version as:
$$z(x) = \frac{x - \mathrm{median}}{\mathrm{MAD}}$$
where \(\mathrm{MAD}\) is the median absolute deviation of the population.
Examples
#set seed for reproducibility
set.seed(seed = 5381L)
#x is a vector
x = sample(x = -10:10, size = 10)
#Standard z-score transformation
zStandardisation(x)
#> [1] -1.41475949 -0.23579325 0.64843143 -0.08842247 0.20631909 -1.12001793
#> [7] -0.97264715 1.53265611 1.38528533 0.05894831
#Robust z-score transformation
zStandardisation(x=x,robust=TRUE)
#> [1] -1.16502949 -0.18395203 0.55185608 -0.06131734 0.18395203 -0.91976013
#> [7] -0.79712544 1.28766418 1.16502949 0.06131734
#x is a matrix
#Define row/col size
nr = 20
nc = 10
#Create input matrix
x = matrix(
data = stats::runif(n = nr*nc, min = 0, max = 1000),
nrow = nr,
ncol = nc,
dimnames = list(
paste0("g",seq(nr)),
paste0("S",seq(nc))
)
)
#Standardise the features
zStandardisation(x=x,by="rows")
#> S1 S2 S3 S4 S5 S6
#> g1 0.2624428 1.3983881 -1.2758636 0.39909284 -0.9867582 -1.2788577
#> g2 0.6523120 -0.7437961 0.2621862 1.19488521 -0.7478923 1.4920740
#> g3 0.5847869 1.2768872 -0.5757830 -0.03202588 -1.2299604 -0.7867920
#> g4 -0.5617825 0.5872960 -0.8746203 2.08259570 0.6642502 -0.7662553
#> g5 1.1165266 0.9268175 0.0958604 -0.41205776 0.7865037 -1.4131087
#> g6 -0.4444489 -1.3039473 -0.2113584 0.29304713 -1.1270272 0.5192974
#> g7 1.5895720 0.7187668 -0.0182766 -1.29355304 -0.4509851 -1.3759829
#> g8 0.1819454 -1.4511335 -1.3353409 1.36179270 0.7494688 -0.8841805
#> g9 -0.9635881 0.2449203 0.1162947 -1.23515323 1.3458378 1.5182848
#> g10 -0.2687879 -1.2355109 1.4876072 1.07157927 0.2922403 -0.5319888
#> g11 -1.3776246 -0.4602545 0.6368065 1.13916871 -1.3658621 0.5293702
#> g12 1.1815417 1.1183107 -0.6148590 -2.12087803 0.3811298 0.1477492
#> g13 -0.2007323 -1.1445889 1.4490767 -0.88046583 0.9096155 0.5500329
#> g14 -0.9547043 -1.2967449 0.8282594 0.98531315 1.4846790 -1.1695634
#> g15 0.9961759 0.1775545 -1.0283102 1.14208651 -1.8502588 -0.8952862
#> g16 -1.4807871 -0.7475768 -0.1406406 0.21670002 1.3159851 1.0663156
#> g17 0.9903776 -1.1880350 0.1330936 0.25051540 1.1031101 -0.7374619
#> g18 1.1567776 0.1638224 -0.9447115 0.03774972 -0.6517946 1.1554756
#> g19 0.7391937 -1.6999425 0.1864886 1.21838844 -0.1822111 -1.5290585
#> g20 0.7186636 0.5768870 -1.0179943 0.07429993 -0.7470519 1.1552963
#> S7 S8 S9 S10
#> g1 0.64955437 0.4577074 -0.78184676 1.1561407
#> g2 0.85198985 -1.0994598 -0.74358737 -1.1187117
#> g3 -0.47076522 1.7788413 0.44457901 -0.9897679
#> g4 -0.80413002 0.5319365 0.24495524 -1.1042455
#> g5 0.69212634 -1.3897165 0.69529523 -1.0982469
#> g6 0.26783845 0.7982746 -0.80877350 2.0170977
#> g7 -0.78457318 0.6317676 -0.09497461 1.0782390
#> g8 -0.28603707 0.5548376 -0.10741738 1.2160648
#> g9 -1.18502405 0.8449683 -0.31169904 -0.3748415
#> g10 0.12817198 -0.7571421 -1.33093955 1.1447705
#> g11 1.24381157 -1.0926921 0.27111483 0.4761615
#> g12 -0.20350400 0.9338379 -0.24185057 -0.5814778
#> g13 -0.39212572 1.1614591 0.01120639 -1.4634778
#> g14 0.84917935 -0.1948104 0.10677267 -0.6383805
#> g15 0.74855161 0.2320067 -0.33622947 0.8137094
#> g16 0.07359999 0.9911408 -1.50959375 0.2148567
#> g17 -1.92223436 1.0004672 0.15252811 0.2176392
#> g18 -1.24658113 1.2860775 0.33259622 -1.2894118
#> g19 0.36658272 0.4797967 -0.57071152 0.9914735
#> g20 -1.42887574 1.3811833 0.35547671 -1.0678849