Class: Statsample::Anova::TwoWay

Inherits:
Object
  • Object
show all
Includes:
Summarizable
Defined in:
lib/statsample/anova/twoway.rb

Overview

Generic Anova two-way.

You could enter the sum of squares or the mean squares for a, b, axb and within. You should enter the degrees of freedom for a,b and within, because df_axb=df_a*df_b

Usage

anova=Statsample::Anova::TwoWay(:ss_a=>10,:ss_b=>20,:ss_axb=>10, :ss_within=>20, :df_a=>2, :df_b=>3,df_within=100 @name=>"ANOVA for....")

Direct Known Subclasses

TwoWayWithVectors

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Summarizable

#summary

Constructor Details

#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

Instance Attribute Details

#df_aObject (readonly)

Returns the value of attribute df_a.



10
11
12
# File 'lib/statsample/anova/twoway.rb', line 10

def df_a
  @df_a
end

#df_axbObject (readonly)

Returns the value of attribute df_axb.



10
11
12
# File 'lib/statsample/anova/twoway.rb', line 10

def df_axb
  @df_axb
end

#df_bObject (readonly)

Returns the value of attribute df_b.



10
11
12
# File 'lib/statsample/anova/twoway.rb', line 10

def df_b
  @df_b
end

#df_totalObject (readonly)

Returns the value of attribute df_total.



10
11
12
# File 'lib/statsample/anova/twoway.rb', line 10

def df_total
  @df_total
end

#df_withinObject (readonly)

Returns the value of attribute df_within.



10
11
12
# File 'lib/statsample/anova/twoway.rb', line 10

def df_within
  @df_within
end

#f_a_objectObject (readonly)

Returns the value of attribute f_a_object.



22
23
24
# File 'lib/statsample/anova/twoway.rb', line 22

def f_a_object
  @f_a_object
end

#f_axb_objectObject (readonly)

Returns the value of attribute f_axb_object.



22
23
24
# File 'lib/statsample/anova/twoway.rb', line 22

def f_axb_object
  @f_axb_object
end

#f_b_objectObject (readonly)

Returns the value of attribute f_b_object.



22
23
24
# File 'lib/statsample/anova/twoway.rb', line 22

def f_b_object
  @f_b_object
end

#ms_aObject (readonly)

Returns the value of attribute ms_a.



12
13
14
# File 'lib/statsample/anova/twoway.rb', line 12

def ms_a
  @ms_a
end

#ms_axbObject (readonly)

Returns the value of attribute ms_axb.



12
13
14
# File 'lib/statsample/anova/twoway.rb', line 12

def ms_axb
  @ms_axb
end

#ms_bObject (readonly)

Returns the value of attribute ms_b.



12
13
14
# File 'lib/statsample/anova/twoway.rb', line 12

def ms_b
  @ms_b
end

#ms_totalObject (readonly)

Returns the value of attribute ms_total.



12
13
14
# File 'lib/statsample/anova/twoway.rb', line 12

def ms_total
  @ms_total
end

#ms_withinObject (readonly)

Returns the value of attribute ms_within.



12
13
14
# File 'lib/statsample/anova/twoway.rb', line 12

def ms_within
  @ms_within
end

#nameObject

Name of ANOVA Analisys



14
15
16
# File 'lib/statsample/anova/twoway.rb', line 14

def name
  @name
end

#name_aObject

Name of a factor



16
17
18
# File 'lib/statsample/anova/twoway.rb', line 16

def name_a
  @name_a
end

#name_bObject

Name of b factor



18
19
20
# File 'lib/statsample/anova/twoway.rb', line 18

def name_b
  @name_b
end

#name_withinObject

Name of within factor



20
21
22
# File 'lib/statsample/anova/twoway.rb', line 20

def name_within
  @name_within
end

#ss_aObject (readonly)

Returns the value of attribute ss_a.



11
12
13
# File 'lib/statsample/anova/twoway.rb', line 11

def ss_a
  @ss_a
end

#ss_axbObject (readonly)

Returns the value of attribute ss_axb.



11
12
13
# File 'lib/statsample/anova/twoway.rb', line 11

def ss_axb
  @ss_axb
end

#ss_bObject (readonly)

Returns the value of attribute ss_b.



11
12
13
# File 'lib/statsample/anova/twoway.rb', line 11

def ss_b
  @ss_b
end

#ss_totalObject (readonly)

Returns the value of attribute ss_total.



11
12
13
# File 'lib/statsample/anova/twoway.rb', line 11

def ss_total
  @ss_total
end

#ss_withinObject (readonly)

Returns the value of attribute ss_within.



11
12
13
# File 'lib/statsample/anova/twoway.rb', line 11

def ss_within
  @ss_within
end

Instance Method Details

#f_aObject



72
73
74
# File 'lib/statsample/anova/twoway.rb', line 72

def f_a
  @f_a_object.f
end

#f_a_probabilityObject



81
82
83
# File 'lib/statsample/anova/twoway.rb', line 81

def f_a_probability
  @f_a_object.probability
end

#f_axbObject



78
79
80
# File 'lib/statsample/anova/twoway.rb', line 78

def f_axb
  @f_axb_object.f
end

#f_axb_probabilityObject



87
88
89
# File 'lib/statsample/anova/twoway.rb', line 87

def f_axb_probability
  @f_axb_object.probability
end

#f_bObject



75
76
77
# File 'lib/statsample/anova/twoway.rb', line 75

def f_b
  @f_b_object.f
end

#f_b_probabilityObject



84
85
86
# File 'lib/statsample/anova/twoway.rb', line 84

def f_b_probability
  @f_b_object.probability
end

#report_building(builder) ⇒ Object

:nodoc:



92
93
94
95
96
# File 'lib/statsample/anova/twoway.rb', line 92

def report_building(builder) #:nodoc:
  builder.section(:name=>@name) do |b|
    report_building_table(b)
  end
end

#report_building_table(builder) ⇒ Object

:nodoc:



97
98
99
100
101
102
103
104
105
# File 'lib/statsample/anova/twoway.rb', line 97

def report_building_table(builder) #:nodoc:
  builder.table(:name=>_("%s Table") % @name, :header=>%w{source ss df ms f p}.map {|v| _(v)}) do |t|
    t.row([@name_a, "%0.3f" % @ss_a,   @df_a, "%0.3f" % @ms_a , "%0.3f" % f_a, "%0.4f" % f_a_probability] )
    t.row([@name_b, "%0.3f" % @ss_b,   @df_b, "%0.3f" % @ms_b , "%0.3f" % f_b, "%0.4f" % f_b_probability] )
    t.row(["%s X %s" % [@name_a, @name_b], "%0.3f" % @ss_axb,   @df_axb, "%0.3f" % @ms_axb , "%0.3f" % f_axb, "%0.4f" % f_axb_probability] )          
    t.row([@name_within, "%0.3f" % @ss_within,   @df_within, nil,nil,nil] )
    t.row([_("Total"), "%0.3f" % @ss_total,   @df_total, nil,nil,nil] )          
  end
end