Class: Statsample::Factor::Rotation
- Inherits:
-
Object
- Object
- Statsample::Factor::Rotation
- Includes:
- DirtyMemoize
- Defined in:
- lib/statsample/factor/rotation.rb
Overview
Base class for rotate matrixes References:
-
SPSS Manual
-
Johnny Lin code for IDL: www.johnny-lin.com/idl_code/varimax_k58.pro
Use Varimax, Equimax or Quartimax for desired type of rotation
Use:
a = Matrix[ [ 0.4320, 0.8129, 0.3872]
, [ 0.7950, -0.5416, 0.2565]
, [ 0.5944, 0.7234, -0.3441]
, [ 0.8945, -0.3921, -0.1863] ]
rotation = Statsample::Factor::Varimax(a)
rotation.iterate
p rotation.rotated
p rotation.component_transformation_matrix
Constant Summary collapse
- EPSILON =
1e-15- MAX_ITERATIONS =
25
Instance Attribute Summary collapse
-
#component_transformation_matrix ⇒ Object
readonly
Returns the value of attribute component_transformation_matrix.
-
#epsilon ⇒ Object
Maximum precision.
-
#h2 ⇒ Object
(also: #communalities)
readonly
Returns the value of attribute h2.
-
#iterations ⇒ Object
readonly
Returns the value of attribute iterations.
-
#max_iterations ⇒ Object
Maximum number of iterations.
-
#rotated ⇒ Object
(also: #rotated_component_matrix)
readonly
Returns the value of attribute rotated.
Instance Method Summary collapse
- #compute ⇒ Object
-
#initialize(matrix, opts = Hash.new) ⇒ Rotation
constructor
A new instance of Rotation.
-
#iterate ⇒ Object
Start iteration.
Constructor Details
#initialize(matrix, opts = Hash.new) ⇒ Rotation
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/statsample/factor/rotation.rb', line 31 def initialize(matrix, opts=Hash.new) @matrix=matrix @n=@matrix.row_size # Variables, p on original @m=@matrix.column_size # Factors, r on original @component_transformation_matrix=nil @max_iterations=MAX_ITERATIONS @epsilon=EPSILON @rotated=nil @h2=(@matrix.collect {|c| c**2} * Matrix.column_vector([1]*@m)).column(0).to_a opts.each{|k,v| self.send("#{k}=",v) if self.respond_to? k } end |
Instance Attribute Details
#component_transformation_matrix ⇒ Object (readonly)
Returns the value of attribute component_transformation_matrix.
22 23 24 |
# File 'lib/statsample/factor/rotation.rb', line 22 def component_transformation_matrix @component_transformation_matrix end |
#epsilon ⇒ Object
Maximum precision
26 27 28 |
# File 'lib/statsample/factor/rotation.rb', line 26 def epsilon @epsilon end |
#h2 ⇒ Object (readonly) Also known as: communalities
Returns the value of attribute h2.
22 23 24 |
# File 'lib/statsample/factor/rotation.rb', line 22 def h2 @h2 end |
#iterations ⇒ Object (readonly)
Returns the value of attribute iterations.
22 23 24 |
# File 'lib/statsample/factor/rotation.rb', line 22 def iterations @iterations end |
#max_iterations ⇒ Object
Maximum number of iterations
24 25 26 |
# File 'lib/statsample/factor/rotation.rb', line 24 def max_iterations @max_iterations end |
#rotated ⇒ Object (readonly) Also known as: rotated_component_matrix
Returns the value of attribute rotated.
22 23 24 |
# File 'lib/statsample/factor/rotation.rb', line 22 def rotated @rotated end |
Instance Method Details
#compute ⇒ Object
46 47 48 |
# File 'lib/statsample/factor/rotation.rb', line 46 def compute iterate end |
#iterate ⇒ Object
Start iteration
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/statsample/factor/rotation.rb', line 50 def iterate t=Matrix.identity(@m) b=@matrix.dup h=Matrix.diagonal(*@h2).collect {|c| Math::sqrt(c)} h_inverse=h.collect {|c| c!=0 ? 1/c : 0 } bh=h_inverse*b @not_converged=true @iterations=0 while @not_converged break if @iterations>@max_iterations @iterations+=1 #puts "Iteration #{iterations}" num_pairs=@m*(@m-1).quo(2) (0..(@m-2)).each do |i| #+ go through factor index 0:r-1-1 (begin) ((i+1)..(@m-1)).each do |j| #+ pair i to "rest" of factors (begin) xx = bh.column(i) yy = bh.column(j) tx = t.column(i) ty = t.column(j) uu = @n.times.collect {|var_i| xx[var_i]**2-yy[var_i]**2} vv = @n.times.collect {|var_i| 2*xx[var_i]*yy[var_i]} a = @n.times.inject(0) {|ac,var_i| ac+ uu[var_i] } b = @n.times.inject(0) {|ac,var_i| ac+ vv[var_i] } c = @n.times.inject(0) {|ac,var_i| ac+ (uu[var_i]**2 - vv[var_i]**2) } d = @n.times.inject(0) {|ac,var_i| ac+ (2*uu[var_i]*vv[var_i]) } num=x(a,b,c,d) den=y(a,b,c,d) phi=Math::atan2(num,den) / 4.0 # puts "#{i}-#{j}: #{phi}" if(Math::sin(phi.abs) >= @epsilon) xx_rot=( Math::cos(phi)*xx)+(Math::sin(phi)*yy) yy_rot=((-Math::sin(phi))*xx)+(Math::cos(phi)*yy) tx_rot=( Math::cos(phi)*tx)+(Math::sin(phi)*ty) ty_rot=((-Math::sin(phi))*tx)+(Math::cos(phi)*ty) bh=bh.to_a @n.times {|row_i| bh[row_i][i] = xx_rot[row_i] bh[row_i][j] = yy_rot[row_i] } t=t.to_a @m.times {|row_i| t[row_i][i]=tx_rot[row_i] t[row_i][j]=ty_rot[row_i] } bh=Matrix.rows(bh) t=Matrix.rows(t) else num_pairs=num_pairs-1 @not_converged=false if num_pairs==0 end # if end #j end #i end # while @rotated=h*bh @component_transformation_matrix=t @rotated end |