Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional show info for PCA #90

Merged
merged 13 commits into from
Mar 3, 2022
2 changes: 1 addition & 1 deletion src/MultivariateStats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 25 additions & 1 deletion src/pca.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Tokazama marked this conversation as resolved.
Show resolved Hide resolved

#### PCA Training

## auxiliary
Expand Down