Class: Statsample::Anova::OneWayWithVectors

Inherits:
OneWay show all
Defined in:
lib/statsample/anova/oneway.rb

Overview

One Way Anova with vectors Example:

v1 = Daru::Vector.new([2,3,4,5,6])
v2 = Daru::Vector.new([3,3,4,5,6])
v3 = Daru::Vector.new([5,3,1,5,6])
anova=Statsample::Anova::OneWayWithVectors.new([v1,v2,v3])
anova.f
=> 0.0243902439024391
anova.probability
=> 0.975953044203438
anova.sst 
=> 32.9333333333333

Instance Attribute Summary collapse

Attributes inherited from OneWay

#df_den, #df_num, #df_total, #ms_den, #ms_num, #ms_total, #name, #name_denominator, #name_numerator, #ss_den, #ss_num, #ss_total

Instance Method Summary collapse

Methods inherited from OneWay

#f, #probability, #report_building_table

Methods included from Summarizable

#summary

Constructor Details

#initialize(*args) ⇒ OneWayWithVectors

Returns a new instance of OneWayWithVectors.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/statsample/anova/oneway.rb', line 91

def initialize(*args)
  if args[0].is_a? Array
    @vectors = args.shift
  else
    @vectors = args.find_all {|v| v.is_a? Daru::Vector}
    opts     = args.find {|v| v.is_a? Hash}
  end
  opts||=Hash.new
  opts_default={:name=>_("Anova One-Way"), 
                :name_numerator=>_("Between Groups"),
                :name_denominator=>_("Within Groups"),
                :summary_descriptives=>false,
                :summary_levene=>true,
                :summary_contrasts=>true
  }
  @opts=opts_default.merge(opts).merge(:ss_num=>ssbg, :ss_den=>sswg, :df_num=>df_bg, :df_den=>df_wg)
  @contrasts=[]
  super(@opts)
end

Instance Attribute Details

#contrastsObject (readonly)

Array with stored contrasts



89
90
91
# File 'lib/statsample/anova/oneway.rb', line 89

def contrasts
  @contrasts
end

#summary_contrastsObject

Show on summary of contrasts



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

def summary_contrasts
  @summary_contrasts
end

#summary_descriptivesObject

Show on summary descriptives for vectors



85
86
87
# File 'lib/statsample/anova/oneway.rb', line 85

def summary_descriptives
  @summary_descriptives
end

#summary_leveneObject

Show on summary Levene test



83
84
85
# File 'lib/statsample/anova/oneway.rb', line 83

def summary_levene
  @summary_levene
end

Instance Method Details

#contrast(opts = Hash.new) ⇒ Object

Generates and store a contrast. Options should be provided as a hash [:c]=>contrast vector [:c1 - :c2]=>index for automatic construction of contrast [:name]=>contrast name



120
121
122
123
124
125
126
# File 'lib/statsample/anova/oneway.rb', line 120

def contrast(opts=Hash.new)
  name=opts[:name] || _("Contrast for %s") % @name
  opts=opts.merge({:vectors=>@vectors, :name=>name})
  c=Statsample::Anova::Contrast.new(opts)
  @contrasts.push(c)
  c
end

#df_bgObject

Degrees of freedom between groups



155
156
157
# File 'lib/statsample/anova/oneway.rb', line 155

def df_bg
    k-1
end

#df_wgObject

Degrees of freedom within groups



148
149
150
# File 'lib/statsample/anova/oneway.rb', line 148

def df_wg
  @dk_wg||=n-k
end

#kObject



151
152
153
# File 'lib/statsample/anova/oneway.rb', line 151

def k
  @k||=@vectors.size
end

#leveneObject



128
129
130
# File 'lib/statsample/anova/oneway.rb', line 128

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

#nObject

Total number of cases



159
160
161
# File 'lib/statsample/anova/oneway.rb', line 159

def n
    @vectors.inject(0){|a,v| a+v.size}
end

#report_building(builder) ⇒ Object

:nodoc:



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/oneway.rb', line 162

def report_building(builder) # :nodoc:
  builder.section(:name=>@name) do |s|
    if summary_descriptives
      s.table(:name=>_("Descriptives"),:header=>%w{Name N Mean SD Min Max}.map {|v| _(v)}) do |t|
        @vectors.each do |v|
          t.row [v.name, v.reject_values(*Daru::MISSING_VALUES).size, "%0.4f" % v.mean, "%0.4f" %  v.sd, "%0.4f" % v.min, "%0.4f" % v.max]
        end
      end
    end
    
    if summary_levene
      s.parse_element(levene)
    end
    report_building_table(s)
    if summary_contrasts and @contrasts.size>0

      @contrasts.each do |c|
        s.parse_element(c)
      end
    end
    
  end
end

#ssbgObject

Sum of squares between groups



141
142
143
144
145
146
# File 'lib/statsample/anova/oneway.rb', line 141

def ssbg
  m=total_mean
  @vectors.inject(0) do |total,vector|
    total + (vector.mean-m).square * vector.size 
  end
end

#sswgObject

Sum of squares within groups



137
138
139
# File 'lib/statsample/anova/oneway.rb', line 137

def sswg
  @sswg||=@vectors.inject(0) {|total,vector| total+vector.ss }
end

#total_meanObject

Total mean



132
133
134
135
# File 'lib/statsample/anova/oneway.rb', line 132

def total_mean
  sum=@vectors.inject(0){|a,v| a+v.sum}
  sum.quo(n)
end