statsample-bivariate-extension 0.16.0 → 0.16.1
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/History.txt +4 -0
- data/Manifest.txt +3 -3
- data/Rakefile +0 -11
- data/lib/statsample/bivariate/extension_version.rb +1 -1
- data/lib/statsample/bivariate/tetrachoric.rb +51 -22
- data/spec/{statsample/bivariate/polychoric_processor_spec.rb → polychoric_processor_spec.rb} +7 -6
- data/spec/{statsample/bivariate/polychoric_spec.rb → polychoric_spec.rb} +0 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/{statsample/bivariate/tetrachoric_spec.rb → tetrachoric_spec.rb} +26 -13
- metadata +11 -27
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
data/History.txt
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
=== 0.16.1 / 2010-12-22
|
2
|
+
|
3
|
+
* Added support for optimized tetrachoric correlation calculation. Update statsample-optimization before report any error, please.
|
4
|
+
|
1
5
|
=== 0.16.0 / 2010-11-14
|
2
6
|
* Bug fix: Specs work without gsl. Counts iterations when using only ruby
|
3
7
|
* Updated specs to rspec 2
|
data/Manifest.txt
CHANGED
@@ -10,8 +10,8 @@ lib/statsample/bivariate/polychoric.rb
|
|
10
10
|
lib/statsample/bivariate/polychoric/processor.rb
|
11
11
|
lib/statsample/bivariate/tetrachoric.rb
|
12
12
|
references.txt
|
13
|
+
spec/polychoric_processor_spec.rb
|
14
|
+
spec/polychoric_spec.rb
|
13
15
|
spec/spec.opts
|
14
16
|
spec/spec_helper.rb
|
15
|
-
spec/
|
16
|
-
spec/statsample/bivariate/polychoric_spec.rb
|
17
|
-
spec/statsample/bivariate/tetrachoric_spec.rb
|
17
|
+
spec/tetrachoric_spec.rb
|
data/Rakefile
CHANGED
@@ -17,15 +17,4 @@ Hoe.spec 'statsample-bivariate-extension' do
|
|
17
17
|
self.developer('Claudio Bustos', 'clbustos_at_gmail.com')
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
desc "Run all spec with RCov"
|
23
|
-
RSpec::Core::RakeTask.new do |t|
|
24
|
-
t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
|
25
|
-
t.pattern = 'spec/**/*_spec.rb'
|
26
|
-
#t.rcov = true
|
27
|
-
#t.rcov_opts = ['--exclude', 'spec']
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
20
|
# vim: syntax=ruby
|
@@ -67,11 +67,16 @@ module Statsample
|
|
67
67
|
# * Uebersax, J.S. (2006). The tetrachoric and polychoric correlation coefficients. Statistical Methods for Rater Agreement web site. 2006. Available at: http://john-uebersax.com/stat/tetra.htm . Accessed February, 11, 2010
|
68
68
|
|
69
69
|
class Tetrachoric
|
70
|
-
|
70
|
+
RequerimentNotMeet=Class.new(Exception)
|
71
71
|
include Summarizable
|
72
|
+
# Value for tethrachoric correlation
|
72
73
|
attr_reader :r
|
74
|
+
# Name on the analysis
|
73
75
|
attr_accessor :name
|
74
|
-
|
76
|
+
# Use ruby version of algorithm.
|
77
|
+
# By default, this attribute is set to false,
|
78
|
+
# and C version of algorithm is used
|
79
|
+
attr_accessor :ruby_engine
|
75
80
|
TWOPI=Math::PI*2
|
76
81
|
SQT2PI= 2.50662827
|
77
82
|
RLIMIT = 0.9999
|
@@ -85,12 +90,12 @@ module Statsample
|
|
85
90
|
X=[0,0.9972638618, 0.9856115115, 0.9647622556, 0.9349060759, 0.8963211558, 0.8493676137, 0.7944837960, 0.7321821187, 0.6630442669, 0.5877157572, 0.5068999089, 0.4213512761, 0.3318686023, 0.2392873623, 0.1444719616, 0.0483076657]
|
86
91
|
W=[0, 0.0070186100, 0.0162743947, 0.0253920653, 0.0342738629, 0.0428358980, 0.0509980593, 0.0586840935, 0.0658222228, 0.0723457941, 0.0781938958, 0.0833119242, 0.0876520930, 0.0911738787, 0.0938443991, 0.0956387201, 0.0965400885]
|
87
92
|
# Creates a Tetrachoric object based on a 2x2 Matrix.
|
88
|
-
def self.new_with_matrix(m)
|
89
|
-
Tetrachoric.new(m[0,0], m[0,1], m[1,0],m[1,1])
|
93
|
+
def self.new_with_matrix(m, opts=Hash.new)
|
94
|
+
Tetrachoric.new(m[0,0], m[0,1], m[1,0],m[1,1], opts)
|
90
95
|
end
|
91
96
|
# Creates a Tetrachoric object based on two vectors.
|
92
97
|
# The vectors are dichotomized previously.
|
93
|
-
def self.new_with_vectors(v1,v2)
|
98
|
+
def self.new_with_vectors(v1,v2, opts=Hash.new)
|
94
99
|
v1a, v2a=Statsample.only_valid(v1,v2)
|
95
100
|
v1a=v1a.dichotomize
|
96
101
|
v2a=v2a.dichotomize
|
@@ -104,7 +109,7 @@ module Statsample
|
|
104
109
|
c+=1 if x==1 and y==0
|
105
110
|
d+=1 if x==1 and y==1
|
106
111
|
}
|
107
|
-
Tetrachoric.new(a,b,c,d)
|
112
|
+
Tetrachoric.new(a,b,c,d, opts)
|
108
113
|
end
|
109
114
|
# Standard error
|
110
115
|
def se
|
@@ -138,34 +143,42 @@ module Statsample
|
|
138
143
|
end
|
139
144
|
|
140
145
|
# Creates a new tetrachoric object for analysis
|
141
|
-
def initialize(a,b,c,d)
|
146
|
+
def initialize(a,b,c,d, opts=Hash.new)
|
142
147
|
@a,@b,@c,@d=a,b,c,d
|
143
|
-
|
148
|
+
|
149
|
+
opts_default={
|
150
|
+
:name=>_("Tetrachoric correlation"),
|
151
|
+
:ruby_engine=>false
|
152
|
+
}
|
153
|
+
|
154
|
+
@opts=opts_default.merge opts
|
155
|
+
@opts.each{|k,v| self.send("#{k}=",v) if self.respond_to? k}
|
144
156
|
#
|
145
157
|
# CHECK IF ANY CELL FREQUENCY IS NEGATIVE
|
146
158
|
#
|
147
159
|
raise "All frequencies should be positive" if (@a < 0 or @b < 0 or @c < 0 or @d < 0)
|
148
160
|
compute
|
149
161
|
end
|
150
|
-
# Compute the tetrachoric correlation.
|
151
|
-
# Called on object creation.
|
152
|
-
#
|
153
162
|
def compute
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
163
|
+
if !ruby_engine and Statsample::OPTIMIZED and Statsample::STATSAMPLE__.respond_to? :tetrachoric
|
164
|
+
compute_optimized
|
165
|
+
else
|
166
|
+
compute_ruby
|
167
|
+
end
|
168
|
+
end
|
169
|
+
# Compute the tetrachoric correlation
|
170
|
+
def compute_optimized
|
171
|
+
check_frequencies
|
172
|
+
t=Statsample::STATSAMPLE__.tetrachoric(@a,@b,@c,@d)
|
173
|
+
raise "Error on calculation of tetrachoric correlation: #{t['ifault']}" if t['ifault']>0
|
174
|
+
@r,@sdr,@itype,@ifault,@zab, @zac = t['r'],t['sdr'],t['itype'],t['ifault'], t['threshold_x'], t['threshold_y']
|
175
|
+
end
|
176
|
+
def check_frequencies
|
164
177
|
#
|
165
178
|
# CHECK IF ANY FREQUENCY IS 0.0 AND SET kdelta
|
166
179
|
#
|
167
180
|
@kdelta = 1
|
168
|
-
|
181
|
+
|
169
182
|
@kdelta = 2 if (@a == 0 or @d == 0)
|
170
183
|
@kdelta += 2 if (@b == 0 or @c == 0)
|
171
184
|
#
|
@@ -173,6 +186,22 @@ module Statsample
|
|
173
186
|
#
|
174
187
|
|
175
188
|
raise RequerimentNotMeet, "Rows and columns should have more than 0 items" if @kdelta==4
|
189
|
+
end
|
190
|
+
# Compute the tetrachoric correlation using ruby
|
191
|
+
# Called on object creation.
|
192
|
+
#
|
193
|
+
def compute_ruby
|
194
|
+
check_frequencies
|
195
|
+
#
|
196
|
+
# INITIALIZATION
|
197
|
+
#
|
198
|
+
@r = 0
|
199
|
+
sdzero = 0
|
200
|
+
@sdr = 0
|
201
|
+
@itype = 0
|
202
|
+
@ifault = 0
|
203
|
+
delta = 0
|
204
|
+
|
176
205
|
|
177
206
|
# GOTO (4, 1, 2 , 92), kdelta
|
178
207
|
#
|
data/spec/{statsample/bivariate/polychoric_processor_spec.rb → polychoric_processor_spec.rb}
RENAMED
@@ -9,16 +9,17 @@ describe Statsample::Bivariate::Polychoric::Processor do
|
|
9
9
|
@processor=Statsample::Bivariate::Polychoric::Processor.new(@alpha,@beta,@rho,@matrix)
|
10
10
|
end
|
11
11
|
it "im_function method should return correct values according to index" do
|
12
|
-
@processor.im_function(0,0,0).should
|
13
|
-
@processor.im_function(1,0,0).should
|
14
|
-
@processor.im_function(2,0,0).should
|
15
|
-
@processor.im_function(3,1,0).should
|
16
|
-
@processor.im_function(4,0,1).should
|
12
|
+
@processor.im_function(0,0,0).should eq @processor.fd_loglike_cell_rho(0,0)
|
13
|
+
@processor.im_function(1,0,0).should eq @processor.fd_loglike_cell_a(0,0,0)
|
14
|
+
@processor.im_function(2,0,0).should eq @processor.fd_loglike_cell_a(0,0,1)
|
15
|
+
@processor.im_function(3,1,0).should eq @processor.fd_loglike_cell_b(1,0,0)
|
16
|
+
@processor.im_function(4,0,1).should eq @processor.fd_loglike_cell_b(0,1,1)
|
17
17
|
lambda {@processor.im_function(5)}.should raise_error
|
18
18
|
|
19
19
|
end
|
20
20
|
it "should return informacion matrix" do
|
21
|
-
|
21
|
+
im=@processor.information_matrix
|
22
|
+
im.should be_instance_of(::Matrix)
|
22
23
|
end
|
23
24
|
it "fd a loglike should be equal usign eq.6 and eq.13" do
|
24
25
|
2.times {|k|
|
File without changes
|
data/spec/spec_helper.rb
CHANGED
@@ -6,9 +6,9 @@ describe "Statsample::Bivariate tetrachoric extensions" do
|
|
6
6
|
Statsample::Bivariate.should respond_to(:tetrachoric)
|
7
7
|
end
|
8
8
|
it "should return correct tetrachoric_matrix"do
|
9
|
-
ds=Statsample::PlainText.read(File.dirname(__FILE__)+"
|
9
|
+
ds=Statsample::PlainText.read(File.dirname(__FILE__)+"/../data/tetmat_test.txt", %w{a b c d e})
|
10
10
|
tcm_obs=Statsample::Bivariate.tetrachoric_correlation_matrix(ds)
|
11
|
-
tcm_exp=Statsample::PlainText.read(File.dirname(__FILE__)+"
|
11
|
+
tcm_exp=Statsample::PlainText.read(File.dirname(__FILE__)+"/../data/tetmat_matrix.txt", %w{a b c d e}).to_matrix
|
12
12
|
tcm_obs.row_size.times do |i|
|
13
13
|
tcm_obs.column_size do |j|
|
14
14
|
tcm_obs[i,j].should be_within( 0.00001).of(tcm_exp[i,k])
|
@@ -18,11 +18,11 @@ describe "Statsample::Bivariate tetrachoric extensions" do
|
|
18
18
|
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
shared_examples_for "tetrachoric implementation" do
|
22
22
|
context "Polychoric 2x2 vs tetrachoric" do
|
23
23
|
before do
|
24
24
|
@matrix=Matrix[[150+rand(10),1000+rand(20)],[1000+rand(20),200+rand(20)]]
|
25
|
-
@tetra = Statsample::Bivariate::Tetrachoric.new_with_matrix(@matrix)
|
25
|
+
@tetra = Statsample::Bivariate::Tetrachoric.new_with_matrix(@matrix,t_opts)
|
26
26
|
@poly = Statsample::Bivariate::Polychoric.new(@matrix)
|
27
27
|
end
|
28
28
|
it "should return similar values for two step ruby" do
|
@@ -42,35 +42,34 @@ describe Statsample::Bivariate::Tetrachoric do
|
|
42
42
|
|
43
43
|
it "should raise error on contingence table without cases" do
|
44
44
|
a,b,c,d=0,0,0,0
|
45
|
-
|
46
|
-
lambda {Statsample::Bivariate::Tetrachoric.new(a,b,c,d)}.should raise_error(Statsample::Bivariate::Tetrachoric::RequerimentNotMeet)
|
45
|
+
lambda {Statsample::Bivariate::Tetrachoric.new(a,b,c,d, t_opts)}.should raise_error(Statsample::Bivariate::Tetrachoric::RequerimentNotMeet)
|
47
46
|
end
|
48
47
|
it "should raise error on contingence table without cases on a row" do
|
49
48
|
|
50
49
|
a,b,c,d=10,10,0,0
|
51
50
|
|
52
|
-
lambda {Statsample::Bivariate::Tetrachoric.new(a,b,c,d)}.should raise_error(Statsample::Bivariate::Tetrachoric::RequerimentNotMeet)
|
51
|
+
lambda {Statsample::Bivariate::Tetrachoric.new(a,b,c,d, t_opts)}.should raise_error(Statsample::Bivariate::Tetrachoric::RequerimentNotMeet)
|
53
52
|
end
|
54
53
|
it "should raise error on contingence table without cases on a column" do
|
55
54
|
a,b,c,d=10,0,10,0
|
56
|
-
lambda {Statsample::Bivariate::Tetrachoric.new(a,b,c,d)}.should raise_error(Statsample::Bivariate::Tetrachoric::RequerimentNotMeet)
|
55
|
+
lambda {Statsample::Bivariate::Tetrachoric.new(a,b,c,d, t_opts)}.should raise_error(Statsample::Bivariate::Tetrachoric::RequerimentNotMeet)
|
57
56
|
end
|
58
57
|
it "should return correct values for perfect correlation" do
|
59
58
|
a,b,c,d=10,0,0,10
|
60
|
-
tc = Statsample::Bivariate::Tetrachoric.new(a,b,c,d)
|
59
|
+
tc = Statsample::Bivariate::Tetrachoric.new(a,b,c,d,t_opts)
|
61
60
|
tc.r.should==1
|
62
61
|
tc.se.should==0
|
63
62
|
end
|
64
63
|
it "should return correct values for perfect inverse correlation" do
|
65
64
|
a,b,c,d=0,10,10,0
|
66
|
-
tc = Statsample::Bivariate::Tetrachoric.new(a,b,c,d)
|
65
|
+
tc = Statsample::Bivariate::Tetrachoric.new(a,b,c,d, t_opts)
|
67
66
|
tc.r.should==-1
|
68
67
|
tc.se.should==0
|
69
68
|
end
|
70
69
|
|
71
70
|
it "should return correct value for standard contingence table" do
|
72
71
|
a,b,c,d = 30,40,70,20
|
73
|
-
tc = Statsample::Bivariate::Tetrachoric.new(a,b,c,d)
|
72
|
+
tc = Statsample::Bivariate::Tetrachoric.new(a,b,c,d, t_opts)
|
74
73
|
tc.r.should be_within(0.0001).of(-0.53980)
|
75
74
|
tc.se.should be_within(0.0001).of(0.09940)
|
76
75
|
tc.threshold_x.should be_within( 0.0001).of(-0.15731)
|
@@ -84,11 +83,25 @@ describe Statsample::Bivariate::Tetrachoric do
|
|
84
83
|
# a 4 3
|
85
84
|
# b 2 5
|
86
85
|
a,b,c,d=4,3,2,5
|
87
|
-
tc1 = Statsample::Bivariate::Tetrachoric.new(a,b,c,d)
|
88
|
-
tc2 = Statsample::Bivariate::Tetrachoric.new_with_vectors(x,y)
|
86
|
+
tc1 = Statsample::Bivariate::Tetrachoric.new(a,b,c,d, t_opts)
|
87
|
+
tc2 = Statsample::Bivariate::Tetrachoric.new_with_vectors(x,y, t_opts)
|
89
88
|
tc1.r.should==tc2.r
|
90
89
|
tc1.se.should==tc2.se
|
91
90
|
tc2.summary.size.should>0
|
92
91
|
end
|
92
|
+
end
|
93
93
|
|
94
|
+
describe "Statsample::Bivariate::Tetrachoric with ruby engine" do
|
95
|
+
it_should_behave_like 'tetrachoric implementation' do
|
96
|
+
let(:t_opts) { {:ruby_engine=>true}}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
describe "Statsample::Bivariate::Tetrachoric with optimized engine" do
|
100
|
+
if Statsample::OPTIMIZED and Statsample::STATSAMPLE__.respond_to? :tetrachoric
|
101
|
+
it_should_behave_like 'tetrachoric implementation' do
|
102
|
+
let(:t_opts) { {:ruby_engine=>false}}
|
103
|
+
end
|
104
|
+
else
|
105
|
+
pending("not optimized tetrachoric engine available")
|
106
|
+
end
|
94
107
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: statsample-bivariate-extension
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 93
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 16
|
9
|
-
-
|
10
|
-
version: 0.16.
|
9
|
+
- 1
|
10
|
+
version: 0.16.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Claudio Bustos
|
@@ -36,41 +36,25 @@ cert_chain:
|
|
36
36
|
rpP0jjs0
|
37
37
|
-----END CERTIFICATE-----
|
38
38
|
|
39
|
-
date: 2010-
|
39
|
+
date: 2010-12-22 00:00:00 -03:00
|
40
40
|
default_executable:
|
41
41
|
dependencies:
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
|
-
name:
|
43
|
+
name: hoe
|
44
44
|
prerelease: false
|
45
45
|
requirement: &id001 !ruby/object:Gem::Requirement
|
46
46
|
none: false
|
47
47
|
requirements:
|
48
48
|
- - ">="
|
49
49
|
- !ruby/object:Gem::Version
|
50
|
-
hash:
|
50
|
+
hash: 47
|
51
51
|
segments:
|
52
52
|
- 2
|
53
|
+
- 8
|
53
54
|
- 0
|
54
|
-
|
55
|
-
version: 2.0.4
|
55
|
+
version: 2.8.0
|
56
56
|
type: :development
|
57
57
|
version_requirements: *id001
|
58
|
-
- !ruby/object:Gem::Dependency
|
59
|
-
name: hoe
|
60
|
-
prerelease: false
|
61
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
62
|
-
none: false
|
63
|
-
requirements:
|
64
|
-
- - ">="
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
hash: 19
|
67
|
-
segments:
|
68
|
-
- 2
|
69
|
-
- 6
|
70
|
-
- 2
|
71
|
-
version: 2.6.2
|
72
|
-
type: :development
|
73
|
-
version_requirements: *id002
|
74
58
|
description: |-
|
75
59
|
Provides advanced bivariate statistics:
|
76
60
|
* Tetrachoric correlation
|
@@ -99,11 +83,11 @@ files:
|
|
99
83
|
- lib/statsample/bivariate/polychoric/processor.rb
|
100
84
|
- lib/statsample/bivariate/tetrachoric.rb
|
101
85
|
- references.txt
|
86
|
+
- spec/polychoric_processor_spec.rb
|
87
|
+
- spec/polychoric_spec.rb
|
102
88
|
- spec/spec.opts
|
103
89
|
- spec/spec_helper.rb
|
104
|
-
- spec/
|
105
|
-
- spec/statsample/bivariate/polychoric_spec.rb
|
106
|
-
- spec/statsample/bivariate/tetrachoric_spec.rb
|
90
|
+
- spec/tetrachoric_spec.rb
|
107
91
|
has_rdoc: true
|
108
92
|
homepage: http://ruby-statsample.rubyforge.org/
|
109
93
|
licenses: []
|
metadata.gz.sig
CHANGED
Binary file
|