Class: Statsample::Anova::TwoWayWithVectors

Inherits:
TwoWay show all
Defined in:
lib/statsample/anova/twoway.rb

Overview

Two Way Anova with vectors Example:

v1 = Daru::Vector.new([1,1,2,2])
v2 = Daru::Vector.new([1,2,1,2])
v3 = Daru::Vector.new([5,3,1,5])
anova=Statsample::Anova::TwoWayWithVectors.new(:a=>v1,:b=>v2, :dependent=>v3)

Instance Attribute Summary collapse

Attributes inherited from TwoWay

#df_a, #df_axb, #df_b, #df_total, #df_within, #f_a_object, #f_axb_object, #f_b_object, #ms_a, #ms_axb, #ms_b, #ms_total, #ms_within, #name, #name_a, #name_b, #name_within, #ss_a, #ss_axb, #ss_b, #ss_total, #ss_within

Instance Method Summary collapse

Methods inherited from TwoWay

#f_a, #f_a_probability, #f_axb, #f_axb_probability, #f_b, #f_b_probability, #report_building_table

Methods included from Summarizable

#summary

Constructor Details

#initialize(opts = Hash.new) ⇒ TwoWayWithVectors

For now, only equal sample cells allowed


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
179
180
181
182
183
184
# File 'lib/statsample/anova/twoway.rb', line 122

def initialize(opts=Hash.new)
  raise "You should insert at least :a, :b and :dependent" unless  [:a, :b, :dependent].all? {|v| opts.has_key? v}
  @a_var   = :a
  @b_var   = :b
  @dep_var = :dependent
  @a_vector, @b_vector, @dep_vector = 
    Statsample.only_valid_clone opts[:a], opts[:b], opts[:dependent]
  
  ds  = Daru::DataFrame.new({@a_var=>@a_vector, @b_var=>@b_vector, @dep_var=>@dep_vector})
  @ds = ds.clone_only_valid
  _p  = @a_vector.factors.size
  _q  = @b_vector.factors.size
  @x_general = @dep_vector.mean
  @axb_means = {}
  @axb_sd    = {}
  @vectors   = []
  n=nil
  @ds.to_multiset_by_split(a_var,b_var).each_vector(dep_var) {|k,v|
    @axb_means[k] = v.mean
    @axb_sd[k]    = v.sd
    @vectors << v
    n ||= v.size
    raise "All cell sizes should be equal" if n!=v.size
  }

  @a_means={}
  @ds.to_multiset_by_split(a_var).each_vector(dep_var) {|k,v|
    @a_means[k]=v.mean
  }
  @b_means={}
  @ds.to_multiset_by_split(b_var).each_vector(dep_var) {|k,v|
    @b_means[k]=v.mean
  }
  ss_a = n*_q*@ds[a_var].factors.inject(0) {|ac,v|
    ac + (@a_means[v]-@x_general)**2
  }
  ss_b=n*_p*@ds[b_var].factors.inject(0) {|ac,v|
    ac+(@b_means[v]-@x_general)**2
  }
  ss_within = @ds.collect(:row) { |row|
    (row[dep_var]-@axb_means[[row[a_var],row[b_var]]])**2
  }.sum
  ss_axb = n*@axb_means.inject(0) {|ac,v|
    j,k=v[0]
    xjk=v[1]
    ac+(xjk-@a_means[j]-@b_means[k]+@x_general)**2
  }

  df_a=_p-1
  df_b=_q-1
  df_within=(_p*_q)*(n-1)
  
  opts_default={:name=>_("Anova Two-Way on %s") % @ds[dep_var].name, 
    :name_a=>@ds[a_var].name,
      :name_b=>@ds[b_var].name,
      :summary_descriptives=>true,
      :summary_levene=>false}
      
  @opts=opts_default.merge(opts).merge({:ss_a=>ss_a,:ss_b=>ss_b, :ss_axb=>ss_axb, :ss_within=>ss_within, :df_a=>df_a, :df_b=>df_b, :df_within=>df_within})
  
  
  super(@opts)
end

Instance Attribute Details

#a_varObject (readonly)

Returns the value of attribute a_var


120
121
122
# File 'lib/statsample/anova/twoway.rb', line 120

def a_var
  @a_var
end

#b_varObject (readonly)

Returns the value of attribute b_var


120
121
122
# File 'lib/statsample/anova/twoway.rb', line 120

def b_var
  @b_var
end

#dep_varObject (readonly)

Returns the value of attribute dep_var


120
121
122
# File 'lib/statsample/anova/twoway.rb', line 120

def dep_var
  @dep_var
end

#summary_descriptivesObject

Show summary descriptives for variables (means)


119
120
121
# File 'lib/statsample/anova/twoway.rb', line 119

def summary_descriptives
  @summary_descriptives
end

#summary_leveneObject

Show summary Levene test


117
118
119
# File 'lib/statsample/anova/twoway.rb', line 117

def summary_levene
  @summary_levene
end

Instance Method Details

#leveneObject


185
186
187
# File 'lib/statsample/anova/twoway.rb', line 185

def levene
  Statsample::Test.levene(@vectors, :name=>_("Test of Homogeneity of variances (Levene)"))
end

#report_building(builder) ⇒ Object

:nodoc:#


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/statsample/anova/twoway.rb', line 188

def report_building(builder) #:nodoc:#
  builder.section(:name=>@name) do |s|
    if summary_descriptives
      s.table(:header =>['']+@ds[a_var].factors.map {|a| @ds[a_var].index_of(a)}+[_("%s Mean") % @name_b]) do |t|
        @ds[b_var].factors.each do |b|
          t.row([@ds[b_var].index_of(b)]+@ds[a_var].factors.map {|a| "%0.3f" % @axb_means[[a,b]] } + ["%0.3f" % @b_means[b]])
        end
        t.row([_("%s Mean") % @name_a]+@ds[a_var].factors.map {|a| "%0.3f" % @a_means[a]}+ ["%0.3f" % @x_general])
      end
    end
    if summary_levene
      s.parse_element(levene)
    end
    report_building_table(s)

  end
end