Rotate an existing loading matrix
This guide considers the basic use case of FactorRotations.jl: Given an existing factor loading matrix L
, calculate the rotation of the loading matrix according to some rotation criterion. In this example we will first consider the simple case of orthogonal Varimax rotation. At a later stage we will see how to easily switch the factor rotation criterion to arrive at a different rotation.
First, we assume a factor loading matrix L
. In this example we will use the loading matrix given by Bernaard & Jennrich (2005),
julia> using FactorRotations
julia> L = [
0.830 -0.396
0.818 -0.469
0.777 -0.470
0.798 -0.401
0.786 0.500
0.672 0.458
0.594 0.444
0.647 0.333
]
8×2 Matrix{Float64}:
0.83 -0.396
0.818 -0.469
0.777 -0.47
0.798 -0.401
0.786 0.5
0.672 0.458
0.594 0.444
0.647 0.333
Rotating the loading matrix consists of a single call to rotate
. This function takes the unrotated loading matrix as the first argument, and an instance of a factor rotation method as a second argument.
For clarity we first set up our Varimax
rotation method,
julia> criterion = Varimax()
Varimax()
Finally we perform the rotation using rotate
,
julia> L_rotated = rotate(L, criterion)
FactorRotation{Float64} with loading matrix:
8×2 Matrix{Float64}:
0.886061 0.246196
0.924934 0.183253
0.894664 0.155581
0.865205 0.221416
0.264636 0.893176
0.206218 0.786653
0.156572 0.724884
0.269424 0.67595
Different rotation can be achieved by simply changing criterion
or passing it directly to rotate
.
julia> L_rotated = rotate(L, MinimumEntropy())
FactorRotation{Float64} with loading matrix:
8×2 Matrix{Float64}:
0.90711 0.151221
0.939117 0.084524
0.906093 0.0602051
0.883753 0.128783
0.357504 0.860225
0.28816 0.760468
0.232268 0.704289
0.339319 0.643709
The resulting FactorRotation
object contains the rotated loading matrix, the rotation matrix, and the factor correlation matrix. To access the fields you can use loadings
, rotation
, and factor_correlation
respectively.
julia> loadings(L_rotated)
8×2 Matrix{Float64}:
0.90711 0.151221
0.939117 0.084524
0.906093 0.0602051
0.883753 0.128783
0.357504 0.860225
0.28816 0.760468
0.232268 0.704289
0.339319 0.643709
julia> rotation(L_rotated)
2×2 Matrix{Float64}:
0.819445 0.573158
-0.573158 0.819445
julia> factor_correlation(L_rotated)
2×2 Matrix{Float64}:
1.0 -1.66533e-16
-1.66533e-16 1.0
In-place rotation
In some cases it can be useful to modify L
directly. For this use case the package provides an in-place rotation, rotate!
with the same function signature as before.
Contrary to rotate
, the in-place rotate!
returns the loading matrix instead of a FactorRotation
object.
julia> rotate!(L, MinimumEntropy())
8×2 Matrix{Float64}:
0.90711 0.151221
0.939117 0.084524
0.906093 0.0602051
0.883753 0.128783
0.357504 0.860225
0.28816 0.760468
0.232268 0.704289
0.339319 0.643709
julia> L == loadings(L_rotated)
true