Skip to content
Merged
2 changes: 1 addition & 1 deletion src/MultivariateStats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module MultivariateStats
using StatsBase: SimpleCovariance, CovarianceEstimator
import Statistics: mean, var, cov, covm
import Base: length, size, show, dump
import StatsBase: fit, predict, ConvergenceException
import StatsBase: fit, predict, ConvergenceException, CoefTable

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import is for when you overload a function with a new method. It shouldn't be needed here.

import SparseArrays

export
Expand Down
18 changes: 17 additions & 1 deletion src/pca.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,27 @@ transform(M::PCA{T}, x::AbstractVecOrMat{T}) where {T<:Real} = transpose(M.proj)
reconstruct(M::PCA{T}, y::AbstractVecOrMat{T}) where {T<:Real} = decentralize(M.proj * y, M.mean)

## show & dump

function show(io::IO, M::PCA)
print(io, "PCA(indim = $(indim(M)), outdim = $(outdim(M)), principalratio = $(principalratio(M)))")
end

function show(io::IO, ::MIME"text/plain", M::PCA)
print(io, "PCA(indim = $(indim(M)), outdim = $(outdim(M)), principalratio = $(principalratio(M)))")
ldgs = projection(M) * diagm(0 => sqrt.(M.prinvars))
rot = diag(ldgs' * ldgs)
ldgs = ldgs[:,sortperm(rot, rev=true)]
Comment thread
Tokazama marked this conversation as resolved.
Outdated
ldgs_signs = sign.(sum(ldgs, dims=1))
ldgs_signs[ldgs_signs .== 0] .= 1
Comment thread
Tokazama marked this conversation as resolved.
Outdated
ldgs = ldgs * diagm(0 => ldgs_signs[:])
print(io, "\n\nPattern matrix\n")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(io, "\n\nPattern matrix\n")
print(io, "\n\nPattern matrix:\n")

(for consistency with below)

Also, better be specific about what this matrix is: psych says "standardized loadings".

display(ldgs)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if this came out with columns and rows labeled but without access to the variable names that's not possible. In the current state the columns are the components in the same order as those in the following coeftable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's annoying. For now maybe print a CoefTable object without row names. If you pass an empty vector as row names, numbers will automatically be generated. That way at least the names of PCs will be consistent with the table below.

Also, how about printing the second table first? That's the most important result I think.

Comment thread
Tokazama marked this conversation as resolved.
Outdated
print(io, "\n")
print(io, "Importance of components:\n")
print(io, CoefTable(vcat(principalvars(M)', (principalvars(M) ./ tvar(M))', (cumsum(principalvars(M) ./tvar(M)))'),
string.("PC", 1:length(principalvars(M))), # components in order
["Loadings", "Proportion explained", "Cumulative proportion"])) # row names

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Loadings" is "SS loadings" from psych, right? Shouldn't that be specified? Also "proportion" should say of what (the variance). And how about adding "proportion of variance" and "cumulative variance" which are in base R and psych?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not "Loadings", they are "Eigenvalues" or "SS loadings" as R calls them.

Loadings would be

projection(M).*sqrt.(principalvars(M))'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wildart You're definitely right. I apologize for the oversight.

end
Comment thread
Tokazama marked this conversation as resolved.

function dump(io::IO, M::PCA)
show(io, M)
println(io)
Expand Down