{ "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\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://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\", \"https://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\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://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\\\", \\\"https://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 2016-03-26 10:24:24 +0000\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.883 | 88.326 |\n", "| v1 | 1.000 | 0.875 | 87.503 |\n", "| v2 | 1.000 | 0.856 | 85.559 |\n", "| v3 | 1.000 | 0.886 | 88.600 |\n", "| v4 | 1.000 | 0.887 | 88.659 |\n", "| v5 | 1.000 | 0.819 | 81.855 |\n", "| v6 | 1.000 | 0.852 | 85.159 |\n", "| v7 | 1.000 | 0.822 | 82.226 |\n", "| v8 | 1.000 | 0.861 | 86.081 |\n", "| v9 | 1.000 | 0.872 | 87.165 |\n", "+----------+---------+------------+--------+\n", "\n", " Total Variance Explained\n", "+--------------+---------+---------+---------+\n", "| Component | E.Total | % | Cum. % |\n", "+--------------+---------+---------+---------+\n", "| Component 1 | 8.611 | 86.113% | 86.113 |\n", "| Component 2 | 0.926 | 9.263% | 95.377 |\n", "| Component 3 | 0.100 | 1.004% | 96.380 |\n", "| Component 4 | 0.089 | 0.894% | 97.274 |\n", "| Component 5 | 0.067 | 0.674% | 97.948 |\n", "| Component 6 | 0.060 | 0.597% | 98.545 |\n", "| Component 7 | 0.043 | 0.429% | 98.974 |\n", "| Component 8 | 0.040 | 0.399% | 99.373 |\n", "| Component 9 | 0.032 | 0.319% | 99.692 |\n", "| Component 10 | 0.031 | 0.308% | 100.000 |\n", "+--------------+---------+---------+---------+\n", "\n", " Component matrix\n", "+----+------+\n", "| | PC_1 |\n", "+----+------+\n", "| v0 | .940 |\n", "| v1 | .935 |\n", "| v2 | .925 |\n", "| v3 | .941 |\n", "| v4 | .942 |\n", "| v5 | .905 |\n", "| v6 | .923 |\n", "| v7 | .907 |\n", "| v8 | .928 |\n", "| v9 | .934 |\n", "+----+------+\n", "\n", " Traditional Kaiser criterion (k>1) returns 1 factors\n", " == Velicer's MAP\n", " Eigenvalues\n", "+----------+\n", "| Value |\n", "+----------+\n", "| 8.611330 |\n", "| 0.926336 |\n", "| 0.100374 |\n", "| 0.089363 |\n", "| 0.067434 |\n", "| 0.059687 |\n", "| 0.042917 |\n", "| 0.039857 |\n", "| 0.031882 |\n", "| 0.030820 |\n", "+----------+\n", "\n", " Velicer's Average Squared Correlations\n", "+----------------------+----------------------------+\n", "| number of components | average square correlation |\n", "+----------------------+----------------------------+\n", "| 0 | 0.722719 |\n", "| 1 | 0.407274 |\n", "| 2 | 0.036444 |\n", "| 3 | 0.060324 |\n", "| 4 | 0.106041 |\n", "| 5 | 0.188101 |\n", "| 6 | 0.225866 |\n", "| 7 | 0.310435 |\n", "| 8 | 0.538523 |\n", "| 9 | 1.000000 |\n", "+----------------------+----------------------------+\n", "\n", " The smallest average squared correlation is : 0.036444\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 }