Class: Statsample::Bivariate::Pearson

Inherits:
Object
  • Object
show all
Includes:
Summarizable, Test
Defined in:
lib/statsample/bivariate/pearson.rb

Overview

Pearson correlation coefficient ®

The moment-product Pearson’s correlation coefficient, known as ‘r’ is a measure of bivariate associate between two continous variables.

Usage

a = Daru::Vector.new([1,2,3,4,5,6])
b = Daru::Vector.new([2,3,4,5,6,7])
pearson = Statsample::Bivariate::Pearson.new(a,b)
puts pearson.r
puts pearson.t
puts pearson.probability
puts pearson.summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Summarizable

#summary

Methods included from Test

chi_square, levene, #p_using_cdf, #t_critical, t_one_sample, t_two_samples_independent, u_mannwhitney, wilcoxon_signed_rank, #z_critical

Constructor Details

#initialize(v1, v2, opts = Hash.new) ⇒ Pearson

Returns a new instance of Pearson.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/statsample/bivariate/pearson.rb', line 27

def initialize(v1,v2,opts=Hash.new)
  @v1_name,@v2_name = v1.name,v2.name
  @v1,@v2           = Statsample.only_valid_clone(v1,v2)
  @n=@v1.size
  opts_default={
    :name=>_("Correlation (%s - %s)") % [@v1_name, @v2_name],
    :tails=>:both
  }
  @opts=opts.merge(opts_default)
  @opts.each{|k,v|
    self.send("#{k}=",v) if self.respond_to? k
  }
end

Instance Attribute Details

#nObject

Returns the value of attribute n.



26
27
28
# File 'lib/statsample/bivariate/pearson.rb', line 26

def n
  @n
end

#nameObject

Name of correlation



23
24
25
# File 'lib/statsample/bivariate/pearson.rb', line 23

def name
  @name
end

#tailsObject

Tails for probability (:both, :left or :right)



25
26
27
# File 'lib/statsample/bivariate/pearson.rb', line 25

def tails
  @tails
end

Instance Method Details

#probabilityObject



46
47
48
# File 'lib/statsample/bivariate/pearson.rb', line 46

def probability
  p_using_cdf(Distribution::T.cdf(t, @v1.size-2), tails)
end

#rObject



40
41
42
# File 'lib/statsample/bivariate/pearson.rb', line 40

def r
  Statsample::Bivariate.pearson(@v1,@v2)
end

#report_building(builder) ⇒ Object



49
50
51
# File 'lib/statsample/bivariate/pearson.rb', line 49

def report_building(builder)
  builder.text(_("%s : r=%0.3f (t:%0.3f, g.l.=%d, p:%0.3f / %s tails)") % [@name, r,t, (n-2), probability, tails])
end

#tObject



43
44
45
# File 'lib/statsample/bivariate/pearson.rb', line 43

def t
  Statsample::Bivariate.t_pearson(@v1,@v2)
end