Class: Statsample::Test::T::TwoSamplesIndependent

Inherits:
Object
  • Object
show all
Includes:
DirtyMemoize, Math, Summarizable, Statsample::Test
Defined in:
lib/statsample/test/t.rb

Overview

Two Sample t-test.

Usage

a = Daru::Vector.new(1000.times.map {rand(100)})
b = Daru::Vector.new(1000.times.map {rand(100)})
t_2=Statsample::Test::T::TwoSamplesIndependent.new(a,b)
t_2.summary

Output

= Two Sample T Test
Mean and standard deviation
+----------+---------+---------+------+
| Variable |    m    |   sd    |  n   |
+----------+---------+---------+------+
| 1        | 49.3310 | 29.3042 | 1000 |
| 2        | 47.8180 | 28.8640 | 1000 |
+----------+---------+---------+------+

== Levene Test
 Levene Test
 F: 0.3596
 p: 0.5488
 T statistics
 +--------------------+--------+-----------+----------------+
 |        Type        |   t    |    df     | p (both tails) |
 +--------------------+--------+-----------+----------------+
 | Equal variance     | 1.1632 | 1998      | 0.2449         |
 | Non equal variance | 1.1632 | 1997.5424 | 0.1362         |
 +--------------------+--------+-----------+----------------+

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Summarizable

#summary

Methods included from Statsample::Test

chi_square, levene, #p_using_cdf, #t_critical, t_one_sample, t_two_samples_independent, u_mannwhitney, wilcoxon_signed_rank, #z_critical

Constructor Details

#initialize(v1, v2, opts = Hash.new) ⇒ TwoSamplesIndependent

Create a Two Independent T Test Options:

  • :name = Name of the analysis

  • :tails = Tail for probability. Could be :both, :left, :right



256
257
258
259
260
261
262
263
# File 'lib/statsample/test/t.rb', line 256

def initialize(v1, v2, opts=Hash.new)
  @v1=v1
  @v2=v2
  default={:u=>0, :name=>"Two Sample T Test",  :tails=>:both}
  @opts=default.merge(opts)
  @name=@opts[:name]
  @tails=@opts[:tails]          
end

Instance Attribute Details

#df_equal_varianceObject (readonly)

Degress of freedom (equal variance)



234
235
236
# File 'lib/statsample/test/t.rb', line 234

def df_equal_variance
  @df_equal_variance
end

#df_not_equal_varianceObject (readonly)

Degress of freedom (not equal variance)



236
237
238
# File 'lib/statsample/test/t.rb', line 236

def df_not_equal_variance
  @df_not_equal_variance
end

#nameObject

Name of test



232
233
234
# File 'lib/statsample/test/t.rb', line 232

def name
  @name
end

#optsObject

Options



230
231
232
# File 'lib/statsample/test/t.rb', line 230

def opts
  @opts
end

#probability_equal_varianceObject (readonly)

Probability(equal variance)



242
243
244
# File 'lib/statsample/test/t.rb', line 242

def probability_equal_variance
  @probability_equal_variance
end

#probability_not_equal_varianceObject (readonly)

Probability(unequal variance)



244
245
246
# File 'lib/statsample/test/t.rb', line 244

def probability_not_equal_variance
  @probability_not_equal_variance
end

#t_equal_varianceObject (readonly)

Value of t for equal_variance



238
239
240
# File 'lib/statsample/test/t.rb', line 238

def t_equal_variance
  @t_equal_variance
end

#t_not_equal_varianceObject (readonly)

Value of t for non-equal_variance



240
241
242
# File 'lib/statsample/test/t.rb', line 240

def t_not_equal_variance
  @t_not_equal_variance
end

#tailsObject

Tails for probability (:both, :left or :right)



246
247
248
# File 'lib/statsample/test/t.rb', line 246

def tails
  @tails
end

Instance Method Details

#computeObject

Set t and probability for given u



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/statsample/test/t.rb', line 266

def compute
  @t_equal_variance= T.two_sample_independent(@v1.mean, @v2.mean, @v1.sd, @v2.sd, @v1.reject_values(*Daru::MISSING_VALUES).size, @v2.reject_values(*Daru::MISSING_VALUES).size,true)
  
  @t_not_equal_variance= T.two_sample_independent(@v1.mean, @v2.mean, @v1.sd, @v2.sd, @v1.reject_values(*Daru::MISSING_VALUES).size, @v2.reject_values(*Daru::MISSING_VALUES).size, false)

  @df_equal_variance=T.df_equal_variance(@v1.reject_values(*Daru::MISSING_VALUES).size, @v2.reject_values(*Daru::MISSING_VALUES).size)
  @df_not_equal_variance=T.df_not_equal_variance(@v1.sd, @v2.sd, @v1.reject_values(*Daru::MISSING_VALUES).size, @v2.reject_values(*Daru::MISSING_VALUES).size)
  
  @probability_equal_variance = p_using_cdf(Distribution::T.cdf(@t_equal_variance, @df_equal_variance), tails)
  
  @probability_not_equal_variance = p_using_cdf(Distribution::T.cdf(@t_not_equal_variance, @df_not_equal_variance), tails)

end

#dObject

Cohen’s d is a measure of effect size. Its defined as the difference between two means divided by a standard deviation for the data



280
281
282
283
284
285
286
# File 'lib/statsample/test/t.rb', line 280

def d
  n1=@v1.reject_values(*Daru::MISSING_VALUES).size
  n2=@v2.reject_values(*Daru::MISSING_VALUES).size
  num=@v1.mean-@v2.mean
  den=Math::sqrt( ((n1-1)*@v1.sd+(n2-1)*@v2.sd).quo(n1+n2))
  num.quo(den)
end

#report_building(b) ⇒ Object

:nodoc:



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/statsample/test/t.rb', line 288

def report_building(b) # :nodoc:
  b.section(:name=>@name) {|g|
    g.table(:name=>_("Mean and standard deviation"), :header=>[_("Variable"), _("mean"), _("sd"),_("n")]) {|t|
      t.row([@v1.name,"%0.4f" % @v1.mean,"%0.4f" % @v1.sd, @v1.reject_values(*Daru::MISSING_VALUES).size])
      t.row([@v2.name,"%0.4f" % @v2.mean,"%0.4f" % @v2.sd, @v2.reject_values(*Daru::MISSING_VALUES).size])
    }
    g.parse_element(Statsample::Test.levene([@v1,@v2],:name=>_("Levene test for equality of variances")))
    
    g.table(:name=>_("T statistics"),:header=>["Type","t","df", "p (#{tails} tails)"].map{|v| _(v)}) {|t|
      t.row([_("Equal variance"), "%0.4f" % t_equal_variance, df_equal_variance, "%0.4f" % probability_equal_variance])
      t.row([_("Non equal variance"), "%0.4f" % t_not_equal_variance, "%0.4f" % df_not_equal_variance, "%0.4f" % probability_not_equal_variance])
    }
    g.table(:name=>_("Effect size")) do |t|
      t.row ['x1-x2', "%0.4f" % (@v1.mean-@v2.mean)]
      t.row ['d', "%0.4f" % d]
    end
  }
end