Class: Statsample::Reliability::MultiScaleAnalysis

Inherits:
Object
  • Object
show all
Includes:
Summarizable
Defined in:
lib/statsample/reliability/multiscaleanalysis.rb

Overview

DSL for analysis of multiple scales analysis. Retrieves reliability analysis for each scale and provides fast accessors to correlations matrix, PCA and Factor Analysis.

Usage

@x1 = Daru::Vector.new([1,1,1,1,2,2,2,2,3,3,3,30])
@x2 = Daru::Vector.new([1,1,1,2,2,3,3,3,3,4,4,50])
@x3 = Daru::Vector.new([2,2,1,1,1,2,2,2,3,4,5,40])
@x4 = Daru::Vector.new([1,2,3,4,4,4,4,3,4,4,5,30])
ds  = Daru::DataFrame.new({:x1 => @x1,:x2 => @x2,:x3 => @x3,:x4 => @x4})
opts={:name=>"Scales", # Name of analysis
      :summary_correlation_matrix=>true, # Add correlation matrix
      :summary_pca } # Add PCA between scales
msa=Statsample::Reliability::MultiScaleAnalysis.new(opts) do |m|
  m.scale :s1, ds.clone([:x1, :x2])
  m.scale :s2, ds.clone([:x3, :x4]), {:name=>"Scale 2"}
end
# Retrieve summary
puts msa.summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Summarizable

#summary

Constructor Details

#initialize(opts = Hash.new, &block) ⇒ MultiScaleAnalysis

Generates a new MultiScaleAnalysis Opts could be any accessor of the class

  • :name,

  • :summary_correlation_matrix

  • :summary_pca

  • :summary_principal_axis

  • :summary_map

  • :pca_options

  • :factor_analysis_options

  • :map_options

If block given, all methods should be called inside object environment.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 64

def initialize(opts=Hash.new, &block)
  @scales=Hash.new
  @scales_keys=Array.new
  opts_default={  :name=>_("Multiple Scale analysis"),
                  :summary_correlation_matrix=>false,
                  :summary_pca=>false,
                  :summary_principal_axis=>false,
                  :summary_parallel_analysis=>false,
                  :summary_map=>false,
                  :pca_options=>Hash.new,
                  :principal_axis_options=>Hash.new,
                  :parallel_analysis_options=>Hash.new,
                  :map_options=>Hash.new
  }
  @opts=opts_default.merge(opts)
  @opts.each{|k,v|
    self.send("#{k}=",v) if self.respond_to? k
  }

  if block
    block.arity<1 ? instance_eval(&block) : block.call(self)
  end
end

Instance Attribute Details

#map_optionsObject

Options for MAP



48
49
50
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 48

def map_options
  @map_options
end

#nameObject

Name of analysis



28
29
30
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 28

def name
  @name
end

#parallel_analysis_optionsObject

Options for Parallel Analysis



43
44
45
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 43

def parallel_analysis_options
  @parallel_analysis_options
end

#pca_optionsObject

Options for Factor::PCA object



36
37
38
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 36

def pca_options
  @pca_options
end

#principal_axis_optionsObject

Options for Factor::PrincipalAxis



38
39
40
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 38

def principal_axis_options
  @principal_axis_options
end

#scalesObject (readonly)

Hash with scales



26
27
28
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 26

def scales
  @scales
end

#summary_correlation_matrixObject

Add a correlation matrix on summary



30
31
32
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 30

def summary_correlation_matrix
  @summary_correlation_matrix
end

#summary_mapObject

Add MPA to summary



46
47
48
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 46

def summary_map
  @summary_map
end

#summary_parallel_analysisObject

Add Parallel Analysis to summary



41
42
43
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 41

def summary_parallel_analysis
  @summary_parallel_analysis
end

#summary_pcaObject

Add PCA to summary



32
33
34
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 32

def summary_pca
  @summary_pca
end

#summary_principal_axisObject

Add Principal Axis to summary



34
35
36
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 34

def summary_principal_axis
  @summary_principal_axis
end

Instance Method Details

#correlation_matrixObject

Retrieves a Correlation Matrix between scales.



140
141
142
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 140

def correlation_matrix
  Statsample::Bivariate.correlation_matrix(dataset_from_scales)
end

#dataset_from_scalesObject



125
126
127
128
129
130
131
132
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 125

def dataset_from_scales
  ds = Daru::DataFrame.new({}, order: @scales_keys.map(&:to_sym))
  @scales.each_pair do |code,scale|
    ds[code.to_sym] = scale.ds.vector_sum
  end
  
  ds
end

#delete_scale(code) ⇒ Object

Delete ScaleAnalysis named code



103
104
105
106
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 103

def delete_scale(code)
  @scales_keys.delete code
  @scales.delete code
end

#map(opts = nil) ⇒ Object

Retrieve Velicer’s MAP using all scales.



115
116
117
118
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 115

def map(opts=nil)
  opts||=map_options
  Statsample::Factor::MAP.new(correlation_matrix, opts)
end

#parallel_analysis(opts = nil) ⇒ Object



134
135
136
137
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 134

def parallel_analysis(opts=nil)
  opts||=parallel_analysis_options
  Statsample::Factor::ParallelAnalysis.new(dataset_from_scales, opts)
end

#pca(opts = nil) ⇒ Object

Retrieves a Principal Component Analysis (Factor::PCA) using all scales, using opts a options.



109
110
111
112
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 109

def pca(opts=nil)
  opts ||= pca_options        
  Statsample::Factor::PCA.new(correlation_matrix, opts)
end

#principal_axis_analysis(opts = nil) ⇒ Object

Retrieves a PrincipalAxis Analysis (Factor::PrincipalAxis) using all scales, using opts a options.



121
122
123
124
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 121

def principal_axis_analysis(opts=nil)
  opts||=principal_axis_options
  Statsample::Factor::PrincipalAxis.new(correlation_matrix, opts)
end

#report_building(b) ⇒ Object

:nodoc:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 144

def report_building(b) # :nodoc:
  b.section(:name=>name) do |s|
    s.section(:name=>_("Reliability analysis of scales")) do |s2|
      @scales.each_pair do |k, scale|
        s2.parse_element(scale)
      end
    end
    if summary_correlation_matrix
      s.section(:name=>_("Correlation matrix for %s") % name) do |s2|
        s2.parse_element(correlation_matrix)
      end
    end
    if summary_pca
      s.section(:name=>_("PCA for %s") % name) do |s2|
        s2.parse_element(pca)
      end
    end
    if summary_principal_axis
      s.section(:name=>_("Principal Axis for %s") % name) do |s2|
        s2.parse_element(principal_axis_analysis)
      end
    end
    
    if summary_parallel_analysis
      s.section(:name=>_("Parallel Analysis for %s") % name) do |s2|
        s2.parse_element(parallel_analysis)
      end
    end 
    if summary_map
      s.section(:name=>_("MAP for %s") % name) do |s2|
        s2.parse_element(map)
      end
    end           
  end
end

#scale(code, ds = nil, opts = nil) ⇒ Object

Add or retrieve a scale to analysis. If second parameters is a dataset, generates a ScaleAnalysis for ds, named code with options opts.

If second parameters is empty, returns the ScaleAnalysis code.



93
94
95
96
97
98
99
100
101
# File 'lib/statsample/reliability/multiscaleanalysis.rb', line 93

def scale(code, ds=nil, opts=nil)
  if ds.nil?
    @scales[code]
  else
    opts={:name=>_("Scale %s") % code} if opts.nil?
    @scales_keys.push(code)
    @scales[code]=ScaleAnalysis.new(ds, opts)
  end
end