test/test_factor.rb in statsample-0.13.1 vs test/test_factor.rb in statsample-0.14.0

- old
+ new

@@ -1,8 +1,33 @@ require(File.dirname(__FILE__)+'/helpers_tests.rb') class StatsampleFactorTestCase < MiniTest::Unit::TestCase + def test_parallelanalysis + pa=Statsample::Factor::ParallelAnalysis.with_random_data(305,8,150) + assert_in_delta(1.2454, pa.ds_eigenvalues['ev_00001'].mean, 0.01) + assert_in_delta(1.1542, pa.ds_eigenvalues['ev_00002'].mean, 0.01) + assert_in_delta(1.0836, pa.ds_eigenvalues['ev_00003'].mean, 0.01) + end + def test_map + fields=%w{height arm.span forearm lower.leg weight bitro.diameter chest.girth chest.width} + m=Matrix[ + [ 1, 0.846, 0.805, 0.859, 0.473, 0.398, 0.301, 0.382], + [ 0.846, 1, 0.881, 0.826, 0.376, 0.326, 0.277, 0.415], + [ 0.805, 0.881, 1, 0.801, 0.38, 0.319, 0.237, 0.345], + [ 0.859, 0.826, 0.801, 1, 0.436, 0.329, 0.327, 0.365], + [ 0.473, 0.376, 0.38, 0.436, 1, 0.762, 0.73, 0.629], + [ 0.398, 0.326, 0.319, 0.329, 0.762, 1, 0.583, 0.577], + [ 0.301, 0.277, 0.237, 0.327, 0.73, 0.583, 1, 0.539], + [ 0.382, 0.415, 0.345, 0.365, 0.629, 0.577, 0.539, 1] + ] + map=Statsample::Factor::MAP.new(m) + assert_in_delta(map.minfm, 0.066445,0.00001) + assert_equal(map.number_of_factors, 2) + assert_in_delta(map.fm[0], 0.312475,0.00001) + assert_in_delta(map.fm[1], 0.245121,0.00001) + + end # Tested with SPSS and R def test_pca a=[2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1].to_scale b=[2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9].to_scale a.recode! {|c| c-a.mean} @@ -42,11 +67,11 @@ # Tested with R def test_principalaxis 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]] - fa=Statsample::Factor::PrincipalAxis.new(matrix,:m=>1) + fa=Statsample::Factor::PrincipalAxis.new(matrix,:m=>1, :max_iterations=>50) cm=::Matrix[[0.923],[0.912],[0.507],[0.483]] _test_matrix(cm,fa.component_matrix) @@ -54,16 +79,16 @@ h2.each_with_index{|ev,i| assert_in_delta(ev,fa.communalities[i],0.001) } eigen1=2.175 assert_in_delta(eigen1, fa.eigenvalues[0],0.001) - + assert(fa.summary.size>0) fa=Statsample::Factor::PrincipalAxis.new(matrix,:smc=>false) + assert_raise RuntimeError do fa.iterate end - assert(fa.summary.size>0) end def test_rotation_varimax @@ -80,9 +105,10 @@ assert(!varimax.rotated.nil?, "Rotated shouldn't be empty") assert(!varimax.component_transformation_matrix.nil?, "Component matrix shouldn't be empty") assert(!varimax.h2.nil?, "H2 shouldn't be empty") _test_matrix(expected,varimax.rotated) + assert(varimax.summary.size>0) end def _test_matrix(a,b) a.row_size.times {|i| a.column_size.times {|j| assert_in_delta(a[i,j], b[i,j],0.001)