Module: Statsample::Bivariate

Defined in:
lib/statsample/bivariate/polychoric.rb,
lib/statsample/bivariate/tetrachoric.rb,
lib/statsample/bivariate/extension_version.rb

Defined Under Namespace

Classes: Polychoric, Tetrachoric

Constant Summary collapse

EXTENSION_VERSION =

Version of bivariate extension

"1.2.0"

Class Method Summary collapse

Class Method Details

.polychoric(v1, v2) ⇒ Object

Calculate Polychoric correlation for two vectors.


6
7
8
9
# File 'lib/statsample/bivariate/polychoric.rb', line 6

def self.polychoric(v1,v2)
  pc=Polychoric.new_with_vectors(v1,v2)
  pc.r
end

.polychoric_correlation_matrix(ds) ⇒ Object

Polychoric correlation matrix. Order of rows and columns depends on DataFrame#vectors order


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/statsample/bivariate/polychoric.rb', line 13

def self.polychoric_correlation_matrix(ds)
  cache={}
  matrix=ds.collect_matrix do |row,col|
    if row==col
      1.0
    else
      begin
        if cache[[col,row]].nil?
          poly=polychoric(ds[row],ds[col])
          cache[[row,col]]=poly
          poly
        else
          cache[[col,row]]
        end
      rescue RuntimeError
        nil
      end
    end
  end
  matrix.extend CovariateMatrix
  matrix.fields=ds.vectors.to_a
  matrix
end

.tetrachoric(v1, v2) ⇒ Object

Calculate Tetrachoric correlation for two vectors.


4
5
6
7
# File 'lib/statsample/bivariate/tetrachoric.rb', line 4

def self.tetrachoric(v1,v2)
  tc=Tetrachoric.new_with_vectors(v1,v2)
  tc.r
end

.tetrachoric_correlation_matrix(ds) ⇒ Object

Tetrachoric correlation matrix. Order of rows and columns depends on DataFrame#vectors order


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/statsample/bivariate/tetrachoric.rb', line 11

def self.tetrachoric_correlation_matrix(ds)
  cache={}
  matrix=ds.collect_matrix do |row,col|
    if row==col
      1.0
    else
      begin
        if cache[[col,row]].nil?
          r=tetrachoric(ds[row],ds[col])
          cache[[row,col]]=r
          r
        else
          cache[[col,row]]
        end
      rescue RuntimeError
        nil
      end
    end
  end
  
  matrix.extend CovariateMatrix
  matrix.fields=ds.vectors.to_a
  matrix
end