statsample 0.6.5 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/History.txt +15 -0
  2. data/Manifest.txt +6 -0
  3. data/README.txt +30 -12
  4. data/Rakefile +91 -0
  5. data/demo/levene.rb +9 -0
  6. data/demo/multiple_regression.rb +1 -7
  7. data/demo/polychoric.rb +1 -0
  8. data/demo/principal_axis.rb +8 -0
  9. data/lib/distribution/f.rb +22 -22
  10. data/lib/spss.rb +99 -99
  11. data/lib/statsample/bivariate/polychoric.rb +32 -22
  12. data/lib/statsample/bivariate/tetrachoric.rb +212 -207
  13. data/lib/statsample/bivariate.rb +6 -6
  14. data/lib/statsample/codification.rb +65 -65
  15. data/lib/statsample/combination.rb +60 -59
  16. data/lib/statsample/converter/csv19.rb +12 -12
  17. data/lib/statsample/converters.rb +1 -1
  18. data/lib/statsample/dataset.rb +93 -36
  19. data/lib/statsample/dominanceanalysis/bootstrap.rb +66 -3
  20. data/lib/statsample/dominanceanalysis.rb +5 -6
  21. data/lib/statsample/factor/pca.rb +41 -11
  22. data/lib/statsample/factor/principalaxis.rb +105 -29
  23. data/lib/statsample/factor/rotation.rb +20 -3
  24. data/lib/statsample/factor.rb +1 -1
  25. data/lib/statsample/graph/gdchart.rb +13 -13
  26. data/lib/statsample/graph/svggraph.rb +166 -167
  27. data/lib/statsample/matrix.rb +22 -12
  28. data/lib/statsample/mle/logit.rb +3 -2
  29. data/lib/statsample/mle/probit.rb +7 -5
  30. data/lib/statsample/mle.rb +4 -2
  31. data/lib/statsample/multiset.rb +125 -124
  32. data/lib/statsample/permutation.rb +2 -1
  33. data/lib/statsample/regression/binomial/logit.rb +4 -3
  34. data/lib/statsample/regression/binomial/probit.rb +2 -1
  35. data/lib/statsample/regression/binomial.rb +62 -81
  36. data/lib/statsample/regression/multiple/baseengine.rb +1 -1
  37. data/lib/statsample/regression/multiple/gslengine.rb +1 -1
  38. data/lib/statsample/regression/multiple/matrixengine.rb +12 -6
  39. data/lib/statsample/regression/multiple.rb +15 -42
  40. data/lib/statsample/regression/simple.rb +93 -78
  41. data/lib/statsample/regression.rb +74 -2
  42. data/lib/statsample/reliability.rb +117 -120
  43. data/lib/statsample/srs.rb +156 -153
  44. data/lib/statsample/test/levene.rb +90 -0
  45. data/lib/statsample/test/umannwhitney.rb +25 -9
  46. data/lib/statsample/test.rb +2 -0
  47. data/lib/statsample/vector.rb +388 -413
  48. data/lib/statsample.rb +74 -30
  49. data/po/es/statsample.mo +0 -0
  50. data/test/test_bivariate.rb +5 -4
  51. data/test/test_combination.rb +1 -1
  52. data/test/test_dataset.rb +2 -2
  53. data/test/test_factor.rb +53 -6
  54. data/test/test_gsl.rb +1 -1
  55. data/test/test_mle.rb +1 -1
  56. data/test/test_regression.rb +18 -33
  57. data/test/test_statistics.rb +15 -33
  58. data/test/test_stest.rb +35 -0
  59. data/test/test_svg_graph.rb +2 -2
  60. data/test/test_vector.rb +331 -333
  61. metadata +38 -11
data/test/test_vector.rb CHANGED
@@ -5,124 +5,124 @@ require 'tempfile'
5
5
  class TestStatsample
6
6
  end
7
7
  class TestStatsample::TestVector < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @c = Statsample::Vector.new([5,5,5,5,5,6,6,7,8,9,10,1,2,3,4,nil,-99,-99], :nominal)
11
+ @c.missing_values=[-99]
12
+
13
+ end
14
+ def test_save_load
15
+ outfile=Tempfile.new("vector.vec")
16
+ @c.save(outfile.path)
17
+ a=Statsample.load(outfile.path)
18
+ assert_equal(@c,a)
19
+
20
+ end
21
+ def test_lazy_methods
22
+ data=[1,2,3,4,5,nil]
23
+ correct=Statsample::Vector.new(data,:scale)
24
+ lazy1=data.to_vector(:scale)
25
+ lazy2=data.to_scale
26
+ assert_equal(correct,lazy1)
27
+ assert_equal(correct,lazy2)
28
+ assert_equal(:scale,lazy2.type)
29
+ assert_equal([1,2,3,4,5],lazy2.valid_data)
30
+ end
31
+ def test_enumerable
32
+ val=@c.collect {|v| v}
33
+ assert_equal(val,[5,5,5,5,5,6,6,7,8,9,10,1,2,3,4,nil,-99,-99])
34
+ end
35
+ def test_recode
36
+ a=@c.recode{|v| @c.is_valid?(v) ? 0 : 1 }
37
+ exp=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1].to_vector
38
+ assert_equal(exp,a)
39
+ exp.recode!{|v| v==0 ? 1:0}
40
+ exp2=(([1]*15)+([0]*3)).to_vector
41
+ assert_equal(exp2,exp)
42
+ end
43
+ def test_product
44
+ a=[1,2,3,4,5].to_vector(:scale)
45
+ assert_equal(120,a.product)
46
+ end
47
+ def test_matrix
48
+ a=[1,2,3,4,5].to_vector(:scale)
49
+ mh=Matrix[[1,2,3,4,5]]
50
+ mv=Matrix.columns([[1,2,3,4,5]])
51
+ assert_equal(mh,a.to_matrix)
52
+ assert_equal(mv,a.to_matrix(:vertical))
53
+ # 3*4 + 2*5 = 22
54
+ a=[3,2].to_vector(:scale)
55
+ b=[4,5].to_vector(:scale)
56
+ assert_equal(22,(a.to_matrix*b.to_matrix(:vertical))[0,0])
57
+ end
58
+ def test_missing_values
59
+ @c.missing_values=[10]
60
+ assert_equal([-99,-99,1,2,3,4,5,5,5,5,5,6,6,7,8,9], @c.valid_data.sort)
61
+ assert_equal([5,5,5,5,5,6,6,7,8,9,nil,1,2,3,4,nil,-99,-99], @c.data_with_nils)
62
+ @c.missing_values=[-99]
63
+ assert_equal(@c.valid_data.sort,[1,2,3,4,5,5,5,5,5,6,6,7,8,9,10])
64
+ assert_equal(@c.data_with_nils,[5,5,5,5,5,6,6,7,8,9,10,1,2,3,4,nil,nil,nil])
65
+ @c.missing_values=[]
66
+ assert_equal(@c.valid_data.sort,[-99,-99,1,2,3,4,5,5,5,5,5,6,6,7,8,9,10])
67
+ assert_equal(@c.data_with_nils,[5,5,5,5,5,6,6,7,8,9,10,1,2,3,4,nil,-99,-99])
8
68
 
9
- def setup
10
- @c = Statsample::Vector.new([5,5,5,5,5,6,6,7,8,9,10,1,2,3,4,nil,-99,-99], :nominal)
11
- @c.missing_values=[-99]
69
+ end
70
+ def test_has_missing_data
71
+ a=[1,2,3,nil].to_vector
72
+ assert(a.has_missing_data?)
73
+ a=[1,2,3,4,10].to_vector
74
+ assert(!a.has_missing_data?)
75
+ a.missing_values=[10]
76
+ assert(a.has_missing_data?)
77
+ end
78
+ def test_labeled
79
+ @c.labels={5=>'FIVE'}
80
+ assert_equal(["FIVE","FIVE","FIVE","FIVE","FIVE",6,6,7,8,9,10,1,2,3,4,nil,-99, -99],@c.vector_labeled.to_a)
81
+ end
82
+ def test_split
83
+ a = Statsample::Vector.new(["a","a,b","c,d","a,d","d",10,nil],:nominal)
84
+ assert_equal([%w{a},%w{a b},%w{c d},%w{a d},%w{d},[10],nil], a.splitted)
85
+ end
86
+ def test_verify
87
+ h=@c.verify{|d| !d.nil? and d>0}
88
+ e={15=>nil,16=>-99,17=>-99}
89
+ assert_equal(e,h)
90
+ end
91
+ def test_split_by_separator
92
+ a = Statsample::Vector.new(["a","a,b","c,d","a,d",10,nil],:nominal)
93
+ b=a.split_by_separator(",")
94
+ assert_kind_of(Hash, b)
95
+ assert_instance_of(Statsample::Vector,b['a'])
96
+ assert_instance_of(Statsample::Vector,b['b'])
97
+ assert_instance_of(Statsample::Vector,b['c'])
98
+ assert_instance_of(Statsample::Vector,b['d'])
99
+ assert_instance_of(Statsample::Vector,b[10])
100
+ assert_equal([1,1,0,1,0,nil],b['a'].to_a)
101
+ assert_equal([0,1,0,0,0,nil],b['b'].to_a)
102
+ assert_equal([0,0,1,0,0,nil],b['c'].to_a)
103
+ assert_equal([0,0,1,1,0,nil],b['d'].to_a)
104
+ assert_equal([0,0,0,0,1,nil],b[10].to_a)
105
+ assert_equal({'a'=>3,'b'=>1,'c'=>1,'d'=>2,10=>1}, a.split_by_separator_freq())
12
106
 
107
+ a = Statsample::Vector.new(["a","a*b","c*d","a*d",10,nil],:nominal)
108
+ b=a.split_by_separator("*")
109
+ assert_equal([1,1,0,1,0,nil],b['a'].to_a)
110
+ assert_equal([0,1,0,0,0,nil],b['b'].to_a)
111
+ assert_equal([0,0,1,0,0,nil],b['c'].to_a)
112
+ assert_equal([0,0,1,1,0,nil],b['d'].to_a)
113
+ assert_equal([0,0,0,0,1,nil],b[10].to_a)
114
+ end
115
+ def test_types
116
+ @c.type=:nominal
117
+ assert_raise NoMethodError do
118
+ @c.median
13
119
  end
14
- def test_save_load
15
- outfile=Tempfile.new("vector.vec")
16
- @c.save(outfile.path)
17
- a=Statsample.load(outfile.path)
18
- assert_equal(@c,a)
19
-
20
- end
21
- def test_lazy_methods
22
- data=[1,2,3,4,5,nil]
23
- correct=Statsample::Vector.new(data,:scale)
24
- lazy1=data.to_vector(:scale)
25
- lazy2=data.to_scale
26
- assert_equal(correct,lazy1)
27
- assert_equal(correct,lazy2)
28
- assert_equal(:scale,lazy2.type)
29
- assert_equal([1,2,3,4,5],lazy2.valid_data)
30
- end
31
- def test_enumerable
32
- val=@c.collect {|v| v}
33
- assert_equal(val,[5,5,5,5,5,6,6,7,8,9,10,1,2,3,4,nil,-99,-99])
34
- end
35
- def test_recode
36
- a=@c.recode{|v| @c.is_valid?(v) ? 0 : 1 }
37
- exp=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1].to_vector
38
- assert_equal(exp,a)
39
- exp.recode!{|v| v==0 ? 1:0}
40
- exp2=(([1]*15)+([0]*3)).to_vector
41
- assert_equal(exp2,exp)
42
- end
43
- def test_product
44
- a=[1,2,3,4,5].to_vector(:scale)
45
- assert_equal(120,a.product)
46
- end
47
- def test_matrix
48
- a=[1,2,3,4,5].to_vector(:scale)
49
- mh=Matrix[[1,2,3,4,5]]
50
- mv=Matrix.columns([[1,2,3,4,5]])
51
- assert_equal(mh,a.to_matrix)
52
- assert_equal(mv,a.to_matrix(:vertical))
53
- # 3*4 + 2*5 = 22
54
- a=[3,2].to_vector(:scale)
55
- b=[4,5].to_vector(:scale)
56
- assert_equal(22,(a.to_matrix*b.to_matrix(:vertical))[0,0])
57
- end
58
- def test_missing_values
59
- @c.missing_values=[10]
60
- assert_equal([-99,-99,1,2,3,4,5,5,5,5,5,6,6,7,8,9], @c.valid_data.sort)
61
- assert_equal([5,5,5,5,5,6,6,7,8,9,nil,1,2,3,4,nil,-99,-99], @c.data_with_nils)
62
- @c.missing_values=[-99]
63
- assert_equal(@c.valid_data.sort,[1,2,3,4,5,5,5,5,5,6,6,7,8,9,10])
64
- assert_equal(@c.data_with_nils,[5,5,5,5,5,6,6,7,8,9,10,1,2,3,4,nil,nil,nil])
65
- @c.missing_values=[]
66
- assert_equal(@c.valid_data.sort,[-99,-99,1,2,3,4,5,5,5,5,5,6,6,7,8,9,10])
67
- assert_equal(@c.data_with_nils,[5,5,5,5,5,6,6,7,8,9,10,1,2,3,4,nil,-99,-99])
68
-
69
- end
70
- def test_has_missing_data
71
- a=[1,2,3,nil].to_vector
72
- assert(a.has_missing_data?)
73
- a=[1,2,3,4,10].to_vector
74
- assert(!a.has_missing_data?)
75
- a.missing_values=[10]
76
- assert(a.has_missing_data?)
120
+ @c.type=:ordinal
121
+ assert_raise NoMethodError do
122
+ @c.mean
77
123
  end
78
- def test_labeled
79
- @c.labels={5=>'FIVE'}
80
- assert_equal(["FIVE","FIVE","FIVE","FIVE","FIVE",6,6,7,8,9,10,1,2,3,4,nil,-99, -99],@c.vector_labeled.to_a)
81
- end
82
- def test_split
83
- a = Statsample::Vector.new(["a","a,b","c,d","a,d","d",10,nil],:nominal)
84
- assert_equal([%w{a},%w{a b},%w{c d},%w{a d},%w{d},[10],nil], a.splitted)
85
- end
86
- def test_verify
87
- h=@c.verify{|d| !d.nil? and d>0}
88
- e={15=>nil,16=>-99,17=>-99}
89
- assert_equal(e,h)
90
- end
91
- def test_split_by_separator
92
- a = Statsample::Vector.new(["a","a,b","c,d","a,d",10,nil],:nominal)
93
- b=a.split_by_separator(",")
94
- assert_kind_of(Hash, b)
95
- assert_instance_of(Statsample::Vector,b['a'])
96
- assert_instance_of(Statsample::Vector,b['b'])
97
- assert_instance_of(Statsample::Vector,b['c'])
98
- assert_instance_of(Statsample::Vector,b['d'])
99
- assert_instance_of(Statsample::Vector,b[10])
100
- assert_equal([1,1,0,1,0,nil],b['a'].to_a)
101
- assert_equal([0,1,0,0,0,nil],b['b'].to_a)
102
- assert_equal([0,0,1,0,0,nil],b['c'].to_a)
103
- assert_equal([0,0,1,1,0,nil],b['d'].to_a)
104
- assert_equal([0,0,0,0,1,nil],b[10].to_a)
105
- assert_equal({'a'=>3,'b'=>1,'c'=>1,'d'=>2,10=>1}, a.split_by_separator_freq())
106
-
107
- a = Statsample::Vector.new(["a","a*b","c*d","a*d",10,nil],:nominal)
108
- b=a.split_by_separator("*")
109
- assert_equal([1,1,0,1,0,nil],b['a'].to_a)
110
- assert_equal([0,1,0,0,0,nil],b['b'].to_a)
111
- assert_equal([0,0,1,0,0,nil],b['c'].to_a)
112
- assert_equal([0,0,1,1,0,nil],b['d'].to_a)
113
- assert_equal([0,0,0,0,1,nil],b[10].to_a)
114
- end
115
- def test_types
116
- @c.type=:nominal
117
- assert_raise NoMethodError do
118
- @c.median
119
- end
120
- @c.type=:ordinal
121
- assert_raise NoMethodError do
122
- @c.mean
123
- end
124
- end
125
- def test_nominal
124
+ end
125
+ def test_nominal
126
126
  assert_equal(@c[1],5)
127
127
  assert_equal({ 1=>1,2=>1,3=>1,4=>1,5=>5,6=>2,7=>1,8=>1, 9=>1,10=>1},@c.frequencies)
128
128
  assert_equal({ 1=>1,2=>1,3=>1,4=>1,5=>5,6=>2,7=>1,8=>1, 9=>1,10=>1},@c._frequencies)
@@ -132,233 +132,231 @@ class TestStatsample::TestVector < Test::Unit::TestCase
132
132
  assert_equal([1,2,3,4,5,6,7,8,9,10], @c.factors.sort)
133
133
  assert_equal(@c.mode,5)
134
134
  assert_equal(@c.n_valid,15)
135
- end
136
- def test_equality
137
- v1=[1,2,3].to_vector
138
- v2=[1,2,3].to_vector
139
- assert_equal(v1,v2)
140
- v1=[1,2,3].to_vector(:nominal)
141
- v2=[1,2,3].to_vector(:ordinal)
142
- assert_not_equal(v1,v2)
143
- v1=[1,2,3].to_vector()
144
- v2=[1,2,3].to_vector()
145
- assert_equal(v1,v2)
146
- end
147
- def test_ordinal
148
- @c.type=:ordinal
149
- assert_equal(5,@c.median)
150
- assert_equal(4,@c.percentil(25))
151
- assert_equal(7,@c.percentil(75))
135
+ end
136
+ def test_equality
137
+ v1=[1,2,3].to_vector
138
+ v2=[1,2,3].to_vector
139
+ assert_equal(v1,v2)
140
+ v1=[1,2,3].to_vector(:nominal)
141
+ v2=[1,2,3].to_vector(:ordinal)
142
+ assert_not_equal(v1,v2)
143
+ v1=[1,2,3].to_vector()
144
+ v2=[1,2,3].to_vector()
145
+ assert_equal(v1,v2)
146
+ end
147
+ def test_ordinal
148
+ @c.type=:ordinal
149
+ assert_equal(5,@c.median)
150
+ assert_equal(4,@c.percentil(25))
151
+ assert_equal(7,@c.percentil(75))
152
152
 
153
153
  v=[200000, 200000, 210000, 220000, 230000, 250000, 250000, 250000, 270000, 300000, 450000, 130000, 140000, 140000, 140000, 145000, 148000, 165000, 170000, 180000, 180000, 180000, 180000, 180000, 180000 ].to_scale
154
154
  assert_equal(180000,v.median)
155
-
156
- end
157
- def test_ranked
158
- v1=[0.8,1.2,1.2,2.3,18].to_vector(:ordinal)
159
- expected=[1,2.5,2.5,4,5].to_vector(:ordinal)
160
- assert_equal(expected,v1.ranked)
161
- end
162
- def test_scale
163
- a=Statsample::Vector.new([1,2,3,4,"STRING"], :scale)
164
- assert_equal(10, a.sum)
165
- i=0
166
- factors=a.factors.sort
167
- [0,1,2,3,4].each{|v|
168
- assert(v==factors[i])
169
- assert(v.class==factors[i].class,"#{v} - #{v.class} != #{factors[i]} - #{factors[i].class}")
170
- i+=1
171
- }
155
+
156
+ end
157
+ def test_ranked
158
+ v1=[0.8,1.2,1.2,2.3,18].to_vector(:ordinal)
159
+ expected=[1,2.5,2.5,4,5].to_vector(:ordinal)
160
+ assert_equal(expected,v1.ranked)
161
+ end
162
+ def test_scale
163
+ a=Statsample::Vector.new([1,2,3,4,"STRING"], :scale)
164
+ assert_equal(10, a.sum)
165
+ i=0
166
+ factors=a.factors.sort
167
+ [0,1,2,3,4].each{|v|
168
+ assert(v==factors[i])
169
+ assert(v.class==factors[i].class,"#{v} - #{v.class} != #{factors[i]} - #{factors[i].class}")
170
+ i+=1
171
+ }
172
+ end
173
+ def test_vector_standarized
174
+ v1=[1,2,3,4,nil].to_vector(:scale)
175
+ sds=v1.sds
176
+ expected=[((1-2.5).quo(sds)),((2-2.5).quo(sds)),((3-2.5).quo(sds)),((4-2.5).quo(sds)), nil].to_vector(:scale)
177
+ vs=v1.vector_standarized
178
+ assert_equal(expected, vs)
179
+ assert_equal(0,vs.mean)
180
+ assert_equal(1,vs.sds)
181
+ end
182
+ def test_summary
183
+ @c.type=:nominal
184
+ assert_match(/Distribution/, @c.summary())
185
+ @c.type=:ordinal
186
+ assert_match(/median/, @c.summary())
187
+ @c.type=:scale
188
+ assert_match(/mean/, @c.summary())
189
+ end
190
+ def test_add
191
+ a=Statsample::Vector.new([1,2,3,4,5], :scale)
192
+ b=Statsample::Vector.new([11,12,13,14,15], :scale)
193
+ assert_equal([3,4,5,6,7], (a+2).to_a)
194
+ assert_equal([12,14,16,18,20], (a+b).to_a)
195
+ assert_raise ArgumentError do
196
+ a + @c
172
197
  end
173
- def test_vector_standarized
174
- v1=[1,2,3,4,nil].to_vector(:scale)
175
- sds=v1.sds
176
- expected=[((1-2.5).quo(sds)),((2-2.5).quo(sds)),((3-2.5).quo(sds)),((4-2.5).quo(sds)), nil].to_vector(:scale)
177
- vs=v1.vector_standarized
178
- assert_equal(expected, vs)
179
- assert_equal(0,vs.mean)
180
- assert_equal(1,vs.sds)
198
+ assert_raise TypeError do
199
+ a+"string"
181
200
  end
182
- def test_summary
183
- @c.type=:nominal
184
- assert_match(/Distribution/, @c.summary())
185
- @c.type=:ordinal
186
- assert_match(/median/, @c.summary())
187
- @c.type=:scale
188
- assert_match(/mean/, @c.summary())
201
+ a=Statsample::Vector.new([nil,1, 2 ,3 ,4 ,5], :scale)
202
+ b=Statsample::Vector.new([11, 12,nil,13,14,15], :scale)
203
+ assert_equal([nil,13,nil,16,18,20], (a+b).to_a)
204
+ assert_equal([nil,13,nil,16,18,20], (a+b.to_a).to_a)
205
+ end
206
+ def test_minus
207
+ a=Statsample::Vector.new([1,2,3,4,5], :scale)
208
+ b=Statsample::Vector.new([11,12,13,14,15], :scale)
209
+ assert_equal([-1,0,1,2,3], (a-2).to_a)
210
+ assert_equal([10,10,10,10,10], (b-a).to_a)
211
+ assert_raise ArgumentError do
212
+ a-@c
189
213
  end
190
- def test_add
191
- a=Statsample::Vector.new([1,2,3,4,5], :scale)
192
- b=Statsample::Vector.new([11,12,13,14,15], :scale)
193
- assert_equal([3,4,5,6,7], (a+2).to_a)
194
- assert_equal([12,14,16,18,20], (a+b).to_a)
195
- assert_raise ArgumentError do
196
- a + @c
197
- end
198
- assert_raise TypeError do
199
- a+"string"
200
- end
201
- a=Statsample::Vector.new([nil,1, 2 ,3 ,4 ,5], :scale)
202
- b=Statsample::Vector.new([11, 12,nil,13,14,15], :scale)
203
- assert_equal([nil,13,nil,16,18,20], (a+b).to_a)
204
- assert_equal([nil,13,nil,16,18,20], (a+b.to_a).to_a)
214
+ assert_raise TypeError do
215
+ a-"string"
205
216
  end
206
- def test_minus
207
- a=Statsample::Vector.new([1,2,3,4,5], :scale)
208
- b=Statsample::Vector.new([11,12,13,14,15], :scale)
209
- assert_equal([-1,0,1,2,3], (a-2).to_a)
210
- assert_equal([10,10,10,10,10], (b-a).to_a)
211
- assert_raise ArgumentError do
212
- a-@c
213
- end
214
- assert_raise TypeError do
215
- a-"string"
216
- end
217
- a=Statsample::Vector.new([nil,1, 2 ,3 ,4 ,5], :scale)
218
- b=Statsample::Vector.new([11, 12,nil,13,14,15], :scale)
219
- assert_equal([nil,11,nil,10,10,10], (b-a).to_a)
220
- assert_equal([nil,11,nil,10,10,10], (b-a.to_a).to_a)
217
+ a=Statsample::Vector.new([nil,1, 2 ,3 ,4 ,5], :scale)
218
+ b=Statsample::Vector.new([11, 12,nil,13,14,15], :scale)
219
+ assert_equal([nil,11,nil,10,10,10], (b-a).to_a)
220
+ assert_equal([nil,11,nil,10,10,10], (b-a.to_a).to_a)
221
+ end
222
+ def test_sum_of_squares
223
+ a=[1,2,3,4,5,6].to_vector(:scale)
224
+ assert_equal(17.5, a.sum_of_squared_deviation)
225
+ end
226
+ def test_samples
227
+ srand(1)
228
+ assert_equal(100,@c.sample_with_replacement(100).size)
229
+ assert_equal(@c.valid_data.to_a.sort, @c.sample_without_replacement(15).sort)
230
+ assert_raise ArgumentError do
231
+ @c.sample_without_replacement(20)
221
232
  end
222
- def test_sum_of_squares
223
- a=[1,2,3,4,5,6].to_vector(:scale)
224
- assert_equal(17.5, a.sum_of_squared_deviation)
225
- end
226
- def test_samples
227
- srand(1)
228
- assert_equal(100,@c.sample_with_replacement(100).size)
229
- assert_equal(@c.valid_data.to_a.sort, @c.sample_without_replacement(15).sort)
230
- assert_raise ArgumentError do
231
- @c.sample_without_replacement(20)
232
- end
233
- @c.type=:scale
234
- srand(1)
235
- assert_equal(100, @c.sample_with_replacement(100).size)
236
- assert_equal(@c.valid_data.to_a.sort, @c.sample_without_replacement(15).sort)
237
-
238
- end
239
- def test_valid_data
240
- a=Statsample::Vector.new([1,2,3,4,"STRING"])
241
- a.missing_values=[-99]
242
- a.add(1,false)
243
- a.add(2,false)
244
- a.add(-99,false)
245
- a.set_valid_data
246
- exp_valid_data=[1,2,3,4,"STRING",1,2]
247
- assert_equal(exp_valid_data,a.valid_data)
248
- a.add(20,false)
249
- a.add(30,false)
250
- assert_equal(exp_valid_data,a.valid_data)
251
- a.set_valid_data
252
- exp_valid_data_2=[1,2,3,4,"STRING",1,2,20,30]
253
- assert_equal(exp_valid_data_2,a.valid_data)
254
- end
255
- def test_set_value
256
- @c[2]=10
257
- expected=[5,5,10,5,5,6,6,7,8,9,10,1,2,3,4,nil,-99,-99].to_vector
258
- assert_equal(expected.data,@c.data)
259
- end
260
- def test_gsl
261
- if HAS_GSL
262
- a=Statsample::Vector.new([1,2,3,4,"STRING"], :scale)
263
- assert_equal(2,a.mean)
264
- assert_equal(a.variance_sample_slow,a.variance_sample)
265
- assert_equal(a.standard_deviation_sample_slow,a.sds)
266
- assert_equal(a.variance_population_slow,a.variance_population)
267
- assert_equal(a.standard_deviation_population_slow,a.standard_deviation_population)
268
- assert_nothing_raised do
269
- a=[].to_vector(:scale)
270
- end
271
- a.add(1,false)
272
- a.add(2,false)
273
- a.set_valid_data
274
- assert_equal(3,a.sum)
275
- b=[1,2,nil,3,4,5,nil,6].to_vector(:scale)
276
- assert_equal(21, b.sum)
277
- assert_equal(3.5, b.mean)
278
- assert_equal(6,b.gsl.size)
279
- c=[10,20,30,40,50,100,1000,2000,5000].to_scale
280
- assert_in_delta(c.skew, c.skew_slow ,0.0001)
281
- assert_in_delta(c.kurtosis, c.kurtosis_slow ,0.0001)
282
-
283
- end
284
- end
285
- def test_vector_matrix
286
- v1=%w{a a a b b b c c}.to_vector
287
- v2=%w{1 3 4 5 6 4 3 2}.to_vector
288
- v3=%w{1 0 0 0 1 1 1 0}.to_vector
289
- ex=Matrix.rows([["a", "1", "1"], ["a", "3", "0"], ["a", "4", "0"], ["b", "5", "0"], ["b", "6", "1"], ["b", "4", "1"], ["c", "3", "1"], ["c", "2", "0"]])
290
- assert_equal(ex,Statsample.vector_cols_matrix(v1,v2,v3))
291
- end
292
- def test_marshalling
293
- v1=(0..100).to_a.collect{|n| rand(100)}.to_vector(:scale)
294
- v2=Marshal.load(Marshal.dump(v1))
295
- assert_equal(v1,v2)
296
- end
297
- def test_dup
298
- v1=%w{a a a b b b c c}.to_vector
299
- v2=v1.dup
300
- assert_equal(v1.data,v2.data)
301
- assert_not_same(v1.data,v2.data)
302
- assert_equal(v1.type,v2.type)
303
-
304
- v1.type=:ordinal
305
- assert_not_equal(v1.type,v2.type)
306
- assert_equal(v1.missing_values,v2.missing_values)
307
- assert_not_same(v1.missing_values,v2.missing_values)
308
- assert_equal(v1.labels,v2.labels)
309
- assert_not_same(v1.labels,v2.labels)
310
-
311
- v3=v1.dup_empty
312
- assert_equal([],v3.data)
313
- assert_not_equal(v1.data,v3.data)
314
- assert_not_same(v1.data,v3.data)
315
- assert_equal(v1.type,v3.type)
316
- v1.type=:ordinal
317
- v3.type=:nominal
318
- assert_not_equal(v1.type,v3.type)
319
- assert_equal(v1.missing_values,v3.missing_values)
320
- assert_not_same(v1.missing_values,v3.missing_values)
321
- assert_equal(v1.labels,v3.labels)
322
- assert_not_same(v1.labels,v3.labels)
323
- end
324
- def test_paired_ties
325
- a=[0,0,0,1,1,2,3,3,4,4,4].to_vector(:ordinal)
326
- expected=[2,2,2,4.5,4.5,6,7.5,7.5,10,10,10].to_vector(:ordinal)
327
- assert_equal(expected,a.ranked)
328
- end
329
- def test_dichotomize
330
- a= [0,0,0,1,2,3,nil].to_vector
331
- exp=[0,0,0,1,1,1,nil].to_scale
332
- assert_equal(exp,a.dichotomize)
333
- a= [1,1,1,2,2,2,3].to_vector
334
- exp=[0,0,0,1,1,1,1].to_scale
335
- assert_equal(exp,a.dichotomize)
336
- a= [0,0,0,1,2,3,nil].to_vector
337
- exp=[0,0,0,0,1,1,nil].to_scale
338
- assert_equal(exp,a.dichotomize(1))
339
- a= %w{a a a b c d}.to_vector
340
- exp=[0,0,0,1,1,1].to_scale
341
- assert_equal(exp, a.dichotomize)
342
- end
343
- def test_can_be_methods
344
- a= [0,0,0,1,2,3,nil].to_vector
345
- assert(a.can_be_scale?)
346
- a=[0,"s",0,1,2,3,nil].to_vector
347
- assert(!a.can_be_scale?)
348
- a.missing_values=["s"]
349
- assert(a.can_be_scale?)
233
+ @c.type=:scale
234
+ srand(1)
235
+ assert_equal(100, @c.sample_with_replacement(100).size)
236
+ assert_equal(@c.valid_data.to_a.sort, @c.sample_without_replacement(15).sort)
350
237
 
351
- a=[Date.new(2009,10,10), Date.today(), "2009-10-10", "2009-1-1", nil, "NOW"].to_vector
352
- assert(a.can_be_date?)
353
- a=[Date.new(2009,10,10), Date.today(),nil,"sss"].to_vector
354
- assert(!a.can_be_date?)
355
- end
356
- def test_date_vector
357
- a=[Date.new(2009,10,10), :NOW, "2009-10-10", "2009-1-1", nil, "NOW","MISSING"].to_vector(:date, :missing_values=>["MISSING"])
358
-
359
- assert(a.type==:date)
360
- expected=[Date.new(2009,10,10), Date.today(), Date.new(2009,10,10), Date.new(2009,1,1), nil, Date.today(), nil ]
361
- assert_equal(expected, a.date_data_with_nils)
362
-
238
+ end
239
+ def test_valid_data
240
+ a=Statsample::Vector.new([1,2,3,4,"STRING"])
241
+ a.missing_values=[-99]
242
+ a.add(1,false)
243
+ a.add(2,false)
244
+ a.add(-99,false)
245
+ a.set_valid_data
246
+ exp_valid_data=[1,2,3,4,"STRING",1,2]
247
+ assert_equal(exp_valid_data,a.valid_data)
248
+ a.add(20,false)
249
+ a.add(30,false)
250
+ assert_equal(exp_valid_data,a.valid_data)
251
+ a.set_valid_data
252
+ exp_valid_data_2=[1,2,3,4,"STRING",1,2,20,30]
253
+ assert_equal(exp_valid_data_2,a.valid_data)
254
+ end
255
+ def test_set_value
256
+ @c[2]=10
257
+ expected=[5,5,10,5,5,6,6,7,8,9,10,1,2,3,4,nil,-99,-99].to_vector
258
+ assert_equal(expected.data,@c.data)
259
+ end
260
+ def test_gsl
261
+ if Statsample.has_gsl?
262
+ a=Statsample::Vector.new([1,2,3,4,"STRING"], :scale)
263
+ assert_equal(2,a.mean)
264
+ assert_equal(a.variance_sample_slow,a.variance_sample)
265
+ assert_equal(a.standard_deviation_sample_slow,a.sds)
266
+ assert_equal(a.variance_population_slow,a.variance_population)
267
+ assert_equal(a.standard_deviation_population_slow,a.standard_deviation_population)
268
+ assert_nothing_raised do
269
+ a=[].to_vector(:scale)
270
+ end
271
+ a.add(1,false)
272
+ a.add(2,false)
273
+ a.set_valid_data
274
+ assert_equal(3,a.sum)
275
+ b=[1,2,nil,3,4,5,nil,6].to_vector(:scale)
276
+ assert_equal(21, b.sum)
277
+ assert_equal(3.5, b.mean)
278
+ assert_equal(6,b.gsl.size)
279
+ c=[10,20,30,40,50,100,1000,2000,5000].to_scale
280
+ assert_in_delta(c.skew, c.skew_slow ,0.0001)
281
+ assert_in_delta(c.kurtosis, c.kurtosis_slow ,0.0001)
363
282
  end
283
+ end
284
+ def test_vector_matrix
285
+ v1=%w{a a a b b b c c}.to_vector
286
+ v2=%w{1 3 4 5 6 4 3 2}.to_vector
287
+ v3=%w{1 0 0 0 1 1 1 0}.to_vector
288
+ ex=Matrix.rows([["a", "1", "1"], ["a", "3", "0"], ["a", "4", "0"], ["b", "5", "0"], ["b", "6", "1"], ["b", "4", "1"], ["c", "3", "1"], ["c", "2", "0"]])
289
+ assert_equal(ex,Statsample.vector_cols_matrix(v1,v2,v3))
290
+ end
291
+ def test_marshalling
292
+ v1=(0..100).to_a.collect{|n| rand(100)}.to_vector(:scale)
293
+ v2=Marshal.load(Marshal.dump(v1))
294
+ assert_equal(v1,v2)
295
+ end
296
+ def test_dup
297
+ v1=%w{a a a b b b c c}.to_vector
298
+ v2=v1.dup
299
+ assert_equal(v1.data,v2.data)
300
+ assert_not_same(v1.data,v2.data)
301
+ assert_equal(v1.type,v2.type)
302
+
303
+ v1.type=:ordinal
304
+ assert_not_equal(v1.type,v2.type)
305
+ assert_equal(v1.missing_values,v2.missing_values)
306
+ assert_not_same(v1.missing_values,v2.missing_values)
307
+ assert_equal(v1.labels,v2.labels)
308
+ assert_not_same(v1.labels,v2.labels)
309
+
310
+ v3=v1.dup_empty
311
+ assert_equal([],v3.data)
312
+ assert_not_equal(v1.data,v3.data)
313
+ assert_not_same(v1.data,v3.data)
314
+ assert_equal(v1.type,v3.type)
315
+ v1.type=:ordinal
316
+ v3.type=:nominal
317
+ assert_not_equal(v1.type,v3.type)
318
+ assert_equal(v1.missing_values,v3.missing_values)
319
+ assert_not_same(v1.missing_values,v3.missing_values)
320
+ assert_equal(v1.labels,v3.labels)
321
+ assert_not_same(v1.labels,v3.labels)
322
+ end
323
+ def test_paired_ties
324
+ a=[0,0,0,1,1,2,3,3,4,4,4].to_vector(:ordinal)
325
+ expected=[2,2,2,4.5,4.5,6,7.5,7.5,10,10,10].to_vector(:ordinal)
326
+ assert_equal(expected,a.ranked)
327
+ end
328
+ def test_dichotomize
329
+ a= [0,0,0,1,2,3,nil].to_vector
330
+ exp=[0,0,0,1,1,1,nil].to_scale
331
+ assert_equal(exp,a.dichotomize)
332
+ a= [1,1,1,2,2,2,3].to_vector
333
+ exp=[0,0,0,1,1,1,1].to_scale
334
+ assert_equal(exp,a.dichotomize)
335
+ a= [0,0,0,1,2,3,nil].to_vector
336
+ exp=[0,0,0,0,1,1,nil].to_scale
337
+ assert_equal(exp,a.dichotomize(1))
338
+ a= %w{a a a b c d}.to_vector
339
+ exp=[0,0,0,1,1,1].to_scale
340
+ assert_equal(exp, a.dichotomize)
341
+ end
342
+ def test_can_be_methods
343
+ a= [0,0,0,1,2,3,nil].to_vector
344
+ assert(a.can_be_scale?)
345
+ a=[0,"s",0,1,2,3,nil].to_vector
346
+ assert(!a.can_be_scale?)
347
+ a.missing_values=["s"]
348
+ assert(a.can_be_scale?)
349
+
350
+ a=[Date.new(2009,10,10), Date.today(), "2009-10-10", "2009-1-1", nil, "NOW"].to_vector
351
+ assert(a.can_be_date?)
352
+ a=[Date.new(2009,10,10), Date.today(),nil,"sss"].to_vector
353
+ assert(!a.can_be_date?)
354
+ end
355
+ def test_date_vector
356
+ a=[Date.new(2009,10,10), :NOW, "2009-10-10", "2009-1-1", nil, "NOW","MISSING"].to_vector(:date, :missing_values=>["MISSING"])
357
+
358
+ assert(a.type==:date)
359
+ expected=[Date.new(2009,10,10), Date.today(), Date.new(2009,10,10), Date.new(2009,1,1), nil, Date.today(), nil ]
360
+ assert_equal(expected, a.date_data_with_nils)
361
+ end
364
362
  end