statsample 1.4.3 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +8 -0
- data/benchmarks/correlation_matrix_15_variables.rb +1 -1
- data/benchmarks/correlation_matrix_5_variables.rb +1 -1
- data/benchmarks/correlation_matrix_methods/correlation_matrix.rb +2 -2
- data/examples/dataset.rb +2 -2
- data/examples/icc.rb +1 -1
- data/examples/levene.rb +2 -2
- data/examples/parallel_analysis.rb +1 -1
- data/examples/u_test.rb +2 -2
- data/examples/vector.rb +1 -1
- data/examples/velicer_map_test.rb +1 -1
- data/lib/statsample.rb +30 -4
- data/lib/statsample/anova/oneway.rb +3 -3
- data/lib/statsample/anova/twoway.rb +3 -3
- data/lib/statsample/bivariate.rb +7 -7
- data/lib/statsample/bivariate/pearson.rb +2 -2
- data/lib/statsample/converter/csv.rb +1 -1
- data/lib/statsample/converters.rb +7 -7
- data/lib/statsample/dataset.rb +8 -8
- data/lib/statsample/dominanceanalysis.rb +4 -4
- data/lib/statsample/dominanceanalysis/bootstrap.rb +8 -8
- data/lib/statsample/factor.rb +2 -4
- data/lib/statsample/factor/map.rb +2 -1
- data/lib/statsample/factor/parallelanalysis.rb +2 -2
- data/lib/statsample/factor/pca.rb +2 -2
- data/lib/statsample/factor/principalaxis.rb +2 -2
- data/lib/statsample/graph/boxplot.rb +4 -4
- data/lib/statsample/graph/histogram.rb +2 -2
- data/lib/statsample/graph/scatterplot.rb +4 -4
- data/lib/statsample/matrix.rb +20 -6
- data/lib/statsample/regression.rb +2 -2
- data/lib/statsample/regression/multiple.rb +3 -3
- data/lib/statsample/regression/multiple/alglibengine.rb +5 -5
- data/lib/statsample/regression/multiple/baseengine.rb +3 -3
- data/lib/statsample/regression/multiple/gslengine.rb +5 -5
- data/lib/statsample/regression/multiple/rubyengine.rb +4 -4
- data/lib/statsample/reliability/icc.rb +1 -1
- data/lib/statsample/reliability/multiscaleanalysis.rb +4 -4
- data/lib/statsample/reliability/scaleanalysis.rb +6 -6
- data/lib/statsample/reliability/skillscaleanalysis.rb +1 -1
- data/lib/statsample/resample.rb +1 -1
- data/lib/statsample/shorthand.rb +1 -1
- data/lib/statsample/test/bartlettsphericity.rb +1 -1
- data/lib/statsample/test/levene.rb +4 -4
- data/lib/statsample/test/t.rb +3 -3
- data/lib/statsample/test/umannwhitney.rb +2 -2
- data/lib/statsample/vector.rb +103 -80
- data/lib/statsample/vector/gsl.rb +16 -16
- data/lib/statsample/version.rb +1 -1
- data/test/test_analysis.rb +1 -1
- data/test/test_anova_contrast.rb +4 -4
- data/test/test_anovatwowaywithdataset.rb +1 -1
- data/test/test_anovawithvectors.rb +6 -6
- data/test/test_awesome_print_bug.rb +1 -1
- data/test/test_bartlettsphericity.rb +3 -3
- data/test/test_bivariate.rb +38 -38
- data/test/test_crosstab.rb +2 -2
- data/test/test_csv.rb +6 -6
- data/test/test_dataset.rb +79 -79
- data/test/test_factor.rb +55 -49
- data/test/test_factor_pa.rb +4 -4
- data/test/test_ggobi.rb +3 -3
- data/test/test_gsl.rb +3 -3
- data/test/test_histogram.rb +3 -3
- data/test/test_matrix.rb +5 -5
- data/test/test_multiset.rb +19 -19
- data/test/test_regression.rb +27 -27
- data/test/test_reliability.rb +14 -14
- data/test/test_reliability_icc.rb +7 -7
- data/test/test_reliability_skillscale.rb +6 -6
- data/test/test_resample.rb +1 -1
- data/test/test_rserve_extension.rb +4 -4
- data/test/test_statistics.rb +5 -5
- data/test/test_stest.rb +8 -8
- data/test/test_stratified.rb +3 -3
- data/test/test_test_t.rb +5 -5
- data/test/test_umannwhitney.rb +2 -2
- data/test/test_vector.rb +153 -119
- data/test/test_wilcoxonsignedrank.rb +4 -4
- data/test/test_xls.rb +6 -6
- metadata +3 -53
@@ -5,10 +5,10 @@ $reliability_icc = nil
|
|
5
5
|
class StatsampleReliabilityIccTestCase < Minitest::Test
|
6
6
|
context Statsample::Reliability::ICC do
|
7
7
|
setup do
|
8
|
-
a = [9, 6, 8, 7, 10, 6].
|
9
|
-
b = [2, 1, 4, 1, 5, 2].
|
10
|
-
c = [5, 3, 6, 2, 6, 4].
|
11
|
-
d = [8, 2, 8, 6, 9, 7].
|
8
|
+
a = [9, 6, 8, 7, 10, 6].to_numeric
|
9
|
+
b = [2, 1, 4, 1, 5, 2].to_numeric
|
10
|
+
c = [5, 3, 6, 2, 6, 4].to_numeric
|
11
|
+
d = [8, 2, 8, 6, 9, 7].to_numeric
|
12
12
|
@ds = { 'a' => a, 'b' => b, 'c' => c, 'd' => d }.to_dataset
|
13
13
|
@icc = Statsample::Reliability::ICC.new(@ds)
|
14
14
|
end
|
@@ -122,7 +122,7 @@ class StatsampleReliabilityIccTestCase < Minitest::Test
|
|
122
122
|
setup do
|
123
123
|
if $reliability_icc.nil?
|
124
124
|
size = 100
|
125
|
-
a = size.times.map { rand(10) }.
|
125
|
+
a = size.times.map { rand(10) }.to_numeric
|
126
126
|
b = a.recode { |i| i + rand(4) - 2 }
|
127
127
|
c = a.recode { |i| i + rand(4) - 2 }
|
128
128
|
d = a.recode { |i| i + rand(4) - 2 }
|
@@ -182,8 +182,8 @@ class StatsampleReliabilityIccTestCase < Minitest::Test
|
|
182
182
|
should 'bounds be equal' do
|
183
183
|
@icc.type = t
|
184
184
|
@r_icc = @iccs[t.to_s]
|
185
|
-
assert_in_delta(@r_icc['lbound'], @icc.lbound)
|
186
|
-
assert_in_delta(@r_icc['ubound'], @icc.ubound)
|
185
|
+
assert_in_delta(@r_icc['lbound'], @icc.lbound, 0.1)
|
186
|
+
assert_in_delta(@r_icc['ubound'], @icc.ubound, 0.1)
|
187
187
|
end
|
188
188
|
should 'summary generated' do
|
189
189
|
assert(@icc.summary.size > 0)
|
@@ -5,7 +5,7 @@ class StatsampleReliabilitySkillScaleTestCase < Minitest::Test
|
|
5
5
|
setup do
|
6
6
|
options = %w(a b c d e)
|
7
7
|
cases = 20
|
8
|
-
@id = cases.times.map { |v| v }.
|
8
|
+
@id = cases.times.map { |v| v }.to_numeric
|
9
9
|
@a = cases.times.map { options[rand(5)] }.to_vector
|
10
10
|
@b = cases.times.map { options[rand(5)] }.to_vector
|
11
11
|
@c = cases.times.map { options[rand(5)] }.to_vector
|
@@ -17,11 +17,11 @@ class StatsampleReliabilitySkillScaleTestCase < Minitest::Test
|
|
17
17
|
@ds = { 'id' => @id, 'a' => @a, 'b' => @b, 'c' => @c, 'd' => @d, 'e' => @e }.to_dataset
|
18
18
|
@key = { 'a' => 'a', 'b' => options[rand(5)], 'c' => options[rand(5)], 'd' => options[rand(5)], 'e' => options[rand(5)] }
|
19
19
|
@ssa = Statsample::Reliability::SkillScaleAnalysis.new(@ds, @key)
|
20
|
-
@ac = @a.map { |v| v == @key['a'] ? 1 : 0 }.
|
21
|
-
@bc = @b.map { |v| v == @key['b'] ? 1 : 0 }.
|
22
|
-
@cc = @c.map { |v| v == @key['c'] ? 1 : 0 }.
|
23
|
-
@dc = @d.map { |v| v == @key['d'] ? 1 : 0 }.
|
24
|
-
@ec = @e.map { |v| v.nil? ? nil : (v == @key['e'] ? 1 : 0) }.
|
20
|
+
@ac = @a.map { |v| v == @key['a'] ? 1 : 0 }.to_numeric
|
21
|
+
@bc = @b.map { |v| v == @key['b'] ? 1 : 0 }.to_numeric
|
22
|
+
@cc = @c.map { |v| v == @key['c'] ? 1 : 0 }.to_numeric
|
23
|
+
@dc = @d.map { |v| v == @key['d'] ? 1 : 0 }.to_numeric
|
24
|
+
@ec = @e.map { |v| v.nil? ? nil : (v == @key['e'] ? 1 : 0) }.to_numeric
|
25
25
|
end
|
26
26
|
should 'return proper corrected dataset' do
|
27
27
|
cds = { 'id' => @id, 'a' => @ac, 'b' => @bc, 'c' => @cc, 'd' => @dc, 'e' => @ec }.to_dataset
|
data/test/test_resample.rb
CHANGED
@@ -17,7 +17,7 @@ class StatsampleResampleTestCase < Minitest::Test
|
|
17
17
|
Statsample::Resample.generate(20, 1, 10).count(1)
|
18
18
|
}
|
19
19
|
assert_equal(400, r.size)
|
20
|
-
v = Statsample::Vector.new(r, :
|
20
|
+
v = Statsample::Vector.new(r, :numeric)
|
21
21
|
a = v.count { |x| x > 3 }
|
22
22
|
assert(a >= 30 && a <= 70)
|
23
23
|
end
|
@@ -12,7 +12,7 @@ begin
|
|
12
12
|
@r.close
|
13
13
|
end
|
14
14
|
should 'return a valid rexp for numeric vector' do
|
15
|
-
a = 100.times.map { |i| rand > 0.9 ? nil : i + rand }.
|
15
|
+
a = 100.times.map { |i| rand > 0.9 ? nil : i + rand }.to_numeric
|
16
16
|
rexp = a.to_REXP
|
17
17
|
assert(rexp.is_a? Rserve::REXP::Double)
|
18
18
|
assert_equal(rexp.to_ruby, a.data_with_nils)
|
@@ -20,9 +20,9 @@ begin
|
|
20
20
|
assert_equal(a.data_with_nils, @r.eval('a').to_ruby)
|
21
21
|
end
|
22
22
|
should 'return a valid rserve dataframe for statsample datasets' do
|
23
|
-
a = 100.times.map { |i| rand > 0.9 ? nil : i + rand }.
|
24
|
-
b = 100.times.map { |i| rand > 0.9 ? nil : i + rand }.
|
25
|
-
c = 100.times.map { |i| rand > 0.9 ? nil : i + rand }.
|
23
|
+
a = 100.times.map { |i| rand > 0.9 ? nil : i + rand }.to_numeric
|
24
|
+
b = 100.times.map { |i| rand > 0.9 ? nil : i + rand }.to_numeric
|
25
|
+
c = 100.times.map { |i| rand > 0.9 ? nil : i + rand }.to_numeric
|
26
26
|
ds = { 'a' => a, 'b' => b, 'c' => c }.to_dataset
|
27
27
|
rexp = ds.to_REXP
|
28
28
|
assert(rexp.is_a? Rserve::REXP::GenericVector)
|
data/test/test_statistics.rb
CHANGED
@@ -32,7 +32,7 @@ class StatsampleStatisicsTestCase < Minitest::Test
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_estimation_mean
|
35
|
-
v = ([42] * 23 + [41] * 4 + [36] * 1 + [32] * 1 + [29] * 1 + [27] * 2 + [23] * 1 + [19] * 1 + [16] * 2 + [15] * 2 + [14, 11, 10, 9, 7] + [6] * 3 + [5] * 2 + [4, 3]).to_vector(:
|
35
|
+
v = ([42] * 23 + [41] * 4 + [36] * 1 + [32] * 1 + [29] * 1 + [27] * 2 + [23] * 1 + [19] * 1 + [16] * 2 + [15] * 2 + [14, 11, 10, 9, 7] + [6] * 3 + [5] * 2 + [4, 3]).to_vector(:numeric)
|
36
36
|
assert_equal(50, v.size)
|
37
37
|
assert_equal(1471, v.sum)
|
38
38
|
# limits=Statsample::SRS.mean_confidence_interval_z(v.mean(), v.sds(), v.size,676,0.80)
|
@@ -57,17 +57,17 @@ class StatsampleStatisicsTestCase < Minitest::Test
|
|
57
57
|
|
58
58
|
def test_ml
|
59
59
|
if true
|
60
|
-
# real=[1,1,1,1].to_vector(:
|
60
|
+
# real=[1,1,1,1].to_vector(:numeric)
|
61
61
|
|
62
|
-
# pred=[0.0001,0.0001,0.0001,0.0001].to_vector(:
|
62
|
+
# pred=[0.0001,0.0001,0.0001,0.0001].to_vector(:numeric)
|
63
63
|
# puts Statsample::Bivariate.maximum_likehood_dichotomic(pred,real)
|
64
64
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
68
|
def test_simple_linear_regression
|
69
|
-
a = [1, 2, 3, 4, 5, 6].to_vector(:
|
70
|
-
b = [6, 2, 4, 10, 12, 8].to_vector(:
|
69
|
+
a = [1, 2, 3, 4, 5, 6].to_vector(:numeric)
|
70
|
+
b = [6, 2, 4, 10, 12, 8].to_vector(:numeric)
|
71
71
|
reg = Statsample::Regression::Simple.new_from_vectors(a, b)
|
72
72
|
assert_in_delta((reg.ssr + reg.sse).to_f, reg.sst, 0.001)
|
73
73
|
assert_in_delta(Statsample::Bivariate.pearson(a, b), reg.r, 0.001)
|
data/test/test_stest.rb
CHANGED
@@ -24,25 +24,25 @@ class StatsampleTestTestCase < Minitest::Test
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_u_mannwhitney
|
27
|
-
a = [1, 2, 3, 4, 5, 6].
|
28
|
-
b = [0, 5, 7, 9, 10, 11].
|
27
|
+
a = [1, 2, 3, 4, 5, 6].to_numeric
|
28
|
+
b = [0, 5, 7, 9, 10, 11].to_numeric
|
29
29
|
assert_equal(7.5, Statsample::Test.u_mannwhitney(a, b).u)
|
30
30
|
assert_equal(7.5, Statsample::Test.u_mannwhitney(b, a).u)
|
31
|
-
a = [1, 7, 8, 9, 10, 11].
|
32
|
-
b = [2, 3, 4, 5, 6, 12].
|
31
|
+
a = [1, 7, 8, 9, 10, 11].to_numeric
|
32
|
+
b = [2, 3, 4, 5, 6, 12].to_numeric
|
33
33
|
assert_equal(11, Statsample::Test.u_mannwhitney(a, b).u)
|
34
34
|
end
|
35
35
|
|
36
36
|
def test_levene
|
37
|
-
a = [1, 2, 3, 4, 5, 6, 7, 8, 100, 10].
|
38
|
-
b = [30, 40, 50, 60, 70, 80, 90, 100, 110, 120].
|
37
|
+
a = [1, 2, 3, 4, 5, 6, 7, 8, 100, 10].to_numeric
|
38
|
+
b = [30, 40, 50, 60, 70, 80, 90, 100, 110, 120].to_numeric
|
39
39
|
levene = Statsample::Test::Levene.new([a, b])
|
40
40
|
assert_levene(levene)
|
41
41
|
end
|
42
42
|
|
43
43
|
def test_levene_dataset
|
44
|
-
a = [1, 2, 3, 4, 5, 6, 7, 8, 100, 10].
|
45
|
-
b = [30, 40, 50, 60, 70, 80, 90, 100, 110, 120].
|
44
|
+
a = [1, 2, 3, 4, 5, 6, 7, 8, 100, 10].to_numeric
|
45
|
+
b = [30, 40, 50, 60, 70, 80, 90, 100, 110, 120].to_numeric
|
46
46
|
ds = { 'a' => a, 'b' => b }.to_dataset
|
47
47
|
levene = Statsample::Test::Levene.new(ds)
|
48
48
|
assert_levene(levene)
|
data/test/test_stratified.rb
CHANGED
@@ -9,9 +9,9 @@ class StatsampleStratifiedTestCase < Minitest::Test
|
|
9
9
|
a = [10, 20, 30, 40, 50]
|
10
10
|
b = [110, 120, 130, 140]
|
11
11
|
pop = a + b
|
12
|
-
av = a.to_vector(:
|
13
|
-
bv = b.to_vector(:
|
14
|
-
popv = pop.to_vector(:
|
12
|
+
av = a.to_vector(:numeric)
|
13
|
+
bv = b.to_vector(:numeric)
|
14
|
+
popv = pop.to_vector(:numeric)
|
15
15
|
assert_equal(popv.mean, Statsample::StratifiedSample.mean(av, bv))
|
16
16
|
end
|
17
17
|
end
|
data/test/test_test_t.rb
CHANGED
@@ -4,8 +4,8 @@ class StatsampleTestTTestCase < Minitest::Test
|
|
4
4
|
include Math
|
5
5
|
context T do
|
6
6
|
setup do
|
7
|
-
@a = [30.02, 29.99, 30.11, 29.97, 30.01, 29.99].
|
8
|
-
@b = [29.89, 29.93, 29.72, 29.98, 30.02, 29.98].
|
7
|
+
@a = [30.02, 29.99, 30.11, 29.97, 30.01, 29.99].to_numeric
|
8
|
+
@b = [29.89, 29.93, 29.72, 29.98, 30.02, 29.98].to_numeric
|
9
9
|
@x1 = @a.mean
|
10
10
|
@x2 = @b.mean
|
11
11
|
@s1 = @a.sd
|
@@ -20,8 +20,8 @@ class StatsampleTestTTestCase < Minitest::Test
|
|
20
20
|
assert(t.summary.size > 0)
|
21
21
|
end
|
22
22
|
should 'calculate correctly t for one sample' do
|
23
|
-
t1 = [6, 4, 6, 7, 4, 5, 5, 12, 6, 1].
|
24
|
-
t2 = [9, 6, 5, 10, 10, 8, 7, 10, 6, 5].
|
23
|
+
t1 = [6, 4, 6, 7, 4, 5, 5, 12, 6, 1].to_numeric
|
24
|
+
t2 = [9, 6, 5, 10, 10, 8, 7, 10, 6, 5].to_numeric
|
25
25
|
d = t1 - t2
|
26
26
|
t = Statsample::Test::T::OneSample.new(d)
|
27
27
|
assert_in_delta(-2.631, t.t, 0.001)
|
@@ -48,7 +48,7 @@ class StatsampleTestTTestCase < Minitest::Test
|
|
48
48
|
assert_in_delta(0.09095, t.probability_not_equal_variance, 0.001)
|
49
49
|
end
|
50
50
|
should 'be the same using shorthand' do
|
51
|
-
v = 100.times.map { rand(100) }.
|
51
|
+
v = 100.times.map { rand(100) }.to_numeric
|
52
52
|
assert_equal(Statsample::Test.t_one_sample(v).t, T::OneSample.new(v).t)
|
53
53
|
end
|
54
54
|
should 'calculate all values for one sample T test' do
|
data/test/test_umannwhitney.rb
CHANGED
@@ -4,8 +4,8 @@ class StatsampleUMannWhitneyTestCase < Minitest::Test
|
|
4
4
|
include Statsample::Test
|
5
5
|
context Statsample::Test::UMannWhitney do
|
6
6
|
setup do
|
7
|
-
@v1 = [1, 2, 3, 4, 7, 8, 9, 10, 14, 15].
|
8
|
-
@v2 = [5, 6, 11, 12, 13, 16, 17, 18, 19].
|
7
|
+
@v1 = [1, 2, 3, 4, 7, 8, 9, 10, 14, 15].to_numeric
|
8
|
+
@v2 = [5, 6, 11, 12, 13, 16, 17, 18, 19].to_numeric
|
9
9
|
@u = Statsample::Test::UMannWhitney.new(@v1, @v2)
|
10
10
|
end
|
11
11
|
should 'have same result using class or Test#u_mannwhitney' do
|
data/test/test_vector.rb
CHANGED
@@ -4,7 +4,7 @@ class StatsampleTestVector < Minitest::Test
|
|
4
4
|
include Statsample::Shorthand
|
5
5
|
|
6
6
|
def setup
|
7
|
-
@c = Statsample::Vector.new([5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, nil, -99, -99], :
|
7
|
+
@c = Statsample::Vector.new([5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, nil, -99, -99], :object)
|
8
8
|
@c.name = 'Test Vector'
|
9
9
|
@c.missing_values = [-99]
|
10
10
|
end
|
@@ -19,8 +19,8 @@ class StatsampleTestVector < Minitest::Test
|
|
19
19
|
context Statsample do
|
20
20
|
setup do
|
21
21
|
@sample = 100
|
22
|
-
@a = @sample.times.map { |i| (i + rand(10)) % 10 == 0 ? nil : rand(100) }.
|
23
|
-
@b = @sample.times.map { |i| (i + rand(10)) % 10 == 0 ? nil : rand(100) }.
|
22
|
+
@a = @sample.times.map { |i| (i + rand(10)) % 10 == 0 ? nil : rand(100) }.to_numeric
|
23
|
+
@b = @sample.times.map { |i| (i + rand(10)) % 10 == 0 ? nil : rand(100) }.to_numeric
|
24
24
|
@correct_a = []
|
25
25
|
@correct_b = []
|
26
26
|
@a.each_with_index do |_v, i|
|
@@ -29,8 +29,8 @@ class StatsampleTestVector < Minitest::Test
|
|
29
29
|
@correct_b.push(@b[i])
|
30
30
|
end
|
31
31
|
end
|
32
|
-
@correct_a = @correct_a.
|
33
|
-
@correct_b = @correct_b.
|
32
|
+
@correct_a = @correct_a.to_numeric
|
33
|
+
@correct_b = @correct_b.to_numeric
|
34
34
|
|
35
35
|
@common = lambda do |av, bv|
|
36
36
|
assert_equal(@correct_a, av, 'A no es esperado')
|
@@ -58,7 +58,7 @@ class StatsampleTestVector < Minitest::Test
|
|
58
58
|
end
|
59
59
|
context Statsample::Vector do
|
60
60
|
setup do
|
61
|
-
@c = Statsample::Vector.new([5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, nil, -99, -99], :
|
61
|
+
@c = Statsample::Vector.new([5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, nil, -99, -99], :object)
|
62
62
|
@c.name = 'Test Vector'
|
63
63
|
@c.missing_values = [-99]
|
64
64
|
end
|
@@ -71,7 +71,7 @@ class StatsampleTestVector < Minitest::Test
|
|
71
71
|
|
72
72
|
context 'using matrix operations' do
|
73
73
|
setup do
|
74
|
-
@a = [1, 2, 3, 4, 5].
|
74
|
+
@a = [1, 2, 3, 4, 5].to_numeric
|
75
75
|
end
|
76
76
|
should 'to_matrix returns a matrix with 1 row' do
|
77
77
|
mh = Matrix[[1, 2, 3, 4, 5]]
|
@@ -83,22 +83,22 @@ class StatsampleTestVector < Minitest::Test
|
|
83
83
|
end
|
84
84
|
should 'returns valid submatrixes' do
|
85
85
|
# 3*4 + 2*5 = 22
|
86
|
-
a = [3, 2].to_vector(:
|
87
|
-
b = [4, 5].to_vector(:
|
86
|
+
a = [3, 2].to_vector(:numeric)
|
87
|
+
b = [4, 5].to_vector(:numeric)
|
88
88
|
assert_equal(22, (a.to_matrix * b.to_matrix(:vertical))[0, 0])
|
89
89
|
end
|
90
90
|
end
|
91
91
|
context 'when initializing' do
|
92
92
|
setup do
|
93
93
|
@data = (10.times.map { rand(100) }) + [nil]
|
94
|
-
@original = Statsample::Vector.new(@data, :
|
94
|
+
@original = Statsample::Vector.new(@data, :numeric)
|
95
95
|
end
|
96
96
|
should 'be the sample using []' do
|
97
97
|
second = Statsample::Vector[*@data]
|
98
98
|
assert_equal(@original, second)
|
99
99
|
end
|
100
100
|
should '[] returns same results as R-c()' do
|
101
|
-
reference = [0, 4, 5, 6, 10].
|
101
|
+
reference = [0, 4, 5, 6, 10].to_numeric
|
102
102
|
assert_equal(reference, Statsample::Vector[0, 4, 5, 6, 10])
|
103
103
|
assert_equal(reference, Statsample::Vector[0, 4..6, 10])
|
104
104
|
assert_equal(reference, Statsample::Vector[[0], [4, 5, 6], [10]])
|
@@ -107,36 +107,82 @@ class StatsampleTestVector < Minitest::Test
|
|
107
107
|
assert_equal(reference, Statsample::Vector[[0], [4, 5, 6].to_vector, [10]])
|
108
108
|
end
|
109
109
|
should 'be the same usign #to_vector' do
|
110
|
-
lazy1 = @data.to_vector(:
|
110
|
+
lazy1 = @data.to_vector(:numeric)
|
111
111
|
assert_equal(@original, lazy1)
|
112
112
|
end
|
113
|
-
should 'be the same using #
|
114
|
-
lazy2 = @data.
|
113
|
+
should 'be the same using #to_numeric' do
|
114
|
+
lazy2 = @data.to_numeric
|
115
115
|
assert_equal(@original, lazy2)
|
116
|
-
assert_equal(:
|
116
|
+
assert_equal(:numeric, lazy2.type)
|
117
117
|
assert_equal(@data.find_all { |v| !v.nil? }, lazy2.valid_data)
|
118
118
|
end
|
119
|
-
should 'could use
|
120
|
-
v1 = 10.times.map { nil }.
|
121
|
-
v2 = Statsample::Vector.
|
119
|
+
should 'could use new_numeric with size only' do
|
120
|
+
v1 = 10.times.map { nil }.to_numeric
|
121
|
+
v2 = Statsample::Vector.new_numeric(10)
|
122
122
|
assert_equal(v1, v2)
|
123
123
|
end
|
124
|
-
should 'could use
|
124
|
+
should 'could use new_numeric with size and value' do
|
125
125
|
a = rand
|
126
|
-
v1 = 10.times.map { a }.
|
127
|
-
v2 = Statsample::Vector.
|
126
|
+
v1 = 10.times.map { a }.to_numeric
|
127
|
+
v2 = Statsample::Vector.new_numeric(10, a)
|
128
128
|
assert_equal(v1, v2)
|
129
129
|
end
|
130
|
-
should 'could use
|
131
|
-
v1 = 10.times.map { |i| i * 2 }.
|
132
|
-
v2 = Statsample::Vector.
|
130
|
+
should 'could use new_numeric with func' do
|
131
|
+
v1 = 10.times.map { |i| i * 2 }.to_numeric
|
132
|
+
v2 = Statsample::Vector.new_numeric(10) { |i| i * 2 }
|
133
133
|
assert_equal(v1, v2)
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
|
+
context "new types :numeric and :object" do
|
138
|
+
should "set default type of vector to :object" do
|
139
|
+
v = Statsample::Vector.new [1,2,3,4,5]
|
140
|
+
assert_equal(:object, v.type)
|
141
|
+
end
|
142
|
+
|
143
|
+
should "initialize Vector with :numeric type" do
|
144
|
+
v = Statsample::Vector.new [1,2,3,4,5,nil], :numeric
|
145
|
+
assert_equal(:numeric, v.type)
|
146
|
+
assert_equal([1,2,3,4,5], v.valid_data)
|
147
|
+
end
|
148
|
+
|
149
|
+
should "show a warning when initializing with :nominal, :numeric or :ordinal" do
|
150
|
+
assert_output(nil,"WARNING: nominal has been deprecated. Use :object instead.\n") do
|
151
|
+
Statsample::Vector.new [1,2,3,4,5,nil,'hello'], :nominal
|
152
|
+
end
|
153
|
+
|
154
|
+
assert_output(nil,"WARNING: scale has been deprecated. Use :numeric instead.\n") do
|
155
|
+
Statsample::Vector.new [1,2,3,4,nil,5], :scale
|
156
|
+
end
|
157
|
+
|
158
|
+
assert_output(nil,"WARNING: ordinal has been deprecated. Use :numeric instead.\n") do
|
159
|
+
Statsample::Vector.new [1,2,3,4,5], :ordinal
|
160
|
+
end
|
161
|
+
|
162
|
+
assert_output(nil,"WARNING: .new_scale has been deprecated. Use .new_numeric instead.\n") do
|
163
|
+
Statsample::Vector.new_scale 10, 1
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
should "test that new shorthands work" do
|
168
|
+
numeric = Statsample::Vector.new([1,2,3,4,nil,5], :numeric)
|
169
|
+
assert_equal(numeric, [1,2,3,4,nil,5].to_numeric)
|
170
|
+
assert_equal(numeric, [1,2,3,4,nil,5].to_vector(:numeric))
|
171
|
+
|
172
|
+
obj = Statsample::Vector.new([1,2,3,4,'one','two'], :object)
|
173
|
+
assert_equal(obj, [1,2,3,4,'one','two'].to_vector(:object))
|
174
|
+
end
|
175
|
+
|
176
|
+
should "test that old shorthands raise warnings" do
|
177
|
+
assert_output(nil,"WARNING: to_scale has been deprecated. Use to_numeric instead.\n") do
|
178
|
+
[1,2,3,4,nil,5].to_scale
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
137
183
|
context '#split_by_separator' do
|
138
184
|
setup do
|
139
|
-
@a = Statsample::Vector.new(['a', 'a,b', 'c,d', 'a,d', 10, nil], :
|
185
|
+
@a = Statsample::Vector.new(['a', 'a,b', 'c,d', 'a,d', 10, nil], :object)
|
140
186
|
@b = @a.split_by_separator(',')
|
141
187
|
end
|
142
188
|
should 'returns a Hash' do
|
@@ -157,17 +203,17 @@ class StatsampleTestVector < Minitest::Test
|
|
157
203
|
assert_equal({ 'a' => 3, 'b' => 1, 'c' => 1, 'd' => 2, 10 => 1 }, @a.split_by_separator_freq)
|
158
204
|
end
|
159
205
|
should 'using a different separator give the same values' do
|
160
|
-
a = Statsample::Vector.new(['a', 'a*b', 'c*d', 'a*d', 10, nil], :
|
206
|
+
a = Statsample::Vector.new(['a', 'a*b', 'c*d', 'a*d', 10, nil], :object)
|
161
207
|
b = a.split_by_separator('*')
|
162
208
|
assert_counting_tokens(b)
|
163
209
|
end
|
164
210
|
end
|
165
211
|
should 'return correct median_absolute_deviation' do
|
166
|
-
a = [1, 1, 2, 2, 4, 6, 9].
|
212
|
+
a = [1, 1, 2, 2, 4, 6, 9].to_numeric
|
167
213
|
assert_equal(1, a.median_absolute_deviation)
|
168
214
|
end
|
169
215
|
should 'return correct histogram' do
|
170
|
-
a = 10.times.map { |v| v }.
|
216
|
+
a = 10.times.map { |v| v }.to_numeric
|
171
217
|
hist = a.histogram(2)
|
172
218
|
assert_equal([5, 5], hist.bin)
|
173
219
|
3.times do |i|
|
@@ -178,8 +224,8 @@ class StatsampleTestVector < Minitest::Test
|
|
178
224
|
@c.name == 'Test Vector'
|
179
225
|
end
|
180
226
|
should 'without explicit name, returns vector with succesive numbers' do
|
181
|
-
a = 10.times.map { rand(100) }.
|
182
|
-
b = 10.times.map { rand(100) }.
|
227
|
+
a = 10.times.map { rand(100) }.to_numeric
|
228
|
+
b = 10.times.map { rand(100) }.to_numeric
|
183
229
|
assert_match(/Vector \d+/, a.name)
|
184
230
|
a.name =~ /Vector (\d+)/
|
185
231
|
next_number = Regexp.last_match(1).to_i + 1
|
@@ -205,7 +251,7 @@ class StatsampleTestVector < Minitest::Test
|
|
205
251
|
assert_equal(exp2, exp)
|
206
252
|
end
|
207
253
|
should '#product returns the * of all values' do
|
208
|
-
a = [1, 2, 3, 4, 5].to_vector(:
|
254
|
+
a = [1, 2, 3, 4, 5].to_vector(:numeric)
|
209
255
|
assert_equal(120, a.product)
|
210
256
|
end
|
211
257
|
|
@@ -248,7 +294,7 @@ class StatsampleTestVector < Minitest::Test
|
|
248
294
|
|
249
295
|
should 'GSL::Vector based should push correcty' do
|
250
296
|
if Statsample.has_gsl?
|
251
|
-
v = GSL::Vector[1, 2, 3, 4, 5].
|
297
|
+
v = GSL::Vector[1, 2, 3, 4, 5].to_numeric
|
252
298
|
v.push(nil)
|
253
299
|
assert_equal([1, 2, 3, 4, 5, nil], v.to_a)
|
254
300
|
assert(v.flawed?)
|
@@ -258,36 +304,32 @@ class StatsampleTestVector < Minitest::Test
|
|
258
304
|
end
|
259
305
|
|
260
306
|
should 'split correctly' do
|
261
|
-
a = Statsample::Vector.new(['a', 'a,b', 'c,d', 'a,d', 'd', 10, nil], :
|
307
|
+
a = Statsample::Vector.new(['a', 'a,b', 'c,d', 'a,d', 'd', 10, nil], :object)
|
262
308
|
assert_equal([%w(a), %w(a b), %w(c d), %w(a d), %w(d), [10], nil], a.splitted)
|
263
309
|
end
|
264
310
|
should 'multiply correct for scalar' do
|
265
|
-
a = [1, 2, 3].
|
266
|
-
assert_equal([5, 10, 15].
|
311
|
+
a = [1, 2, 3].to_numeric
|
312
|
+
assert_equal([5, 10, 15].to_numeric, a * 5)
|
267
313
|
end
|
268
314
|
should 'multiply correct with other vector' do
|
269
|
-
a = [1, 2, 3].
|
270
|
-
b = [2, 4, 6].
|
315
|
+
a = [1, 2, 3].to_numeric
|
316
|
+
b = [2, 4, 6].to_numeric
|
271
317
|
|
272
|
-
assert_equal([2, 8, 18].
|
318
|
+
assert_equal([2, 8, 18].to_numeric, a * b)
|
273
319
|
end
|
274
320
|
should 'sum correct for scalar' do
|
275
|
-
a = [1, 2, 3].
|
276
|
-
assert_equal([11, 12, 13].
|
321
|
+
a = [1, 2, 3].to_numeric
|
322
|
+
assert_equal([11, 12, 13].to_numeric, a + 10)
|
277
323
|
end
|
278
324
|
|
279
|
-
should 'raise NoMethodError when method requires
|
280
|
-
@c.type = :
|
325
|
+
should 'raise NoMethodError when method requires numeric and vector is object' do
|
326
|
+
@c.type = :object
|
281
327
|
assert_raise(::NoMethodError) { @c.median }
|
282
328
|
end
|
283
329
|
|
284
|
-
should 'raise NoMethodError when method requires scalar and vector is ordinal' do
|
285
|
-
@c.type = :ordinal
|
286
|
-
assert_raise(::NoMethodError) { @c.mean }
|
287
|
-
end
|
288
330
|
should 'jacknife correctly with named method' do
|
289
331
|
# First example
|
290
|
-
a = [1, 2, 3, 4].
|
332
|
+
a = [1, 2, 3, 4].to_numeric
|
291
333
|
ds = a.jacknife(:mean)
|
292
334
|
assert_equal(a.mean, ds[:mean].mean)
|
293
335
|
ds = a.jacknife([:mean, :sd])
|
@@ -296,9 +338,9 @@ class StatsampleTestVector < Minitest::Test
|
|
296
338
|
end
|
297
339
|
should 'jacknife correctly with custom method' do
|
298
340
|
# Second example
|
299
|
-
a = [17.23, 18.71, 13.93, 18.81, 15.78, 11.29, 14.91, 13.39, 18.21, 11.57, 14.28, 10.94, 18.83, 15.52, 13.45, 15.25].
|
341
|
+
a = [17.23, 18.71, 13.93, 18.81, 15.78, 11.29, 14.91, 13.39, 18.21, 11.57, 14.28, 10.94, 18.83, 15.52, 13.45, 15.25].to_numeric
|
300
342
|
ds = a.jacknife(log_s2: ->(v) { Math.log(v.variance) })
|
301
|
-
exp = [1.605, 2.972, 1.151, 3.097, 0.998, 3.308, 0.942, 1.393, 2.416, 2.951, 1.043, 3.806, 3.122, 0.958, 1.362, 0.937].
|
343
|
+
exp = [1.605, 2.972, 1.151, 3.097, 0.998, 3.308, 0.942, 1.393, 2.416, 2.951, 1.043, 3.806, 3.122, 0.958, 1.362, 0.937].to_numeric
|
302
344
|
|
303
345
|
assert_similar_vector(exp, ds[:log_s2], 0.001)
|
304
346
|
assert_in_delta(2.00389, ds[:log_s2].mean, 0.00001)
|
@@ -308,7 +350,7 @@ class StatsampleTestVector < Minitest::Test
|
|
308
350
|
a = rnorm(6)
|
309
351
|
ds = a.jacknife(:mean, 2)
|
310
352
|
mean = a.mean
|
311
|
-
exp = [3 * mean - 2 * (a[2] + a[3] + a[4] + a[5]) / 4, 3 * mean - 2 * (a[0] + a[1] + a[4] + a[5]) / 4, 3 * mean - 2 * (a[0] + a[1] + a[2] + a[3]) / 4].
|
353
|
+
exp = [3 * mean - 2 * (a[2] + a[3] + a[4] + a[5]) / 4, 3 * mean - 2 * (a[0] + a[1] + a[4] + a[5]) / 4, 3 * mean - 2 * (a[0] + a[1] + a[2] + a[3]) / 4].to_numeric
|
312
354
|
assert_similar_vector(exp, ds[:mean], 1e-13)
|
313
355
|
end
|
314
356
|
should 'bootstrap should return a vector with mean=mu and sd=se' do
|
@@ -320,7 +362,7 @@ class StatsampleTestVector < Minitest::Test
|
|
320
362
|
end
|
321
363
|
end
|
322
364
|
|
323
|
-
def
|
365
|
+
def test_object
|
324
366
|
assert_equal(@c[1], 5)
|
325
367
|
assert_equal({ 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 5, 6 => 2, 7 => 1, 8 => 1, 9 => 1, 10 => 1 }, @c.frequencies)
|
326
368
|
assert_equal({ 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 5, 6 => 2, 7 => 1, 8 => 1, 9 => 1, 10 => 1 }, @c._frequencies)
|
@@ -336,8 +378,8 @@ class StatsampleTestVector < Minitest::Test
|
|
336
378
|
v1 = [1, 2, 3].to_vector
|
337
379
|
v2 = [1, 2, 3].to_vector
|
338
380
|
assert_equal(v1, v2)
|
339
|
-
v1 = [1, 2, 3].to_vector(:
|
340
|
-
v2 = [1, 2, 3].to_vector(:
|
381
|
+
v1 = [1, 2, 3].to_vector(:object)
|
382
|
+
v2 = [1, 2, 3].to_vector(:numeric)
|
341
383
|
assert_not_equal(v1, v2)
|
342
384
|
v2 = [1, 2, 3]
|
343
385
|
assert_not_equal(v1, v2)
|
@@ -348,23 +390,23 @@ class StatsampleTestVector < Minitest::Test
|
|
348
390
|
end
|
349
391
|
|
350
392
|
def test_vector_percentil
|
351
|
-
a = [1, 2, 2, 3, 4, 5, 5, 5, 6, 10].
|
352
|
-
expected = [10, 25, 25, 40, 50, 70, 70, 70, 90, 100].
|
393
|
+
a = [1, 2, 2, 3, 4, 5, 5, 5, 6, 10].to_numeric
|
394
|
+
expected = [10, 25, 25, 40, 50, 70, 70, 70, 90, 100].to_numeric
|
353
395
|
assert_equal(expected, a.vector_percentil)
|
354
|
-
a = [1, nil, nil, 2, 2, 3, 4, nil, nil, 5, 5, 5, 6, 10].
|
355
|
-
expected = [10, nil, nil, 25, 25, 40, 50, nil, nil, 70, 70, 70, 90, 100].
|
396
|
+
a = [1, nil, nil, 2, 2, 3, 4, nil, nil, 5, 5, 5, 6, 10].to_numeric
|
397
|
+
expected = [10, nil, nil, 25, 25, 40, 50, nil, nil, 70, 70, 70, 90, 100].to_numeric
|
356
398
|
assert_equal(expected, a.vector_percentil)
|
357
399
|
end
|
358
400
|
|
359
|
-
def
|
360
|
-
@c.type = :
|
401
|
+
def test_numeric
|
402
|
+
@c.type = :numeric
|
361
403
|
assert_equal(5, @c.median)
|
362
404
|
assert_equal(4, @c.percentil(25))
|
363
405
|
assert_equal(7, @c.percentil(75))
|
364
406
|
|
365
|
-
v = [200_000, 200_000, 210_000, 220_000, 230_000, 250_000, 250_000, 250_000, 270_000, 300_000, 450_000, 130_000, 140_000, 140_000, 140_000, 145_000, 148_000, 165_000, 170_000, 180_000, 180_000, 180_000, 180_000, 180_000, 180_000].
|
407
|
+
v = [200_000, 200_000, 210_000, 220_000, 230_000, 250_000, 250_000, 250_000, 270_000, 300_000, 450_000, 130_000, 140_000, 140_000, 140_000, 145_000, 148_000, 165_000, 170_000, 180_000, 180_000, 180_000, 180_000, 180_000, 180_000].to_numeric
|
366
408
|
assert_equal(180_000, v.median)
|
367
|
-
a = [7.0, 7.0, 7.0, 7.0, 7.0, 8.0, 8.0, 8.0, 9.0, 9.0, 10.0, 10.0, 10.0, 10.0, 10.0, 12.0, 12.0, 13.0, 14.0, 14.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 4.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0].
|
409
|
+
a = [7.0, 7.0, 7.0, 7.0, 7.0, 8.0, 8.0, 8.0, 9.0, 9.0, 10.0, 10.0, 10.0, 10.0, 10.0, 12.0, 12.0, 13.0, 14.0, 14.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 4.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0].to_numeric
|
368
410
|
assert_equal(4.5, a.percentil(25))
|
369
411
|
assert_equal(6.5, a.percentil(50))
|
370
412
|
assert_equal(9.5, a.percentil(75))
|
@@ -372,14 +414,14 @@ class StatsampleTestVector < Minitest::Test
|
|
372
414
|
end
|
373
415
|
|
374
416
|
def test_linear_percentil_strategy
|
375
|
-
values = [102, 104, 105, 107, 108, 109, 110, 112, 115, 116].shuffle.
|
417
|
+
values = [102, 104, 105, 107, 108, 109, 110, 112, 115, 116].shuffle.to_numeric
|
376
418
|
assert_equal 102, values.percentil(0, :linear)
|
377
419
|
assert_equal 104.75, values.percentil(25, :linear)
|
378
420
|
assert_equal 108.5, values.percentil(50, :linear)
|
379
421
|
assert_equal 112.75, values.percentil(75, :linear)
|
380
422
|
assert_equal 116, values.percentil(100, :linear)
|
381
423
|
|
382
|
-
values = [102, 104, 105, 107, 108, 109, 110, 112, 115, 116, 118].shuffle.
|
424
|
+
values = [102, 104, 105, 107, 108, 109, 110, 112, 115, 116, 118].shuffle.to_numeric
|
383
425
|
assert_equal 102, values.percentil(0, :linear)
|
384
426
|
assert_equal 105, values.percentil(25, :linear)
|
385
427
|
assert_equal 109, values.percentil(50, :linear)
|
@@ -388,16 +430,16 @@ class StatsampleTestVector < Minitest::Test
|
|
388
430
|
end
|
389
431
|
|
390
432
|
def test_ranked
|
391
|
-
v1 = [0.8, 1.2, 1.2, 2.3, 18].to_vector(:
|
392
|
-
expected = [1, 2.5, 2.5, 4, 5].to_vector(:
|
433
|
+
v1 = [0.8, 1.2, 1.2, 2.3, 18].to_vector(:numeric)
|
434
|
+
expected = [1, 2.5, 2.5, 4, 5].to_vector(:numeric)
|
393
435
|
assert_equal(expected, v1.ranked)
|
394
|
-
v1 = [nil, 0.8, 1.2, 1.2, 2.3, 18, nil].to_vector(:
|
395
|
-
expected = [nil, 1, 2.5, 2.5, 4, 5, nil].to_vector(:
|
436
|
+
v1 = [nil, 0.8, 1.2, 1.2, 2.3, 18, nil].to_vector(:numeric)
|
437
|
+
expected = [nil, 1, 2.5, 2.5, 4, 5, nil].to_vector(:numeric)
|
396
438
|
assert_equal(expected, v1.ranked)
|
397
439
|
end
|
398
440
|
|
399
|
-
def
|
400
|
-
a = Statsample::Vector.new([1, 2, 3, 4, 'STRING'], :
|
441
|
+
def test_numeric
|
442
|
+
a = Statsample::Vector.new([1, 2, 3, 4, 'STRING'], :numeric)
|
401
443
|
assert_equal(10, a.sum)
|
402
444
|
i = 0
|
403
445
|
factors = a.factors.sort
|
@@ -411,7 +453,7 @@ class StatsampleTestVector < Minitest::Test
|
|
411
453
|
def test_vector_centered
|
412
454
|
mean = rand
|
413
455
|
samples = 11
|
414
|
-
centered = samples.times.map { |i| i - ((samples / 2).floor).to_i }.
|
456
|
+
centered = samples.times.map { |i| i - ((samples / 2).floor).to_i }.to_numeric
|
415
457
|
not_centered = centered.recode { |v| v + mean }
|
416
458
|
obs = not_centered.centered
|
417
459
|
centered.each_with_index do |v, i|
|
@@ -420,9 +462,9 @@ class StatsampleTestVector < Minitest::Test
|
|
420
462
|
end
|
421
463
|
|
422
464
|
def test_vector_standarized
|
423
|
-
v1 = [1, 2, 3, 4, nil].to_vector(:
|
465
|
+
v1 = [1, 2, 3, 4, nil].to_vector(:numeric)
|
424
466
|
sds = v1.sds
|
425
|
-
expected = [((1 - 2.5).quo(sds)), ((2 - 2.5).quo(sds)), ((3 - 2.5).quo(sds)), ((4 - 2.5).quo(sds)), nil].to_vector(:
|
467
|
+
expected = [((1 - 2.5).quo(sds)), ((2 - 2.5).quo(sds)), ((3 - 2.5).quo(sds)), ((4 - 2.5).quo(sds)), nil].to_vector(:numeric)
|
426
468
|
vs = v1.vector_standarized
|
427
469
|
assert_equal(expected, vs)
|
428
470
|
assert_equal(0, vs.mean)
|
@@ -430,39 +472,31 @@ class StatsampleTestVector < Minitest::Test
|
|
430
472
|
end
|
431
473
|
|
432
474
|
def test_vector_standarized_with_zero_variance
|
433
|
-
v1 = 100.times.map { |_i| 1 }.
|
434
|
-
exp = 100.times.map { nil }.
|
475
|
+
v1 = 100.times.map { |_i| 1 }.to_numeric
|
476
|
+
exp = 100.times.map { nil }.to_numeric
|
435
477
|
assert_equal(exp, v1.standarized)
|
436
478
|
end
|
437
479
|
|
438
480
|
def test_check_type
|
439
481
|
v = Statsample::Vector.new
|
440
|
-
v.type = :
|
441
|
-
assert_raise(NoMethodError) { v.check_type(:
|
442
|
-
|
443
|
-
assert(v.check_type(:nominal).nil?)
|
444
|
-
|
445
|
-
v.type = :ordinal
|
446
|
-
|
447
|
-
assert_raise(NoMethodError) { v.check_type(:scale) }
|
482
|
+
v.type = :object
|
483
|
+
assert_raise(NoMethodError) { v.check_type(:numeric) }
|
484
|
+
assert(v.check_type(:object).nil?)
|
448
485
|
|
449
|
-
|
450
|
-
assert(v.check_type(:nominal).nil?)
|
486
|
+
v.type = :numeric
|
451
487
|
|
452
|
-
v.
|
453
|
-
assert(v.check_type(:
|
454
|
-
assert(v.check_type(:ordinal).nil?)
|
455
|
-
assert(v.check_type(:nominal).nil?)
|
488
|
+
assert(v.check_type(:numeric).nil?)
|
489
|
+
assert(v.check_type(:object).nil?)
|
456
490
|
|
457
491
|
v.type = :date
|
458
|
-
assert_raise(NoMethodError) { v.check_type(:
|
459
|
-
assert_raise(NoMethodError) { v.check_type(:
|
460
|
-
assert_raise(NoMethodError) { v.check_type(:
|
492
|
+
assert_raise(NoMethodError) { v.check_type(:numeric) }
|
493
|
+
assert_raise(NoMethodError) { v.check_type(:numeric) }
|
494
|
+
assert_raise(NoMethodError) { v.check_type(:object) }
|
461
495
|
end
|
462
496
|
|
463
497
|
def test_add
|
464
|
-
a = Statsample::Vector.new([1, 2, 3, 4, 5], :
|
465
|
-
b = Statsample::Vector.new([11, 12, 13, 14, 15], :
|
498
|
+
a = Statsample::Vector.new([1, 2, 3, 4, 5], :numeric)
|
499
|
+
b = Statsample::Vector.new([11, 12, 13, 14, 15], :numeric)
|
466
500
|
assert_equal([3, 4, 5, 6, 7], (a + 2).to_a)
|
467
501
|
assert_equal([12, 14, 16, 18, 20], (a + b).to_a)
|
468
502
|
assert_raise ArgumentError do
|
@@ -471,15 +505,15 @@ end
|
|
471
505
|
assert_raise TypeError do
|
472
506
|
a + 'string'
|
473
507
|
end
|
474
|
-
a = Statsample::Vector.new([nil, 1, 2, 3, 4, 5], :
|
475
|
-
b = Statsample::Vector.new([11, 12, nil, 13, 14, 15], :
|
508
|
+
a = Statsample::Vector.new([nil, 1, 2, 3, 4, 5], :numeric)
|
509
|
+
b = Statsample::Vector.new([11, 12, nil, 13, 14, 15], :numeric)
|
476
510
|
assert_equal([nil, 13, nil, 16, 18, 20], (a + b).to_a)
|
477
511
|
assert_equal([nil, 13, nil, 16, 18, 20], (a + b.to_a).to_a)
|
478
512
|
end
|
479
513
|
|
480
514
|
def test_minus
|
481
|
-
a = Statsample::Vector.new([1, 2, 3, 4, 5], :
|
482
|
-
b = Statsample::Vector.new([11, 12, 13, 14, 15], :
|
515
|
+
a = Statsample::Vector.new([1, 2, 3, 4, 5], :numeric)
|
516
|
+
b = Statsample::Vector.new([11, 12, 13, 14, 15], :numeric)
|
483
517
|
assert_equal([-1, 0, 1, 2, 3], (a - 2).to_a)
|
484
518
|
assert_equal([10, 10, 10, 10, 10], (b - a).to_a)
|
485
519
|
assert_raise ArgumentError do
|
@@ -488,19 +522,19 @@ end
|
|
488
522
|
assert_raise TypeError do
|
489
523
|
a - 'string'
|
490
524
|
end
|
491
|
-
a = Statsample::Vector.new([nil, 1, 2, 3, 4, 5], :
|
492
|
-
b = Statsample::Vector.new([11, 12, nil, 13, 14, 15], :
|
525
|
+
a = Statsample::Vector.new([nil, 1, 2, 3, 4, 5], :numeric)
|
526
|
+
b = Statsample::Vector.new([11, 12, nil, 13, 14, 15], :numeric)
|
493
527
|
assert_equal([nil, 11, nil, 10, 10, 10], (b - a).to_a)
|
494
528
|
assert_equal([nil, 11, nil, 10, 10, 10], (b - a.to_a).to_a)
|
495
529
|
end
|
496
530
|
|
497
531
|
def test_sum_of_squares
|
498
|
-
a = [1, 2, 3, 4, 5, 6].to_vector(:
|
532
|
+
a = [1, 2, 3, 4, 5, 6].to_vector(:numeric)
|
499
533
|
assert_equal(17.5, a.sum_of_squared_deviation)
|
500
534
|
end
|
501
535
|
|
502
536
|
def test_average_deviation
|
503
|
-
a = [1, 2, 3, 4, 5, 6, 7, 8, 9].
|
537
|
+
a = [1, 2, 3, 4, 5, 6, 7, 8, 9].to_numeric
|
504
538
|
assert_equal(20.quo(9), a.average_deviation_population)
|
505
539
|
end
|
506
540
|
|
@@ -511,7 +545,7 @@ end
|
|
511
545
|
assert_raise ArgumentError do
|
512
546
|
@c.sample_without_replacement(20)
|
513
547
|
end
|
514
|
-
@c.type = :
|
548
|
+
@c.type = :numeric
|
515
549
|
srand(1)
|
516
550
|
assert_equal(100, @c.sample_with_replacement(100).size)
|
517
551
|
assert_equal(@c.valid_data.to_a.sort, @c.sample_without_replacement(15).sort)
|
@@ -542,7 +576,7 @@ end
|
|
542
576
|
|
543
577
|
def test_gsl
|
544
578
|
if Statsample.has_gsl?
|
545
|
-
a = Statsample::Vector.new([1, 2, 3, 4, 'STRING'], :
|
579
|
+
a = Statsample::Vector.new([1, 2, 3, 4, 'STRING'], :numeric)
|
546
580
|
|
547
581
|
assert_equal(2, a.mean)
|
548
582
|
assert_equal(a.variance_sample_ruby, a.variance_sample)
|
@@ -550,17 +584,17 @@ end
|
|
550
584
|
assert_equal(a.variance_population_ruby, a.variance_population)
|
551
585
|
assert_equal(a.standard_deviation_population_ruby, a.standard_deviation_population)
|
552
586
|
assert_nothing_raised do
|
553
|
-
a = [].to_vector(:
|
587
|
+
a = [].to_vector(:numeric)
|
554
588
|
end
|
555
589
|
a.add(1, false)
|
556
590
|
a.add(2, false)
|
557
591
|
a.set_valid_data
|
558
592
|
assert_equal(3, a.sum)
|
559
|
-
b = [1, 2, nil, 3, 4, 5, nil, 6].to_vector(:
|
593
|
+
b = [1, 2, nil, 3, 4, 5, nil, 6].to_vector(:numeric)
|
560
594
|
assert_equal(21, b.sum)
|
561
595
|
assert_equal(3.5, b.mean)
|
562
596
|
assert_equal(6, b.gsl.size)
|
563
|
-
c = [10, 20, 30, 40, 50, 100, 1000, 2000, 5000].
|
597
|
+
c = [10, 20, 30, 40, 50, 100, 1000, 2000, 5000].to_numeric
|
564
598
|
assert_in_delta(c.skew, c.skew_ruby, 0.0001)
|
565
599
|
assert_in_delta(c.kurtosis, c.kurtosis_ruby, 0.0001)
|
566
600
|
end
|
@@ -575,7 +609,7 @@ end
|
|
575
609
|
end
|
576
610
|
|
577
611
|
def test_marshalling
|
578
|
-
v1 = (0..100).to_a.collect { |_n| rand(100) }.to_vector(:
|
612
|
+
v1 = (0..100).to_a.collect { |_n| rand(100) }.to_vector(:numeric)
|
579
613
|
v2 = Marshal.load(Marshal.dump(v1))
|
580
614
|
assert_equal(v1, v2)
|
581
615
|
end
|
@@ -587,7 +621,7 @@ end
|
|
587
621
|
assert_not_same(v1.data, v2.data)
|
588
622
|
assert_equal(v1.type, v2.type)
|
589
623
|
|
590
|
-
v1.type = :
|
624
|
+
v1.type = :numeric
|
591
625
|
assert_not_equal(v1.type, v2.type)
|
592
626
|
assert_equal(v1.missing_values, v2.missing_values)
|
593
627
|
assert_not_same(v1.missing_values, v2.missing_values)
|
@@ -599,8 +633,8 @@ end
|
|
599
633
|
assert_not_equal(v1.data, v3.data)
|
600
634
|
assert_not_same(v1.data, v3.data)
|
601
635
|
assert_equal(v1.type, v3.type)
|
602
|
-
v1.type = :
|
603
|
-
v3.type = :
|
636
|
+
v1.type = :numeric
|
637
|
+
v3.type = :object
|
604
638
|
assert_not_equal(v1.type, v3.type)
|
605
639
|
assert_equal(v1.missing_values, v3.missing_values)
|
606
640
|
assert_not_same(v1.missing_values, v3.missing_values)
|
@@ -609,33 +643,33 @@ end
|
|
609
643
|
end
|
610
644
|
|
611
645
|
def test_paired_ties
|
612
|
-
a = [0, 0, 0, 1, 1, 2, 3, 3, 4, 4, 4].to_vector(:
|
613
|
-
expected = [2, 2, 2, 4.5, 4.5, 6, 7.5, 7.5, 10, 10, 10].to_vector(:
|
646
|
+
a = [0, 0, 0, 1, 1, 2, 3, 3, 4, 4, 4].to_vector(:numeric)
|
647
|
+
expected = [2, 2, 2, 4.5, 4.5, 6, 7.5, 7.5, 10, 10, 10].to_vector(:numeric)
|
614
648
|
assert_equal(expected, a.ranked)
|
615
649
|
end
|
616
650
|
|
617
651
|
def test_dichotomize
|
618
652
|
a = [0, 0, 0, 1, 2, 3, nil].to_vector
|
619
|
-
exp = [0, 0, 0, 1, 1, 1, nil].
|
653
|
+
exp = [0, 0, 0, 1, 1, 1, nil].to_numeric
|
620
654
|
assert_equal(exp, a.dichotomize)
|
621
655
|
a = [1, 1, 1, 2, 2, 2, 3].to_vector
|
622
|
-
exp = [0, 0, 0, 1, 1, 1, 1].
|
656
|
+
exp = [0, 0, 0, 1, 1, 1, 1].to_numeric
|
623
657
|
assert_equal(exp, a.dichotomize)
|
624
658
|
a = [0, 0, 0, 1, 2, 3, nil].to_vector
|
625
|
-
exp = [0, 0, 0, 0, 1, 1, nil].
|
659
|
+
exp = [0, 0, 0, 0, 1, 1, nil].to_numeric
|
626
660
|
assert_equal(exp, a.dichotomize(1))
|
627
661
|
a = %w(a a a b c d).to_vector
|
628
|
-
exp = [0, 0, 0, 1, 1, 1].
|
662
|
+
exp = [0, 0, 0, 1, 1, 1].to_numeric
|
629
663
|
assert_equal(exp, a.dichotomize)
|
630
664
|
end
|
631
665
|
|
632
666
|
def test_can_be_methods
|
633
667
|
a = [0, 0, 0, 1, 2, 3, nil].to_vector
|
634
|
-
assert(a.
|
668
|
+
assert(a.can_be_numeric?)
|
635
669
|
a = [0, 's', 0, 1, 2, 3, nil].to_vector
|
636
|
-
assert(!a.
|
670
|
+
assert(!a.can_be_numeric?)
|
637
671
|
a.missing_values = ['s']
|
638
|
-
assert(a.
|
672
|
+
assert(a.can_be_numeric?)
|
639
673
|
|
640
674
|
a = [Date.new(2009, 10, 10), Date.today, '2009-10-10', '2009-1-1', nil, 'NOW'].to_vector
|
641
675
|
assert(a.can_be_date?)
|