Class: Rust::StatisticalTests::Wilcoxon

Inherits:
Object
  • Object
show all
Defined in:
lib/rust/stats/tests.rb

Overview

Class with utilities for running Wilcoxon Signed-Rank test and Ranked-Sum test (a.k.a. Mann-Whitney U test).

Class Method Summary collapse

Class Method Details

.paired(d1, d2, alpha = 0.05, **options) ⇒ Object

Runs a Wilxoson Signed-Rank test for d1 and d2, with a given alpha (0.05, by default). options can be specified and directly passed to the R function.

Raises:

  • (TypeError)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rust/stats/tests.rb', line 133

def self.paired(d1, d2, alpha = 0.05, **options)
    raise TypeError, "Expecting Array of numerics" if !d1.is_a?(Array) || !d1.all? { |e| e.is_a?(Numeric) }
    raise TypeError, "Expecting Array of numerics" if !d2.is_a?(Array) || !d2.all? { |e| e.is_a?(Numeric) }
    raise "The two distributions have different size" if d1.size != d2.size
                
    Rust.exclusive do
        Rust["wilcox.a"] = d1
        Rust["wilcox.b"] = d2
        
        _, warnings = Rust._eval("wilcox.result = wilcox.test(wilcox.a, wilcox.b, alternative='two.sided', paired=T)", true)
        result = Rust::StatisticalTests::Result.new
        result.name       = "Wilcoxon Signed-Rank test"
        result.pvalue     = Rust._pull("wilcox.result$p.value")
        result[:w]        = Rust._pull("wilcox.result$statistic")
        result.exact      = !warnings.include?("cannot compute exact p-value with zeroes")
        result.alpha      = alpha
        result.hypothesis = Rust::StatisticalTests::Hypothesis.find(options[:hypothesis])
    
        return result
    end
end

.unpaired(d1, d2, alpha = 0.05, **options) ⇒ Object

Runs a Wilxoson Ranked-Sum (a.k.a. Mann-Whitney U) test for d1 and d2, with a given alpha (0.05, by default). options can be specified and directly passed to the R function.

Raises:

  • (TypeError)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rust/stats/tests.rb', line 159

def self.unpaired(d1, d2, alpha = 0.05, **options)
    raise TypeError, "Expecting Array of numerics" if !d1.is_a?(Array) || !d1.all? { |e| e.is_a?(Numeric) }
    raise TypeError, "Expecting Array of numerics" if !d2.is_a?(Array) || !d2.all? { |e| e.is_a?(Numeric) }
    
    Rust.exclusive do
        Rust["wilcox.a"] = d1
        Rust["wilcox.b"] = d2
        
        _, warnings = Rust._eval("wilcox.result = wilcox.test(wilcox.a, wilcox.b, alternative='two.sided', paired=F)", true)
        result = Rust::StatisticalTests::Result.new
        result.name       = "Wilcoxon Ranked-Sum test (a.k.a. Mann–Whitney U test)"
        result.pvalue     = Rust._pull("wilcox.result$p.value")
        result[:w]        = Rust._pull("wilcox.result$statistic")
        result.exact      = !warnings.include?("cannot compute exact p-value with ties")
        result.alpha      = alpha
        result.hypothesis = Rust::StatisticalTests::Hypothesis.find(options[:hypothesis])
        
        return result
    end
end