# PCA example from documentation -- not running

**URL:** <https://discourse.julialang.org/t/pca-example-from-documentation-not-running/8900>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [February 7, 2018, 5:45pm UTC](https://discourse.julialang.org/t/pca-example-from-documentation-not-running/8900 "2018-02-07T17:45:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![burfel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/burfel/32/3314_2.png) [@burfel](https://discourse.julialang.org/u/burfel)\
**Post date:** [February 7, 2018, 5:45pm UTC](https://discourse.julialang.org/t/pca-example-from-documentation-not-running/8900/1 "2018-02-07T17:45:38Z")

</div>

Hello,

I am trying to get dimensionality reduction methods running on a sample dataset and I am constantly running into dependency issues.  
I use Julia 0.6.2.

For instance:  
[http://multivariatestatsjl.readthedocs.io/en/latest/pca.html](http://multivariatestatsjl.readthedocs.io/en/latest/pca.html)  
does not run.  
I tried a DataFrames.DataFrame instead of a DataArray but then I don’t get the scatterplot produced.

Same with Kernel PCA  
[http://multivariatestatsjl.readthedocs.io/en/latest/kpca.html](http://multivariatestatsjl.readthedocs.io/en/latest/kpca.html)  
Does not work for me…

Could anyone help out?

Thanks.

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [February 7, 2018, 6:49pm UTC](https://discourse.julialang.org/t/pca-example-from-documentation-not-running/8900/2 "2018-02-07T18:49:00Z")

</div>

I’m not familiar with the inner workings of multivariatestats.jl but as a general comment, it’s hard to say what your problem might be from the information given. Could you please post a minimal example of what you’re trying to run and the error you get?

---

<div class="post-metadata">

**Author:** ![burfel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/burfel/32/3314_2.png) [@burfel](https://discourse.julialang.org/u/burfel)\
**Post date:** [February 8, 2018, 3:26pm UTC](https://discourse.julialang.org/t/pca-example-from-documentation-not-running/8900/3 "2018-02-08T15:26:13Z")

</div>

Thanks for the prompt response.  
Sure. After adding the package _MultivariateStats_ and creating _Xtr_, I run

> M = fit(KernelPCA, Xtr; maxoutdim=100, inverse=true)

I get  
_ERROR: UndefVarError: KernelPCA not defined_

PCA seems to be working. However, to produce the scatterplot with

> p = scatter(setosa[1,:],setosa[2,:],setosa[3,:],marker=:circle,linewidth=0)

does not work on DataFrames (I used a DataFrame instead of DataArray as the latter didn’t run or is outdated).  
If someone has the right method at hand for DataFrames, that would be great.

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [February 8, 2018, 6:46pm UTC](https://discourse.julialang.org/t/pca-example-from-documentation-not-running/8900/4 "2018-02-08T18:46:31Z")

</div>

KernelPCA was added to `MultivariateStats` pretty recently and the developers haven’t tagged a new release since it was added. If you installed `MultivariateStats` by running

```julia
Pkg.add("MultivariateStats")

```

then you’ll be using the latest release of the package, which does not include KernelPCA. When you go to [GitHub - JuliaStats/MultivariateStats.jl: A Julia package for multivariate statistics and data analysis (e.g. dimension reduction)](https://github.com/JuliaStats/MultivariateStats.jl) and click on the documentation link at the bottom of the README it brings you to the documentation as it exists on the master branch of the repository. You can run `Pkg.checkout("MultivariateStats")` to try out the latest features. If you want to go back to the release version just do `Pkg.free("MultivariateStats")`. Check out the package manager [documentation](https://docs.julialang.org/en/stable/manual/packages/#Checkout,-Pin-and-Free-1) for more information. The following worked for me:

```julia
julia> Pkg.checkout("MultivariateStats")
INFO: Checking out MultivariateStats master...
INFO: Pulling MultivariateStats latest master...                                                  
INFO: No packages to install, update or remove                                                    

julia> Xtr = rand(100,10);
using MultivariateStats
INFO: Recompiling stale cache file /home/patrick/.julia/lib/v0.6/MultivariateStats.ji for module MultivariateStats.

julia> M = fit(KernelPCA, Xtr; maxoutdim=100, inverse=true);

```

I don’t know about your second question unfortunately. To help out someone who might be able to answer it, are you using Plots.jl? In your example is `setosa` a DataFrame? Can you make a scatter plot if you just pass three plain vectors to `scatter`?

---

<div class="post-metadata">

**Author:** ![burfel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/burfel/32/3314_2.png) [@burfel](https://discourse.julialang.org/u/burfel)\
**Post date:** [February 15, 2018, 11:50am UTC](https://discourse.julialang.org/t/pca-example-from-documentation-not-running/8900/5 "2018-02-15T11:50:21Z")

</div>

Thanks for your help.  
Regarding the first question: Even if I pull it from the master branch by using `checkout` I get the same error message…  
Regarding my second question:  
I am using _Plots.jl_ and scatter is working on arrays only for me now which is fine. So that problem is solved.

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [February 19, 2018, 5:28pm UTC](https://discourse.julialang.org/t/pca-example-from-documentation-not-running/8900/6 "2018-02-19T17:28:48Z")

</div>

Had you already loaded the `MultivariateStats` packaged into your Julia session when you did `Pkg.checkout`? If so you should just need to restart Julia and try again. If that doesn’t solve your problem I don’t know what’s going on.

Patrick

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [February 19, 2018, 6:21pm UTC](https://discourse.julialang.org/t/pca-example-from-documentation-not-running/8900/7 "2018-02-19T18:21:58Z")

</div>

Based on the [documentation](http://multivariatestatsjl.readthedocs.io/en/latest/pca.html) at MultiVariateStats, it looks like PCA is still designed to be performed on an array with each observation as a column. This seems far from a relatively intuitive use in economics, where you have each observation in a row and you want to combine multiple variables (columns) into a few key components.

Is full integrations with DataFrames planned for PCA analysis?

Perhaps you can provide a code snippet that shows how you might want to do this with the MultivariateStats PCA?
