Method: Statsample::Anova::TwoWay#initialize

Defined in:
lib/statsample/anova/twoway.rb

#initialize(opts = Hash.new) ⇒ TwoWay

Returns a new instance of TwoWay.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/statsample/anova/twoway.rb', line 23

def initialize(opts=Hash.new)
  # First see if sum of squares or mean squares are entered
  raise ArgumentError, "You should set all d.f." unless [:df_a, :df_b, :df_within].all? {|v| opts.has_key? v}
  
  @df_a=opts.delete :df_a
  @df_b=opts.delete :df_b
  @df_axb=@df_a*@df_b
  @df_within=opts.delete :df_within
  @df_total=@df_a+@df_b+@df_axb+@df_within
  
  if [:ss_a, :ss_b, :ss_axb, :ss_within].all? {|v| opts.has_key? v}
    @ss_a = opts.delete :ss_a
    @ss_b = opts.delete :ss_b
    @ss_axb = opts.delete :ss_axb
    @ss_within = opts.delete :ss_within
    
    @ms_a =@ss_a.quo(@df_a)
    @ms_b =@ss_b.quo(@df_b) 
    @ms_axb =@ss_axb.quo(@df_axb)
    @ms_within =@ss_within.quo(@df_within) 

  elsif [:ms_a, :ms_b, :ms_axb, :ms_within].all? {|v| opts.has_key? v}
    @ms_a = opts.delete :ms_a
    @ms_b = opts.delete :ms_b
    @ms_axb = opts.delete :ms_axb
    @ms_within = opts.delete :ms_within
    
    @ss_a =@ms_a*@df_a
    @ss_b =@ms_b*@df_b 
    @ss_axb =@ms_axb*@df_axb
    @ss_within =@ms_within*@df_within
  else
    raise "You should set all ss or ss"
  end
  @ss_total=@ss_a+@ss_b+@ss_axb+@ss_within
  @ms_total=@ms_a+@ms_b+@ms_axb+@ms_within
  opts_default={:name=>_("ANOVA Two-Way"),
                :name_a=>_("A"),
                :name_b=>_("B"),
                :name_within=>_("Within")                      
  }
  @opts=opts_default.merge(opts)
  opts_default.keys.each {|k|
    send("#{k}=", @opts[k])
  }
  @f_a_object=Statsample::Test::F.new(@ms_a,@ms_within,@df_a,@df_within)
  @f_b_object=Statsample::Test::F.new(@ms_b,@ms_within,@df_b,@df_within)
  @f_axb_object=Statsample::Test::F.new(@ms_axb,@ms_within,@df_axb,@df_within)
end