statsample 0.6.5 → 0.6.7
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.
- data/History.txt +15 -0
- data/Manifest.txt +6 -0
- data/README.txt +30 -12
- data/Rakefile +91 -0
- data/demo/levene.rb +9 -0
- data/demo/multiple_regression.rb +1 -7
- data/demo/polychoric.rb +1 -0
- data/demo/principal_axis.rb +8 -0
- data/lib/distribution/f.rb +22 -22
- data/lib/spss.rb +99 -99
- data/lib/statsample/bivariate/polychoric.rb +32 -22
- data/lib/statsample/bivariate/tetrachoric.rb +212 -207
- data/lib/statsample/bivariate.rb +6 -6
- data/lib/statsample/codification.rb +65 -65
- data/lib/statsample/combination.rb +60 -59
- data/lib/statsample/converter/csv19.rb +12 -12
- data/lib/statsample/converters.rb +1 -1
- data/lib/statsample/dataset.rb +93 -36
- data/lib/statsample/dominanceanalysis/bootstrap.rb +66 -3
- data/lib/statsample/dominanceanalysis.rb +5 -6
- data/lib/statsample/factor/pca.rb +41 -11
- data/lib/statsample/factor/principalaxis.rb +105 -29
- data/lib/statsample/factor/rotation.rb +20 -3
- data/lib/statsample/factor.rb +1 -1
- data/lib/statsample/graph/gdchart.rb +13 -13
- data/lib/statsample/graph/svggraph.rb +166 -167
- data/lib/statsample/matrix.rb +22 -12
- data/lib/statsample/mle/logit.rb +3 -2
- data/lib/statsample/mle/probit.rb +7 -5
- data/lib/statsample/mle.rb +4 -2
- data/lib/statsample/multiset.rb +125 -124
- data/lib/statsample/permutation.rb +2 -1
- data/lib/statsample/regression/binomial/logit.rb +4 -3
- data/lib/statsample/regression/binomial/probit.rb +2 -1
- data/lib/statsample/regression/binomial.rb +62 -81
- data/lib/statsample/regression/multiple/baseengine.rb +1 -1
- data/lib/statsample/regression/multiple/gslengine.rb +1 -1
- data/lib/statsample/regression/multiple/matrixengine.rb +12 -6
- data/lib/statsample/regression/multiple.rb +15 -42
- data/lib/statsample/regression/simple.rb +93 -78
- data/lib/statsample/regression.rb +74 -2
- data/lib/statsample/reliability.rb +117 -120
- data/lib/statsample/srs.rb +156 -153
- data/lib/statsample/test/levene.rb +90 -0
- data/lib/statsample/test/umannwhitney.rb +25 -9
- data/lib/statsample/test.rb +2 -0
- data/lib/statsample/vector.rb +388 -413
- data/lib/statsample.rb +74 -30
- data/po/es/statsample.mo +0 -0
- data/test/test_bivariate.rb +5 -4
- data/test/test_combination.rb +1 -1
- data/test/test_dataset.rb +2 -2
- data/test/test_factor.rb +53 -6
- data/test/test_gsl.rb +1 -1
- data/test/test_mle.rb +1 -1
- data/test/test_regression.rb +18 -33
- data/test/test_statistics.rb +15 -33
- data/test/test_stest.rb +35 -0
- data/test/test_svg_graph.rb +2 -2
- data/test/test_vector.rb +331 -333
- metadata +38 -11
data/lib/statsample.rb
CHANGED
@@ -23,8 +23,8 @@ $:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext"))
|
|
23
23
|
|
24
24
|
require 'matrix'
|
25
25
|
require 'distribution'
|
26
|
+
raise "Install reportbuilder ~>0.2.0" unless gem 'reportbuilder','~>0.2.0'
|
26
27
|
require 'reportbuilder'
|
27
|
-
|
28
28
|
class Numeric
|
29
29
|
def square ; self * self ; end
|
30
30
|
end
|
@@ -88,28 +88,32 @@ rescue LoadError
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
# ++
|
105
|
-
# Modules for statistical analysis
|
106
|
-
# See first:
|
107
|
-
# * Converter : several modules to import and export data
|
108
|
-
# * Vector: The base class of all analysis
|
109
|
-
# * Dataset: An union of vectors.
|
91
|
+
# Library for statistical analysis on Ruby
|
92
|
+
#
|
93
|
+
# * Classes for manipulation and storage of data:
|
94
|
+
# * Module Statsample::Bivariate provides covariance and pearson, spearman, point biserial, tau a, tau b, gamma, tetrachoric (see Bivariate::Tetrachoric) and polychoric (see Bivariate::Polychoric) correlations. Include methods to create correlation and covariance matrices
|
95
|
+
# * Multiple types of regression on Statsample::Regression
|
96
|
+
# * Factorial Analysis algorithms on Statsample::Factor module.
|
97
|
+
# * Dominance Analysis. Based on Budescu and Azen papers.link[http://psycnet.apa.org/journals/met/8/2/129/].
|
98
|
+
# * Module Statsample::Codification, to help to codify open questions
|
99
|
+
# * Converters to import and export data from databases, csv and excel files.
|
100
|
+
# * Module Statsample::Crosstab provides function to create crosstab for categorical data
|
101
|
+
# * Reliability analysis provides functions to analyze scales.
|
102
|
+
# * Module Statsample::SRS (Simple Random Sampling) provides a lot of functions to estimate standard error for several type of samples
|
103
|
+
# * Interfaces to gdchart, gnuplot and SVG::Graph
|
110
104
|
#
|
111
105
|
module Statsample
|
112
|
-
|
106
|
+
begin
|
107
|
+
require 'rbgsl'
|
108
|
+
def self.has_gsl?
|
109
|
+
true
|
110
|
+
end
|
111
|
+
rescue LoadError
|
112
|
+
def self.has_gsl?
|
113
|
+
false
|
114
|
+
end
|
115
|
+
end
|
116
|
+
VERSION = '0.6.7'
|
113
117
|
SPLIT_TOKEN = ","
|
114
118
|
autoload(:Database, 'statsample/converters')
|
115
119
|
autoload(:Anova, 'statsample/anova')
|
@@ -137,16 +141,56 @@ module Statsample
|
|
137
141
|
autoload(:Test, 'statsample/test')
|
138
142
|
autoload(:Factor, 'statsample/factor')
|
139
143
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
144
|
+
|
145
|
+
|
146
|
+
class << self
|
147
|
+
# Load a object saved on a file.
|
148
|
+
def load(filename)
|
149
|
+
if File.exists? filename
|
150
|
+
o=false
|
151
|
+
File.open(filename,"r") {|fp| o=Marshal.load(fp) }
|
152
|
+
o
|
153
|
+
else
|
154
|
+
false
|
155
|
+
end
|
147
156
|
end
|
148
|
-
|
149
|
-
|
157
|
+
# Create a matrix using vectors as columns.
|
158
|
+
# Use:
|
159
|
+
#
|
160
|
+
# matrix=Statsample.vector_cols_matrix(v1,v2)
|
161
|
+
def vector_cols_matrix(*vs)
|
162
|
+
# test
|
163
|
+
size=vs[0].size
|
164
|
+
vs.each{|v|
|
165
|
+
raise ArgumentError,"Arguments should be Vector" unless v.instance_of? Statsample::Vector
|
166
|
+
raise ArgumentError,"Vectors size should be the same" if v.size!=size
|
167
|
+
}
|
168
|
+
Matrix.rows((0...size).to_a.collect() {|i|
|
169
|
+
vs.collect{|v| v[i]}
|
170
|
+
})
|
171
|
+
end
|
172
|
+
# Returns a duplicate of the input vectors, without missing data
|
173
|
+
# for any of the vectors.
|
174
|
+
#
|
175
|
+
# a=[1,2,3,6,7,nil,3,5].to_scale
|
176
|
+
# b=[nil,nil,5,6,4,5,10,2].to_scale
|
177
|
+
# c=[2,4,6,7,4,5,6,7].to_scale
|
178
|
+
# a2,b2,c2=Statsample.only_valid(a,b,c)
|
179
|
+
# => [#<Statsample::Scale:0xb748c8c8 @data=[3, 6, 7, 3, 5]>,
|
180
|
+
# #<Statsample::Scale:0xb748c814 @data=[5, 6, 4, 10, 2]>,
|
181
|
+
# #<Statsample::Scale:0xb748c760 @data=[6, 7, 4, 6, 7]>]
|
182
|
+
#
|
183
|
+
def only_valid(*vs)
|
184
|
+
i=1
|
185
|
+
h=vs.inject({}) {|a,v| a["v#{i}"]=v;i+=1;a}
|
186
|
+
ds=Statsample::Dataset.new(h).dup_only_valid
|
187
|
+
ds.vectors.values
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
150
194
|
module Util
|
151
195
|
# Reference: http://www.itl.nist.gov/div898/handbook/eda/section3/normprpl.htm
|
152
196
|
def normal_order_statistic_medians(i,n)
|
data/po/es/statsample.mo
ADDED
Binary file
|
data/test/test_bivariate.rb
CHANGED
@@ -33,7 +33,7 @@ class StatsampleBivariateTestCase < Test::Unit::TestCase
|
|
33
33
|
poly = Statsample::Bivariate::Polychoric.new(matrix)
|
34
34
|
poly.compute_two_step_mle_drasgow_ruby
|
35
35
|
assert_in_delta(tetra.r,poly.r,0.0001)
|
36
|
-
if
|
36
|
+
if Statsample.has_gsl?
|
37
37
|
poly.compute_two_step_mle_drasgow_gsl
|
38
38
|
assert_in_delta(tetra.r,poly.r,0.0001)
|
39
39
|
end
|
@@ -59,7 +59,7 @@ class StatsampleBivariateTestCase < Test::Unit::TestCase
|
|
59
59
|
|
60
60
|
|
61
61
|
|
62
|
-
if
|
62
|
+
if Statsample.has_gsl?
|
63
63
|
poly.method=:polychoric_series
|
64
64
|
poly.compute
|
65
65
|
|
@@ -93,6 +93,7 @@ class StatsampleBivariateTestCase < Test::Unit::TestCase
|
|
93
93
|
else
|
94
94
|
puts "Two-step optimized, polychoric series and Joint method for Polychoric requires GSL"
|
95
95
|
end
|
96
|
+
assert(poly.summary)
|
96
97
|
end
|
97
98
|
def test_tetrachoric
|
98
99
|
a,b,c,d=0,0,0,0
|
@@ -134,7 +135,7 @@ class StatsampleBivariateTestCase < Test::Unit::TestCase
|
|
134
135
|
tc2 = Statsample::Bivariate::Tetrachoric.new_with_vectors(x,y)
|
135
136
|
assert_equal(tc1.r,tc2.r)
|
136
137
|
assert_equal(tc1.se,tc2.se)
|
137
|
-
|
138
|
+
assert(tc.summary)
|
138
139
|
end
|
139
140
|
def test_matrix_correlation
|
140
141
|
v1=[6,5,4,7,8,4,3,2].to_vector(:scale)
|
@@ -174,7 +175,7 @@ class StatsampleBivariateTestCase < Test::Unit::TestCase
|
|
174
175
|
assert(Statsample::Bivariate.prop_pearson(t,n,:left)<0.05)
|
175
176
|
end
|
176
177
|
def test_covariance
|
177
|
-
if
|
178
|
+
if Statsample.has_gsl?
|
178
179
|
v1=[6,5,4,7,8,4,3,2].to_vector(:scale)
|
179
180
|
v2=[2,3,7,8,6,4,3,2].to_vector(:scale)
|
180
181
|
assert_in_delta(Statsample::Bivariate.covariance(v1,v2), Statsample::Bivariate.covariance_slow(v1,v2), 0.001)
|
data/test/test_combination.rb
CHANGED
data/test/test_dataset.rb
CHANGED
@@ -13,7 +13,7 @@ class StatsampleDatasetTestCase < Test::Unit::TestCase
|
|
13
13
|
assert_equal(%w{id name age city a1}, @ds.fields)
|
14
14
|
end
|
15
15
|
def test_saveload
|
16
|
-
outfile=Tempfile.new("
|
16
|
+
outfile=Tempfile.new("dataset.ds")
|
17
17
|
@ds.save(outfile.path)
|
18
18
|
a=Statsample.load(outfile.path)
|
19
19
|
assert_equal(@ds,a)
|
@@ -389,4 +389,4 @@ class StatsampleDatasetTestCase < Test::Unit::TestCase
|
|
389
389
|
|
390
390
|
end
|
391
391
|
|
392
|
-
end
|
392
|
+
end
|
data/test/test_factor.rb
CHANGED
@@ -2,11 +2,12 @@ $:.unshift(File.dirname(__FILE__)+'/../lib/')
|
|
2
2
|
require 'statsample'
|
3
3
|
require 'test/unit'
|
4
4
|
class StatsampleFactorTestCase < Test::Unit::TestCase
|
5
|
+
# Tested with SPSS and R
|
5
6
|
def test_pca
|
6
|
-
if
|
7
|
+
if Statsample.has_gsl?
|
7
8
|
require 'gsl'
|
8
9
|
a=[2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1].to_scale
|
9
|
-
b=[2.4,0.7,2.9,2.2,3.0,2.7,1.6,1.1,1.6,0.9].to_scale
|
10
|
+
b=[2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9].to_scale
|
10
11
|
a.recode! {|c| c-a.mean}
|
11
12
|
b.recode! {|c| c-b.mean}
|
12
13
|
ds={'a'=>a,'b'=>b}.to_dataset
|
@@ -16,20 +17,66 @@ class StatsampleFactorTestCase < Test::Unit::TestCase
|
|
16
17
|
expected_eigenvalues.each_with_index{|ev,i|
|
17
18
|
assert_in_delta(ev,pca.eigenvalues[i],0.001)
|
18
19
|
}
|
20
|
+
expected_communality=[0.590, 0.694]
|
21
|
+
expected_communality.each_with_index{|ev,i|
|
22
|
+
assert_in_delta(ev,pca.communalities[i],0.001)
|
23
|
+
}
|
24
|
+
expected_cm=[0.768, 0.833]
|
25
|
+
|
26
|
+
obs=pca.component_matrix(1).column(0).to_a
|
27
|
+
expected_cm.each_with_index{|ev,i|
|
28
|
+
assert_in_delta(ev,obs[i],0.001)
|
29
|
+
}
|
30
|
+
|
19
31
|
expected_fm_1=GSL::Matrix[[0.677], [0.735]]
|
20
32
|
expected_fm_2=GSL::Matrix[[0.677,0.735], [0.735, -0.677]]
|
21
33
|
_test_matrix(expected_fm_1,pca.feature_vector(1))
|
22
34
|
_test_matrix(expected_fm_2,pca.feature_vector(2))
|
35
|
+
assert(pca.summary)
|
23
36
|
else
|
24
37
|
puts "PCA not tested. Requires GSL"
|
25
38
|
end
|
26
39
|
end
|
40
|
+
|
41
|
+
# Tested with R
|
42
|
+
def test_principalaxis
|
43
|
+
if Statsample.has_gsl?
|
44
|
+
require 'gsl'
|
45
|
+
matrix=Matrix[
|
46
|
+
[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]]
|
47
|
+
fa=Statsample::Factor::PrincipalAxis.new(matrix,:m=>1)
|
48
|
+
cm=Matrix[[0.923],[0.912],[0.507],[0.483]].to_gsl
|
49
|
+
_test_matrix(cm,fa.component_matrix)
|
50
|
+
h2=[0.852,0.832,0.257,0.233]
|
51
|
+
h2.each_with_index{|ev,i|
|
52
|
+
assert_in_delta(ev,fa.communalities[i],0.001)
|
53
|
+
}
|
54
|
+
eigen1=2.175
|
55
|
+
assert_in_delta(eigen1, fa.eigenvalues[0],0.001)
|
56
|
+
|
57
|
+
fa=Statsample::Factor::PrincipalAxis.new(matrix,:smc=>false)
|
58
|
+
assert_raise RuntimeError do
|
59
|
+
fa.iterate
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
assert(fa.summary)
|
66
|
+
|
67
|
+
else
|
68
|
+
puts "Principal Axis not tested. Requires GSL"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
27
73
|
def test_rotation_varimax
|
28
|
-
if
|
74
|
+
if Statsample.has_gsl?
|
29
75
|
a = Matrix[ [ 0.4320, 0.8129, 0.3872] ,
|
30
76
|
[0.7950, -0.5416, 0.2565] ,
|
31
77
|
[0.5944, 0.7234, -0.3441],
|
32
78
|
[0.8945, -0.3921, -0.1863] ]
|
79
|
+
|
33
80
|
expected= Matrix[[-0.0204423, 0.938674, -0.340334],
|
34
81
|
[0.983662, 0.0730206, 0.134997],
|
35
82
|
[0.0826106, 0.435975, -0.893379],
|
@@ -37,9 +84,9 @@ class StatsampleFactorTestCase < Test::Unit::TestCase
|
|
37
84
|
varimax=Statsample::Factor::Varimax.new(a)
|
38
85
|
varimax.iterate
|
39
86
|
_test_matrix(expected,varimax.rotated)
|
40
|
-
|
41
|
-
|
42
|
-
|
87
|
+
else
|
88
|
+
puts "Rotation not tested. Requires GSL"
|
89
|
+
end
|
43
90
|
end
|
44
91
|
def _test_matrix(a,b)
|
45
92
|
a.size1.times {|i|
|
data/test/test_gsl.rb
CHANGED
data/test/test_mle.rb
CHANGED
@@ -49,7 +49,7 @@ class StatsampleMLETestCase < Test::Unit::TestCase
|
|
49
49
|
#p coeffs_nr
|
50
50
|
ds=@ds_indep.dup
|
51
51
|
ds.add_vector('y',y)
|
52
|
-
lr=Statsample::Regression
|
52
|
+
lr=Statsample::Regression.multiple(ds,'y')
|
53
53
|
lr_constant = lr.constant
|
54
54
|
lr_coeffs = lr.coeffs
|
55
55
|
assert_in_delta(coeffs_nr[0,0], lr_constant,0.0000001)
|
data/test/test_regression.rb
CHANGED
@@ -2,17 +2,24 @@ $:.unshift(File.dirname(__FILE__)+'/../lib/')
|
|
2
2
|
require 'statsample'
|
3
3
|
require 'test/unit'
|
4
4
|
class StatsampleRegressionTestCase < Test::Unit::TestCase
|
5
|
-
def initialize(*args)
|
6
|
-
@x=[13,20,10,33,15].to_vector(:scale)
|
7
|
-
@y=[23,18,35,10,27 ].to_vector(:scale)
|
8
|
-
@reg=Statsample::Regression::Simple.new_from_vectors(@x,@y)
|
9
|
-
super
|
10
|
-
end
|
11
5
|
def test_parameters
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
@x=[13,20,10,33,15].to_vector(:scale)
|
7
|
+
@y=[23,18,35,10,27 ].to_vector(:scale)
|
8
|
+
reg=Statsample::Regression::Simple.new_from_vectors(@x,@y)
|
9
|
+
_test_simple_regression(reg)
|
10
|
+
ds={'x'=>@x,'y'=>@y}.to_dataset
|
11
|
+
reg=Statsample::Regression::Simple.new_from_dataset(ds,'x','y')
|
12
|
+
_test_simple_regression(reg)
|
13
|
+
reg=Statsample::Regression.simple(@x,@y)
|
14
|
+
_test_simple_regression(reg)
|
15
|
+
|
15
16
|
end
|
17
|
+
def _test_simple_regression(reg)
|
18
|
+
assert_in_delta(40.009, reg.a,0.001)
|
19
|
+
assert_in_delta(-0.957, reg.b,0.001)
|
20
|
+
assert_in_delta(4.248,reg.standard_error,0.002)
|
21
|
+
end
|
22
|
+
|
16
23
|
def test_multiple_dependent
|
17
24
|
complete=Matrix[
|
18
25
|
[1,0.53,0.62,0.19,-0.09,0.08,0.02,-0.12,0.08],
|
@@ -54,7 +61,7 @@ class StatsampleRegressionTestCase < Test::Unit::TestCase
|
|
54
61
|
|
55
62
|
|
56
63
|
def test_multiple_regression_gsl
|
57
|
-
if
|
64
|
+
if Statsample.has_gsl?
|
58
65
|
@a=[1,3,2,4,3,5,4,6,5,7].to_vector(:scale)
|
59
66
|
@b=[3,3,4,4,5,5,6,6,4,4].to_vector(:scale)
|
60
67
|
@c=[11,22,30,40,50,65,78,79,99,100].to_vector(:scale)
|
@@ -78,29 +85,7 @@ class StatsampleRegressionTestCase < Test::Unit::TestCase
|
|
78
85
|
end
|
79
86
|
|
80
87
|
|
81
|
-
|
82
|
-
if HAS_ALGIB
|
83
|
-
@a=[1,3,2,4,3,5,4,6,5,7].to_vector(:scale)
|
84
|
-
@b=[3,3,4,4,5,5,6,6,4,4].to_vector(:scale)
|
85
|
-
@c=[11,22,30,40,50,65,78,79,99,100].to_vector(:scale)
|
86
|
-
@y=[3,4,5,6,7,8,9,10,20,30].to_vector(:scale)
|
87
|
-
ds={'a'=>@a,'b'=>@b,'c'=>@c,'y'=>@y}.to_dataset
|
88
|
-
lr=Statsample::Regression::Multiple::AlglibEngine.new(ds,'y')
|
89
|
-
model_test(lr,'alglib')
|
90
|
-
predicted=[1.7857, 6.0989, 3.2433, 7.2908, 4.9667, 10.3428, 8.8158, 10.4717, 23.6639, 25.3198]
|
91
|
-
c_predicted=lr.predicted
|
92
|
-
predicted.each_index{|i|
|
93
|
-
assert_in_delta(predicted[i],c_predicted[i],0.001)
|
94
|
-
}
|
95
|
-
residuals=[1.2142, -2.0989, 1.7566, -1.29085, 2.033, -2.3428, 0.18414, -0.47177, -3.66395, 4.6801]
|
96
|
-
c_residuals=lr.residuals
|
97
|
-
residuals.each_index{|i|
|
98
|
-
assert_in_delta(residuals[i],c_residuals[i],0.001)
|
99
|
-
}
|
100
|
-
else
|
101
|
-
puts "Regression::Multiple::AlglibEngine not tested (no Alglib)"
|
102
|
-
end
|
103
|
-
end
|
88
|
+
|
104
89
|
def model_test_matrix(lr,name='undefined')
|
105
90
|
|
106
91
|
stan_coeffs={'a'=>0.151,'b'=>-0.547,'c'=>0.997}
|
data/test/test_statistics.rb
CHANGED
@@ -3,9 +3,9 @@ require 'statsample'
|
|
3
3
|
require 'test/unit'
|
4
4
|
class StatsampleStatisicsTestCase < Test::Unit::TestCase
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def initialize(*args)
|
7
|
+
super
|
8
|
+
end
|
9
9
|
def test_recode_repeated
|
10
10
|
a=%w{a b c c d d d e}
|
11
11
|
exp=["a","b","c_1","c_2","d_1","d_2","d_3","e"]
|
@@ -21,19 +21,11 @@ class StatsampleStatisicsTestCase < Test::Unit::TestCase
|
|
21
21
|
assert(!"1212-1212-1".is_number?)
|
22
22
|
assert(!"a10".is_number?)
|
23
23
|
assert(!"".is_number?)
|
24
|
-
|
25
|
-
end
|
26
|
-
def test_chi_square
|
27
|
-
real=Matrix[[95,95],[45,155]]
|
28
|
-
expected=Matrix[[68,122],[72,128]]
|
29
|
-
assert_nothing_raised do
|
30
|
-
chi=Statsample::Test.chi_square(real,expected)
|
31
|
-
end
|
32
|
-
chi=Statsample::Test.chi_square(real,expected)
|
33
|
-
assert_in_delta(32.53,chi,0.1)
|
24
|
+
|
34
25
|
end
|
35
|
-
|
36
|
-
|
26
|
+
|
27
|
+
|
28
|
+
def test_estimation_mean
|
37
29
|
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(:scale)
|
38
30
|
assert_equal(50,v.size)
|
39
31
|
assert_equal(1471,v.sum())
|
@@ -45,7 +37,7 @@ class StatsampleStatisicsTestCase < Test::Unit::TestCase
|
|
45
37
|
sam=200
|
46
38
|
prop=0.19
|
47
39
|
assert_in_delta(81.8, Statsample::SRS.proportion_total_sd_ep_wor(prop, sam, pop), 0.1)
|
48
|
-
|
40
|
+
|
49
41
|
# confidence limits
|
50
42
|
pop=500
|
51
43
|
sam=100
|
@@ -57,13 +49,15 @@ class StatsampleStatisicsTestCase < Test::Unit::TestCase
|
|
57
49
|
end
|
58
50
|
def test_ml
|
59
51
|
if(true)
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
52
|
+
real=[1,1,1,1].to_vector(:scale)
|
53
|
+
|
54
|
+
pred=[0.0001,0.0001,0.0001,0.0001].to_vector(:scale)
|
55
|
+
# puts Statsample::Bivariate.maximum_likehood_dichotomic(pred,real)
|
56
|
+
|
65
57
|
end
|
66
58
|
end
|
59
|
+
|
60
|
+
|
67
61
|
def test_simple_linear_regression
|
68
62
|
a=[1,2,3,4,5,6].to_vector(:scale)
|
69
63
|
b=[6,2,4,10,12,8].to_vector(:scale)
|
@@ -74,17 +68,5 @@ class StatsampleStatisicsTestCase < Test::Unit::TestCase
|
|
74
68
|
assert_in_delta(1.314,reg.b,0.001)
|
75
69
|
assert_in_delta(0.657,reg.r,0.001)
|
76
70
|
assert_in_delta(0.432,reg.r2,0.001)
|
77
|
-
end
|
78
|
-
def test_u_mannwhitney
|
79
|
-
a=[1,2,3,4,5,6].to_scale
|
80
|
-
b=[0,5,7,9,10,11].to_scale
|
81
|
-
assert_equal(7.5, Statsample::Test.u_mannwhitney(a,b).u)
|
82
|
-
assert_equal(7.5, Statsample::Test.u_mannwhitney(b,a).u)
|
83
|
-
a=[1, 7,8,9,10,11].to_scale
|
84
|
-
b=[2,3,4,5,6,12].to_scale
|
85
|
-
assert_equal(11, Statsample::Test.u_mannwhitney(a,b).u)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
71
|
end
|
90
72
|
end
|
data/test/test_stest.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)+'/../lib/')
|
2
|
+
require 'statsample'
|
3
|
+
require "tempfile"
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class StatsampleStatisticalTestCase < Test::Unit::TestCase
|
7
|
+
def test_chi_square
|
8
|
+
real=Matrix[[95,95],[45,155]]
|
9
|
+
expected=Matrix[[68,122],[72,128]]
|
10
|
+
assert_nothing_raised do
|
11
|
+
chi=Statsample::Test.chi_square(real,expected)
|
12
|
+
end
|
13
|
+
chi=Statsample::Test.chi_square(real,expected)
|
14
|
+
assert_in_delta(32.53,chi,0.1)
|
15
|
+
end
|
16
|
+
def test_u_mannwhitney
|
17
|
+
a=[1,2,3,4,5,6].to_scale
|
18
|
+
b=[0,5,7,9,10,11].to_scale
|
19
|
+
assert_equal(7.5, Statsample::Test.u_mannwhitney(a,b).u)
|
20
|
+
assert_equal(7.5, Statsample::Test.u_mannwhitney(b,a).u)
|
21
|
+
a=[1, 7,8,9,10,11].to_scale
|
22
|
+
b=[2,3,4,5,6,12].to_scale
|
23
|
+
assert_equal(11, Statsample::Test.u_mannwhitney(a,b).u)
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def test_levene
|
28
|
+
|
29
|
+
a=[1,2,3,4,5,6,7,8,100,10].to_scale
|
30
|
+
b=[30,40,50,60,70,80,90,100,110,120].to_scale
|
31
|
+
levene=Statsample::Test::Levene.new([a,b])
|
32
|
+
assert_in_delta(0.778, levene.f, 0.001)
|
33
|
+
assert_in_delta(0.389, levene.probability, 0.001)
|
34
|
+
end
|
35
|
+
end
|
data/test/test_svg_graph.rb
CHANGED
@@ -15,7 +15,7 @@ class StatsampleSvgGraphTestCase < Test::Unit::TestCase
|
|
15
15
|
super
|
16
16
|
end
|
17
17
|
def test_histogram
|
18
|
-
if
|
18
|
+
if Statsample.has_gsl?
|
19
19
|
ar=(1..1000).to_a.collect {|a|
|
20
20
|
rand(10)
|
21
21
|
}.to_vector(:scale)
|
@@ -47,7 +47,7 @@ class StatsampleSvgGraphTestCase < Test::Unit::TestCase
|
|
47
47
|
vector.svggraph_frequencies(file,800,600,SVG::Graph::PieNoOp,:graph_title=>'Pie')
|
48
48
|
assert(File.exists?(file))
|
49
49
|
vector.type=:scale
|
50
|
-
if
|
50
|
+
if Statsample.has_gsl?
|
51
51
|
file=Tempfile.new("svg_histogram.svg").path
|
52
52
|
hist=vector.svggraph_histogram(5)
|
53
53
|
File.open(file,"wb") {|fp|
|