Class: Rust::Correlation::Spearman

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

Overview

Spearman correlation utility methods.

Class Method Summary collapse

Class Method Details

.estimate(d1, d2) ⇒ Object

Returns the Spearman correlation index between d1 and d2



77
78
79
# File 'lib/rust/stats/correlation.rb', line 77

def self.estimate(d1, d2)
    self.test(d1, d2).correlation
end

.test(d1, d2) ⇒ Object

Runs the Spearman correlation test between d1 and d2.

Raises:

  • (TypeError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rust/stats/correlation.rb', line 53

def self.test(d1, d2)
    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['correlation.a'] = d1
        Rust['correlation.b'] = d2
        
        _, warnings = Rust._eval("correlation.result <- cor.test(correlation.a, correlation.b, method='s')", true)
        
        result = Result.new
        result.name             = "Spearman's rank correlation rho"
        result.statistics['S']  = Rust._pull('correlation.result$statistic')
        result.pvalue           = Rust._pull('correlation.result$p.value')
        result.correlation      = Rust._pull('correlation.result$estimate')
        result.exact            = !warnings.include?("Cannot compute exact p-value with ties")
        
        return result
    end
end