From ripley at stats.ox.ac.uk  Thu Jan 15 16:06:51 2004
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu Jan 15 16:05:33 2004
Subject: [Rd] prcomp(scale=T) actually works correctly (PR#6433)
In-Reply-To: <20040115143125.DE0971047A@slim.kubism.ku.dk>
Message-ID: <Pine.LNX.4.44.0401151455110.27477-100000@gannet.stats>

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


[Rd] prcomp(scale=T) actually works correctly (PR#6433)
Prof Brian Ripley