statsample 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. data/History.txt +7 -0
  2. data/Manifest.txt +15 -9
  3. data/README.txt +6 -0
  4. data/Rakefile +8 -0
  5. data/{demo → examples}/correlation_matrix.rb +0 -0
  6. data/{demo/dominanceanalysis.rb → examples/dominance_analysis.rb} +0 -0
  7. data/{demo → examples}/dominance_analysis_bootstrap.rb +0 -0
  8. data/{demo → examples}/levene.rb +0 -0
  9. data/{demo → examples}/multiple_regression.rb +5 -3
  10. data/{demo → examples}/multivariate_correlation.rb +0 -0
  11. data/{demo → examples}/polychoric.rb +0 -0
  12. data/{demo → examples}/principal_axis.rb +0 -0
  13. data/examples/t_test.rb +11 -0
  14. data/{demo → examples}/tetrachoric.rb +0 -0
  15. data/lib/statistics2.rb +1 -1
  16. data/lib/statsample.rb +57 -6
  17. data/lib/statsample/bivariate/polychoric.rb +12 -25
  18. data/lib/statsample/bivariate/tetrachoric.rb +1 -3
  19. data/lib/statsample/converter/csv.rb +11 -12
  20. data/lib/statsample/dominanceanalysis/bootstrap.rb +2 -3
  21. data/lib/statsample/factor/principalaxis.rb +0 -2
  22. data/lib/statsample/factor/rotation.rb +6 -8
  23. data/lib/statsample/graph.rb +8 -0
  24. data/lib/statsample/graph/svggraph.rb +0 -4
  25. data/lib/statsample/regression/multiple/baseengine.rb +25 -28
  26. data/lib/statsample/regression/multiple/matrixengine.rb +30 -34
  27. data/lib/statsample/test.rb +36 -1
  28. data/lib/statsample/test/levene.rb +11 -7
  29. data/lib/statsample/test/t.rb +189 -0
  30. data/test/test_anova.rb +8 -10
  31. data/test/test_bivariate.rb +40 -37
  32. data/test/test_codification.rb +9 -13
  33. data/test/test_combination.rb +37 -39
  34. data/test/test_crosstab.rb +46 -48
  35. data/test/test_csv.rb +40 -45
  36. data/test/test_dataset.rb +150 -152
  37. data/test/test_distribution.rb +24 -21
  38. data/test/test_dominance_analysis.rb +10 -12
  39. data/test/test_factor.rb +95 -91
  40. data/test/test_ggobi.rb +30 -33
  41. data/test/test_gsl.rb +4 -4
  42. data/test/test_helpers.rb +26 -0
  43. data/test/test_histogram.rb +5 -6
  44. data/test/test_logit.rb +20 -21
  45. data/test/test_matrix.rb +47 -48
  46. data/test/test_mle.rb +130 -131
  47. data/test/test_multiset.rb +95 -96
  48. data/test/test_permutation.rb +35 -36
  49. data/test/test_promise_after.rb +39 -0
  50. data/test/test_regression.rb +49 -51
  51. data/test/test_reliability.rb +29 -30
  52. data/test/test_resample.rb +22 -23
  53. data/test/test_srs.rb +8 -9
  54. data/test/test_statistics.rb +12 -6
  55. data/test/test_stest.rb +18 -10
  56. data/test/test_stratified.rb +15 -16
  57. data/test/test_svg_graph.rb +11 -22
  58. data/test/test_test_t.rb +40 -0
  59. data/test/test_umannwhitney.rb +14 -15
  60. data/test/test_vector.rb +33 -37
  61. data/test/test_xls.rb +34 -41
  62. metadata +22 -11
@@ -1,7 +1,5 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'minitest/unit'
4
- MiniTest::Unit.autorun
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
2
+
5
3
  class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
6
4
  def test_sum_of_squares
7
5
  v1=[1,2,3,4,5,6].to_vector(:scale)
@@ -16,25 +14,25 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
16
14
  else
17
15
  puts "Bivariate::covariance not tested (needs GSL)"
18
16
  end
19
-
17
+
20
18
  end
21
19
 
22
20
  def test_gsl_pearson
23
- if Statsample.has_gsl?
24
- v1=100.times.collect {|a| rand()}.to_scale
25
- v2=100.times.collect {|a| rand()}.to_scale
26
-
27
- assert_in_delta(GSL::Stats::correlation(v1.gsl, v2.gsl), Statsample::Bivariate.pearson_slow(v1,v2), 1e-10)
28
- else
29
- puts "Not tested gsl versus ruby correlation (needs GSL)"
30
- end
21
+ if Statsample.has_gsl?
22
+ v1=100.times.collect {|a| rand()}.to_scale
23
+ v2=100.times.collect {|a| rand()}.to_scale
24
+
25
+ assert_in_delta(GSL::Stats::correlation(v1.gsl, v2.gsl), Statsample::Bivariate.pearson_slow(v1,v2), 1e-10)
26
+ else
27
+ puts "Not tested gsl versus ruby correlation (needs GSL)"
28
+ end
31
29
  end
32
30
  def test_pearson
33
31
  v1=[6,5,4,7,8,4,3,2].to_vector(:scale)
34
32
  v2=[2,3,7,8,6,4,3,2].to_vector(:scale)
35
33
  assert_in_delta(0.525,Statsample::Bivariate.pearson(v1,v2), 0.001)
36
34
  assert_in_delta(0.525,Statsample::Bivariate.pearson_slow(v1,v2), 0.001)
37
-
35
+
38
36
  v3=[6,2, 1000,1000,5,4,7,8,4,3,2,nil].to_vector(:scale)
39
37
  v4=[2,nil,nil,nil, 3,7,8,6,4,3,2,500].to_vector(:scale)
40
38
  assert_in_delta(0.525,Statsample::Bivariate.pearson(v3,v4),0.001)
@@ -53,6 +51,11 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
53
51
  end
54
52
  end
55
53
  end
54
+ def test_polychoric_summary
55
+ matrix=Matrix[[150+rand(2),150+rand(2)],[150+rand(2),150+rand(2)],[200,300]]
56
+ poly = Statsample::Bivariate::Polychoric.new(matrix)
57
+ assert(poly.summary.size>0)
58
+ end
56
59
  def test_poly_vs_tetra
57
60
  5.times {
58
61
  # Should be the same results as Tetrachoric for 2x2 matrix
@@ -68,19 +71,19 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
68
71
  }
69
72
  end
70
73
  def test_polychoric
71
-
72
- matrix=Matrix[[58,52,1],[26,58,3],[8,12,9]]
73
- poly=Statsample::Bivariate::Polychoric.new(matrix)
74
- poly.compute_two_step_mle_drasgow_ruby
75
- assert_in_delta(0.420, poly.r, 0.001)
76
- assert_in_delta(-0.240, poly.threshold_y[0],0.001)
77
- assert_in_delta(-0.027, poly.threshold_x[0],0.001)
78
- assert_in_delta(1.578, poly.threshold_y[1],0.001)
79
- assert_in_delta(1.137, poly.threshold_x[1],0.001)
80
- if Statsample.has_gsl?
74
+
75
+ matrix=Matrix[[58,52,1],[26,58,3],[8,12,9]]
76
+ poly=Statsample::Bivariate::Polychoric.new(matrix)
77
+ poly.compute_two_step_mle_drasgow_ruby
78
+ assert_in_delta(0.420, poly.r, 0.001)
79
+ assert_in_delta(-0.240, poly.threshold_y[0],0.001)
80
+ assert_in_delta(-0.027, poly.threshold_x[0],0.001)
81
+ assert_in_delta(1.578, poly.threshold_y[1],0.001)
82
+ assert_in_delta(1.137, poly.threshold_x[1],0.001)
83
+ if Statsample.has_gsl?
81
84
  poly.method=:polychoric_series
82
85
  poly.compute
83
-
86
+
84
87
  assert_in_delta(0.556, poly.r, 0.001)
85
88
  assert_in_delta(-0.240, poly.threshold_y[0],0.001)
86
89
  assert_in_delta(-0.027, poly.threshold_x[0],0.001)
@@ -88,7 +91,7 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
88
91
  assert_in_delta(1.137, poly.threshold_x[1],0.001)
89
92
 
90
93
  # Example for Tallis(1962, cited by Drasgow, 2006)
91
-
94
+
92
95
  matrix=Matrix[[58,52,1],[26,58,3],[8,12,9]]
93
96
  poly=Statsample::Bivariate::Polychoric.new(matrix)
94
97
  poly.compute_two_step_mle_drasgow_gsl
@@ -98,11 +101,11 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
98
101
  assert_in_delta(1.578, poly.threshold_y[1],0.001)
99
102
  assert_in_delta(1.137, poly.threshold_x[1],0.001)
100
103
 
101
-
104
+
102
105
  poly.method=:joint
103
106
  poly.compute
104
-
105
-
107
+
108
+
106
109
  assert_in_delta(0.4192, poly.r, 0.0001)
107
110
  assert_in_delta(-0.2421, poly.threshold_y[0],0.0001)
108
111
  assert_in_delta(-0.0297, poly.threshold_x[0],0.0001)
@@ -113,7 +116,7 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
113
116
  end
114
117
  assert(poly.summary)
115
118
  end
116
-
119
+
117
120
  def test_tetrachoric
118
121
  a,b,c,d=0,0,0,0
119
122
  assert_raises RuntimeError do
@@ -135,7 +138,7 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
135
138
  tc = Statsample::Bivariate::Tetrachoric.new(a,b,c,d)
136
139
  assert_equal(-1,tc.r)
137
140
  assert_equal(0,tc.se)
138
-
141
+
139
142
  a,b,c,d = 30,40,70,20
140
143
  tc = Statsample::Bivariate::Tetrachoric.new(a,b,c,d)
141
144
  assert_in_delta(-0.53980,tc.r,0.0001)
@@ -162,9 +165,9 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
162
165
  v3=[6,2, 1000,1000,5,4,7,8].to_vector(:scale)
163
166
  v4=[2,nil,nil,nil, 3,7,8,6].to_vector(:scale)
164
167
  ds={'v1'=>v1,'v2'=>v2,'v3'=>v3,'v4'=>v4}.to_dataset
165
- c=Proc.new {|n1,n2|Statsample::Bivariate.pearson(n1,n2)}
168
+ c=Proc.new {|n1,n2|Statsample::Bivariate.pearson(n1,n2)}
166
169
  expected=Matrix[ [c.call(v1,v1),c.call(v1,v2),c.call(v1,v3),c.call(v1,v4)], [c.call(v2,v1),c.call(v2,v2),c.call(v2,v3),c.call(v2,v4)], [c.call(v3,v1),c.call(v3,v2),c.call(v3,v3),c.call(v3,v4)],
167
- [c.call(v4,v1),c.call(v4,v2),c.call(v4,v3),c.call(v4,v4)]
170
+ [c.call(v4,v1),c.call(v4,v2),c.call(v4,v3),c.call(v4,v4)]
168
171
  ]
169
172
  obt=Statsample::Bivariate.correlation_matrix(ds)
170
173
  for i in 0...expected.row_size
@@ -193,12 +196,12 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
193
196
  assert(Statsample::Bivariate.prop_pearson(t,n,:right)>0.05)
194
197
  assert(Statsample::Bivariate.prop_pearson(t,n,:left)<0.05)
195
198
  end
196
-
199
+
197
200
  def test_spearman
198
201
  v1=[86,97,99,100,101,103,106,110,112,113].to_vector(:scale)
199
202
  v2=[0,20,28,27,50,29,7,17,6,12].to_vector(:scale)
200
203
  assert_in_delta(-0.175758,Statsample::Bivariate.spearman(v1,v2),0.0001)
201
-
204
+
202
205
  end
203
206
  def test_point_biserial
204
207
  c=[1,3,5,6,7,100,200,300,400,300].to_vector(:scale)
@@ -222,7 +225,7 @@ class StatsampleBivariateTestCase < MiniTest::Unit::TestCase
222
225
  assert_in_delta(0.636,Statsample::Bivariate.gamma(m),0.001)
223
226
  m2=Matrix[[15,12,6,5],[12,8,10,8],[4,6,9,10]]
224
227
  assert_in_delta(0.349,Statsample::Bivariate.gamma(m2),0.001)
225
-
226
-
228
+
229
+
227
230
  end
228
231
  end
@@ -1,21 +1,17 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'tempfile'
4
- require 'tmpdir'
5
- require 'test/unit'
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
6
2
 
7
- class StatsampleCodificationTestCase < Test::Unit::TestCase
3
+ class StatsampleCodificationTestCase < MiniTest::Unit::TestCase
8
4
 
9
5
  def initialize(*args)
10
6
  v1=%w{run walk,run walking running sleep sleeping,dreaming sleep,dream}.to_vector
11
7
  @dict={'run'=>'r','walk'=>'w','walking'=>'w','running'=>'r','sleep'=>'s', 'sleeping'=>'s', 'dream'=>'d', 'dreaming'=>'d'}
12
8
  @ds={"v1"=>v1}.to_dataset
13
- super
9
+ super
14
10
  end
15
11
  def test_create_hash
16
12
  expected_keys_v1=%w{run walk walking running sleep sleeping dream dreaming}.sort
17
13
  hash=Statsample::Codification.create_hash(@ds,['v1'])
18
- assert_equal(['v1'],hash.keys)
14
+ assert_equal(['v1'],hash.keys)
19
15
  assert_equal(expected_keys_v1,hash['v1'].keys.sort)
20
16
  assert_equal(expected_keys_v1,hash['v1'].values.sort)
21
17
  end
@@ -32,11 +28,11 @@ class StatsampleCodificationTestCase < Test::Unit::TestCase
32
28
  hash=Statsample::Codification.excel_to_recoded_hash(filename)
33
29
  assert_equal(keys.data, hash['v1'].keys.sort)
34
30
  assert_equal(keys.data, hash['v1'].values.sort)
35
-
31
+
36
32
  end
37
33
  def test_create_yaml
38
34
  assert_raise ArgumentError do
39
- Statsample::Codification.create_yaml(@ds,[])
35
+ Statsample::Codification.create_yaml(@ds,[])
40
36
  end
41
37
  expected_keys_v1=%w{run walk walking running sleep sleeping dream dreaming}.sort
42
38
  yaml_hash=Statsample::Codification.create_yaml(@ds,['v1'])
@@ -73,9 +69,9 @@ class StatsampleCodificationTestCase < Test::Unit::TestCase
73
69
  e['s']=[0,0,0,0,1,1,1].to_vector
74
70
  e['d']=[0,0,0,0,0,1,1].to_vector
75
71
  e.each{|k,expected|
76
- assert_equal(expected,@ds['v1_'+k],"Error on key #{k}")
77
-
72
+ assert_equal(expected,@ds['v1_'+k],"Error on key #{k}")
73
+
78
74
  }
79
75
  end
80
76
 
81
- end
77
+ end
@@ -1,42 +1,40 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'test/unit'
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
4
2
 
5
- class StatsampleCombinationTestCase < Test::Unit::TestCase
6
- def initialize(*args)
7
- super
8
- end
9
- def test_basic
10
- k=3
11
- n=5
12
- expected=[[0,1,2],[0,1,3],[0,1,4],[0,2,3],[0,2,4],[0,3,4],[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
13
- comb=Statsample::Combination.new(k,n)
14
- a=[]
15
- comb.each{|y|
16
- a.push(y)
17
- }
18
- assert_equal(expected,a)
19
- end
20
- def test_gsl_versus_ruby
21
- if Statsample.has_gsl?
22
- k=3
23
- n=10
24
- gsl=Statsample::Combination.new(k,n,false)
25
- gsl_array=[]
26
- gsl.each{|y|
27
- gsl_array.push(y)
28
- }
29
- rb=Statsample::Combination.new(k,n,true)
30
- rb_array=[]
31
- rb.each{|y|
32
- rb_array.push(y)
33
- }
34
- assert(gsl.d.is_a?(Statsample::Combination::CombinationGsl))
35
- assert(rb.d.is_a?(Statsample::Combination::CombinationRuby))
36
-
37
- assert_equal(rb_array,gsl_array)
3
+ class StatsampleCombinationTestCase < MiniTest::Unit::TestCase
4
+ def initialize(*args)
5
+ super
6
+ end
7
+ def test_basic
8
+ k=3
9
+ n=5
10
+ expected=[[0,1,2],[0,1,3],[0,1,4],[0,2,3],[0,2,4],[0,3,4],[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
11
+ comb=Statsample::Combination.new(k,n)
12
+ a=[]
13
+ comb.each{|y|
14
+ a.push(y)
15
+ }
16
+ assert_equal(expected,a)
17
+ end
18
+ def test_gsl_versus_ruby
19
+ if Statsample.has_gsl?
20
+ k=3
21
+ n=10
22
+ gsl=Statsample::Combination.new(k,n,false)
23
+ gsl_array=[]
24
+ gsl.each{|y|
25
+ gsl_array.push(y)
26
+ }
27
+ rb=Statsample::Combination.new(k,n,true)
28
+ rb_array=[]
29
+ rb.each{|y|
30
+ rb_array.push(y)
31
+ }
32
+ assert(gsl.d.is_a?(Statsample::Combination::CombinationGsl))
33
+ assert(rb.d.is_a?(Statsample::Combination::CombinationRuby))
34
+
35
+ assert_equal(rb_array,gsl_array)
38
36
  else
39
- puts "Not CombinationRuby vs CombinationGSL (no gsl)"
40
- end
37
+ puts "Not CombinationRuby vs CombinationGSL (no gsl)"
41
38
  end
42
- end
39
+ end
40
+ end
@@ -1,52 +1,50 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require 'test/unit'
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
4
2
 
5
- class StatsampleCrosstabTestCase < Test::Unit::TestCase
3
+ class StatsampleCrosstabTestCase < MiniTest::Unit::TestCase
6
4
 
7
- def initialize(*args)
8
- @v1=%w{black blonde black black red black brown black blonde black red black blonde}.to_vector
9
- @v2=%w{woman man man woman man man man woman man woman woman man man}.to_vector
10
- @ct=Statsample::Crosstab.new(@v1,@v2)
11
- super
12
- end
13
- def test_crosstab_errors
14
- e1=%w{black blonde black black red black brown black blonde black}
15
- assert_raise ArgumentError do
16
- Statsample::Crosstab.new(e1,@v2)
17
- end
18
- e2=%w{black blonde black black red black brown black blonde black black}.to_vector
19
-
20
- assert_raise ArgumentError do
21
- Statsample::Crosstab.new(e2,@v2)
22
- end
23
- assert_nothing_raised do
24
- Statsample::Crosstab.new(@v1,@v2)
25
- end
26
- end
27
- def test_crosstab_basic
28
- assert_equal(%w{black blonde brown red}, @ct.rows_names)
29
- assert_equal(%w{man woman}, @ct.cols_names)
30
- assert_equal({'black'=>7,'blonde'=>3,'red'=>2,'brown'=>1}, @ct.rows_total)
31
- assert_equal({'man'=>8,'woman'=>5}, @ct.cols_total)
32
- end
33
- def test_crosstab_frequencies
34
- fq=@ct.frequencies
35
- assert_equal(8,fq.size)
36
- sum=fq.inject(0) {|s,x| s+x[1]}
37
- assert_equal(13,sum)
38
- fr=@ct.frequencies_by_row
39
- assert_equal(4,fr.size)
40
- assert_equal(%w{black blonde brown red},fr.keys.sort)
41
- fc=@ct.frequencies_by_col
42
- assert_equal(2,fc.size)
43
- assert_equal(%w{man woman},fc.keys.sort)
44
- assert_equal(Matrix.rows([[3,4],[3,0],[1,0],[1,1]]),@ct.to_matrix)
45
- end
46
- def test_expected
47
- v1=%w{1 1 1 1 1 0 0 0 0 0}.to_vector
48
- v2=%w{0 0 0 0 0 1 1 1 1 1}.to_vector
49
- ct=Statsample::Crosstab.new(v1,v2)
50
- assert_equal(Matrix[[2.5,2.5],[2.5,2.5]],ct.matrix_expected)
5
+ def initialize(*args)
6
+ @v1=%w{black blonde black black red black brown black blonde black red black blonde}.to_vector
7
+ @v2=%w{woman man man woman man man man woman man woman woman man man}.to_vector
8
+ @ct=Statsample::Crosstab.new(@v1,@v2)
9
+ super
10
+ end
11
+ def test_crosstab_errors
12
+ e1=%w{black blonde black black red black brown black blonde black}
13
+ assert_raise ArgumentError do
14
+ Statsample::Crosstab.new(e1,@v2)
51
15
  end
16
+ e2=%w{black blonde black black red black brown black blonde black black}.to_vector
17
+
18
+ assert_raise ArgumentError do
19
+ Statsample::Crosstab.new(e2,@v2)
20
+ end
21
+ assert_nothing_raised do
22
+ Statsample::Crosstab.new(@v1,@v2)
23
+ end
24
+ end
25
+ def test_crosstab_basic
26
+ assert_equal(%w{black blonde brown red}, @ct.rows_names)
27
+ assert_equal(%w{man woman}, @ct.cols_names)
28
+ assert_equal({'black'=>7,'blonde'=>3,'red'=>2,'brown'=>1}, @ct.rows_total)
29
+ assert_equal({'man'=>8,'woman'=>5}, @ct.cols_total)
30
+ end
31
+ def test_crosstab_frequencies
32
+ fq=@ct.frequencies
33
+ assert_equal(8,fq.size)
34
+ sum=fq.inject(0) {|s,x| s+x[1]}
35
+ assert_equal(13,sum)
36
+ fr=@ct.frequencies_by_row
37
+ assert_equal(4,fr.size)
38
+ assert_equal(%w{black blonde brown red},fr.keys.sort)
39
+ fc=@ct.frequencies_by_col
40
+ assert_equal(2,fc.size)
41
+ assert_equal(%w{man woman},fc.keys.sort)
42
+ assert_equal(Matrix.rows([[3,4],[3,0],[1,0],[1,1]]),@ct.to_matrix)
43
+ end
44
+ def test_expected
45
+ v1=%w{1 1 1 1 1 0 0 0 0 0}.to_vector
46
+ v2=%w{0 0 0 0 0 1 1 1 1 1}.to_vector
47
+ ct=Statsample::Crosstab.new(v1,v2)
48
+ assert_equal(Matrix[[2.5,2.5],[2.5,2.5]],ct.matrix_expected)
49
+ end
52
50
  end
data/test/test_csv.rb CHANGED
@@ -1,46 +1,41 @@
1
- $:.unshift(File.dirname(__FILE__)+'/../lib/')
2
- require 'statsample'
3
- require "tempfile"
4
- require 'minitest/unit'
5
- MiniTest::Unit.autorun
6
- class StatsampleCSVTestCase < MiniTest::Unit::TestCase
7
- def setup
8
- @ds=Statsample::CSV.read(File.dirname(__FILE__)+"/test_csv.csv")
9
- end
10
- def test_read
11
- assert_equal(6,@ds.cases)
12
- assert_equal(%w{id name age city a1}, @ds.fields)
13
- id=[1,2,3,4,5,6].to_vector(:scale)
14
- name=["Alex","Claude","Peter","Franz","George","Fernand"].to_vector(:nominal)
15
- age=[20,23,25,27,5.5,nil].to_vector(:scale)
16
- city=["New York","London","London","Paris","Tome",nil].to_vector(:nominal)
17
- a1=["a,b","b,c","a",nil,"a,b,c",nil].to_vector(:nominal)
18
- ds_exp=Statsample::Dataset.new({'id'=>id,'name'=>name,'age'=>age,'city'=>city,'a1'=>a1}, %w{id name age city a1})
19
- ds_exp.fields.each{|f|
20
- assert_equal(ds_exp[f],@ds[f])
21
- }
22
- assert_equal(ds_exp,@ds)
23
-
1
+ require(File.dirname(__FILE__)+'/test_helpers.rb')
24
2
 
25
- end
26
- def test_nil
27
- assert_equal(nil,@ds['age'][5])
28
- end
29
- def test_repeated
30
- ds=Statsample::CSV.read(File.dirname(__FILE__)+"/../data/repeated_fields.csv")
31
- assert_equal(%w{id name_1 age_1 city a1 name_2 age_2},ds.fields)
32
- age=[3,4,5,6,nil,8].to_vector(:scale)
33
- assert_equal(age,ds['age_2'])
34
- end
35
- def test_write
36
- filename=Tempfile.new("afile")
37
- # filename=Dir::tmpdir+"/test_write.csv"
38
- Statsample::CSV.write(@ds, filename.path)
39
- ds2=Statsample::CSV.read(filename.path)
40
- i=0
41
- ds2.each_array{|row|
42
- assert_equal(@ds.case_as_array(i),row)
43
- i+=1
44
- }
45
- end
46
- end
3
+ class StatsampleCSVTestCase < MiniTest::Unit::TestCase
4
+ def setup
5
+ @ds=Statsample::CSV.read(File.dirname(__FILE__)+"/test_csv.csv")
6
+ end
7
+ def test_read
8
+ assert_equal(6,@ds.cases)
9
+ assert_equal(%w{id name age city a1}, @ds.fields)
10
+ id=[1,2,3,4,5,6].to_vector(:scale)
11
+ name=["Alex","Claude","Peter","Franz","George","Fernand"].to_vector(:nominal)
12
+ age=[20,23,25,27,5.5,nil].to_vector(:scale)
13
+ city=["New York","London","London","Paris","Tome",nil].to_vector(:nominal)
14
+ a1=["a,b","b,c","a",nil,"a,b,c",nil].to_vector(:nominal)
15
+ ds_exp=Statsample::Dataset.new({'id'=>id,'name'=>name,'age'=>age,'city'=>city,'a1'=>a1}, %w{id name age city a1})
16
+ ds_exp.fields.each{|f|
17
+ assert_equal(ds_exp[f],@ds[f])
18
+ }
19
+ assert_equal(ds_exp,@ds)
20
+ end
21
+ def test_nil
22
+ assert_equal(nil,@ds['age'][5])
23
+ end
24
+ def test_repeated
25
+ ds=Statsample::CSV.read(File.dirname(__FILE__)+"/../data/repeated_fields.csv")
26
+ assert_equal(%w{id name_1 age_1 city a1 name_2 age_2},ds.fields)
27
+ age=[3,4,5,6,nil,8].to_vector(:scale)
28
+ assert_equal(age,ds['age_2'])
29
+ end
30
+ def test_write
31
+ filename=Tempfile.new("afile")
32
+ # filename=Dir::tmpdir+"/test_write.csv"
33
+ Statsample::CSV.write(@ds, filename.path)
34
+ ds2=Statsample::CSV.read(filename.path)
35
+ i=0
36
+ ds2.each_array{|row|
37
+ assert_equal(@ds.case_as_array(i),row)
38
+ i+=1
39
+ }
40
+ end
41
+ end