diff --git a/src/MultivariateStats.jl b/src/MultivariateStats.jl index 58943f8..3bc91fa 100644 --- a/src/MultivariateStats.jl +++ b/src/MultivariateStats.jl @@ -5,7 +5,7 @@ module MultivariateStats import Statistics: mean, var, cov, covm, cor import Base: length, size, show, dump import StatsBase: fit, predict, predict!, ConvergenceException, coef, weights, - dof, pairwise, r2 + dof, pairwise, r2, CoefTable import SparseArrays import LinearAlgebra: eigvals, eigvecs diff --git a/src/pca.jl b/src/pca.jl index b56a718..eb7e63d 100644 --- a/src/pca.jl +++ b/src/pca.jl @@ -135,12 +135,36 @@ gives the principal components for an observation, and \$\\mathbf{P}\$ is the pr reconstruct(M::PCA, y::AbstractVecOrMat{T}) where {T<:Real} = decentralize(M.proj * y, M.mean) ## show & dump - function show(io::IO, M::PCA) idim, odim = size(M) print(io, "PCA(indim = $idim, outdim = $odim, principalratio = $(r2(M)))") end +function show(io::IO, ::MIME"text/plain", M::PCA) + idim, odim = size(M) + print(io, "PCA(indim = $idim, outdim = $odim, principalratio = $(r2(M)))") + ldgs = loadings(M) + rot = diag(ldgs' * ldgs) + ldgs = ldgs[:, sortperm(rot, rev=true)] + ldgs_signs = sign.(sum(ldgs, dims=1)) + replace!(ldgs_signs, 0=>1) + ldgs = ldgs * diagm(0 => ldgs_signs[:]) + print(io, "\n\nPattern matrix (unstandardized loadings):\n") + cft = CoefTable(ldgs, string.("PC", 1:odim), string.("", 1:idim)) + print(io, cft) + print(io, "\n\n") + print(io, "Importance of components:\n") + λ = eigvals(M) + prp = λ ./ var(M) + prpv = λ ./ sum(λ) + names = ["SS Loadings (Eigenvalues)", + "Variance explained", "Cumulative variance", + "Proportion explained", "Cumulative proportion"] + cft = CoefTable(vcat(λ', prp', cumsum(prp)', prpv', cumsum(prpv)'), + string.("PC", 1:odim), names) + print(io, cft) +end + #### PCA Training ## auxiliary