Class: Statsample::Test::WilcoxonSignedRank

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Summarizable

#summary

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

#nameObject

Name of F analysis



10
11
12
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 10

def name
  @name
end

#nrObject (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

Parameters:

  • value

    the value to set the attribute tails to.



13
14
15
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 13

def tails=(value)
  @tails = value
end

#wObject (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

#calculateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 26

def calculate
df = Daru::DataFrame.new({:v1 => @v1,:v2 => @v2})
# df[:abs]=df.collect(:row) { |row|  (row[:v2] - row[:v1]).abs }
  df[:abs] = (df[:v2] - df[:v1]).abs
df[:sgn] = df.collect(:row) { |row| 
r = row[:v2] - row[:v1]
r == 0 ? 0 : r/r.abs
}
df = df.filter_rows { |row| row[:sgn] != 0}
df[:rank] = df[:abs].ranked
@nr = df.nrows

@w = df.collect(:row) { |row|
    row[:sgn] * row[:rank]
}.sum
end

#probability_exactObject

Calculate exact probability. Don’t calculate for large Nr, please!



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 66

def probability_exact
str_format="%0#{nr}b"
combinations=2**nr
#p str_format
total_w=combinations.times.map do |i|
    comb=sprintf(str_format,i)
    w_local=comb.length.times.inject(0) do |ac,j|
      sgn=comb[j]=="0" ? -1 : 1
  ac+(j+1)*sgn
    end
end.sort

total_w.find_all do |v| 
    if @tails==:both
      v<=-w.abs or v>=w.abs
    elsif @tails==:left
      v<=w
    elsif @tails==:right
  v>=w
    end
end.count/(combinations.to_f)
end

#probability_zObject

Assuming normal distribution of W, this calculate the probability of samples with Z equal or higher than obtained on sample



61
62
63
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 61

def probability_z
(1-Distribution::Normal.cdf(z))*(@tails==:both ? 2:1)
end

#report_building(generator) ⇒ Object

:nodoc:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 43

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

#zObject



54
55
56
57
# File 'lib/statsample/test/wilcoxonsignedrank.rb', line 54

def z
sigma=Math.sqrt((nr*(nr+1)*(2*nr+1))/6)
(w-0.5)/sigma
end