Class: Statsample::Test::WilcoxonSignedRank
- Includes:
- Summarizable, Statsample::Test
- Defined in:
- lib/statsample/test/wilcoxonsignedrank.rb
Overview
From Wikipedia: The Wilcoxon signed-rank test is a non-parametric statistical hypothesis test used when comparing two related samples, matched samples, or repeated measurements on a single sample to assess whether their population mean ranks differ (i.e. it is a paired difference test). It can be used as an alternative to the paired Student’s t-test, t-test for matched pairs, or the t-test for dependent samples when the population cannot be assumed to be normally distributed.
Instance Attribute Summary collapse
-
#name ⇒ Object
Name of F analysis.
-
#nr ⇒ Object
readonly
Returns the value of attribute nr.
-
#tails ⇒ Object
writeonly
Sets the attribute tails.
-
#w ⇒ Object
readonly
Returns the value of attribute w.
Instance Method Summary collapse
- #calculate ⇒ Object
-
#initialize(v1, v2, opts = Hash.new) ⇒ WilcoxonSignedRank
constructor
Parameters:.
-
#probability_exact ⇒ Object
Calculate exact probability.
-
#probability_z ⇒ Object
Assuming normal distribution of W, this calculate the probability of samples with Z equal or higher than obtained on sample.
-
#report_building(generator) ⇒ Object
:nodoc:.
- #z ⇒ Object
Methods included from Summarizable
Methods included from Statsample::Test
chi_square, levene, #p_using_cdf, #t_critical, t_one_sample, t_two_samples_independent, u_mannwhitney, wilcoxon_signed_rank, #z_critical
Constructor Details
#initialize(v1, v2, opts = Hash.new) ⇒ WilcoxonSignedRank
Parameters:
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 15 def initialize(v1,v2, opts=Hash.new) @v1=v1 @v2=v2 opts_default={:name=>_("Wilcoxon Signed Rank Test"),:tails=>:both} @opts=opts_default.merge(opts) opts_default.keys.each {|k| send("#{k}=", @opts[k]) } calculate end |
Instance Attribute Details
#name ⇒ Object
Name of F analysis
10 11 12 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 10 def name @name end |
#nr ⇒ Object (readonly)
Returns the value of attribute nr.
12 13 14 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 12 def nr @nr end |
#tails=(value) ⇒ Object (writeonly)
Sets the attribute tails
13 14 15 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 13 def tails=(value) @tails = value end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
11 12 13 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 11 def w @w end |
Instance Method Details
#calculate ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 25 def calculate df=Statsample::Dataset.new({'v1'=>@v1,'v2'=>@v2}) df["abs"]=df.collect {|row| r=(row["v2"]-row["v1"]).abs } df["sgn"]=df.collect {|row| r=row["v2"]-row["v1"] r==0 ? 0 : r/r.abs } df=df.filter {|row| row["sgn"]!=0} df["rank"]=df["abs"].ranked @nr=df.cases @w=df.collect {|row| row["sgn"]*row["rank"] #p row["sgn"]*row["rank"] }.sum end |
#probability_exact ⇒ Object
Calculate exact probability. Don’t calculate for large Nr, please!
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 65 def probability_exact str_format="%0#{nr}b" combinations=2**nr #p str_format total_w=combinations.times.map {|i| comb=sprintf(str_format,i) w_local=comb.length.times.inject(0) {|ac,j| sgn=comb[j]=="0" ? -1 : 1 ac+(j+1)*sgn } }.sort total_w.find_all {|v| if @tails==:both v<=-w.abs or v>=w.abs elsif @tails==:left v<=w elsif @tails==:right v>=w end }.count/(combinations.to_f) end |
#probability_z ⇒ Object
Assuming normal distribution of W, this calculate the probability of samples with Z equal or higher than obtained on sample
60 61 62 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 60 def probability_z (1-Distribution::Normal.cdf(z))*(@tails==:both ? 2:1) end |
#report_building(generator) ⇒ Object
:nodoc:
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 42 def report_building(generator) # :nodoc: generator.section(:name=>@name) do |s| s.table(:name=>_("%s results") % @name) do |t| t.row([_("W Value"), "%0.3f" % @w]) t.row([_("Z"), "%0.3f (p: %0.3f)" % [z, probability_z]]) if(nr<=10) t.row([_("Exact probability"), "p-exact: %0.3f" % [probability_exact]]) end end end end |
#z ⇒ Object
53 54 55 56 |
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 53 def z sigma=Math.sqrt((nr*(nr+1)*(2*nr+1))/6) (w-0.5)/sigma end |