class Statsample::Test::WilcoxonSignedRank

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.

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.

Attributes

name[RW]

Name of F analysis

nr[R]
tails[W]
w[R]

Public Class Methods

new(v1,v2, opts=Hash.new) click to toggle source

Parameters:

# 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

Public Instance Methods

calculate() click to toggle source
# 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() click to toggle source

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

# 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() click to toggle source

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

# File lib/statsample/test/wilcoxonsignedrank.rb, line 60
def probability_z
          (1-Distribution::Normal.cdf(z))*(@tails==:both ? 2:1)
end
z() click to toggle source
# 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