statsample 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ module Statsample
2
+ module Regression
3
+ # Class for calculation of linear regressions with form
4
+ # y = a+bx
5
+ # To create a SimpleRegression object:
6
+ # * <tt> SimpleRegression.new_from_vectors(vx,vy)</tt>
7
+ # * <tt> SimpleRegression.new_from_gsl(gsl) </tt>
8
+ #
9
+ class Simple
10
+ attr_accessor :a,:b,:cov00, :cov01, :covx1, :chisq, :status
11
+ private_class_method :new
12
+ def initialize(init_method, *argv)
13
+ self.send(init_method, *argv)
14
+ end
15
+ def y(val_x)
16
+ @a+@b*val_x
17
+ end
18
+ def x(val_y)
19
+ (val_y-@a) / @b.to_f
20
+ end
21
+ # Sum of square error
22
+ def sse
23
+ (0...@vx.size).inject(0) {|acum,i|
24
+ acum+((@vy[i]-y(@vx[i]))**2)
25
+ }
26
+ end
27
+ def standard_error
28
+ Math::sqrt(sse / (@vx.size-2).to_f)
29
+ end
30
+ # Sum of square regression
31
+ def ssr
32
+ vy_mean=@vy.mean
33
+ (0...@vx.size).inject(0) {|a,i|
34
+ a+((y(@vx[i])-vy_mean)**2)
35
+ }
36
+
37
+ end
38
+ # Sum of square total
39
+ def sst
40
+ @vy.sum_of_squared_deviation
41
+ end
42
+ # Value of r
43
+ def r
44
+ @b * (@vx.sds / @vy.sds)
45
+ end
46
+ # Value of r^2
47
+ def r2
48
+ r**2
49
+ end
50
+ class << self
51
+ def new_from_gsl(ar)
52
+ new(:init_gsl, *ar)
53
+ end
54
+ def new_from_vectors(vx,vy)
55
+ new(:init_vectors,vx,vy)
56
+ end
57
+ end
58
+ def init_vectors(vx,vy)
59
+ @vx,@vy=Statsample.only_valid(vx,vy)
60
+ x_m=@vx.mean
61
+ y_m=@vy.mean
62
+ num=den=0
63
+ (0...@vx.size).each {|i|
64
+ num+=(@vx[i]-x_m)*(@vy[i]-y_m)
65
+ den+=(@vx[i]-x_m)**2
66
+ }
67
+ @b=num.to_f/den
68
+ @a=y_m - @b*x_m
69
+ end
70
+ def init_gsl(a,b,cov00, cov01, covx1, chisq, status)
71
+ @a=a
72
+ @b=b
73
+ @cov00=cov00
74
+ @cov01=cov01
75
+ @covx1=covx1
76
+ @chisq=chisq
77
+ @status=status
78
+ end
79
+ end
80
+ end
81
+ end
@@ -4,7 +4,7 @@ class StatsampleRegressionTestCase < Test::Unit::TestCase
4
4
  def initialize(*args)
5
5
  @x=[13,20,10,33,15].to_vector(:scale)
6
6
  @y=[23,18,35,10,27 ].to_vector(:scale)
7
- @reg=Statsample::Regression::SimpleRegression.new_from_vectors(@x,@y)
7
+ @reg=Statsample::Regression::Simple.new_from_vectors(@x,@y)
8
8
  super
9
9
  end
10
10
  def test_parameters
@@ -18,7 +18,7 @@ class StatsampleRegressionTestCase < Test::Unit::TestCase
18
18
  @c=[11,22,30,40,50,65,78,79,99,100,nil,3,7,nil,7].to_vector(:scale)
19
19
  @y=[3,4,5,6,7,8,9,10,20,30,30,40,nil,50,nil].to_vector(:scale)
20
20
  ds={'a'=>@a,'b'=>@b,'c'=>@c,'y'=>@y}.to_dataset
21
- lr=Statsample::Regression::MultipleRegressionPairwise.new(ds,'y')
21
+ lr=Statsample::Regression::Multiple::RubyEngine.new(ds,'y')
22
22
  assert_in_delta(2407.436,lr.sst,0.001)
23
23
  assert_in_delta(0.752,lr.r,0.001)
24
24
  assert_in_delta(0.565,lr.r2,0.001)
@@ -34,7 +34,7 @@ class StatsampleRegressionTestCase < Test::Unit::TestCase
34
34
  @c=[11,22,30,40,50,65,78,79,99,100].to_vector(:scale)
35
35
  @y=[3,4,5,6,7,8,9,10,20,30].to_vector(:scale)
36
36
  ds={'a'=>@a,'b'=>@b,'c'=>@c,'y'=>@y}.to_dataset
37
- lr=Statsample::Regression::MultipleRegressionAlglib.new(ds,'y')
37
+ lr=Statsample::Regression::Multiple::AlglibEngine.new(ds,'y')
38
38
  model_test(lr)
39
39
  predicted=[1.7857, 6.0989, 3.2433, 7.2908, 4.9667, 10.3428, 8.8158, 10.4717, 23.6639, 25.3198]
40
40
  c_predicted=lr.predicted
@@ -47,7 +47,7 @@ class StatsampleRegressionTestCase < Test::Unit::TestCase
47
47
  assert_in_delta(residuals[i],c_residuals[i],0.001)
48
48
  }
49
49
  else
50
- puts "Regression::MultipleRegressionAlglib not tested (no Alglib)"
50
+ puts "Regression::Multiple::AlglibEngine not tested (no Alglib)"
51
51
  end
52
52
  end
53
53
  def model_test(lr)
@@ -92,7 +92,7 @@ class StatsampleRegressionTestCase < Test::Unit::TestCase
92
92
  @c=[nil,11,22,30,40,50,65,78,79,99,100].to_vector(:scale)
93
93
  @y=[nil,3,4,5,6,7,8,9,10,20,30].to_vector(:scale)
94
94
  ds={'a'=>@a,'b'=>@b,'c'=>@c,'y'=>@y}.to_dataset
95
- lr=Statsample::Regression::MultipleRegressionPairwise.new(ds,'y')
95
+ lr=Statsample::Regression::Multiple::RubyEngine.new(ds,'y')
96
96
  model_test(lr)
97
97
  predicted=[nil,1.7857, 6.0989, 3.2433, 7.2908, 4.9667, 10.3428, 8.8158, 10.4717, 23.6639, 25.3198]
98
98
  c_predicted = lr.predicted
@@ -1,6 +1,5 @@
1
1
  require File.dirname(__FILE__)+'/../lib/statsample'
2
2
  require 'test/unit'
3
-
4
3
  class StatsampleStatisicsTestCase < Test::Unit::TestCase
5
4
 
6
5
  def initialize(*args)
@@ -131,7 +130,7 @@ class StatsampleStatisicsTestCase < Test::Unit::TestCase
131
130
  def test_simple_linear_regression
132
131
  a=[1,2,3,4,5,6].to_vector(:scale)
133
132
  b=[6,2,4,10,12,8].to_vector(:scale)
134
- reg = Statsample::Regression::SimpleRegression.new_from_vectors(a,b)
133
+ reg = Statsample::Regression::Simple.new_from_vectors(a,b)
135
134
  assert_in_delta((reg.ssr+reg.sse).to_f,reg.sst,0.001)
136
135
  assert_in_delta(Statsample::Bivariate.pearson(a,b),reg.r,0.001)
137
136
  assert_in_delta(2.4,reg.a,0.01)
@@ -139,14 +138,5 @@ class StatsampleStatisicsTestCase < Test::Unit::TestCase
139
138
  assert_in_delta(0.657,reg.r,0.001)
140
139
  assert_in_delta(0.432,reg.r2,0.001)
141
140
 
142
- end
143
- def a_test_multiple_regression
144
- x1=[1,2,3,4,5,6].to_vector(:scale)
145
- x2=[3,5,8,9,10,20].to_vector(:scale)
146
- x3=[100,90,50,30,50,10].to_vector(:scale)
147
- y=[6,2,4,10,12,8].to_vector(:scale)
148
- reg=Statsample::Regression::MultipleRegression.new_from_vectors([x1,x2,x3],y)
149
- # p reg
150
- end
151
-
141
+ end
152
142
  end
data/test/test_xls.xls ADDED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsample
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Bustos
@@ -61,15 +61,20 @@ files:
61
61
  - bin/statsample
62
62
  - demo/benchmark.rb
63
63
  - demo/chi-square.rb
64
+ - demo/crosstab.rb
64
65
  - demo/dice.rb
65
66
  - demo/distribution_t.rb
66
67
  - demo/graph.rb
67
68
  - demo/item_analysis.rb
68
69
  - demo/mean.rb
70
+ - demo/nunnally_6.rb
69
71
  - demo/proportion.rb
72
+ - demo/regression.rb
70
73
  - demo/sample_test.csv
71
74
  - demo/strata_proportion.rb
72
75
  - demo/stratum.rb
76
+ - demo/t-student.rb
77
+ - lib/spss.rb
73
78
  - lib/statsample.rb
74
79
  - lib/statsample/anova.rb
75
80
  - lib/statsample/bivariate.rb
@@ -81,19 +86,22 @@ files:
81
86
  - lib/statsample/dominanceanalysis.rb
82
87
  - lib/statsample/dominanceanalysis/bootstrap.rb
83
88
  - lib/statsample/graph/gdchart.rb
84
- - lib/statsample/graph/svggraph.rb
85
89
  - lib/statsample/graph/svgboxplot.rb
90
+ - lib/statsample/graph/svggraph.rb
86
91
  - lib/statsample/graph/svghistogram.rb
87
92
  - lib/statsample/graph/svgscatterplot.rb
88
93
  - lib/statsample/htmlreport.rb
89
94
  - lib/statsample/multiset.rb
90
95
  - lib/statsample/regression.rb
96
+ - lib/statsample/regression/multiple.rb
97
+ - lib/statsample/regression/multiple/alglibengine.rb
98
+ - lib/statsample/regression/multiple/rubyengine.rb
99
+ - lib/statsample/regression/simple.rb
91
100
  - lib/statsample/reliability.rb
92
101
  - lib/statsample/resample.rb
93
102
  - lib/statsample/srs.rb
94
103
  - lib/statsample/test.rb
95
104
  - lib/statsample/vector.rb
96
- - lib/spss.rb
97
105
  - test/_test_chart.rb
98
106
  - test/test_anova.rb
99
107
  - test/test_codification.rb
@@ -106,10 +114,13 @@ files:
106
114
  - test/test_regression.rb
107
115
  - test/test_reliability.rb
108
116
  - test/test_resample.rb
117
+ - test/test_srs.rb
109
118
  - test/test_statistics.rb
110
119
  - test/test_stratified.rb
111
120
  - test/test_svg_graph.rb
112
121
  - test/test_vector.rb
122
+ - test/test_xls.rb
123
+ - test/test_xls.xls
113
124
  has_rdoc: true
114
125
  homepage: http://rubyforge.org/projects/ruby-statsample/
115
126
  licenses: []