statsample 0.11.1 → 0.11.2
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.tar.gz.sig +2 -2
- data/History.txt +3 -0
- data/Rakefile +4 -2
- data/lib/statsample.rb +1 -1
- data/lib/statsample/factor/pca.rb +32 -12
- data/lib/statsample/matrix.rb +7 -0
- data/test/test_factor.rb +31 -40
- metadata +6 -5
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
2
|
-
�
|
1
|
+
��2�>Pfrg�*}�1���=�1�B�xQ*2���m��yН�:�qW3W�n�/��y�K��Ub)�4�KU�ާ>�B2�q����K�|Z�=V8؎���ݕҕ:<�� R�K��
|
2
|
+
�VW��}�5�j�}�i������$�RR�U�����^c�B�Ҽ�^h��>*���������@�����QhR��Τ�v��[��W3�\���]{!��\P��J��M�D�,�Hq-��b��%g�{U5
|
data/History.txt
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
=== 0.11.2 / 2010-05-05
|
2
|
+
* Updated dependency for 'extendedmatrix' to 0.2 (Matrix#build method)
|
3
|
+
|
1
4
|
=== 0.11.1 / 2010-05-04
|
2
5
|
* Removed Matrix almost all Matrix extensions and replaced by dependency on 'extendmatrix' gem
|
3
6
|
* Added dependency to gsl >=1.12.109. Polychoric with joint method fails without this explicit dependency
|
data/Rakefile
CHANGED
@@ -18,7 +18,9 @@ task :lint do
|
|
18
18
|
}
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
task :release do
|
22
|
+
system %{git push origin master}
|
23
|
+
end
|
22
24
|
desc "Update pot/po files."
|
23
25
|
task :updatepo do
|
24
26
|
require 'gettext/tools'
|
@@ -37,7 +39,7 @@ h=Hoe.spec('statsample') do
|
|
37
39
|
#self.testlib=:minitest
|
38
40
|
self.rubyforge_name = "ruby-statsample"
|
39
41
|
self.developer('Claudio Bustos', 'clbustos@gmail.com')
|
40
|
-
self.extra_deps << ["spreadsheet","~>0.6.0"] << ["svg-graph", "~>1.0"] << ["reportbuilder", "~>1.0"] << ["minimization", "~>0.2.0"] << ["fastercsv"] << ["dirty-memoize", "~>0.0"] << ["statistics2", "~>0.54"] << ["extendmatrix","~>0.
|
42
|
+
self.extra_deps << ["spreadsheet","~>0.6.0"] << ["svg-graph", "~>1.0"] << ["reportbuilder", "~>1.0"] << ["minimization", "~>0.2.0"] << ["fastercsv"] << ["dirty-memoize", "~>0.0"] << ["statistics2", "~>0.54"] << ["extendmatrix","~>0.2.0"] << ["gsl", "~>1.12.109"]
|
41
43
|
self.clean_globs << "test/images/*" << "demo/item_analysis/*" << "demo/Regression"
|
42
44
|
self.need_rdoc=false
|
43
45
|
end
|
data/lib/statsample.rb
CHANGED
@@ -34,20 +34,23 @@ module Factor
|
|
34
34
|
# Number of factors. Set by default to the number of factors
|
35
35
|
# with eigen values > 1
|
36
36
|
attr_accessor :m
|
37
|
+
# Use GSL if available
|
38
|
+
attr_accessor :use_gsl
|
37
39
|
include GetText
|
38
40
|
bindtextdomain("statsample")
|
39
41
|
|
40
42
|
def initialize(matrix ,opts=Hash.new)
|
41
|
-
|
42
|
-
matrix=matrix.to_gsl
|
43
|
-
end
|
43
|
+
@use_gsl=nil
|
44
44
|
@name=_("Principal Component Analysis")
|
45
45
|
@matrix=matrix
|
46
|
-
@n_variables=@matrix.
|
46
|
+
@n_variables=@matrix.column_size
|
47
47
|
@m=nil
|
48
48
|
opts.each{|k,v|
|
49
49
|
self.send("#{k}=",v) if self.respond_to? k
|
50
50
|
}
|
51
|
+
if @use_gsl.nil?
|
52
|
+
@use_gsl=Statsample.has_gsl?
|
53
|
+
end
|
51
54
|
calculate_eigenpairs
|
52
55
|
if @m.nil?
|
53
56
|
# Set number of factors with eigenvalues > 1
|
@@ -67,9 +70,9 @@ module Factor
|
|
67
70
|
# Feature vector for m factors
|
68
71
|
def feature_vector(m=nil)
|
69
72
|
m||=@m
|
70
|
-
omega_m
|
73
|
+
omega_m=::Matrix.build(@n_variables, m) {0}
|
71
74
|
m.times do |i|
|
72
|
-
omega_m.
|
75
|
+
omega_m.column= i, @eigenpairs[i][1]
|
73
76
|
end
|
74
77
|
omega_m
|
75
78
|
end
|
@@ -84,13 +87,13 @@ module Factor
|
|
84
87
|
def component_matrix(m=nil)
|
85
88
|
m||=@m
|
86
89
|
raise "m should be > 0" if m<1
|
87
|
-
omega_m
|
90
|
+
omega_m=::Matrix.build(@n_variables, m) {0}
|
88
91
|
gammas=[]
|
89
92
|
m.times {|i|
|
90
|
-
omega_m.
|
93
|
+
omega_m.column=i, @eigenpairs[i][1]
|
91
94
|
gammas.push(Math::sqrt(@eigenpairs[i][0]))
|
92
95
|
}
|
93
|
-
gamma_m
|
96
|
+
gamma_m=::Matrix.diagonal(*gammas)
|
94
97
|
(omega_m*(gamma_m)).to_matrix
|
95
98
|
end
|
96
99
|
# Communalities for all variables given m factors
|
@@ -112,13 +115,30 @@ module Factor
|
|
112
115
|
end
|
113
116
|
|
114
117
|
def calculate_eigenpairs
|
115
|
-
|
118
|
+
if @use_gsl
|
119
|
+
calculate_eigenpairs_gsl
|
120
|
+
else
|
121
|
+
calculate_eigenpairs_ruby
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def calculate_eigenpairs_ruby
|
126
|
+
eigval, eigvec= @matrix.eigenvaluesJacobi, @matrix.cJacobiV
|
116
127
|
@eigenpairs={}
|
117
|
-
eigval.each_index {|i|
|
118
|
-
@eigenpairs[eigval[i]]=eigvec.
|
128
|
+
eigval.to_a.each_index {|i|
|
129
|
+
@eigenpairs[eigval[i]]=eigvec.column(i)
|
119
130
|
}
|
120
131
|
@eigenpairs=@eigenpairs.sort.reverse
|
121
132
|
end
|
133
|
+
def calculate_eigenpairs_gsl
|
134
|
+
eigval, eigvec= GSL::Eigen.symmv(@matrix.to_gsl)
|
135
|
+
@eigenpairs={}
|
136
|
+
eigval.each_index {|i|
|
137
|
+
@eigenpairs[eigval[i]]=eigvec.get_col(i)
|
138
|
+
}
|
139
|
+
@eigenpairs=@eigenpairs.sort.reverse
|
140
|
+
end
|
141
|
+
|
122
142
|
def summary
|
123
143
|
ReportBuilder.new(:no_title=>true).add(self).to_text
|
124
144
|
end
|
data/lib/statsample/matrix.rb
CHANGED
@@ -2,6 +2,10 @@ require 'extendmatrix'
|
|
2
2
|
|
3
3
|
|
4
4
|
class ::Matrix
|
5
|
+
def to_matrix
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
5
9
|
def to_gsl
|
6
10
|
out=[]
|
7
11
|
self.row_size.times{|i|
|
@@ -13,6 +17,9 @@ end
|
|
13
17
|
|
14
18
|
module GSL
|
15
19
|
class Matrix
|
20
|
+
def to_gsl
|
21
|
+
self
|
22
|
+
end
|
16
23
|
def to_matrix
|
17
24
|
rows=self.size1
|
18
25
|
cols=self.size2
|
data/test/test_factor.rb
CHANGED
@@ -3,15 +3,22 @@ require(File.dirname(__FILE__)+'/helpers_tests.rb')
|
|
3
3
|
class StatsampleFactorTestCase < MiniTest::Unit::TestCase
|
4
4
|
# Tested with SPSS and R
|
5
5
|
def test_pca
|
6
|
-
if Statsample.has_gsl?
|
7
|
-
require 'gsl'
|
8
6
|
a=[2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1].to_scale
|
9
7
|
b=[2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9].to_scale
|
10
8
|
a.recode! {|c| c-a.mean}
|
11
9
|
b.recode! {|c| c-b.mean}
|
12
10
|
ds={'a'=>a,'b'=>b}.to_dataset
|
13
11
|
cov_matrix=Statsample::Bivariate.covariance_matrix(ds)
|
14
|
-
|
12
|
+
if Statsample.has_gsl?
|
13
|
+
pca=Statsample::Factor::PCA.new(cov_matrix,:use_gsl=>true)
|
14
|
+
pca_set(pca)
|
15
|
+
else
|
16
|
+
skip("Eigenvalues could be calculated with GSL (requires gsl)")
|
17
|
+
end
|
18
|
+
pca=Statsample::Factor::PCA.new(cov_matrix,:use_gsl=>false)
|
19
|
+
pca_set(pca)
|
20
|
+
end
|
21
|
+
def pca_set(pca)
|
15
22
|
expected_eigenvalues=[1.284, 0.0490]
|
16
23
|
expected_eigenvalues.each_with_index{|ev,i|
|
17
24
|
assert_in_delta(ev,pca.eigenvalues[i],0.001)
|
@@ -21,31 +28,25 @@ class StatsampleFactorTestCase < MiniTest::Unit::TestCase
|
|
21
28
|
assert_in_delta(ev,pca.communalities[i],0.001)
|
22
29
|
}
|
23
30
|
expected_cm=[0.768, 0.833]
|
24
|
-
|
25
31
|
obs=pca.component_matrix(1).column(0).to_a
|
26
32
|
expected_cm.each_with_index{|ev,i|
|
27
33
|
assert_in_delta(ev,obs[i],0.001)
|
28
34
|
}
|
29
35
|
|
30
|
-
expected_fm_1
|
31
|
-
expected_fm_2
|
36
|
+
expected_fm_1=::Matrix[[0.677], [0.735]]
|
37
|
+
expected_fm_2=::Matrix[[0.677,0.735], [0.735, -0.677]]
|
32
38
|
_test_matrix(expected_fm_1,pca.feature_vector(1))
|
33
39
|
_test_matrix(expected_fm_2,pca.feature_vector(2))
|
34
40
|
assert(pca.summary)
|
35
|
-
else
|
36
|
-
skip "PCA not tested. Requires GSL"
|
37
|
-
end
|
38
41
|
end
|
39
42
|
|
40
43
|
# Tested with R
|
41
44
|
def test_principalaxis
|
42
|
-
|
43
|
-
require 'gsl'
|
44
|
-
matrix=Matrix[
|
45
|
+
matrix=::Matrix[
|
45
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]]
|
46
47
|
fa=Statsample::Factor::PrincipalAxis.new(matrix,:m=>1)
|
47
48
|
|
48
|
-
cm
|
49
|
+
cm=::Matrix[[0.923],[0.912],[0.507],[0.483]]
|
49
50
|
|
50
51
|
_test_matrix(cm,fa.component_matrix)
|
51
52
|
|
@@ -59,42 +60,32 @@ class StatsampleFactorTestCase < MiniTest::Unit::TestCase
|
|
59
60
|
fa=Statsample::Factor::PrincipalAxis.new(matrix,:smc=>false)
|
60
61
|
assert_raise RuntimeError do
|
61
62
|
fa.iterate
|
62
|
-
|
63
63
|
end
|
64
|
+
assert(fa.summary.size>0)
|
64
65
|
|
65
|
-
|
66
|
-
|
67
|
-
assert(fa.summary)
|
68
|
-
|
69
|
-
else
|
70
|
-
skip "Principal Axis not tested. Requires GSL"
|
71
|
-
end
|
72
66
|
end
|
73
67
|
|
74
68
|
|
75
69
|
def test_rotation_varimax
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
[0.8945, -0.3921, -0.1863] ]
|
70
|
+
a = Matrix[ [ 0.4320, 0.8129, 0.3872] ,
|
71
|
+
[0.7950, -0.5416, 0.2565] ,
|
72
|
+
[0.5944, 0.7234, -0.3441],
|
73
|
+
[0.8945, -0.3921, -0.1863] ]
|
81
74
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
skip "Rotation not tested. Requires GSL"
|
93
|
-
end
|
75
|
+
expected= Matrix[[-0.0204423, 0.938674, -0.340334],
|
76
|
+
[0.983662, 0.0730206, 0.134997],
|
77
|
+
[0.0826106, 0.435975, -0.893379],
|
78
|
+
[0.939901, -0.0965213, -0.309596]]
|
79
|
+
varimax=Statsample::Factor::Varimax.new(a)
|
80
|
+
assert(!varimax.rotated.nil?, "Rotated shouldn't be empty")
|
81
|
+
assert(!varimax.component_transformation_matrix.nil?, "Component matrix shouldn't be empty")
|
82
|
+
assert(!varimax.h2.nil?, "H2 shouldn't be empty")
|
83
|
+
|
84
|
+
_test_matrix(expected,varimax.rotated)
|
94
85
|
end
|
95
86
|
def _test_matrix(a,b)
|
96
|
-
a.
|
97
|
-
a.
|
87
|
+
a.row_size.times {|i|
|
88
|
+
a.column_size.times {|j|
|
98
89
|
assert_in_delta(a[i,j], b[i,j],0.001)
|
99
90
|
}
|
100
91
|
}
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 11
|
8
|
-
-
|
9
|
-
version: 0.11.
|
8
|
+
- 2
|
9
|
+
version: 0.11.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Claudio Bustos
|
@@ -35,7 +35,7 @@ cert_chain:
|
|
35
35
|
rpP0jjs0
|
36
36
|
-----END CERTIFICATE-----
|
37
37
|
|
38
|
-
date: 2010-05-
|
38
|
+
date: 2010-05-05 00:00:00 -04:00
|
39
39
|
default_executable:
|
40
40
|
dependencies:
|
41
41
|
- !ruby/object:Gem::Dependency
|
@@ -139,8 +139,9 @@ dependencies:
|
|
139
139
|
- !ruby/object:Gem::Version
|
140
140
|
segments:
|
141
141
|
- 0
|
142
|
-
-
|
143
|
-
|
142
|
+
- 2
|
143
|
+
- 0
|
144
|
+
version: 0.2.0
|
144
145
|
type: :runtime
|
145
146
|
version_requirements: *id008
|
146
147
|
- !ruby/object:Gem::Dependency
|
metadata.gz.sig
CHANGED
Binary file
|