# Proper use of MDS and PCA

**URL:** <https://discourse.julialang.org/t/proper-use-of-mds-and-pca/90257>\
**Category:** Statistics\
**Created:** [November 14, 2022, 8:16pm UTC](https://discourse.julialang.org/t/proper-use-of-mds-and-pca/90257 "2022-11-14T20:16:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [November 14, 2022, 8:16pm UTC](https://discourse.julialang.org/t/proper-use-of-mds-and-pca/90257/1 "2022-11-14T20:16:26Z")

</div>

Assume I have a 20\*85 matrix. Here is my code

```julia
    M = fit(PCA, matrix; maxoutdim=2)
    Yte = predict(M, matrix)
M1 = fit(MDS, matrix; maxoutdim=2, distances=false)
    Yte1 = predict(M1)

```

My Yte and Yte1 are identically same. Did I use PCA and MDS properly?

---

<div class="post-metadata">

**Author:** ![mkoculak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkoculak/32/28310_2.png) [@mkoculak](https://discourse.julialang.org/u/mkoculak)\
**Post date:** [November 14, 2022, 9:15pm UTC](https://discourse.julialang.org/t/proper-use-of-mds-and-pca/90257/2 "2022-11-14T21:15:48Z")

</div>

From the [documentation](https://juliastats.org/MultivariateStats.jl/stable/mds/):

> Compute an embedding of `X` points by classical multidimensional scaling (MDS). There are two calling options, specified via the required keyword argument `distances`:
> 
> ```julia
> mds = fit(MDS, X; distances=false, maxoutdim=size(X,1)-1)
> 
> ```
> 
> where `X` is the data matrix. Distances between pairs of columns of `X` are computed using the Euclidean norm. This is equivalent to performing PCA on `X`.

So your code looks ok. 😉

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [November 14, 2022, 9:19pm UTC](https://discourse.julialang.org/t/proper-use-of-mds-and-pca/90257/3 "2022-11-14T21:19:49Z")

</div>

Do you mean when distance = false, MDS is equivalent to PCA?

---

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [November 14, 2022, 9:29pm UTC](https://discourse.julialang.org/t/proper-use-of-mds-and-pca/90257/4 "2022-11-14T21:29:16Z")

</div>

I changed it to

```julia
M1 = fit(MDS, pairwise(Euclidean(), matrix); maxoutdim=2, distances=true)
  Yte1 = predict(M1)

```

as the documentation said, but Yte and Yte1 are still the same.

---

<div class="post-metadata">

**Author:** ![mkoculak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkoculak/32/28310_2.png) [@mkoculak](https://discourse.julialang.org/u/mkoculak)\
**Post date:** [November 14, 2022, 9:40pm UTC](https://discourse.julialang.org/t/proper-use-of-mds-and-pca/90257/5 "2022-11-14T21:40:49Z")

</div>

As I understand it, doing `distance=false` makes it use Euclidean distance, while `distance=true` makes it use a distance metric you provide. Here you provided Euclidean, so results are exactly the same.  
And it seems that using Euclidean MDS is exactly equal to PCA (I have no experience with MDS, just following comments from the net, e.g. [here](https://stats.stackexchange.com/questions/14002/whats-the-difference-between-principal-component-analysis-and-multidimensional))
