statsample 1.4.1 → 1.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/History.txt +4 -0
- data/README.md +4 -0
- data/lib/statsample/converter/csv.rb +41 -54
- data/lib/statsample/converters.rb +18 -19
- data/lib/statsample/version.rb +1 -1
- data/test/fixtures/scientific_notation.csv +4 -0
- data/test/helpers_tests.rb +37 -38
- data/test/test_analysis.rb +96 -97
- data/test/test_anova_contrast.rb +22 -22
- data/test/test_anovaoneway.rb +12 -12
- data/test/test_anovatwoway.rb +16 -17
- data/test/test_anovatwowaywithdataset.rb +22 -24
- data/test/test_anovawithvectors.rb +67 -69
- data/test/test_awesome_print_bug.rb +9 -9
- data/test/test_bartlettsphericity.rb +13 -13
- data/test/test_bivariate.rb +122 -126
- data/test/test_codification.rb +51 -49
- data/test/test_crosstab.rb +44 -40
- data/test/test_csv.rb +52 -70
- data/test/test_dataset.rb +347 -330
- data/test/test_dominance_analysis.rb +22 -24
- data/test/test_factor.rb +163 -166
- data/test/test_factor_map.rb +25 -30
- data/test/test_factor_pa.rb +28 -28
- data/test/test_ggobi.rb +19 -18
- data/test/test_gsl.rb +13 -15
- data/test/test_histogram.rb +74 -77
- data/test/test_matrix.rb +29 -31
- data/test/test_multiset.rb +132 -126
- data/test/test_regression.rb +143 -149
- data/test/test_reliability.rb +149 -155
- data/test/test_reliability_icc.rb +100 -104
- data/test/test_reliability_skillscale.rb +38 -40
- data/test/test_resample.rb +14 -12
- data/test/test_rserve_extension.rb +33 -33
- data/test/test_srs.rb +5 -5
- data/test/test_statistics.rb +52 -50
- data/test/test_stest.rb +27 -28
- data/test/test_stratified.rb +10 -10
- data/test/test_test_f.rb +17 -17
- data/test/test_test_kolmogorovsmirnov.rb +21 -21
- data/test/test_test_t.rb +52 -52
- data/test/test_umannwhitney.rb +16 -16
- data/test/test_vector.rb +419 -410
- data/test/test_wilcoxonsignedrank.rb +60 -63
- data/test/test_xls.rb +41 -41
- metadata +55 -5
- data/web/Rakefile +0 -39
@@ -1,41 +1,39 @@
|
|
1
|
-
require(File.expand_path(File.dirname(__FILE__)+'/helpers_tests.rb'))
|
2
|
-
class StatsampleDominanceAnalysisTestCase <
|
1
|
+
require(File.expand_path(File.dirname(__FILE__) + '/helpers_tests.rb'))
|
2
|
+
class StatsampleDominanceAnalysisTestCase < Minitest::Test
|
3
3
|
def test_dominance_univariate
|
4
4
|
# Example from Budescu (1993)
|
5
|
-
m=Matrix[[1, 0.683, 0.154, 0.460, 0.618],[0.683, 1, -0.050, 0.297, 0.461], [0.154, -0.050, 1, 0.006, 0.262],[0.460, 0.297, 0.006, 1, 0.507],[0.618, 0.461, 0.262, 0.507, 1]]
|
5
|
+
m = Matrix[[1, 0.683, 0.154, 0.460, 0.618], [0.683, 1, -0.050, 0.297, 0.461], [0.154, -0.050, 1, 0.006, 0.262], [0.460, 0.297, 0.006, 1, 0.507], [0.618, 0.461, 0.262, 0.507, 1]]
|
6
6
|
m.extend Statsample::CovariateMatrix
|
7
|
-
m.fields
|
8
|
-
da=Statsample::DominanceAnalysis.new(m,'y')
|
7
|
+
m.fields = %w(x1 x2 x3 x4 y)
|
8
|
+
da = Statsample::DominanceAnalysis.new(m, 'y')
|
9
9
|
|
10
|
-
contr_x1={'x2'=>0.003, 'x3'=>0.028, 'x4'=>0.063}
|
11
|
-
contr_x1.each do |k,v|
|
10
|
+
contr_x1 = { 'x2' => 0.003, 'x3' => 0.028, 'x4' => 0.063 }
|
11
|
+
contr_x1.each do |k, v|
|
12
12
|
assert_in_delta(v, da.models_data[['x1']].contributions[k], 0.001)
|
13
13
|
end
|
14
|
-
assert_in_delta(0.052, da.models_data[
|
15
|
-
expected_dominances=[1, 1, 0.5, 0.5, 0,0]
|
16
|
-
expected_g_dominances=[1, 1, 1, 1, 0,0]
|
14
|
+
assert_in_delta(0.052, da.models_data[%w(x2 x3 x4)].contributions['x1'], 0.001)
|
15
|
+
expected_dominances = [1, 1, 0.5, 0.5, 0, 0]
|
16
|
+
expected_g_dominances = [1, 1, 1, 1, 0, 0]
|
17
17
|
|
18
|
-
da.pairs.each_with_index do |a,i|
|
19
|
-
assert_equal(expected_dominances[i], da.total_dominance_pairwise(a[0],a[1]))
|
20
|
-
assert_equal(expected_dominances[i], da.conditional_dominance_pairwise(a[0],a[1]))
|
21
|
-
assert_equal(expected_g_dominances[i], da.general_dominance_pairwise(a[0],a[1]))
|
18
|
+
da.pairs.each_with_index do |a, i|
|
19
|
+
assert_equal(expected_dominances[i], da.total_dominance_pairwise(a[0], a[1]))
|
20
|
+
assert_equal(expected_dominances[i], da.conditional_dominance_pairwise(a[0], a[1]))
|
21
|
+
assert_equal(expected_g_dominances[i], da.general_dominance_pairwise(a[0], a[1]))
|
22
22
|
end
|
23
|
-
assert(da.summary.size>0)
|
23
|
+
assert(da.summary.size > 0)
|
24
24
|
end
|
25
|
+
|
25
26
|
def test_dominance_multivariate
|
26
|
-
m=Matrix[[1.0, -0.19, -0.358, -0.343, 0.359, 0.257], [-0.19, 1.0, 0.26, 0.29, -0.11, -0.11], [-0.358, 0.26, 1.0, 0.54, -0.49, -0.23], [-0.343, 0.29, 0.54, 1.0, -0.22, -0.41], [0.359, -0.11, -0.49, -0.22, 1.0, 0.62], [0.257, -0.11, -0.23, -0.41, 0.62, 1]]
|
27
|
+
m = Matrix[[1.0, -0.19, -0.358, -0.343, 0.359, 0.257], [-0.19, 1.0, 0.26, 0.29, -0.11, -0.11], [-0.358, 0.26, 1.0, 0.54, -0.49, -0.23], [-0.343, 0.29, 0.54, 1.0, -0.22, -0.41], [0.359, -0.11, -0.49, -0.22, 1.0, 0.62], [0.257, -0.11, -0.23, -0.41, 0.62, 1]]
|
27
28
|
m.extend Statsample::CovariateMatrix
|
28
|
-
m.fields
|
29
|
-
m2=m.submatrix(%w
|
30
|
-
|
29
|
+
m.fields = %w(y1 y2 x1 x2 x3 x4)
|
30
|
+
m2 = m.submatrix(%w(y1 x1 x2 x3 x4))
|
31
31
|
|
32
|
-
da=Statsample::DominanceAnalysis.new(m,
|
32
|
+
da = Statsample::DominanceAnalysis.new(m, %w(y1 y2), cases: 683, method_association: :p2yx)
|
33
33
|
|
34
|
-
contr_x1={'x2'=>0.027, 'x3'=>0.024, 'x4'=>0.017}
|
35
|
-
contr_x1.each do |k,v|
|
34
|
+
contr_x1 = { 'x2' => 0.027, 'x3' => 0.024, 'x4' => 0.017 }
|
35
|
+
contr_x1.each do |k, v|
|
36
36
|
assert_in_delta(v, da.models_data[['x1']].contributions[k], 0.003)
|
37
37
|
end
|
38
|
-
|
39
|
-
|
40
38
|
end
|
41
39
|
end
|
data/test/test_factor.rb
CHANGED
@@ -1,222 +1,219 @@
|
|
1
|
-
require(File.expand_path(File.dirname(__FILE__)+'/helpers_tests.rb'))
|
2
|
-
#require 'rserve'
|
3
|
-
#require 'statsample/rserve_extension'
|
1
|
+
require(File.expand_path(File.dirname(__FILE__) + '/helpers_tests.rb'))
|
2
|
+
# require 'rserve'
|
3
|
+
# require 'statsample/rserve_extension'
|
4
4
|
|
5
|
-
class StatsampleFactorTestCase <
|
5
|
+
class StatsampleFactorTestCase < Minitest::Test
|
6
6
|
include Statsample::Fixtures
|
7
7
|
# Based on Hardle and Simar
|
8
8
|
def setup
|
9
|
-
@fixtures_dir=File.expand_path(File.dirname(__FILE__)+
|
9
|
+
@fixtures_dir = File.expand_path(File.dirname(__FILE__) + '/fixtures')
|
10
10
|
end
|
11
11
|
# Based on Hurdle example
|
12
12
|
def test_covariance_matrix
|
13
|
-
ds=Statsample::PlainText.read(@fixtures_dir+
|
13
|
+
ds = Statsample::PlainText.read(@fixtures_dir + '/bank2.dat', %w(v1 v2 v3 v4 v5 v6))
|
14
14
|
ds.fields.each {|f|
|
15
|
-
ds[f]=ds[f].centered
|
15
|
+
ds[f] = ds[f].centered
|
16
16
|
}
|
17
|
-
cm=ds.covariance_matrix
|
18
|
-
pca =Statsample::Factor::PCA.new(
|
19
|
-
#puts pca.summary
|
20
|
-
#puts pca.feature_matrix
|
21
|
-
exp_eig=[2.985, 0.931,0.242, 0.194, 0.085, 0.035].to_scale
|
17
|
+
cm = ds.covariance_matrix
|
18
|
+
pca = Statsample::Factor::PCA.new(cm, m: 6)
|
19
|
+
# puts pca.summary
|
20
|
+
# puts pca.feature_matrix
|
21
|
+
exp_eig = [2.985, 0.931, 0.242, 0.194, 0.085, 0.035].to_scale
|
22
22
|
assert_similar_vector(exp_eig, pca.eigenvalues.to_scale, 0.1)
|
23
|
-
pcs=pca.principal_components(ds)
|
24
|
-
k=6
|
25
|
-
comp_matrix=pca.component_matrix
|
23
|
+
pcs = pca.principal_components(ds)
|
24
|
+
k = 6
|
25
|
+
comp_matrix = pca.component_matrix
|
26
26
|
k.times {|i|
|
27
|
-
pc_id="PC_#{i+1}"
|
27
|
+
pc_id = "PC_#{i + 1}"
|
28
28
|
k.times {|j| # variable
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
ds_id = "v#{j + 1}"
|
30
|
+
r = Statsample::Bivariate.correlation(ds[ds_id], pcs[pc_id])
|
31
|
+
assert_in_delta(r, comp_matrix[j, i])
|
32
|
+
}
|
33
33
|
}
|
34
|
-
|
35
34
|
end
|
35
|
+
|
36
36
|
def test_principalcomponents_ruby_gsl
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
v={}
|
45
|
-
v["x0"]=samples.times.map { ran.call()}.to_scale.centered
|
37
|
+
ran = Distribution::Normal.rng
|
38
|
+
|
39
|
+
# @r=::Rserve::Connection.new
|
40
|
+
|
41
|
+
samples = 20
|
42
|
+
[3, 5, 7].each {|k|
|
43
|
+
v = {}
|
44
|
+
v['x0'] = samples.times.map { ran.call }.to_scale.centered
|
46
45
|
(1...k).each {|i|
|
47
|
-
v["x#{i}"]=samples.times.map {|ii| ran.call
|
46
|
+
v["x#{i}"] = samples.times.map { |ii| ran.call * 0.5 + v["x#{i - 1}"][ii] * 0.5 }.to_scale.centered
|
48
47
|
}
|
49
|
-
|
50
|
-
ds=v.to_dataset
|
51
|
-
cm=ds.covariance_matrix
|
52
|
-
# @r.assign('ds',ds)
|
53
|
-
# @r.eval('cm<-cor(ds);sm<-eigen(cm, sym=TRUE);v<-sm$vectors')
|
54
|
-
# puts "eigenvalues"
|
55
|
-
# puts @r.eval('v').to_ruby.to_s
|
56
|
-
pca_ruby=Statsample::Factor::PCA.new(
|
57
|
-
pca_gsl =Statsample::Factor::PCA.new(
|
48
|
+
|
49
|
+
ds = v.to_dataset
|
50
|
+
cm = ds.covariance_matrix
|
51
|
+
# @r.assign('ds',ds)
|
52
|
+
# @r.eval('cm<-cor(ds);sm<-eigen(cm, sym=TRUE);v<-sm$vectors')
|
53
|
+
# puts "eigenvalues"
|
54
|
+
# puts @r.eval('v').to_ruby.to_s
|
55
|
+
pca_ruby = Statsample::Factor::PCA.new(cm, m: k, use_gsl: false)
|
56
|
+
pca_gsl = Statsample::Factor::PCA.new(cm, m: k, use_gsl: true)
|
58
57
|
pc_ruby = pca_ruby.principal_components(ds)
|
59
58
|
pc_gsl = pca_gsl.principal_components(ds)
|
60
59
|
# Test component matrix correlation!
|
61
|
-
cm_ruby=pca_ruby.component_matrix
|
62
|
-
#puts cm_ruby.summary
|
60
|
+
cm_ruby = pca_ruby.component_matrix
|
61
|
+
# puts cm_ruby.summary
|
63
62
|
k.times {|i|
|
64
|
-
pc_id="PC_#{i+1}"
|
65
|
-
assert_in_delta(pca_ruby.eigenvalues[i], pca_gsl.eigenvalues[i],1e-10)
|
63
|
+
pc_id = "PC_#{i + 1}"
|
64
|
+
assert_in_delta(pca_ruby.eigenvalues[i], pca_gsl.eigenvalues[i], 1e-10)
|
66
65
|
# Revert gsl component values
|
67
|
-
pc_gsl_data= (pc_gsl[pc_id][0]-pc_ruby[pc_id][0]).abs>1e-6 ? pc_gsl[pc_id].recode
|
68
|
-
assert_similar_vector(pc_gsl_data, pc_ruby[pc_id], 1e-6,"PC for #{k} variables")
|
66
|
+
pc_gsl_data = (pc_gsl[pc_id][0] - pc_ruby[pc_id][0]).abs > 1e-6 ? pc_gsl[pc_id].recode(&:-@) : pc_gsl[pc_id]
|
67
|
+
assert_similar_vector(pc_gsl_data, pc_ruby[pc_id], 1e-6, "PC for #{k} variables")
|
69
68
|
if false
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
69
|
+
k.times {|j| # variable
|
70
|
+
ds_id = "x#{j}"
|
71
|
+
r = Statsample::Bivariate.correlation(ds[ds_id], pc_ruby[pc_id])
|
72
|
+
puts "#{pc_id}-#{ds_id}:#{r}"
|
73
|
+
}
|
75
74
|
end
|
76
75
|
}
|
77
76
|
}
|
78
|
-
|
77
|
+
# @r.close
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_principalcomponents
|
81
|
+
principalcomponents(true)
|
82
|
+
principalcomponents(false)
|
79
83
|
end
|
80
|
-
|
81
|
-
principalcomponents(true)
|
82
|
-
principalcomponents(false)
|
83
|
-
|
84
|
-
end
|
84
|
+
|
85
85
|
def principalcomponents(gsl)
|
86
|
-
ran=Distribution::Normal.rng
|
87
|
-
samples=50
|
88
|
-
x1=samples.times.map { ran.call
|
89
|
-
x2=samples.times.map {|i| ran.call
|
90
|
-
ds={'x1'=>x1,'x2'=>x2}.to_dataset
|
91
|
-
|
92
|
-
cm=ds.correlation_matrix
|
93
|
-
r=cm[0,1]
|
94
|
-
pca=Statsample::Factor::PCA.new(cm
|
95
|
-
assert_in_delta(1+r,pca.eigenvalues[0],1e-10)
|
96
|
-
assert_in_delta(1-r,pca.eigenvalues[1],1e-10)
|
97
|
-
hs=1.0 / Math.sqrt(2)
|
98
|
-
assert_equal_vector(Vector[1, 1]*hs, pca.eigenvectors[0])
|
99
|
-
m_1=gsl ? Vector[-1,1] : Vector[1
|
100
|
-
|
101
|
-
assert_equal_vector(hs*m_1, pca.eigenvectors[1])
|
102
|
-
|
103
|
-
pcs=pca.principal_components(ds)
|
104
|
-
exp_pc_1=ds.collect_with_index {|row,i|
|
105
|
-
hs*(row['x1']+row['x2'])
|
106
|
-
}
|
107
|
-
exp_pc_2=ds.collect_with_index {|row,i|
|
108
|
-
gsl ? hs*(row['x2']-row['x1']) : hs*(row['x1']-row['x2'])
|
86
|
+
ran = Distribution::Normal.rng
|
87
|
+
samples = 50
|
88
|
+
x1 = samples.times.map { ran.call }.to_scale
|
89
|
+
x2 = samples.times.map { |i| ran.call * 0.5 + x1[i] * 0.5 }.to_scale
|
90
|
+
ds = { 'x1' => x1, 'x2' => x2 }.to_dataset
|
91
|
+
|
92
|
+
cm = ds.correlation_matrix
|
93
|
+
r = cm[0, 1]
|
94
|
+
pca = Statsample::Factor::PCA.new(cm, m: 2, use_gsl: gsl)
|
95
|
+
assert_in_delta(1 + r, pca.eigenvalues[0], 1e-10)
|
96
|
+
assert_in_delta(1 - r, pca.eigenvalues[1], 1e-10)
|
97
|
+
hs = 1.0 / Math.sqrt(2)
|
98
|
+
assert_equal_vector(Vector[1, 1] * hs, pca.eigenvectors[0])
|
99
|
+
m_1 = gsl ? Vector[-1, 1] : Vector[1, -1]
|
109
100
|
|
101
|
+
assert_equal_vector(hs * m_1, pca.eigenvectors[1])
|
102
|
+
|
103
|
+
pcs = pca.principal_components(ds)
|
104
|
+
exp_pc_1 = ds.collect_with_index {|row, _i|
|
105
|
+
hs * (row['x1'] + row['x2'])
|
106
|
+
}
|
107
|
+
exp_pc_2 = ds.collect_with_index {|row, _i|
|
108
|
+
gsl ? hs * (row['x2'] - row['x1']) : hs * (row['x1'] - row['x2'])
|
110
109
|
}
|
111
|
-
assert_similar_vector(exp_pc_1, pcs[
|
112
|
-
assert_similar_vector(exp_pc_2, pcs[
|
110
|
+
assert_similar_vector(exp_pc_1, pcs['PC_1'])
|
111
|
+
assert_similar_vector(exp_pc_2, pcs['PC_2'])
|
113
112
|
end
|
113
|
+
|
114
114
|
def test_antiimage
|
115
|
-
cor=Matrix[[1,0.964, 0.312],[0.964,1,0.411],[0.312,0.411,1]]
|
116
|
-
expected=Matrix[[0.062
|
117
|
-
ai=Statsample::Factor.anti_image_covariance_matrix(cor)
|
118
|
-
assert(Matrix.equal_in_delta?(expected, ai, 0.01), "#{expected
|
115
|
+
cor = Matrix[[1, 0.964, 0.312], [0.964, 1, 0.411], [0.312, 0.411, 1]]
|
116
|
+
expected = Matrix[[0.062, -0.057, 0.074], [-0.057, 0.057, -0.089], [0.074, -0.089, 0.729]]
|
117
|
+
ai = Statsample::Factor.anti_image_covariance_matrix(cor)
|
118
|
+
assert(Matrix.equal_in_delta?(expected, ai, 0.01), "#{expected} not equal to #{ai}")
|
119
119
|
end
|
120
|
+
|
120
121
|
def test_kmo
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
122
|
+
@v1 = [1, 2, 3, 4, 7, 8, 9, 10, 14, 15, 20, 50, 60, 70].to_scale
|
123
|
+
@v2 = [5, 6, 11, 12, 13, 16, 17, 18, 19, 20, 30, 0, 0, 0].to_scale
|
124
|
+
@v3 = [10, 3, 20, 30, 40, 50, 80, 10, 20, 30, 40, 2, 3, 4].to_scale
|
125
|
+
# KMO: 0.490
|
126
|
+
ds = { 'v1' => @v1, 'v2' => @v2, 'v3' => @v3 }.to_dataset
|
127
|
+
cor = Statsample::Bivariate.correlation_matrix(ds)
|
128
|
+
kmo = Statsample::Factor.kmo(cor)
|
129
|
+
assert_in_delta(0.667, kmo, 0.001)
|
130
|
+
assert_in_delta(0.81, Statsample::Factor.kmo(harman_817), 0.01)
|
131
131
|
end
|
132
|
+
|
132
133
|
def test_kmo_univariate
|
133
|
-
m=harman_817
|
134
|
-
expected=[0.73,0.76,0.84,0.87,0.53,0.93,0.78,0.86]
|
134
|
+
m = harman_817
|
135
|
+
expected = [0.73, 0.76, 0.84, 0.87, 0.53, 0.93, 0.78, 0.86]
|
135
136
|
m.row_size.times.map {|i|
|
136
|
-
assert_in_delta(expected[i], Statsample::Factor.kmo_univariate(m,i),0.01)
|
137
|
+
assert_in_delta(expected[i], Statsample::Factor.kmo_univariate(m, i), 0.01)
|
137
138
|
}
|
138
139
|
end
|
139
140
|
# Tested with SPSS and R
|
140
141
|
def test_pca
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
142
|
+
a = [2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1].to_scale
|
143
|
+
b = [2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9].to_scale
|
144
|
+
a.recode! { |c| c - a.mean }
|
145
|
+
b.recode! { |c| c - b.mean }
|
146
|
+
ds = { 'a' => a, 'b' => b }.to_dataset
|
147
|
+
cov_matrix = Statsample::Bivariate.covariance_matrix(ds)
|
148
|
+
if Statsample.has_gsl?
|
149
|
+
pca = Statsample::Factor::PCA.new(cov_matrix, use_gsl: true)
|
150
|
+
pca_set(pca, 'gsl')
|
151
|
+
else
|
152
|
+
skip('Eigenvalues could be calculated with GSL (requires gsl)')
|
153
|
+
end
|
154
|
+
pca = Statsample::Factor::PCA.new(cov_matrix, use_gsl: false)
|
155
|
+
pca_set(pca, 'ruby')
|
155
156
|
end
|
156
|
-
def pca_set(pca,type)
|
157
|
-
expected_eigenvalues=[1.284, 0.0490]
|
158
|
-
expected_eigenvalues.each_with_index{|ev,i|
|
159
|
-
assert_in_delta(ev,pca.eigenvalues[i],0.001)
|
160
|
-
}
|
161
|
-
expected_communality=[0.590, 0.694]
|
162
|
-
expected_communality.each_with_index{|ev,i|
|
163
|
-
assert_in_delta(ev,pca.communalities[i],0.001)
|
164
|
-
}
|
165
|
-
expected_cm=[0.768, 0.833]
|
166
|
-
obs=pca.component_matrix_correlation(1).column(0).to_a
|
167
|
-
expected_cm.each_with_index{|ev,i|
|
168
|
-
assert_in_delta(ev,obs[i],0.001)
|
169
|
-
}
|
170
157
|
|
171
|
-
|
158
|
+
def pca_set(pca, _type)
|
159
|
+
expected_eigenvalues = [1.284, 0.0490]
|
160
|
+
expected_eigenvalues.each_with_index{|ev, i|
|
161
|
+
assert_in_delta(ev, pca.eigenvalues[i], 0.001)
|
162
|
+
}
|
163
|
+
expected_communality = [0.590, 0.694]
|
164
|
+
expected_communality.each_with_index{|ev, i|
|
165
|
+
assert_in_delta(ev, pca.communalities[i], 0.001)
|
166
|
+
}
|
167
|
+
expected_cm = [0.768, 0.833]
|
168
|
+
obs = pca.component_matrix_correlation(1).column(0).to_a
|
169
|
+
expected_cm.each_with_index{|ev, i|
|
170
|
+
assert_in_delta(ev, obs[i], 0.001)
|
171
|
+
}
|
172
|
+
|
173
|
+
assert(pca.summary)
|
172
174
|
end
|
173
175
|
|
174
176
|
# Tested with R
|
175
177
|
def test_principalaxis
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
fa=Statsample::Factor::PrincipalAxis.new(matrix,:m=>1, :max_iterations=>50)
|
181
|
-
|
182
|
-
cm=::Matrix[[0.923],[0.912],[0.507],[0.483]]
|
183
|
-
|
184
|
-
assert_equal_matrix(cm,fa.component_matrix,0.001)
|
185
|
-
|
186
|
-
h2=[0.852,0.832,0.257,0.233]
|
187
|
-
h2.each_with_index{|ev,i|
|
188
|
-
assert_in_delta(ev,fa.communalities[i],0.001)
|
189
|
-
}
|
190
|
-
eigen1=2.175
|
191
|
-
assert_in_delta(eigen1, fa.eigenvalues[0],0.001)
|
192
|
-
assert(fa.summary.size>0)
|
193
|
-
fa=Statsample::Factor::PrincipalAxis.new(matrix,:smc=>false)
|
194
|
-
|
195
|
-
assert_raise RuntimeError do
|
196
|
-
fa.iterate
|
197
|
-
end
|
178
|
+
matrix = ::Matrix[
|
179
|
+
[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]]
|
198
180
|
|
199
|
-
|
181
|
+
fa = Statsample::Factor::PrincipalAxis.new(matrix, m: 1, max_iterations: 50)
|
200
182
|
|
183
|
+
cm = ::Matrix[[0.923], [0.912], [0.507], [0.483]]
|
184
|
+
|
185
|
+
assert_equal_matrix(cm, fa.component_matrix, 0.001)
|
186
|
+
|
187
|
+
h2 = [0.852, 0.832, 0.257, 0.233]
|
188
|
+
h2.each_with_index{|ev, i|
|
189
|
+
assert_in_delta(ev, fa.communalities[i], 0.001)
|
190
|
+
}
|
191
|
+
eigen1 = 2.175
|
192
|
+
assert_in_delta(eigen1, fa.eigenvalues[0], 0.001)
|
193
|
+
assert(fa.summary.size > 0)
|
194
|
+
fa = Statsample::Factor::PrincipalAxis.new(matrix, smc: false)
|
195
|
+
|
196
|
+
assert_raise RuntimeError do
|
197
|
+
fa.iterate
|
198
|
+
end
|
199
|
+
end
|
201
200
|
|
202
201
|
def test_rotation_varimax
|
203
|
-
a = Matrix[
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
expected= Matrix[[-0.0204423, 0.938674, -0.340334],
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
varimax=Statsample::Factor::Varimax.new(a)
|
202
|
+
a = Matrix[[0.4320, 0.8129, 0.3872],
|
203
|
+
[0.7950, -0.5416, 0.2565],
|
204
|
+
[0.5944, 0.7234, -0.3441],
|
205
|
+
[0.8945, -0.3921, -0.1863]]
|
206
|
+
|
207
|
+
expected = Matrix[[-0.0204423, 0.938674, -0.340334],
|
208
|
+
[0.983662, 0.0730206, 0.134997],
|
209
|
+
[0.0826106, 0.435975, -0.893379],
|
210
|
+
[0.939901, -0.0965213, -0.309596]]
|
211
|
+
varimax = Statsample::Factor::Varimax.new(a)
|
213
212
|
assert(!varimax.rotated.nil?, "Rotated shouldn't be empty")
|
214
213
|
assert(!varimax.component_transformation_matrix.nil?, "Component matrix shouldn't be empty")
|
215
214
|
assert(!varimax.h2.nil?, "H2 shouldn't be empty")
|
216
|
-
|
217
|
-
assert_equal_matrix(expected,varimax.rotated,1e-6)
|
218
|
-
assert(varimax.summary.size>0)
|
219
|
-
end
|
220
|
-
|
221
215
|
|
216
|
+
assert_equal_matrix(expected, varimax.rotated, 1e-6)
|
217
|
+
assert(varimax.summary.size > 0)
|
218
|
+
end
|
222
219
|
end
|
data/test/test_factor_map.rb
CHANGED
@@ -1,43 +1,38 @@
|
|
1
|
-
require(File.expand_path(File.dirname(__FILE__)+'/helpers_tests.rb'))
|
2
|
-
#require 'rserve'
|
3
|
-
#require 'statsample/rserve_extension'
|
1
|
+
require(File.expand_path(File.dirname(__FILE__) + '/helpers_tests.rb'))
|
2
|
+
# require 'rserve'
|
3
|
+
# require 'statsample/rserve_extension'
|
4
4
|
|
5
|
-
class StatsampleFactorMpaTestCase <
|
5
|
+
class StatsampleFactorMpaTestCase < Minitest::Test
|
6
6
|
context Statsample::Factor::MAP do
|
7
7
|
setup do
|
8
|
-
m=Matrix[
|
9
|
-
[
|
10
|
-
[
|
11
|
-
[
|
12
|
-
[
|
13
|
-
[
|
14
|
-
[
|
15
|
-
[
|
16
|
-
[
|
8
|
+
m = Matrix[
|
9
|
+
[1, 0.846, 0.805, 0.859, 0.473, 0.398, 0.301, 0.382],
|
10
|
+
[0.846, 1, 0.881, 0.826, 0.376, 0.326, 0.277, 0.415],
|
11
|
+
[0.805, 0.881, 1, 0.801, 0.38, 0.319, 0.237, 0.345],
|
12
|
+
[0.859, 0.826, 0.801, 1, 0.436, 0.329, 0.327, 0.365],
|
13
|
+
[0.473, 0.376, 0.38, 0.436, 1, 0.762, 0.73, 0.629],
|
14
|
+
[0.398, 0.326, 0.319, 0.329, 0.762, 1, 0.583, 0.577],
|
15
|
+
[0.301, 0.277, 0.237, 0.327, 0.73, 0.583, 1, 0.539],
|
16
|
+
[0.382, 0.415, 0.345, 0.365, 0.629, 0.577, 0.539, 1]
|
17
17
|
]
|
18
|
-
@map=Statsample::Factor::MAP.new(m)
|
18
|
+
@map = Statsample::Factor::MAP.new(m)
|
19
19
|
end
|
20
|
-
should
|
21
|
-
@map.use_gsl=false
|
20
|
+
should 'return correct values with pure ruby' do
|
21
|
+
@map.use_gsl = false
|
22
22
|
map_assertions(@map)
|
23
23
|
end
|
24
|
-
should_with_gsl
|
25
|
-
#require 'ruby-prof'
|
24
|
+
should_with_gsl 'return correct values with gsl' do
|
25
|
+
# require 'ruby-prof'
|
26
26
|
|
27
|
-
@map.use_gsl=true
|
28
|
-
map_assertions(@map)
|
27
|
+
@map.use_gsl = true
|
28
|
+
map_assertions(@map)
|
29
29
|
end
|
30
|
-
|
31
|
-
|
32
30
|
end
|
33
|
-
|
31
|
+
|
34
32
|
def map_assertions(map)
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
assert_in_delta(map.minfm, 0.066445, 0.00001)
|
34
|
+
assert_equal(map.number_of_factors, 2)
|
35
|
+
assert_in_delta(map.fm[0], 0.312475, 0.00001)
|
36
|
+
assert_in_delta(map.fm[1], 0.245121, 0.00001)
|
39
37
|
end
|
40
|
-
|
41
|
-
|
42
38
|
end
|
43
|
-
|