{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Velicer MAP test" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "if(window['d3'] === undefined ||\n", " window['Nyaplot'] === undefined){\n", " var path = {\"d3\":\"http://d3js.org/d3.v3.min\",\"downloadable\":\"http://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", "\n", "\n", "\n", " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", "\n", " require.config({paths: path, shim:shim});\n", "\n", "\n", "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", "\n", "\tvar script = d3.select(\"head\")\n", "\t .append(\"script\")\n", "\t .attr(\"src\", \"http://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", "\t .attr(\"async\", true);\n", "\n", "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", "\n", "\n", "\t var event = document.createEvent(\"HTMLEvents\");\n", "\t event.initEvent(\"load_nyaplot\",false,false);\n", "\t window.dispatchEvent(event);\n", "\t console.log('Finished loading Nyaplotjs');\n", "\n", "\t};\n", "\n", "\n", "});});\n", "}\n" ], "text/plain": [ "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"http://d3js.org/d3.v3.min\\\",\\\"downloadable\\\":\\\"http://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"http://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Analysis 2015-06-05 15:33:08 +0530\n", "= Statsample::Factor::MAP\n", " There are 2 real factors on data\n", " == Principal Component Analysis\n", " Number of factors: 1\n", " Communalities\n", "+----------+---------+------------+--------+\n", "| Variable | Initial | Extraction | % |\n", "+----------+---------+------------+--------+\n", "| v0 | 1.000 | 0.893 | 89.251 |\n", "| v1 | 1.000 | 0.904 | 90.403 |\n", "| v2 | 1.000 | 0.895 | 89.502 |\n", "| v3 | 1.000 | 0.898 | 89.795 |\n", "| v4 | 1.000 | 0.902 | 90.224 |\n", "| v5 | 1.000 | 0.889 | 88.943 |\n", "| v6 | 1.000 | 0.860 | 86.031 |\n", "| v7 | 1.000 | 0.859 | 85.867 |\n", "| v8 | 1.000 | 0.888 | 88.845 |\n", "| v9 | 1.000 | 0.869 | 86.903 |\n", "+----------+---------+------------+--------+\n", "\n", " Total Variance Explained\n", "+--------------+---------+---------+---------+\n", "| Component | E.Total | % | Cum. % |\n", "+--------------+---------+---------+---------+\n", "| Component 1 | 8.858 | 88.576% | 88.576 |\n", "| Component 2 | 0.781 | 7.806% | 96.382 |\n", "| Component 3 | 0.081 | 0.812% | 97.194 |\n", "| Component 4 | 0.058 | 0.578% | 97.772 |\n", "| Component 5 | 0.057 | 0.572% | 98.344 |\n", "| Component 6 | 0.055 | 0.545% | 98.889 |\n", "| Component 7 | 0.035 | 0.354% | 99.243 |\n", "| Component 8 | 0.028 | 0.276% | 99.519 |\n", "| Component 9 | 0.026 | 0.263% | 99.782 |\n", "| Component 10 | 0.022 | 0.218% | 100.000 |\n", "+--------------+---------+---------+---------+\n", "\n", " Component matrix\n", "+----+------+\n", "| | PC_1 |\n", "+----+------+\n", "| v0 | .945 |\n", "| v1 | .951 |\n", "| v2 | .946 |\n", "| v3 | .948 |\n", "| v4 | .950 |\n", "| v5 | .943 |\n", "| v6 | .928 |\n", "| v7 | .927 |\n", "| v8 | .943 |\n", "| v9 | .932 |\n", "+----+------+\n", "\n", " Traditional Kaiser criterion (k>1) returns 1 factors\n", " == Velicer's MAP\n", " Eigenvalues\n", "+----------+\n", "| Value |\n", "+----------+\n", "| 8.857635 |\n", "| 0.780560 |\n", "| 0.081194 |\n", "| 0.057825 |\n", "| 0.057185 |\n", "| 0.054539 |\n", "| 0.035362 |\n", "| 0.027592 |\n", "| 0.026269 |\n", "| 0.021840 |\n", "+----------+\n", "\n", " Velicer's Average Squared Correlations\n", "+----------------------+----------------------------+\n", "| number of components | average square correlation |\n", "+----------------------+----------------------------+\n", "| 0 | 0.767626 |\n", "| 1 | 0.431737 |\n", "| 2 | 0.037660 |\n", "| 3 | 0.061151 |\n", "| 4 | 0.099789 |\n", "| 5 | 0.163735 |\n", "| 6 | 0.192856 |\n", "| 7 | 0.308091 |\n", "| 8 | 0.472457 |\n", "| 9 | 1.000000 |\n", "+----------------------+----------------------------+\n", "\n", " The smallest average squared correlation is : 0.037660\n", " The number of components is : 2\n", " Velicer's MAP Test returns 2 factors to preserve\n", "\n" ] } ], "source": [ "require 'statsample'\n", "\n", "Statsample::Analysis.store(Statsample::Factor::MAP) do\n", " \n", " rng = Distribution::Normal.rng\n", " samples = 100\n", " variables = 10\n", " \n", " f1 = rnorm(samples)\n", " f2 = rnorm(samples)\n", " \n", " vectors={}\n", " \n", " variables.times do |i|\n", " vectors[\"v#{i}\".to_sym]= Daru::Vector.new(\n", " samples.times.collect do |nv| \n", " if i<5\n", " f1[nv]*5 + f2[nv] *2 +rng.call\n", " else\n", " f1[nv]*2 + f2[nv] *3 +rng.call\n", " end\n", " end)\n", " end\n", " \n", " \n", " ds = Daru::DataFrame.new(vectors)\n", " cor=cor(ds)\n", " pca=pca(cor)\n", " \n", " map=Statsample::Factor::MAP.new(cor)\n", " \n", " echo (\"There are 2 real factors on data\")\n", " summary(pca)\n", " echo(\"Traditional Kaiser criterion (k>1) returns #{pca.m} factors\")\n", " summary(map)\n", " echo(\"Velicer's MAP Test returns #{map.number_of_factors} factors to preserve\")\n", "end\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 }