Here we use the Statsample::Factor::PrincipalAnalysis class for principal axis analysis for a correlation or covariance matrix.
require 'statsample'
Statsample::Analysis.store(Statsample::Factor::PrincipalAxis) do
matrix = Matrix[
[1.0, 0.709501601093587, 0.877596585880047, 0.272219316266807],
[0.709501601093587, 1.0, 0.291633797330304, 0.871141831433844],
[0.877596585880047, 0.291633797330304, 1.0, -0.213373722977167],
[0.272219316266807, 0.871141831433844, -0.213373722977167, 1.0]
]
matrix.extend Statsample::CovariateMatrix
fa = principal_axis(matrix,:m=>1,:smc=>false)
summary fa
end
Statsample::Analysis.run_batch
Analysis 2016-03-26 10:21:02 +0000 = Statsample::Factor::PrincipalAxis == Number of factors: 1 Iterations: 5 Communalities +----------+---------+------------+ | Variable | Initial | Extraction | +----------+---------+------------+ | X0 | 1.0000 | 0.854 | | X1 | 1.0000 | 0.830 | | X2 | 1.0000 | 0.258 | | X3 | 1.0000 | 0.233 | +----------+---------+------------+ Total Variance +----------+-----------+---------+------------+-----------+---------+------------+ | Factor | I.E.Total | I.E. % | I.E.Cum. % | S.L.Total | S.L. % | S.L.Cum. % | +----------+-----------+---------+------------+-----------+---------+------------+ | Factor 1 | 2.493 | 62.328% | 62.328 | 2.175 | 54.382% | 54.382 | | Factor 2 | 1.503 | 37.572% | 99.900 | | | | | Factor 3 | 0.004 | 0.099% | 99.998 | | | | | Factor 4 | 0.000 | 0.002% | 100.000 | | | | +----------+-----------+---------+------------+-----------+---------+------------+ Factor Matrix +----+----------+ | | factor_1 | +----+----------+ | X0 | .924 | | X1 | .911 | | X2 | .508 | | X3 | .483 | +----+----------+