Skip to content

Commit

Permalink
Merge pull request #230 from kescobo/ordinations
Browse files Browse the repository at this point in the history
Add ordinations (MDS and PCA)
  • Loading branch information
mkborregaard authored Oct 21, 2019
2 parents 278f38b + 77f4211 commit e8e074c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DataValues = "e7dc6d0d-1eca-5fa6-8ad6-5aecde8b7ea5"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
KernelDensity = "5ab0869b-81aa-558d-bb23-cbf5423bbe9b"
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
Observables = "510215fc-4207-5dde-b226-833fc4488ee2"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Expand All @@ -18,8 +19,9 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Widgets = "cc8bc4a8-27d6-5769-a93b-9d913e69aa62"

[compat]
julia = "≥ 1.0"
Plots = "≥ 0.25"
Widgets = "≥ 0.5"
MultivariateStats = "≥ 0.7"
Observables = "≥ 0.2.2"
Plots = "≥ 0.25"
RecipesBase = "≥ 0.6"
Widgets = "≥ 0.5"
julia = "≥ 1.0"
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This package is a drop-in replacement for Plots.jl that contains many statistica
- marginalhist
- corrplot/cornerplot
- andrewsplot
- MDS plot

It is thus slightly less lightweight, but has more functionality. Main documentation is found in the Plots.jl documentation (https://juliaplots.github.io).

Expand Down Expand Up @@ -374,3 +375,23 @@ Some statistical analysis is computed at the single subject level (for example t
For more information please refer to the [README](https://github.com/piever/GroupedErrors.jl/blob/master/README.md).

A GUI based on QML and the GR Plots.jl backend to simplify the use of StatsPlots.jl and GroupedErrors.jl even further can be found [here](https://github.com/piever/PlugAndPlot.jl) (usable but still in alpha stage).

## Ordinations

MDS from [`MultivariateStats.jl`](https://github.com/JuliaStats/MultivariateStats.jl)
can be plotted as scatter plots.

```julia
using MultivariateStats, RDatasets, StatsPlots

iris = dataset("datasets", "iris")
X = convert(Matrix, iris[:, 1:4])
M = fit(MDS, X'; maxoutdim=2)

plot(M, group=iris.Species)
```

![MDS plot](https://user-images.githubusercontent.com/3502975/64883550-a6186600-d62d-11e9-8f6b-c5094abf5573.png)

PCA will be added once the API in MultivariateStats is changed.
See https://github.com/JuliaStats/MultivariateStats.jl/issues/109 and https://github.com/JuliaStats/MultivariateStats.jl/issues/95.
3 changes: 3 additions & 0 deletions src/StatsPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Widgets: @nodeps
import DataStructures: OrderedDict
import Clustering: Hclust, nnodes
using Interpolations
import MultivariateStats: MDS, eigvals, projection, principalvars,
principalratio, transform

import KernelDensity
@recipe f(k::KernelDensity.UnivariateKDE) = k.x, k.density
Expand All @@ -37,5 +39,6 @@ include("marginalhist.jl")
include("bar.jl")
include("dendrogram.jl")
include("andrews.jl")
include("ordinations.jl")

end # module
26 changes: 26 additions & 0 deletions src/ordinations.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@recipe function f(mds::MDS{<:Real}; mds_axes=(1,2))
length(mds_axes) in [2,3] || throw(ArgumentError("Can only accept 2 or 3 mds axes"))
xax = mds_axes[1]
yax = mds_axes[2]
ev = eigvals(mds)
var_explained = [v / sum(ev) for v in ev]
tfm = collect(transform(mds)')

xlabel --> "MDS$xax"
ylabel --> "MDS$yax"
seriestype := :scatter
aspect_ratio --> 1

if length(mds_axes) == 3
zax = mds_axes[3]
zlabel --> "MDS$zax"
tfm[:,xax], tfm[:,yax], tfm[:,zax]
else
tfm[:,xax], tfm[:,yax]
end
end

#= This needs to wait on a different PCA API in MultivariateStats.jl
@recipe function f(pca::PCA{<:Real}; pca_axes=(1,2))
end
=#

0 comments on commit e8e074c

Please sign in to comment.