Module: Rstat

Defined in:
lib/rstat.rb,
lib/rstat/version.rb,
lib/rstat/regression_analysis/sample_correlation_coefficient.rb,
lib/rstat/regression_analysis/linear_regression/simple_linear_regression.rb

Constant Summary collapse

VERSION =
'0.1.2'

Class Method Summary collapse

Class Method Details

.pearsons_correlation_coefficient(x, y) ⇒ Object



17
18
19
# File 'lib/rstat/regression_analysis/sample_correlation_coefficient.rb', line 17

def self.pearsons_correlation_coefficient(x, y)
  Rstat.sample_correlation_coefficient x, y
end

.sample_correlation_coefficient(x, y) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rstat/regression_analysis/sample_correlation_coefficient.rb', line 2

def self.sample_correlation_coefficient(x, y)
  if x.length != y.length
    return nil
  end

  xm = x.mean
  ym = y.mean

  top = x.zip(y).map { |a, b| (a - xm) * (b - ym) }.inject(:+)

  bottom = Math.sqrt(x.map { |a| (a - xm) ** 2 }.inject(:+)) * Math.sqrt(y.map { |b| (b - ym) ** 2 }.inject(:+))

  top / bottom
end

.simple_linear_regression(x, y) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rstat/regression_analysis/linear_regression/simple_linear_regression.rb', line 2

def self.simple_linear_regression(x, y)
  if x.length != y.length
    return nil
  end

  n = x.length

  xy = x.zip(y).map { |a| a[0] * a[1] }

  xx = x.map { |a| a * a }

  sumx = x.sum

  sumy = y.sum

  sumxy = xy.sum

  sumxx = xx.sum

  slope = ((n * sumxy) - (sumx * sumy)) / ((n * sumxx) - (sumx ** 2))

  intercept = (sumy - (slope * sumx)) / n

  { :slope => slope, :intercept => intercept }
end

.simple_linear_regression_intercept(x, y) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/rstat/regression_analysis/linear_regression/simple_linear_regression.rb', line 36

def self.simple_linear_regression_intercept(x, y)
  if x.length != y.length
    return nil
  end

  Rstat.simple_linear_regression(x, y)[:intercept]
end

.simple_linear_regression_slope(x, y) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/rstat/regression_analysis/linear_regression/simple_linear_regression.rb', line 28

def self.simple_linear_regression_slope(x, y)
  if x.length != y.length
    return nil
  end

  Rstat.simple_linear_regression(x, y)[:slope]
end