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

**URL:** <https://discourse.julialang.org/t/how-to-get-the-principal-components-and-variances-from-multivariatestats/15843>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 3, 2018, 8:30pm UTC](https://discourse.julialang.org/t/how-to-get-the-principal-components-and-variances-from-multivariatestats/15843 "2018-10-03T20:30:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)\
**Post date:** [October 3, 2018, 8:30pm UTC](https://discourse.julialang.org/t/how-to-get-the-principal-components-and-variances-from-multivariatestats/15843/1 "2018-10-03T20:30:26Z")

</div>

Hi all,

I have run PCA and got the model, but I have not find any function to get the principal components and variances.  
[https://multivariatestatsjl.readthedocs.io/en/latest/pca.html](https://multivariatestatsjl.readthedocs.io/en/latest/pca.html)

any hints would be appreciated.  
thanks in advance!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 3, 2018, 9:31pm UTC](https://discourse.julialang.org/t/how-to-get-the-principal-components-and-variances-from-multivariatestats/15843/2 "2018-10-03T21:31:00Z")

</div>

Have you tried

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

```

and

```julia
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?

---

<div class="post-metadata">

**Author:** ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)\
**Post date:** [October 4, 2018, 12:49am UTC](https://discourse.julialang.org/t/how-to-get-the-principal-components-and-variances-from-multivariatestats/15843/3 "2018-10-04T00:49:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)\
**Post date:** [October 4, 2018, 2:31pm UTC](https://discourse.julialang.org/t/how-to-get-the-principal-components-and-variances-from-multivariatestats/15843/4 "2018-10-04T14:31:02Z")

</div>

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:

```julia
"""
    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

```

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [August 21, 2019, 6:08pm UTC](https://discourse.julialang.org/t/how-to-get-the-principal-components-and-variances-from-multivariatestats/15843/5 "2019-08-21T18:08:40Z")

</div>

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
