ryszard.czerminski@pharma.novartis.com
2004-Jan-15 15:29 UTC
[Rd] prcomp scale error (PR#6433)
Full_Name: Ryszard Czerminski Version: 1.8.1 OS: GNU/Linux Submission from: (NULL) (205.181.102.120) prcomp(..., scale = TRUE) does not work correctly: $ uname -a Linux 2.4.20-28.9bigmem #1 SMP Thu Dec 18 13:27:33 EST 2003 i686 i686 i386 GNU/Linux $ gcc --version gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)> a <- matrix(rnorm(6), nrow = 3) > sum((scale(a %*% svd(cov(a))$u, scale = F)[,1] - (prcomp(a, scale F)$x)[,1])^2)[1] 2.465190e-31> sum((scale(a %*% svd(cov(a))$u, scale = T)[,1] - (prcomp(a, scale T)$x)[,1])^2)[1] 0.5493767
Prof Brian Ripley
2004-Jan-15 16:05 UTC
[Rd] prcomp(scale=T) actually works correctly (PR#6433)
Why do you claim that your answer is the correct one? You have not scaled the data as documented: scale.: a logical value indicating whether the variables should be scaled to have unit variance before the analysis takes place. set.seed(123) a <- matrix(rnorm(6), nrow = 3) scale(a %*% svd(cov(a))$u, scale = F)[,1] [1] 0.9915152 0.6985625 -1.6900777 prcomp(a, scale = F)$x[,1] [1] -0.9915152 -0.6985625 1.6900777 A <- scale(a) prcomp(a, scale = T)$x[,1] [1] -0.9366834 -0.6872942 1.6239776 prcomp(A, scale = F)$x[,1] [1] -0.9366834 -0.6872942 1.6239776 scale(A %*% svd(cov(A))$u, scale = F)[,1] [1] 0.9366834 0.6872942 -1.6239776 Please do not waste our time: do read the posting guide and the section on BUGS in the FAQ (especially the bits about being sure and about giving a reproducible example). On Thu, 15 Jan 2004 ryszard.czerminski@pharma.novartis.com wrote:> Full_Name: Ryszard Czerminski > Version: 1.8.1 > OS: GNU/Linux > Submission from: (NULL) (205.181.102.120) > > > prcomp(..., scale = TRUE) does not work correctly: > > $ uname -a > Linux 2.4.20-28.9bigmem #1 SMP Thu Dec 18 13:27:33 EST 2003 i686 i686 i386 > GNU/Linux > $ gcc --version > gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5) > > > a <- matrix(rnorm(6), nrow = 3) > > sum((scale(a %*% svd(cov(a))$u, scale = F)[,1] - (prcomp(a, scale > F)$x)[,1])^2) > [1] 2.465190e-31 > > sum((scale(a %*% svd(cov(a))$u, scale = T)[,1] - (prcomp(a, scale > T)$x)[,1])^2) > [1] 0.5493767 > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-devel > >-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
ryszard.czerminski@pharma.novartis.com writes:> prcomp(..., scale = TRUE) does not work correctly: > > $ uname -a > Linux 2.4.20-28.9bigmem #1 SMP Thu Dec 18 13:27:33 EST 2003 i686 i686 i386 > GNU/Linux > $ gcc --version > gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5) > > > a <- matrix(rnorm(6), nrow = 3) > > sum((scale(a %*% svd(cov(a))$u, scale = F)[,1] - (prcomp(a, scale > F)$x)[,1])^2) > [1] 2.465190e-31 > > sum((scale(a %*% svd(cov(a))$u, scale = T)[,1] - (prcomp(a, scale > T)$x)[,1])^2) > [1] 0.5493767Eh? I may be not quite up to speed on my PCA theory, but I fail to see why you should expect to have any relation between prcomp(..., scale=T) and an SVD of the covariance matrix. What you do have is> sum(((scale(a, scale=T) %*% svd(cor(a))$u)[,1] - (prcomp(a, scale + T)$x)[,1])^2)[1] 6.498858e-30 -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907