class Statsample::Anova::TwoWayWithVectors

Two Way Anova with vectors Example:

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

Two Way Anova with vectors Example:

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

Attributes

a_var[R]
b_var[R]
dep_var[R]
summary_descriptives[RW]

Show summary descriptives for variables (means)

summary_levene[RW]

Show summary Levene test

Public Class Methods

new(opts=Hash.new) click to toggle source

For now, only equal sample cells allowed

Calls superclass method Statsample::Anova::TwoWay.new
# 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={@a_var=>@a_vector, @b_var=>@b_vector, @dep_var=>@dep_vector}.to_dataset
  @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[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

Public Instance Methods

levene() click to toggle source
# File lib/statsample/anova/twoway.rb, line 183
def levene
  Statsample::Test.levene(@vectors, :name=>_("Test of Homogeneity of variances (Levene)"))
end