Class: Rust::StatisticalTests::T

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

Overview

Class with utilities for running the T test.

Class Method Summary collapse

Class Method Details

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

Runs a paired T 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)


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rust/stats/tests.rb', line 190

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["t.a"] = d1
        Rust["t.b"] = d2
        
        warnings = Rust._eval("t.result = t.test(t.a, t.b, alternative='two.sided', paired=T)")
        result = Rust::StatisticalTests::Result.new
        result.name       = "Paired t-test"
        result.pvalue     = Rust._pull("t.result$p.value")
        result[:t]        = Rust._pull("t.result$statistic")
        result.exact      = true
        result.alpha      = alpha
        result.hypothesis = Rust::StatisticalTests::Hypothesis.find(options[:hypothesis])
        
        return result
    end
end

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

Runs an unpaired T 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)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/rust/stats/tests.rb', line 216

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["t.a"] = d1
        Rust["t.b"] = d2
        
        Rust._eval("t.result = t.test(t.a, t.b, alternative='two.sided', paired=F)")
        result = Rust::StatisticalTests::Result.new
        result.name       = "Welch Two Sample t-test"
        result.pvalue     = Rust._pull("t.result$p.value")
        result[:t]        = Rust._pull("t.result$statistic")
        result.exact      = true
        result.alpha      = alpha
        result.hypothesis = Rust::StatisticalTests::Hypothesis.find(options[:hypothesis])
        
        return result
    end
end