Class: Statsample::Factor::MAP

Inherits:
Object
  • Object
show all
Includes:
DirtyMemoize, Summarizable
Defined in:
lib/statsample/factor/map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Summarizable

#summary

Constructor Details

#initialize(matrix, opts = Hash.new) ⇒ MAP

Returns a new instance of MAP.



56
57
58
59
60
61
62
63
64
# File 'lib/statsample/factor/map.rb', line 56

def initialize(matrix, opts=Hash.new)
  @matrix=matrix
  opts_default={
    :use_gsl=>true,
    :name=>_("Velicer's MAP")
  }
  @opts=opts_default.merge(opts)
   opts_default.keys.each {|k| send("#{k}=", @opts[k]) }
end

Instance Attribute Details

#eigenvaluesObject (readonly)

Returns the value of attribute eigenvalues.



44
45
46
# File 'lib/statsample/factor/map.rb', line 44

def eigenvalues
  @eigenvalues
end

#fmObject (readonly)

Average squared correlations



48
49
50
# File 'lib/statsample/factor/map.rb', line 48

def fm
  @fm
end

#minfmObject (readonly)

Smallest average squared correlation



50
51
52
# File 'lib/statsample/factor/map.rb', line 50

def minfm
  @minfm
end

#nameObject

Name of analysis



43
44
45
# File 'lib/statsample/factor/map.rb', line 43

def name
  @name
end

#number_of_factorsObject (readonly)

Number of factors to retain



46
47
48
# File 'lib/statsample/factor/map.rb', line 46

def number_of_factors
  @number_of_factors
end

#use_gslObject

Returns the value of attribute use_gsl.



52
53
54
# File 'lib/statsample/factor/map.rb', line 52

def use_gsl
  @use_gsl
end

Class Method Details

.with_dataset(ds, opts = Hash.new) ⇒ Object



53
54
55
# File 'lib/statsample/factor/map.rb', line 53

def self.with_dataset(ds,opts=Hash.new)
  new(ds.correlation_matrix,opts)
end

Instance Method Details

#computeObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/statsample/factor/map.rb', line 65

def compute
  gsl_m=(use_gsl and Statsample.has_gsl?) ? @matrix.to_gsl : @matrix
  klass_m=gsl_m.class
  eigvect,@eigenvalues=gsl_m.eigenvectors_matrix, gsl_m.eigenvalues
  eigenvalues_sqrt=@eigenvalues.collect {|v| Math.sqrt(v)}
  loadings=eigvect*(klass_m.diagonal(*eigenvalues_sqrt))
  fm=Array.new(@matrix.row_size)
  ncol=@matrix.column_size
  
  fm[0]=(gsl_m.mssq - ncol).quo(ncol*(ncol-1))
  
  (ncol-1).times do |m|
    puts "MAP:Eigenvalue #{m+1}" if $DEBUG
    a=use_gsl ? loadings[0..(loadings.row_size-1),0..m] : 
      loadings.minor(0..(loadings.row_size-1),0..m)
    partcov= gsl_m - (a*a.transpose)
    
    d=klass_m.diagonal(*(partcov.diagonal.collect {|v| Math::sqrt(1/v)}))
    pr=d*partcov*d
    fm[m+1]=(pr.mssq-ncol).quo(ncol*(ncol-1))
  end
  minfm=fm[0]
  nfactors=0
  @errors=[]
  fm.each_with_index do |v,s|
    if defined?(Complex) and v.is_a? ::Complex
      @errors.push(s)
    else
      if v < minfm
        minfm=v
        nfactors=s
      end
    end
  end
  @number_of_factors=nfactors
  @fm=fm
  @minfm=minfm
  
end

#report_building(g) ⇒ Object

:nodoc:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/statsample/factor/map.rb', line 104

def report_building(g) #:nodoc:
  g.section(:name=>@name) do |s|
    s.table(:name=>_("Eigenvalues"),:header=>[_("Value")]) do |t|
      eigenvalues.each_with_index do |e,i|
          t.row([@errors.include?(i) ? "*" : "%0.6f" % e])
      end
    end
    s.table(:name=>_("Velicer's Average Squared Correlations"), :header=>[_("number of components"),_("average square correlation")]) do |t|
      fm.each_with_index do |v,i|
        t.row(["%d" % i, @errors.include?(i) ? "*" : "%0.6f" % v])
      end
    end
    s.text(_("The smallest average squared correlation is : %0.6f" % minfm))
    s.text(_("The number of components is : %d" % number_of_factors))
  end
end