test/test_reliability.rb in statsample-0.11.2 vs test/test_reliability.rb in statsample-0.12.0

- old
+ new

@@ -1,8 +1,6 @@ require(File.dirname(__FILE__)+'/helpers_tests.rb') - - class StatsampleReliabilityTestCase < MiniTest::Unit::TestCase context Statsample::Reliability do context "Cronbach's alpha" do setup do @samples=40 @@ -28,10 +26,23 @@ total_sum=@cm.total_sum ind_var=@ds.fields.inject(0) {|ac,v| ac+@ds[v].variance} expected = @k.quo(@k-1) * (1-(ind_var.quo(total_sum))) assert_in_delta(expected, @a,1e-10) end + should "method cronbach_alpha_from_n_s2_cov return correct values" do + sa=Statsample::Reliability::ScaleAnalysis.new(@ds) + vm, cm = sa.variances_mean, sa.covariances_mean + assert_in_delta(sa.alpha, Statsample::Reliability.cronbach_alpha_from_n_s2_cov(@n_variables, vm,cm), 1e-10 ) + + end + should "return correct n for desired alpha, covariance and variance" do + sa=Statsample::Reliability::ScaleAnalysis.new(@ds) + vm, cm = sa.variances_mean, sa.covariances_mean + n_obtained=Statsample::Reliability.n_for_desired_alpha(@a, vm,cm) + #p n_obtained + assert_in_delta(Statsample::Reliability.cronbach_alpha_from_n_s2_cov(n_obtained, vm,cm) ,@a,0.001) + end should "standarized alpha will be equal to sum of matrix covariance less the individual variances on standarized values" do total_sum=@cme.total_sum ind_var=@dse.fields.inject(0) {|ac,v| ac+@dse[v].variance} expected = @k.quo(@k-1) * (1-(ind_var.quo(total_sum))) assert_in_delta(expected, @as, 1e-10) @@ -85,17 +96,85 @@ assert_equal(expected, @icc.curve_field('a',index)) end end - context Statsample::Reliability::ItemAnalysis do + + context Statsample::Reliability::MultiScaleAnalysis do + + setup do + + size=100 + @scales=4 + @items_per_scale=10 + h={} + @scales.times {|s| + @items_per_scale.times {|i| + h["#{s}_#{i}"] = (size.times.map {(s*2)+rand}).to_scale + } + } + @ds=h.to_dataset + @msa=Statsample::Reliability::MultiScaleAnalysis.new(:name=>'Multiple Analysis') do |m| + m.scale "complete", @ds + @scales.times {|s| + m.scale "scale_#{s}", @ds.clone(@items_per_scale.times.map {|i| "#{s}_#{i}"}), {:name=>"Scale #{s}"} + } + end + end + should "Retrieve correct ScaleAnalysis for whole scale" do + sa=Statsample::Reliability::ScaleAnalysis.new(@ds, :name=>"Scale complete") + assert_equal(sa.variances_mean, @msa.scale("complete").variances_mean) + end + should "Retrieve correct ScaleAnalysis for each scale" do + @scales.times {|s| + sa=Statsample::Reliability::ScaleAnalysis.new(@ds.dup(@items_per_scale.times.map {|i| "#{s}_#{i}"}), :name=>"Scale #{s}") + assert_equal(sa.variances_mean,@msa.scale("scale_#{s}").variances_mean) + } + end + should "retrieve correct correlation matrix for each scale" do + vectors={'complete'=>@ds.vector_sum} + + @scales.times {|s| + vectors["scale_#{s}"]=@ds.dup(@items_per_scale.times.map {|i| "#{s}_#{i}"}).vector_sum + } + ds2=vectors.to_dataset + assert_equal(Statsample::Bivariate.correlation_matrix(ds2), @msa.correlation_matrix) + end + should "delete scale using delete_scale" do + @msa.delete_scale("complete") + assert_equal(@msa.scales.keys.sort, @scales.times.map {|s| "scale_#{s}"}) + end + should "retrieve pca for scales" do + @msa.delete_scale("complete") + vectors=Hash.new + @scales.times {|s| + vectors["scale_#{s}"]=@ds.dup(@items_per_scale.times.map {|i| "#{s}_#{i}"}).vector_sum + } + ds2=vectors.to_dataset + cor_matrix=Statsample::Bivariate.correlation_matrix(ds2) + m=3 + pca=Statsample::Factor::PCA.new(cor_matrix, :m=>m) + assert_equal(pca.component_matrix, @msa.pca(:m=>m).component_matrix) + end + should "retrieve acceptable summary" do + @msa.delete_scale("scale_0") + @msa.delete_scale("scale_1") + @msa.delete_scale("scale_2") + + + #@msa.summary_correlation_matrix=true + #@msa.summary_pca=true + assert(@msa.summary.size>0) + end + end + context Statsample::Reliability::ScaleAnalysis do setup do - @x1=[1,1,1,1,2,2,2,2,3,3,3,30].to_vector(:scale) - @x2=[1,1,1,2,2,3,3,3,3,4,4,50].to_vector(:scale) - @x3=[2,2,1,1,1,2,2,2,3,4,5,40].to_vector(:scale) - @x4=[1,2,3,4,4,4,4,3,4,4,5,30].to_vector(:scale) + @x1=[1,1,1,1,2,2,2,2,3,3,3,30].to_scale + @x2=[1,1,1,2,2,3,3,3,3,4,4,50].to_scale + @x3=[2,2,1,1,1,2,2,2,3,4,5,40].to_scale + @x4=[1,2,3,4,4,4,4,3,4,4,5,30].to_scale @ds={'x1'=>@x1,'x2'=>@x2,'x3'=>@x3,'x4'=>@x4}.to_dataset - @ia=Statsample::Reliability::ItemAnalysis.new(@ds) + @ia=Statsample::Reliability::ScaleAnalysis.new(@ds) @cov_matrix=Statsample::Bivariate.covariance_matrix(@ds) end should "return correct values for item analysis" do assert_in_delta(0.980,@ia.alpha,0.001) assert_in_delta(0.999,@ia.alpha_standarized,0.001)