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 2016-03-26 10:24:24 +0000
= 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.883      | 88.326 |
| v1       | 1.000   | 0.875      | 87.503 |
| v2       | 1.000   | 0.856      | 85.559 |
| v3       | 1.000   | 0.886      | 88.600 |
| v4       | 1.000   | 0.887      | 88.659 |
| v5       | 1.000   | 0.819      | 81.855 |
| v6       | 1.000   | 0.852      | 85.159 |
| v7       | 1.000   | 0.822      | 82.226 |
| v8       | 1.000   | 0.861      | 86.081 |
| v9       | 1.000   | 0.872      | 87.165 |
+----------+---------+------------+--------+

    Total Variance Explained
+--------------+---------+---------+---------+
|  Component   | E.Total |    %    | Cum. %  |
+--------------+---------+---------+---------+
| Component 1  | 8.611   | 86.113% | 86.113  |
| Component 2  | 0.926   | 9.263%  | 95.377  |
| Component 3  | 0.100   | 1.004%  | 96.380  |
| Component 4  | 0.089   | 0.894%  | 97.274  |
| Component 5  | 0.067   | 0.674%  | 97.948  |
| Component 6  | 0.060   | 0.597%  | 98.545  |
| Component 7  | 0.043   | 0.429%  | 98.974  |
| Component 8  | 0.040   | 0.399%  | 99.373  |
| Component 9  | 0.032   | 0.319%  | 99.692  |
| Component 10 | 0.031   | 0.308%  | 100.000 |
+--------------+---------+---------+---------+

    Component matrix
+----+------+
|    | PC_1 |
+----+------+
| v0 | .940 |
| v1 | .935 |
| v2 | .925 |
| v3 | .941 |
| v4 | .942 |
| v5 | .905 |
| v6 | .923 |
| v7 | .907 |
| v8 | .928 |
| v9 | .934 |
+----+------+

  Traditional Kaiser criterion (k>1) returns 1 factors
  == Velicer's MAP
    Eigenvalues
+----------+
|  Value   |
+----------+
| 8.611330 |
| 0.926336 |
| 0.100374 |
| 0.089363 |
| 0.067434 |
| 0.059687 |
| 0.042917 |
| 0.039857 |
| 0.031882 |
| 0.030820 |
+----------+

    Velicer's Average Squared Correlations
+----------------------+----------------------------+
| number of components | average square correlation |
+----------------------+----------------------------+
| 0                    | 0.722719                   |
| 1                    | 0.407274                   |
| 2                    | 0.036444                   |
| 3                    | 0.060324                   |
| 4                    | 0.106041                   |
| 5                    | 0.188101                   |
| 6                    | 0.225866                   |
| 7                    | 0.310435                   |
| 8                    | 0.538523                   |
| 9                    | 1.000000                   |
+----------------------+----------------------------+

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