statsample 0.6.5 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/History.txt +15 -0
  2. data/Manifest.txt +6 -0
  3. data/README.txt +30 -12
  4. data/Rakefile +91 -0
  5. data/demo/levene.rb +9 -0
  6. data/demo/multiple_regression.rb +1 -7
  7. data/demo/polychoric.rb +1 -0
  8. data/demo/principal_axis.rb +8 -0
  9. data/lib/distribution/f.rb +22 -22
  10. data/lib/spss.rb +99 -99
  11. data/lib/statsample/bivariate/polychoric.rb +32 -22
  12. data/lib/statsample/bivariate/tetrachoric.rb +212 -207
  13. data/lib/statsample/bivariate.rb +6 -6
  14. data/lib/statsample/codification.rb +65 -65
  15. data/lib/statsample/combination.rb +60 -59
  16. data/lib/statsample/converter/csv19.rb +12 -12
  17. data/lib/statsample/converters.rb +1 -1
  18. data/lib/statsample/dataset.rb +93 -36
  19. data/lib/statsample/dominanceanalysis/bootstrap.rb +66 -3
  20. data/lib/statsample/dominanceanalysis.rb +5 -6
  21. data/lib/statsample/factor/pca.rb +41 -11
  22. data/lib/statsample/factor/principalaxis.rb +105 -29
  23. data/lib/statsample/factor/rotation.rb +20 -3
  24. data/lib/statsample/factor.rb +1 -1
  25. data/lib/statsample/graph/gdchart.rb +13 -13
  26. data/lib/statsample/graph/svggraph.rb +166 -167
  27. data/lib/statsample/matrix.rb +22 -12
  28. data/lib/statsample/mle/logit.rb +3 -2
  29. data/lib/statsample/mle/probit.rb +7 -5
  30. data/lib/statsample/mle.rb +4 -2
  31. data/lib/statsample/multiset.rb +125 -124
  32. data/lib/statsample/permutation.rb +2 -1
  33. data/lib/statsample/regression/binomial/logit.rb +4 -3
  34. data/lib/statsample/regression/binomial/probit.rb +2 -1
  35. data/lib/statsample/regression/binomial.rb +62 -81
  36. data/lib/statsample/regression/multiple/baseengine.rb +1 -1
  37. data/lib/statsample/regression/multiple/gslengine.rb +1 -1
  38. data/lib/statsample/regression/multiple/matrixengine.rb +12 -6
  39. data/lib/statsample/regression/multiple.rb +15 -42
  40. data/lib/statsample/regression/simple.rb +93 -78
  41. data/lib/statsample/regression.rb +74 -2
  42. data/lib/statsample/reliability.rb +117 -120
  43. data/lib/statsample/srs.rb +156 -153
  44. data/lib/statsample/test/levene.rb +90 -0
  45. data/lib/statsample/test/umannwhitney.rb +25 -9
  46. data/lib/statsample/test.rb +2 -0
  47. data/lib/statsample/vector.rb +388 -413
  48. data/lib/statsample.rb +74 -30
  49. data/po/es/statsample.mo +0 -0
  50. data/test/test_bivariate.rb +5 -4
  51. data/test/test_combination.rb +1 -1
  52. data/test/test_dataset.rb +2 -2
  53. data/test/test_factor.rb +53 -6
  54. data/test/test_gsl.rb +1 -1
  55. data/test/test_mle.rb +1 -1
  56. data/test/test_regression.rb +18 -33
  57. data/test/test_statistics.rb +15 -33
  58. data/test/test_stest.rb +35 -0
  59. data/test/test_svg_graph.rb +2 -2
  60. data/test/test_vector.rb +331 -333
  61. metadata +38 -11
@@ -1,28 +1,28 @@
1
1
  module Statsample
2
2
  module Reliability
3
- class << self
3
+ class << self
4
4
  # Calculate Chonbach's alpha for a given dataset.
5
5
  # only uses tuples without missing data
6
- def cronbach_alpha(ods)
7
- ds=ods.dup_only_valid
8
- n_items=ds.fields.size
9
- sum_var_items=ds.vectors.inject(0) {|ac,v|
10
- ac+v[1].variance_sample }
11
- total=ds.vector_sum
12
- (n_items / (n_items-1).to_f) * (1-(sum_var_items/ total.variance_sample))
13
- end
6
+ def cronbach_alpha(ods)
7
+ ds=ods.dup_only_valid
8
+ n_items=ds.fields.size
9
+ sum_var_items=ds.vectors.inject(0) {|ac,v|
10
+ ac+v[1].variance_sample }
11
+ total=ds.vector_sum
12
+ (n_items / (n_items-1).to_f) * (1-(sum_var_items/ total.variance_sample))
13
+ end
14
14
  # Calculate Chonbach's alpha for a given dataset
15
15
  # using standarized values for every vector.
16
16
  # Only uses tuples without missing data
17
-
17
+
18
18
  def cronbach_alpha_standarized(ods)
19
19
  ds=ods.dup_only_valid.fields.inject({}){|a,f|
20
20
  a[f]=ods[f].vector_standarized; a
21
21
  }.to_dataset
22
22
  cronbach_alpha(ds)
23
23
  end
24
- end
25
- class ItemCharacteristicCurve
24
+ end
25
+ class ItemCharacteristicCurve
26
26
  attr_reader :totals, :counts,:vector_total
27
27
  def initialize (ds, vector_total=nil)
28
28
  vector_total||=ds.vector_sum
@@ -39,28 +39,28 @@ module Statsample
39
39
  tot=@vector_total[i]
40
40
  @totals[tot]||=0
41
41
  @totals[tot]+=1
42
- @ds.fields.each do |f|
42
+ @ds.fields.each do |f|
43
43
  item=row[f].to_s
44
44
  @counts[f][tot]||={}
45
45
  @counts[f][tot][item]||=0
46
- @counts[f][tot][item] += 1
46
+ @counts[f][tot][item] += 1
47
47
  end
48
- i+=1
48
+ i+=1
49
49
  end
50
50
  end
51
51
  def curve_field(field, item)
52
52
  out={}
53
53
  item=item.to_s
54
54
  @totals.each{|value,n|
55
- count_value= @counts[field][value][item].nil? ? 0 : @counts[field][value][item]
56
- out[value]=count_value.to_f/n.to_f
55
+ count_value= @counts[field][value][item].nil? ? 0 : @counts[field][value][item]
56
+ out[value]=count_value.to_f/n.to_f
57
57
  }
58
58
  out
59
59
  end
60
60
  end
61
- class ItemAnalysis
61
+ class ItemAnalysis
62
62
  attr_reader :mean, :sd,:valid_n, :alpha , :alpha_standarized
63
- def initialize(ds)
63
+ def initialize(ds)
64
64
  @ds=ds.dup_only_valid
65
65
  @total=@ds.vector_sum
66
66
  @item_mean=@ds.vector_mean.mean
@@ -74,11 +74,11 @@ module Statsample
74
74
  @alpha = Statsample::Reliability.cronbach_alpha(ds)
75
75
  @alpha_standarized = Statsample::Reliability.cronbach_alpha_standarized(ds)
76
76
  rescue => e
77
- raise DatasetException.new(@ds,e), "Problem on calculate alpha"
77
+ raise DatasetException.new(@ds,e), "Problem on calculate alpha"
78
78
  end
79
- end
80
- # Returns a hash with structure
81
- def item_characteristic_curve
79
+ end
80
+ # Returns a hash with structure
81
+ def item_characteristic_curve
82
82
  i=0
83
83
  out={}
84
84
  total={}
@@ -89,7 +89,7 @@ module Statsample
89
89
  total[f]||={}
90
90
  out[f][tot]||= 0
91
91
  total[f][tot]||=0
92
- out[f][tot]+= row[f]
92
+ out[f][tot]+= row[f]
93
93
  total[f][tot]+=1
94
94
  end
95
95
  i+=1
@@ -100,79 +100,77 @@ module Statsample
100
100
  end
101
101
  end
102
102
  out
103
- end
103
+ end
104
104
  def gnuplot_item_characteristic_curve(directory, base="crd",options={})
105
105
  require 'gnuplot'
106
-
106
+
107
107
  crd=item_characteristic_curve
108
- @ds.fields.each {|f|
109
- x=[]
110
- y=[]
111
- Gnuplot.open do |gp|
112
- Gnuplot::Plot.new( gp ) do |plot|
113
- crd[f].sort.each{|tot,prop|
114
- x.push(tot)
115
- y.push((prop*100).to_i.to_f/100)
116
- }
117
- plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
118
- ds.with = "linespoints"
119
- ds.notitle
120
- end
121
-
122
- end
108
+ @ds.fields.each do |f|
109
+ x=[]
110
+ y=[]
111
+ Gnuplot.open do |gp|
112
+ Gnuplot::Plot.new( gp ) do |plot|
113
+ crd[f].sort.each do |tot,prop|
114
+ x.push(tot)
115
+ y.push((prop*100).to_i.to_f/100)
116
+ end
117
+ plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
118
+ ds.with = "linespoints"
119
+ ds.notitle
120
+ end
121
+
122
+ end
123
+ end
123
124
  end
124
- }
125
-
126
125
  end
127
126
  def svggraph_item_characteristic_curve(directory, base="icc",options={})
128
127
  require 'statsample/graph/svggraph'
129
128
  crd=ItemCharacteristicCurve.new(@ds)
130
- @ds.fields.each {|f|
131
- factors=@ds[f].factors.sort
132
- options={
133
- :height=>500,
134
- :width=>800,
135
- :key=>true
136
- }.update(options)
137
- graph = ::SVG::Graph::Plot.new(options)
138
- factors.each{|factor|
139
- factor=factor.to_s
140
- dataset=[]
141
- crd.curve_field(f, factor).each{|tot,prop|
142
- dataset.push(tot)
143
- dataset.push((prop*100).to_i.to_f/100)
144
- }
145
- graph.add_data({
146
- :title=>"#{factor}",
147
- :data=>dataset
148
- })
149
- }
150
- File.open(directory+"/"+base+"_#{f}.svg","w") {|fp|
151
- fp.puts(graph.burn())
152
- }
153
- }
154
-
129
+ @ds.fields.each do |f|
130
+ factors=@ds[f].factors.sort
131
+ options={
132
+ :height=>500,
133
+ :width=>800,
134
+ :key=>true
135
+ }.update(options)
136
+ graph = ::SVG::Graph::Plot.new(options)
137
+ factors.each do |factor|
138
+ factor=factor.to_s
139
+ dataset=[]
140
+ crd.curve_field(f, factor).each do |tot,prop|
141
+ dataset.push(tot)
142
+ dataset.push((prop*100).to_i.to_f/100)
143
+ end
144
+ graph.add_data({
145
+ :title=>"#{factor}",
146
+ :data=>dataset
147
+ })
148
+ end
149
+ File.open(directory+"/"+base+"_#{f}.svg","w") {|fp|
150
+ fp.puts(graph.burn())
151
+ }
152
+ end
155
153
  end
156
- def item_total_correlation
157
- @ds.fields.inject({}) do |a,v|
158
- vector=@ds[v].dup
159
- ds2=@ds.dup
160
- ds2.delete_vector(v)
161
- total=ds2.vector_sum
162
- a[v]=Statsample::Bivariate.pearson(vector,total)
163
- a
154
+ def item_total_correlation
155
+ @ds.fields.inject({}) do |a,v|
156
+ vector=@ds[v].dup
157
+ ds2=@ds.dup
158
+ ds2.delete_vector(v)
159
+ total=ds2.vector_sum
160
+ a[v]=Statsample::Bivariate.pearson(vector,total)
161
+ a
164
162
  end
165
- end
166
- def item_statistics
167
- @ds.fields.inject({}) do |a,v|
168
- a[v]={:mean=>@ds[v].mean,:sds=>@ds[v].sds}
169
- a
170
- end
171
- end
163
+ end
164
+ def item_statistics
165
+ @ds.fields.inject({}) do |a,v|
166
+ a[v]={:mean=>@ds[v].mean,:sds=>@ds[v].sds}
167
+ a
168
+ end
169
+ end
172
170
  # Returns a dataset with cases ordered by score
173
171
  # and variables ordered by difficulty
174
-
175
- def item_difficulty_analysis
172
+
173
+ def item_difficulty_analysis
176
174
  dif={}
177
175
  @ds.fields.each{|f| dif[f]=@ds[f].mean }
178
176
  dif_sort=dif.sort{|a,b| -(a[1]<=>b[1])}
@@ -190,20 +188,20 @@ module Statsample
190
188
  ds_new.update_valid_data
191
189
  ds_new
192
190
  end
193
- def stats_if_deleted
194
- @ds.fields.inject({}) do |a,v|
195
- ds2=@ds.dup
196
- ds2.delete_vector(v)
197
- total=ds2.vector_sum
198
- a[v]={}
199
- a[v][:mean]=total.mean
200
- a[v][:sds]=total.sds
201
- a[v][:variance_sample]=total.variance_sample
202
- a[v][:alpha]=Statsample::Reliability.cronbach_alpha(ds2)
203
- a
191
+ def stats_if_deleted
192
+ @ds.fields.inject({}) do |a,v|
193
+ ds2=@ds.dup
194
+ ds2.delete_vector(v)
195
+ total=ds2.vector_sum
196
+ a[v]={}
197
+ a[v][:mean]=total.mean
198
+ a[v][:sds]=total.sds
199
+ a[v][:variance_sample]=total.variance_sample
200
+ a[v][:alpha]=Statsample::Reliability.cronbach_alpha(ds2)
201
+ a
204
202
  end
205
- end
206
- def html_summary
203
+ end
204
+ def html_summary
207
205
  html = <<EOF
208
206
  <p><strong>Summary for scale:</strong></p>
209
207
  <ul>
@@ -229,27 +227,26 @@ Correl.</th><th>Alpha if
229
227
  deleted</th></thead>
230
228
  EOF
231
229
 
232
- itc=item_total_correlation
233
- sid=stats_if_deleted
234
- is=item_statistics
235
- @ds.fields.each {|f|
236
- html << <<EOF
237
- <tr>
238
- <td>#{f}</td>
239
- <td>#{sprintf("%0.5f",is[f][:mean])}</td>
240
- <td>#{sprintf("%0.5f",is[f][:sds])}</td>
241
- <td>#{sprintf("%0.5f",sid[f][:mean])}</td>
242
- <td>#{sprintf("%0.5f",sid[f][:variance_sample])}</td>
243
- <td>#{sprintf("%0.5f",sid[f][:sds])}</td>
244
- <td>#{sprintf("%0.5f",itc[f])}</td>
245
- <td>#{sprintf("%0.5f",sid[f][:alpha])}</td>
246
- </tr>
230
+ itc=item_total_correlation
231
+ sid=stats_if_deleted
232
+ is=item_statistics
233
+ @ds.fields.each {|f|
234
+ html << <<EOF
235
+ <tr>
236
+ <td>#{f}</td>
237
+ <td>#{sprintf("%0.5f",is[f][:mean])}</td>
238
+ <td>#{sprintf("%0.5f",is[f][:sds])}</td>
239
+ <td>#{sprintf("%0.5f",sid[f][:mean])}</td>
240
+ <td>#{sprintf("%0.5f",sid[f][:variance_sample])}</td>
241
+ <td>#{sprintf("%0.5f",sid[f][:sds])}</td>
242
+ <td>#{sprintf("%0.5f",itc[f])}</td>
243
+ <td>#{sprintf("%0.5f",sid[f][:alpha])}</td>
244
+ </tr>
247
245
  EOF
248
- }
249
- html << "</table><hr />"
250
- html
251
- end
252
- end
253
-
254
- end
246
+ }
247
+ html << "</table><hr />"
248
+ html
249
+ end
250
+ end
251
+ end
255
252
  end
@@ -2,161 +2,164 @@ module Statsample
2
2
  # Several methods to estimate parameters for simple random sampling
3
3
  module SRS
4
4
  class << self
5
-
6
- ########################
7
- #
8
- # Proportion estimation
9
- #
10
- ########################
11
- # Finite population correction (over variance)
12
- # Source: Cochran(1972)
13
- def fpc_var(sam,pop)
14
- (pop - sam).quo(pop - 1)
15
- end
16
- # Finite population correction (over standard deviation)
17
- def fpc(sam,pop)
18
- Math::sqrt((pop-sam).quo(pop-1))
19
- end
20
-
21
- # Non sample fraction.
22
- #
23
- # 1 - sample fraction
24
- def qf(sam , pop)
25
- 1-(sam.quo(pop))
26
- end
27
- # Sample size estimation for proportions, infinite poblation
28
- def estimation_n0(d,prop,margin=0.95)
29
- t=Distribution::Normal.p_value(1-(1-margin).quo(2))
30
- var=prop*(1-prop)
31
- t**2*var.quo(d**2)
32
- end
33
- # Sample size estimation for proportions, finite poblation.
34
- def estimation_n(d,prop,n_pobl,margin=0.95)
35
- n0=estimation_n0(d,prop,margin)
36
- n0.quo( 1 + ((n0 - 1).quo(n_pobl)))
37
- end
38
-
39
-
40
- # Proportion confidence interval with t values
41
- # Uses estimated proportion, sample without replacement.
42
-
43
- def proportion_confidence_interval_t(prop, n_sample, n_population, margin=0.95)
44
- t = Distribution::T.p_value(1-((1-margin).quo(2)) , n_sample-1)
45
- proportion_confidence_interval(prop,n_sample,n_population, t)
46
- end
47
-
48
- # Proportion confidence interval with z values
49
- # Uses estimated proportion, sample without replacement.
50
- def proportion_confidence_interval_z(p, n_sample, n_population, margin=0.95)
51
- z=Distribution::Normal.p_value(1-((1-margin).quo(2)))
52
- proportion_confidence_interval(p,n_sample,n_population, z)
53
- end
54
- # Proportion confidence interval with x value
55
- # Uses estimated proportion, sample without replacement
56
-
57
- def proportion_confidence_interval(p, sam,pop , x)
58
- f=sam.quo(pop)
59
- one_range=x * Math::sqrt((qf(sam, pop) * p * (1-p)).quo(sam-1)) + (1.quo(sam * 2.0))
60
- [p-one_range, p+one_range]
61
- end
62
- # Standard deviation for sample distribution of a proportion
63
- # Know proportion, sample with replacement.
64
- # Based on http://stattrek.com/Lesson6/SRS.aspx
65
- def proportion_sd_kp_wr(p, n_sample)
66
- Math::sqrt(p*(1-p).quo(n_sample))
67
- end
68
- # Standard deviation for sample distribution of a proportion
69
- # Know proportion, sample without replacement.
70
- #
71
- # Sources:
72
- # * http://stattrek.com/Lesson6/SRS.aspx
73
- # * Cochran(1972)
74
- def proportion_sd_kp_wor(p, sam, pop)
75
- fpc(sam,pop)*Math::sqrt(p*(1-p).quo(sam))
76
- end
77
- # Standard deviation for sample distribution of a proportion
78
- # Estimated proportion, sample with replacement
79
- # Based on http://stattrek.com/Lesson6/SRS.aspx.
80
- def proportion_sd_ep_wr(p, n_sample)
81
- Math::sqrt(p*(1-p).quo(n_sample-1))
82
- end
83
- # Standard deviation for sample distribution of a proportion.
84
- # Estimated proportion, sample without replacement.
85
- # Source: Cochran, 1972, Técnicas de muestreo
86
- def proportion_sd_ep_wor(p, sam,pop)
87
- fsc=(pop-sam).quo((sam-1)*pop)
88
- Math::sqrt(fsc*p*(1-p))
89
- end
90
-
91
- # Total estimation sd based on sample.
92
- # Known proportion, sample without replacement
93
- # Source: Cochran(1972)
94
- def proportion_total_sd_kp_wor(prop, sam, pop)
95
- pob * proportion_sd_kp_wor(p, sam, pop)
96
- end
97
- # Total estimation sd based on sample.
98
- # Estimated proportion, sample without replacement
99
- # Source: Cochran(1972)
100
- def proportion_total_sd_ep_wor(prop, sam, pop)
101
- fsc=((pop - sam).to_f / ( sam - 1))
102
- Math::sqrt(fsc*pop*prop*(1-prop))
103
- end
104
-
105
- ########################
106
- #
107
- # Mean stimation
108
- #
109
- ########################
5
+ ########################
6
+ #
7
+ # :SECTION: Proportion estimation
8
+ #
9
+ # Function for estimation of proportions
10
+ ########################
11
+
12
+ #
13
+ # Finite population correction (over variance)
14
+ # Source: Cochran(1972)
15
+ def fpc_var(sam,pop)
16
+ (pop - sam).quo(pop - 1)
17
+ end
18
+ # Finite population correction (over standard deviation)
19
+ def fpc(sam,pop)
20
+ Math::sqrt((pop-sam).quo(pop-1))
21
+ end
22
+
23
+ # Non sample fraction.
24
+ #
25
+ # 1 - sample fraction
26
+ def qf(sam , pop)
27
+ 1-(sam.quo(pop))
28
+ end
29
+ # Sample size estimation for proportions, infinite poblation
30
+ def estimation_n0(d,prop,margin=0.95)
31
+ t=Distribution::Normal.p_value(1-(1-margin).quo(2))
32
+ var=prop*(1-prop)
33
+ t**2*var.quo(d**2)
34
+ end
35
+ # Sample size estimation for proportions, finite poblation.
36
+ def estimation_n(d,prop,n_pobl,margin=0.95)
37
+ n0=estimation_n0(d,prop,margin)
38
+ n0.quo( 1 + ((n0 - 1).quo(n_pobl)))
39
+ end
40
+
41
+
42
+ # Proportion confidence interval with t values
43
+ # Uses estimated proportion, sample without replacement.
44
+
45
+ def proportion_confidence_interval_t(prop, n_sample, n_population, margin=0.95)
46
+ t = Distribution::T.p_value(1-((1-margin).quo(2)) , n_sample-1)
47
+ proportion_confidence_interval(prop,n_sample,n_population, t)
48
+ end
49
+
50
+ # Proportion confidence interval with z values
51
+ # Uses estimated proportion, sample without replacement.
52
+ def proportion_confidence_interval_z(p, n_sample, n_population, margin=0.95)
53
+ z=Distribution::Normal.p_value(1-((1-margin).quo(2)))
54
+ proportion_confidence_interval(p,n_sample,n_population, z)
55
+ end
56
+ # Proportion confidence interval with x value
57
+ # Uses estimated proportion, sample without replacement
58
+
59
+ def proportion_confidence_interval(p, sam,pop , x)
60
+ f=sam.quo(pop)
61
+ one_range=x * Math::sqrt((qf(sam, pop) * p * (1-p)).quo(sam-1)) + (1.quo(sam * 2.0))
62
+ [p-one_range, p+one_range]
63
+ end
64
+ # Standard deviation for sample distribution of a proportion
65
+ # Know proportion, sample with replacement.
66
+ # Based on http://stattrek.com/Lesson6/SRS.aspx
67
+ def proportion_sd_kp_wr(p, n_sample)
68
+ Math::sqrt(p*(1-p).quo(n_sample))
69
+ end
70
+ # Standard deviation for sample distribution of a proportion
71
+ # Know proportion, sample without replacement.
72
+ #
73
+ # Sources:
74
+ # * http://stattrek.com/Lesson6/SRS.aspx
75
+ # * Cochran(1972)
76
+ def proportion_sd_kp_wor(p, sam, pop)
77
+ fpc(sam,pop)*Math::sqrt(p*(1-p).quo(sam))
78
+ end
79
+ # Standard deviation for sample distribution of a proportion
80
+ # Estimated proportion, sample with replacement
81
+ # Based on http://stattrek.com/Lesson6/SRS.aspx.
82
+ def proportion_sd_ep_wr(p, n_sample)
83
+ Math::sqrt(p*(1-p).quo(n_sample-1))
84
+ end
85
+ # Standard deviation for sample distribution of a proportion.
86
+ # Estimated proportion, sample without replacement.
87
+ # Reference:
88
+ # * Cochran, 1972, Técnicas de muestreo
89
+ def proportion_sd_ep_wor(p, sam,pop)
90
+ fsc=(pop-sam).quo((sam-1)*pop)
91
+ Math::sqrt(fsc*p*(1-p))
92
+ end
93
+
94
+ # Total estimation sd based on sample.
95
+ # Known proportion, sample without replacement
96
+ # Reference:
97
+ # * Cochran(1972)
98
+ def proportion_total_sd_kp_wor(prop, sam, pop)
99
+ pob * proportion_sd_kp_wor(p, sam, pop)
100
+ end
101
+ # Total estimation sd based on sample.
102
+ # Estimated proportion, sample without replacement
103
+ # Source: Cochran(1972)
104
+ def proportion_total_sd_ep_wor(prop, sam, pop)
105
+ fsc=((pop - sam).to_f / ( sam - 1))
106
+ Math::sqrt(fsc*pop*prop*(1-prop))
107
+ end
108
+
109
+ ########################
110
+ #
111
+ # :SECTION: Mean stimation
112
+ #
113
+ ########################
110
114
 
111
-
112
- # Standard error. Known variance, sample with replacement.
113
- def standard_error_ksd_wr(s, sam, pop)
114
- s.quo(Math::sqrt(sam)) * Math::sqrt((pop-1).quo(pop))
115
- end
116
-
117
- # Standard error of the mean. Known variance, sample w/o replacement
118
- def standard_error_ksd_wor(s,sam,pop)
119
- s.quo(Math::sqrt(sam)) * Math::sqrt(qf(sam,pop))
120
- end
121
-
122
- alias_method :standard_error_esd_wr, :standard_error_ksd_wr
123
-
124
- # Standard error of the mean.
125
- # Estimated variance, without replacement
126
- # Cochran (1972) p.47
127
- def standard_error_esd_wor(s,sam,pop)
128
- s.quo(Math::sqrt(sam)) * Math::sqrt(qf(sam,pop))
129
- end
130
-
131
- alias_method :standard_error, :standard_error_esd_wor
132
- alias_method :se, :standard_error_esd_wor
115
+
116
+ # Standard error. Known variance, sample with replacement.
117
+ def standard_error_ksd_wr(s, sam, pop)
118
+ s.quo(Math::sqrt(sam)) * Math::sqrt((pop-1).quo(pop))
119
+ end
120
+
121
+ # Standard error of the mean. Known variance, sample w/o replacement
122
+ def standard_error_ksd_wor(s,sam,pop)
123
+ s.quo(Math::sqrt(sam)) * Math::sqrt(qf(sam,pop))
124
+ end
125
+
126
+ alias_method :standard_error_esd_wr, :standard_error_ksd_wr
127
+
128
+ # Standard error of the mean.
129
+ # Estimated variance, without replacement
130
+ # Cochran (1972) p.47
131
+ def standard_error_esd_wor(s,sam,pop)
132
+ s.quo(Math::sqrt(sam)) * Math::sqrt(qf(sam,pop))
133
+ end
134
+
135
+ alias_method :standard_error, :standard_error_esd_wor
136
+ alias_method :se, :standard_error_esd_wor
133
137
 
134
- # Standard error of total estimation
135
-
136
- def standard_error_total(s,sam,pop)
137
- pop*se(s,sam,pop)
138
- end
138
+ # Standard error of total estimation
139
+
140
+ def standard_error_total(s,sam,pop)
141
+ pop*se(s,sam,pop)
142
+ end
139
143
 
140
- # Confidence Interval using T-Student
141
- # Use with n < 60
142
- def mean_confidence_interval_t(mean,s,n_sample,n_population,margin=0.95)
143
- t=Distribution::T.p_value(1-((1-margin) / 2),n_sample-1)
144
- mean_confidence_interval(mean,s,n_sample,n_population,t)
145
- end
146
- # Confidente Interval using Z
147
- # Use with n > 60
148
- def mean_confidence_interval_z(mean,s,n_sample,n_population,margin=0.95)
149
- z=Distribution::Normal.p_value(1-((1-margin) / 2))
150
- mean_confidence_interval(mean,s,n_sample,n_population, z)
151
- end
152
- # Confidente interval using X.
153
- #
154
- # Better use mean_confidence_interval_z or mean_confidence_interval_t
155
- def mean_confidence_interval(mean,s,n_sample,n_population,x)
156
- range=x*se(s,n_sample,n_population)
157
- [mean-range,mean+range]
158
- end
144
+ # Confidence Interval using T-Student
145
+ # Use with n < 60
146
+ def mean_confidence_interval_t(mean,s,n_sample,n_population,margin=0.95)
147
+ t=Distribution::T.p_value(1-((1-margin) / 2),n_sample-1)
148
+ mean_confidence_interval(mean,s,n_sample,n_population,t)
149
+ end
150
+ # Confidente Interval using Z
151
+ # Use with n > 60
152
+ def mean_confidence_interval_z(mean,s,n_sample,n_population,margin=0.95)
153
+ z=Distribution::Normal.p_value(1-((1-margin) / 2))
154
+ mean_confidence_interval(mean,s,n_sample,n_population, z)
155
+ end
156
+ # Confidente interval using X.
157
+ #
158
+ # Better use mean_confidence_interval_z or mean_confidence_interval_t
159
+ def mean_confidence_interval(mean,s,n_sample,n_population,x)
160
+ range=x*se(s,n_sample,n_population)
161
+ [mean-range,mean+range]
162
+ end
159
163
  end
160
164
  end
161
-
162
- end
165
+ end