R: Whitening Data Using Singular Value Decomposition
WhitenSVD
R Documentation
Whitening Data Using Singular Value Decomposition
Description
The function whitens a data matrix using the singular value decomposition.
Usage
WhitenSVD(x, tol = 1e-06)
Arguments
x
A numeric data frame or data matrix with at least two rows.
tol
Tolerance value to decide the rank of the data. See details for
further information. If set to NULL it will be ignored.
Details
The function whitens the data so that the result has mean zero and identity
covariance matrix using the function svd. The data can have
here less observations than variables and svd will determine the rank of the
data automatically as the number of singular values larger than the largest
singular value times tol. If tol=NULL the rank is set to the
number of singular values, which is not advised when one or more singular
values are close to zero.
The output contains among others as attributes the singular values and the
matrix needed to backtransform the whitened data to its original space.
Value
The whitened data.
Author(s)
Klaus Nordhausen
See Also
svd, cov, colMeans
Examples
# more observations than variables
X <- matrix(rnorm(200),ncol=4)
A <- WhitenSVD(X)
round(colMeans(A),4)
round(cov(A),4)
# how to backtransform
summary(sweep(A %*% (attr(A,"backtransform")), 2, attr(A,"center"), "+") - X)
# fewer observations than variables
Y <- cbind(rexp(4),runif(4),rnorm(4), runif(4), rnorm(4), rt(4,4))
B <- WhitenSVD(Y)
round(colMeans(B),4)
round(cov(B),4)
# how to backtransform
summary(sweep(B %*% (attr(B,"backtransform")), 2, attr(B,"center"), "+") - Y)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(REPPlab)
Loading required package: rJava
Loading required package: lattice
Loading required package: LDRTools
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/REPPlab/WhitenSVD.Rd_%03d_medium.png", width=480, height=480)
> ### Name: WhitenSVD
> ### Title: Whitening Data Using Singular Value Decomposition
> ### Aliases: WhitenSVD
> ### Keywords: multivariate
>
> ### ** Examples
>
>
> # more observations than variables
> X <- matrix(rnorm(200),ncol=4)
> A <- WhitenSVD(X)
> round(colMeans(A),4)
[1] 0 0 0 0
> round(cov(A),4)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
> # how to backtransform
> summary(sweep(A %*% (attr(A,"backtransform")), 2, attr(A,"center"), "+") - X)
V1 V2 V3
Min. :-6.661e-16 Min. :-1.443e-15 Min. :-2.220e-15
1st Qu.:-1.041e-16 1st Qu.:-6.384e-16 1st Qu.:-4.441e-16
Median : 0.000e+00 Median :-1.943e-16 Median :-1.110e-16
Mean :-1.818e-17 Mean : 1.832e-17 Mean : 1.336e-17
3rd Qu.: 1.110e-16 3rd Qu.: 4.927e-16 3rd Qu.: 4.441e-16
Max. : 4.441e-16 Max. : 2.887e-15 Max. : 2.220e-15
V4
Min. :-1.776e-15
1st Qu.:-3.053e-16
Median : 5.551e-17
Mean : 1.527e-17
3rd Qu.: 3.331e-16
Max. : 9.992e-16
>
> # fewer observations than variables
> Y <- cbind(rexp(4),runif(4),rnorm(4), runif(4), rnorm(4), rt(4,4))
> B <- WhitenSVD(Y)
> round(colMeans(B),4)
[1] 0 0 0
> round(cov(B),4)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
> # how to backtransform
> summary(sweep(B %*% (attr(B,"backtransform")), 2, attr(B,"center"), "+") - Y)
V1 V2 V3
Min. :-2.498e-16 Min. :-2.220e-16 Min. :-3.261e-16
1st Qu.:-6.245e-17 1st Qu.:-1.388e-16 1st Qu.:-8.153e-17
Median : 5.551e-17 Median : 0.000e+00 Median : 0.000e+00
Mean : 1.388e-17 Mean :-2.776e-17 Mean :-8.153e-17
3rd Qu.: 1.318e-16 3rd Qu.: 1.110e-16 3rd Qu.: 0.000e+00
Max. : 1.943e-16 Max. : 1.110e-16 Max. : 0.000e+00
V4 V5 V6
Min. :-2.776e-16 Min. :-4.441e-16 Min. :-2.220e-16
1st Qu.:-6.939e-17 1st Qu.:-1.110e-16 1st Qu.:-5.551e-17
Median : 8.327e-17 Median : 0.000e+00 Median : 6.939e-17
Mean : 2.776e-17 Mean : 0.000e+00 Mean : 3.469e-17
3rd Qu.: 1.804e-16 3rd Qu.: 1.110e-16 3rd Qu.: 1.596e-16
Max. : 2.220e-16 Max. : 4.441e-16 Max. : 2.220e-16
>
>
>
>
>
>
>
> dev.off()
null device
1
>