Skip to contents

Given a (rank n) PCA fit, return a rank k < n varimax fit

Usage

fit_varimax(
  k,
  pca,
  feature_names,
  obs_names,
  varimax_fn = stats::varimax,
  varimax_opts = NULL,
  positive_skew = TRUE,
  x = NULL
)

Arguments

k

Desired rank of the fitted varimax model

pca

Fitted PCA model or varimaxes object

feature_names

Names of the features (eg, data columns)

obs_names

Names of the observations (eg, data rows)

varimax_fn

Function to use for varimax rotation

varimax_opts

Options passed to varimax_fn

positive_skew

Should negative-skewed factors be flipped to have positive skew?

x

PCA scores matrix (n_obs x max_k), as returned by predict(fitted, newdata). Used when the fitted pca object does not contain scores.

Value

List with components - loadings: Rotated feature loadings - rotmat: Rotation matrix - scores: Rotated observation scores

Details

After the initial rotation, factors with negative skew (left tails) are flipped pca must contain $rotation (feature loadings matrix) and $sdev (standard deviations per PC); $x (PC scores matrix) is also required unless x is supplied directly.

Examples

# \donttest{
set.seed(42)
theta  = rdirichlet(50, 1, k = 3)
phi    = rdirichlet(3, 0.1, k = 20)
corpus = draw_corpus(rep(50L, 50), theta, phi)
dtm    = tidytext::cast_sparse(corpus, doc, word, n)
pca    = irlba::prcomp_irlba(dtm, n = 5)
fit_varimax(k = 3, pca = pca,
            feature_names = colnames(dtm),
            obs_names = rownames(dtm))
#> $loadings
#>             [,1]          [,2]          [,3]
#> 2  -1.543494e+00 -1.519224e+00 -1.7283648049
#> 5   6.571852e-02 -8.608355e-02 -0.0143080031
#> 8   6.641125e+00 -3.139326e+00  5.3903500385
#> 11 -6.242402e-01  6.643635e+00  1.3328232629
#> 12 -4.667281e-02  1.561741e+00  0.3179324781
#> 17 -3.419079e+00 -1.534457e+00 -0.5708738590
#> 20 -1.171382e+00 -1.409401e+00 -3.6377716827
#> 1   6.910355e-01 -3.868725e-01  0.2261162143
#> 10 -6.086037e-01 -5.046854e-01 -1.2253537497
#> 4   6.937302e-02  3.988841e-01  0.1265788554
#> 16 -5.848587e-02  1.575057e-02 -0.0071641039
#> 18 -3.881459e-03 -3.016794e-05 -0.1320655380
#> 7  -5.553028e-03 -3.379211e-02 -0.0389026695
#> 15  1.612053e-06 -9.270514e-03 -0.0397392701
#> 6   1.413891e-02  3.131571e-03  0.0007428317
#> 
#> $rotmat
#>             [,1]       [,2]       [,3]
#> [1,]  0.74518686 -0.2724038  0.6086811
#> [2,] -0.07016217 -0.9397238 -0.3346587
#> [3,]  0.66315445  0.2066769 -0.7193823
#> 
#> $scores
#>             [,1]        [,2]        [,3]
#> 1   4.014703e-01  1.96161454  0.51109747
#> 2  -1.449973e+00 -1.20424553 -0.45410837
#> 3   2.608392e-02  1.02981145  0.68988311
#> 4  -1.402166e+00  0.24256522  0.67327639
#> 5   1.372939e-01  1.10345694  0.28127126
#> 6   4.328892e-01  0.84973922 -1.39198030
#> 7   9.615852e-01 -0.94379276  1.17114669
#> 8  -1.220186e-01  1.88898146 -0.59160201
#> 9   2.212554e-01 -0.84174835 -0.88505775
#> 10 -1.076237e+00  0.75103141  1.16383027
#> 11  1.052675e+00 -0.49088357 -2.37193316
#> 12  8.202189e-01  0.19393072  1.28404116
#> 13 -4.152359e-01 -0.34705225  1.12288963
#> 14  7.475157e-01  1.14198405  0.08478722
#> 15  5.174924e-02 -0.98274672  1.03067891
#> 16  5.773347e-01 -1.16766035  0.31683409
#> 17  2.324127e-01  1.35305057  0.42238105
#> 18  1.283136e+00 -0.01259053 -1.26030471
#> 19  5.149513e-01 -0.63157266 -1.98300194
#> 20 -4.375625e-01 -1.04149646 -0.51035484
#> 21 -2.862608e-01 -0.10939326 -2.55946022
#> 22  4.691444e-01 -0.71306322  1.04061326
#> 23  1.003542e+00  1.30641156  0.45863898
#> 24  1.607225e+00 -0.39752603  0.58954203
#> 25 -1.793918e+00 -0.45186674 -0.57491091
#> 26 -6.243462e-01 -0.78416417 -0.06614102
#> 27  6.574014e-01 -1.44357643  1.44523685
#> 28 -4.512095e-02  1.08347196  0.70287329
#> 29 -3.784345e-01 -0.57454084 -1.68428696
#> 30  8.645409e-01 -1.06085215 -0.12426603
#> 31 -1.577667e-01  2.50859519  0.23592421
#> 32 -3.897296e-01  0.16213173 -1.10967611
#> 33 -2.021437e-01  1.15223250 -0.11715326
#> 34  1.756406e+00 -1.18734272  0.88753552
#> 35  1.762855e+00 -1.13682764  0.55293121
#> 36 -2.690458e-01  0.15819227 -1.21754134
#> 37 -2.378930e-01 -0.04228513  0.23080548
#> 38 -3.964277e-01  1.53290782 -0.27031529
#> 39 -7.041751e-01  1.11511552  0.11189972
#> 40  7.899061e-05 -0.45425520 -1.94722424
#> 41 -3.142034e+00 -0.69924440  1.22915873
#> 42  1.024984e+00 -0.43711148  1.16877688
#> 43  6.364846e-01  0.69129009  0.86169846
#> 44  5.739114e-01 -0.74859865  0.12961814
#> 45 -2.446367e+00 -1.32959877  0.71134819
#> 46  3.657761e-01 -0.68085847  0.21614615
#> 47 -7.259982e-01 -1.01894397 -0.11729096
#> 48  6.928068e-01  0.15344697  0.03639876
#> 49 -7.326117e-01  0.66830099 -0.26432392
#> 50 -1.440262e+00 -0.11442370  0.13967019
#> 
# }