{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Polychoric Correlation using two-step and joint method\n", "\n", "Polychoric correlation in statsample requires installation of the [statsample-bivariate-extension](https://rubygems.org/gems/statsample-bivariate-extension) gem. This gem extends the Statsample::Bivariate class with useful algorithms for polychoric and tetrachoric correlation.\n", "\n", "Statsample will automatically detect presence of polychoric/tetrachoric algorithms so there is no need to explicitly require the gem.\n", "\n", "In this example we'll see how polychoric correlation can be performed using statsample." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Analysis 2015-06-04 12:48:32 +0530\n", "= Statsample::Bivariate::Polychoric\n", " == Polychoric with two-step\n", " Contingence Table\n", "+-------+-----+-----+-----+-------+\n", "| | Y=0 | Y=1 | Y=2 | Total |\n", "+-------+-----+-----+-----+-------+\n", "| X = 0 | 52 | 58 | 8 | 118 |\n", "| X = 1 | 18 | 15 | 7 | 40 |\n", "| X = 2 | 5 | 11 | 3 | 19 |\n", "+-------+-----+-----+-----+-------+\n", "| T | 75 | 84 | 18 | 177 |\n", "+-------+-----+-----+-----+-------+\n", "\n", " r: 0.1683\n", " Thresholds\n", "+---------------+---------+\n", "| | Value |\n", "+---------------+---------+\n", "| Threshold X 0 | 0.4307 |\n", "| Threshold X 1 | 1.2408 |\n", "| Threshold Y 0 | -0.1924 |\n", "| Threshold Y 1 | 1.2720 |\n", "+---------------+---------+\n", "\n", " Iterations: 10\n", " Test of bivariate normality: X^2 = 3.941, df = 3, p= 0.26795\n", " == Polychoric with joint\n", " Contingence Table\n", "+-------+-----+-----+-----+-------+\n", "| | Y=0 | Y=1 | Y=2 | Total |\n", "+-------+-----+-----+-----+-------+\n", "| X = 0 | 52 | 58 | 8 | 118 |\n", "| X = 1 | 18 | 15 | 7 | 40 |\n", "| X = 2 | 5 | 11 | 3 | 19 |\n", "+-------+-----+-----+-----+-------+\n", "| T | 75 | 84 | 18 | 177 |\n", "+-------+-----+-----+-----+-------+\n", "\n", " r: 0.1682\n", " Thresholds\n", "+---------------+---------+\n", "| | Value |\n", "+---------------+---------+\n", "| Threshold X 0 | 0.4296 |\n", "| Threshold X 1 | 1.2411 |\n", "| Threshold Y 0 | -0.1936 |\n", "| Threshold Y 1 | 1.2731 |\n", "+---------------+---------+\n", "\n", " Iterations: 1\n", " Test of bivariate normality: X^2 = 3.940, df = 3, p= 0.26801\n", " == Polychoric with polychoric series\n", " Contingence Table\n", "+-------+------+------+------+-------+\n", "| | Y=0 | Y=1 | Y=2 | Total |\n", "+-------+------+------+------+-------+\n", "| X = 0 | 52 | 58 | 8 | 118.0 |\n", "| X = 1 | 18 | 15 | 7 | 40.0 |\n", "| X = 2 | 5 | 11 | 3 | 19.0 |\n", "+-------+------+------+------+-------+\n", "| T | 75.0 | 84.0 | 18.0 | 177.0 |\n", "+-------+------+------+------+-------+\n", "\n", " r: 0.1701\n", " Thresholds\n", "+---------------+---------+\n", "| | Value |\n", "+---------------+---------+\n", "| Threshold X 0 | 0.4307 |\n", "| Threshold X 1 | 1.2408 |\n", "| Threshold Y 0 | -0.1924 |\n", "| Threshold Y 1 | 1.2720 |\n", "+---------------+---------+\n", "\n", " Test of bivariate normality: X^2 = 3.941, df = 3, p= 0.26791\n", "\n" ] } ], "source": [ "require 'statsample'\n", "\n", "Statsample::Analysis.store(Statsample::Bivariate::Polychoric) do \n", " ct=Matrix[[rand(10)+50, rand(10)+50, rand(10)+1],\n", " [rand(20)+5, rand(50)+4, rand(10)+1],\n", " [rand(8)+1, rand(12)+1, rand(10)+1]]\n", "\n", " # Estimation of polychoric correlation using two-step (default)\n", " poly=polychoric(ct, :name=>\"Polychoric with two-step\", :debug=>false)\n", " summary poly\n", "\n", " # Estimation of polychoric correlation using joint method (slow)\n", " poly=polychoric(ct, :method=>:joint, :name=>\"Polychoric with joint\")\n", " summary poly\n", "\n", " # Uses polychoric series (not recomended)\n", "\n", " poly=polychoric(ct, :method=>:polychoric_series, :name=>\"Polychoric with polychoric series\")\n", " summary poly\n", "end\n", "\n", "Statsample::Analysis.run_batch" ] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.2.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": "rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.2.1" } }, "nbformat": 4, "nbformat_minor": 0 }