Velicer MAP test

In [1]:
require 'statsample'

Statsample::Analysis.store(Statsample::Factor::MAP) do
  
  rng       = Distribution::Normal.rng
  samples   = 100
  variables = 10
  
  f1 = rnorm(samples)
  f2 = rnorm(samples)
  
  vectors={}
  
  variables.times do |i|
  vectors["v#{i}".to_sym]= Daru::Vector.new(
    samples.times.collect do |nv|    
      if i<5
        f1[nv]*5 + f2[nv] *2 +rng.call
      else
        f1[nv]*2 + f2[nv] *3 +rng.call
      end
    end)
  end
  
  
  ds = Daru::DataFrame.new(vectors)
  cor=cor(ds)
  pca=pca(cor)
  
  map=Statsample::Factor::MAP.new(cor)
  
  echo ("There are 2 real factors on data")
  summary(pca)
  echo("Traditional Kaiser criterion (k>1) returns #{pca.m} factors")
  summary(map)
  echo("Velicer's MAP Test returns #{map.number_of_factors} factors to preserve")
end
  Statsample::Analysis.run_batch
Analysis 2015-06-05 15:33:08 +0530
= Statsample::Factor::MAP
  There are 2 real factors on data
  == Principal Component Analysis
    Number of factors: 1
    Communalities
+----------+---------+------------+--------+
| Variable | Initial | Extraction |   %    |
+----------+---------+------------+--------+
| v0       | 1.000   | 0.893      | 89.251 |
| v1       | 1.000   | 0.904      | 90.403 |
| v2       | 1.000   | 0.895      | 89.502 |
| v3       | 1.000   | 0.898      | 89.795 |
| v4       | 1.000   | 0.902      | 90.224 |
| v5       | 1.000   | 0.889      | 88.943 |
| v6       | 1.000   | 0.860      | 86.031 |
| v7       | 1.000   | 0.859      | 85.867 |
| v8       | 1.000   | 0.888      | 88.845 |
| v9       | 1.000   | 0.869      | 86.903 |
+----------+---------+------------+--------+

    Total Variance Explained
+--------------+---------+---------+---------+
|  Component   | E.Total |    %    | Cum. %  |
+--------------+---------+---------+---------+
| Component 1  | 8.858   | 88.576% | 88.576  |
| Component 2  | 0.781   | 7.806%  | 96.382  |
| Component 3  | 0.081   | 0.812%  | 97.194  |
| Component 4  | 0.058   | 0.578%  | 97.772  |
| Component 5  | 0.057   | 0.572%  | 98.344  |
| Component 6  | 0.055   | 0.545%  | 98.889  |
| Component 7  | 0.035   | 0.354%  | 99.243  |
| Component 8  | 0.028   | 0.276%  | 99.519  |
| Component 9  | 0.026   | 0.263%  | 99.782  |
| Component 10 | 0.022   | 0.218%  | 100.000 |
+--------------+---------+---------+---------+

    Component matrix
+----+------+
|    | PC_1 |
+----+------+
| v0 | .945 |
| v1 | .951 |
| v2 | .946 |
| v3 | .948 |
| v4 | .950 |
| v5 | .943 |
| v6 | .928 |
| v7 | .927 |
| v8 | .943 |
| v9 | .932 |
+----+------+

  Traditional Kaiser criterion (k>1) returns 1 factors
  == Velicer's MAP
    Eigenvalues
+----------+
|  Value   |
+----------+
| 8.857635 |
| 0.780560 |
| 0.081194 |
| 0.057825 |
| 0.057185 |
| 0.054539 |
| 0.035362 |
| 0.027592 |
| 0.026269 |
| 0.021840 |
+----------+

    Velicer's Average Squared Correlations
+----------------------+----------------------------+
| number of components | average square correlation |
+----------------------+----------------------------+
| 0                    | 0.767626                   |
| 1                    | 0.431737                   |
| 2                    | 0.037660                   |
| 3                    | 0.061151                   |
| 4                    | 0.099789                   |
| 5                    | 0.163735                   |
| 6                    | 0.192856                   |
| 7                    | 0.308091                   |
| 8                    | 0.472457                   |
| 9                    | 1.000000                   |
+----------------------+----------------------------+

    The smallest average squared correlation is : 0.037660
    The number of components is : 2
  Velicer's MAP Test returns 2 factors to preserve