help-octave
[Top][All Lists]

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

 From: Paul Kienzle Subject: Re: how can I do Principal Components Analysis with octave? Date: Wed, 12 May 2004 07:03:45 -0400

```Up to a possible change in sign in some of the components, the
following, the following appears to be equivalent to princomp:

## Compute principal components of X
## [pc,z,w,Tsq] = princomp(X)
##   pc  the principal components
##   z   the transformed data
##   w   the eigenvalues of the covariance matrix
##   Tsq Hotelling's T^2 statistic for the transformed data
function [pc,z,w,Tsq] = princomp(X)
C = cov(X);
[U,D,pc] = svd(C);
if nargout>1, z = center(X)*pc; end
if nargout>2, w = diag(D); end
if nargout>3, Tsq = sumsq(zscore(z),2); end

Do we want alternative PCA functions in octave-forge as well?
We could extend the princomp interface.  Some things that
seem useful to me are giving it a dimension argument, and
choosing a 'good' one if dimension is 0.  If nargout is zero
we could plot the data in the space of the first two principle
components, or a pareto chart of the eigenvalues.  Other
suggestions?

Thanks,

Paul Kienzle

On May 12, 2004, at 4:49 AM, Fredrik Lingvall wrote:

```
```Try this:

function [pc,sv,n_sv]  = pca(x)
% [pc,sv,n_sv]  = pca(x)
%
% Input:
%   x - Data stored column-vise .
%
% Output:
```
% pc - Principal components (eigenvectors of the covariance matrix).
```%  sv     - Singular values.
%  n_sv - Normalized singular values.

C = cov(x);
[U,D,pc] = svd(C);
sv = diag(D);
n_sv = 100*sv/sum(sv);

\Fredrik

```
```I believe you would need the programs doing the work. They are called
```
functions in Octave or Matlab, something like "PCA.m". I would be interested
```myself so I checked using Google what MATLAB has:

Here is a list of the functions with a short description of each:
PRINCOMP - principal components from raw data matrix
PCACOV - pca from covariance matrix
PCARES - residuals from pca
BARTTEST - Bartlett's test for dimensionality.

Next I checked for the first two, namely "PRINCOMP" and "PCACOV" in
```
octave-forge but apparently neither is present. I guess we're out of luck
```for the time being unless we have the capability to write the program.
Henry

on 5/11/04 1:04 AM, rino mailing at address@hidden wrote:

```
```I'd like to do Principal Components Analysis with octave

What are the command I ave to write?

How to plot the result?

Thank you in advance for the time you spend to answer me, Mario.

```
```

-------------------------------------------------------------
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
-------------------------------------------------------------

```
```

-------------------------------------------------------------
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
-------------------------------------------------------------

```