# How to get the principal components and variances from MultivariateStats?

Hi all,

I have run PCA and got the model, but I have not find any function to get the principal components and variances.

any hints would be appreciated.

Have you tried

``````transform(M, x)
Transform observations x into principal components.
``````

and

``````principalvars(M)
The variances of principal components.
``````

I haven’t actually used the package myself but they sound like they do what you’re after?

1 Like

thanks, should be that function. did not noticed that …

actually, transform is not what I want. `transform` is reducing the data to lower dimension data, rather than getting the principal directional vector (elgenvector of covariance matrix).

I have made my own function to do it based on svd:

``````"""
pca(data)
perform PCA using SVD
inputs:
- data: M x N matrix of input data. (M dimensions, N trials)
outputs:
- PC: each column is a principle component
- V: M x 1 matrix of variances
"""
function pca(data::Array{T,2}) where T
X = data .- mean(data, dims=2)
Y = X' ./ sqrt(T(size(X,2)-1))
U,S,PC = svd(Y)
S = diagm(0=>S)
V = S .* S

# find the least variance vector
indexList = sortperm(diag(V); rev=true)

PCs = map(x->PC[:,x], indexList)
return PCs, diag(V)[indexList]
end
``````

I have data.
A need to do all step by step , without function.
What is ‘T’ in line Y = X’ ./ sqrt(T(size(X,2)-1)) ?
Paul