help-octave
[Top][All Lists]

## RE: how can I do Principal Components Analysis with octave?

 From: Ted Harding Subject: RE: how can I do Principal Components Analysis with octave? Date: Tue, 11 May 2004 09:58:49 +0100 (BST)

```On 11-May-04 rino mailing wrote:
> I'd like to do Principal Components Analysis with octave
>
> What are the command I ave to write?
>
> How to plot the result?

You find the principal components by first finding the eigenvalues
and eigenvectors of the covariance or correlation matrix (depending
on whether you want the results to be independent of the units in
which the variables are measured).

So let C be (say) the covariance matrix. Then

[V,D] = eig(C)

gives you the eigenvectors as columns of V, and the eigenvalues
as the diagonal elements of D, in increasing order of magnitude.

So, therefore, ifr X is the N by k matrix of data, C=cov(X),
[V,D]=eig(C), then X*V(:,k) gives you the first principal component,
X*V(:,k-1) the second, and so on.

I'm not sure what you mean by "plot the result". Some people want
to see a graph which shows the proportions of variance accounted
for by the successive principal components; for this you can plot
the diagonal elements diag(D) of D in reverse order, perhaps in the
form 100*diag(D)/sum(diag(D)). Or the amount of variance accounted
for by the first, first+second, first+second+third, etc; for this
you can plot the (reverse-order) cumsum of diag(D).

Or you may want to say plot the first principal component against
the second, so: PC1=X*V[:,k];PC2=X*V[:,k-1];plot(PC1,PC2)

This may be useful in exploring possible clustering; or you may
already have an index say Grp which identifies each row of X
as belonging to a group by Grp==1, Grp==2, Grp==3, ... , in which
case you can plot these subsets of (PC1,PC2) in different colours,
to see where the groups fall in the plot.

And so on ...

Hoping this helps,
Ted.

--------------------------------------------------------------------
Fax-to-email: +44 (0)870 167 1972
Date: 11-May-04                                       Time: 09:58:49
------------------------------ XFMail ------------------------------

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```