Polychoric Correlation using two-step and joint method

Polychoric correlation in statsample requires installation of the statsample-bivariate-extension gem. This gem extends the Statsample::Bivariate class with useful algorithms for polychoric and tetrachoric correlation.

Statsample will automatically detect presence of polychoric/tetrachoric algorithms so there is no need to explicitly require the gem.

In this example we'll see how polychoric correlation can be performed using statsample.

In [3]:
require 'statsample'

Statsample::Analysis.store(Statsample::Bivariate::Polychoric) do 
  ct=Matrix[[rand(10)+50, rand(10)+50,  rand(10)+1],
            [rand(20)+5,  rand(50)+4,   rand(10)+1],
            [rand(8)+1,   rand(12)+1,   rand(10)+1]]

  # Estimation of polychoric correlation using two-step (default)
  poly=polychoric(ct, :name=>"Polychoric with two-step", :debug=>false)
  summary poly

  # Estimation of polychoric correlation using joint method (slow)
  poly=polychoric(ct, :method=>:joint, :name=>"Polychoric with joint")
  summary poly

  # Uses polychoric series (not recomended)

  poly=polychoric(ct, :method=>:polychoric_series, :name=>"Polychoric with polychoric series")
  summary poly
end

Statsample::Analysis.run_batch
Analysis 2015-06-04 12:48:32 +0530
= Statsample::Bivariate::Polychoric
  == Polychoric with two-step
    Contingence Table
+-------+-----+-----+-----+-------+
|       | Y=0 | Y=1 | Y=2 | Total |
+-------+-----+-----+-----+-------+
| X = 0 | 52  | 58  | 8   | 118   |
| X = 1 | 18  | 15  | 7   | 40    |
| X = 2 | 5   | 11  | 3   | 19    |
+-------+-----+-----+-----+-------+
| T     | 75  | 84  | 18  | 177   |
+-------+-----+-----+-----+-------+

    r: 0.1683
    Thresholds
+---------------+---------+
|               |  Value  |
+---------------+---------+
| Threshold X 0 | 0.4307  |
| Threshold X 1 | 1.2408  |
| Threshold Y 0 | -0.1924 |
| Threshold Y 1 | 1.2720  |
+---------------+---------+

    Iterations: 10
    Test of bivariate normality: X^2 = 3.941, df = 3, p= 0.26795
  == Polychoric with joint
    Contingence Table
+-------+-----+-----+-----+-------+
|       | Y=0 | Y=1 | Y=2 | Total |
+-------+-----+-----+-----+-------+
| X = 0 | 52  | 58  | 8   | 118   |
| X = 1 | 18  | 15  | 7   | 40    |
| X = 2 | 5   | 11  | 3   | 19    |
+-------+-----+-----+-----+-------+
| T     | 75  | 84  | 18  | 177   |
+-------+-----+-----+-----+-------+

    r: 0.1682
    Thresholds
+---------------+---------+
|               |  Value  |
+---------------+---------+
| Threshold X 0 | 0.4296  |
| Threshold X 1 | 1.2411  |
| Threshold Y 0 | -0.1936 |
| Threshold Y 1 | 1.2731  |
+---------------+---------+

    Iterations: 1
    Test of bivariate normality: X^2 = 3.940, df = 3, p= 0.26801
  == Polychoric with polychoric series
    Contingence Table
+-------+------+------+------+-------+
|       | Y=0  | Y=1  | Y=2  | Total |
+-------+------+------+------+-------+
| X = 0 | 52   | 58   | 8    | 118.0 |
| X = 1 | 18   | 15   | 7    | 40.0  |
| X = 2 | 5    | 11   | 3    | 19.0  |
+-------+------+------+------+-------+
| T     | 75.0 | 84.0 | 18.0 | 177.0 |
+-------+------+------+------+-------+

    r: 0.1701
    Thresholds
+---------------+---------+
|               |  Value  |
+---------------+---------+
| Threshold X 0 | 0.4307  |
| Threshold X 1 | 1.2408  |
| Threshold Y 0 | -0.1924 |
| Threshold Y 1 | 1.2720  |
+---------------+---------+

    Test of bivariate normality: X^2 = 3.941, df = 3, p= 0.26791