Class: Statsample::Test::T

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

Overview

A t-test is any statistical hypothesis test in which the test statistic follows a Student’s t distribution, if the null hypothesis is supported

Defined Under Namespace

Classes: OneSample, TwoSamplesIndependent

Instance Attribute Summary collapse

Class Method 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(estimate, standard_error, df, opts = Hash.new) ⇒ T

Creates a generic t test. Use OneSample or TwoSamplesIndependent classes for better summaries. Parameters:

  • estimate: estimate

  • standard_error: standard error of estimate

  • df: degrees of freedom



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/statsample/test/t.rb', line 74

def initialize(estimate, standard_error, df, opts=Hash.new)
  @estimate=estimate
  @standard_error=standard_error
  @df=df
  @t = @estimate / @standard_error.to_f
  opts_default={  :tails=>:both,
                  :name=>_("T Test"),
                  :estimate_name=>_("Estimate"),
                  :standard_error_name=>_("Std.Err.of Estimate"),
  :confidence_level=>0.95}
  @opts = opts_default.merge(opts)
  
  @opts.keys.each {|k|
    send("#{k}=", @opts[k]) if respond_to? k
  }
end

Instance Attribute Details

#confidence_levelObject

Returns the value of attribute confidence_level.



65
66
67
# File 'lib/statsample/test/t.rb', line 65

def confidence_level
  @confidence_level
end

#dfObject (readonly)

Returns the value of attribute df.



60
61
62
# File 'lib/statsample/test/t.rb', line 60

def df
  @df
end

#estimateObject (readonly)

Returns the value of attribute estimate.



60
61
62
# File 'lib/statsample/test/t.rb', line 60

def estimate
  @estimate
end

#estimate_nameObject

Returns the value of attribute estimate_name.



67
68
69
# File 'lib/statsample/test/t.rb', line 67

def estimate_name
  @estimate_name
end

#nameObject

Name of F analysis



64
65
66
# File 'lib/statsample/test/t.rb', line 64

def name
  @name
end

#standard_errorObject (readonly) Also known as: se

Returns the value of attribute standard_error.



60
61
62
# File 'lib/statsample/test/t.rb', line 60

def standard_error
  @standard_error
end

#standard_error_nameObject

Returns the value of attribute standard_error_name.



67
68
69
# File 'lib/statsample/test/t.rb', line 67

def standard_error_name
  @standard_error_name
end

#tObject (readonly)

Returns the value of attribute t.



66
67
68
# File 'lib/statsample/test/t.rb', line 66

def t
  @t
end

#tailsObject

Tails for p-value (:both, :left or :right). Default :both



62
63
64
# File 'lib/statsample/test/t.rb', line 62

def tails
  @tails
end

Class Method Details

.df_equal_variance(n1, n2) ⇒ Object

Degrees of freedom for equal variance on t test



39
40
41
# File 'lib/statsample/test/t.rb', line 39

def df_equal_variance(n1,n2)
  n1+n2-2
end

.df_not_equal_variance(s1, s2, n1, n2) ⇒ Object

Degrees of freedom for unequal variance

  • s1: sample 1 standard deviation

  • s2: sample 2 standard deviation

  • n1: sample 1 size

  • n2: sample 2 size

Reference



49
50
51
52
53
54
55
# File 'lib/statsample/test/t.rb', line 49

def df_not_equal_variance(s1,s2,n1,n2)
  s2_1=s1**2
  s2_2=s2**2
  num=(s2_1.quo(n1)+s2_2.quo(n2))**2
  den=(s2_1.quo(n1)**2).quo(n1-1) + (s2_2.quo(n2)**2).quo(n2-1)
  num.quo(den)
end

.one_sample(x, u, s, n) ⇒ Object

Test the null hypothesis that the population mean is equal to a specified value u, one uses the statistic. Is the same formula used on t-test for paired sample.

  • x: sample/differences mean

  • u: population mean

  • s: sample/differences standard deviation

  • n: sample size



16
17
18
# File 'lib/statsample/test/t.rb', line 16

def one_sample(x,u,s,n)
  (x-u)*Math::sqrt(n).quo(s)
end

.two_sample_independent(x1, x2, s1, s2, n1, n2, equal_variance = false) ⇒ Object

Test if means of two samples are different.

  • x1: sample 1 mean

  • x2: sample 2 mean

  • s1: sample 1 standard deviation

  • s2: sample 2 standard deviation

  • n1: sample 1 size

  • n2: sample 2 size

  • equal_variance: true if equal_variance assumed



28
29
30
31
32
33
34
35
36
37
# File 'lib/statsample/test/t.rb', line 28

def two_sample_independent(x1, x2, s1, s2, n1, n2, equal_variance = false)
  num=x1-x2
  if equal_variance
    sx1x2 = sqrt(((n1-1)*s1**2 + (n2-1)*s2**2).quo(n1+n2-2))
    den   = sx1x2*sqrt(1.quo(n1)+1.quo(n2))
  else
    den=sqrt((s1**2).quo(n1) + (s2**2).quo(n2))
  end
  num.quo(den)
end

Instance Method Details

#confidence_interval(cl = nil) ⇒ Object Also known as: ci



102
103
104
105
106
# File 'lib/statsample/test/t.rb', line 102

def confidence_interval(cl=nil)
    cl||=confidence_level
    t_crit = t_critical(cl, df)
    [estimate - se*t_crit, estimate + se*t_crit]
end

#probabilityObject

probability



98
99
100
# File 'lib/statsample/test/t.rb', line 98

def probability
  p_using_cdf(Distribution::T.cdf(t, df),  tails)
end

#report_building(builder) ⇒ Object

:nodoc:



110
111
112
113
114
115
# File 'lib/statsample/test/t.rb', line 110

def report_building(builder) #:nodoc:
  builder.section(:name=>@name) do |section|
    section.text _("%s: %0.4f | %s: %0.4f") % [@estimate_name, @estimate, @standard_error_name, se]
    report_building_t(section)
  end
end

#report_building_t(s) ⇒ Object



116
117
118
119
120
121
# File 'lib/statsample/test/t.rb', line 116

def report_building_t(s)
  df_f=@df.is_a?(Integer) ? "%d" : "%0.4f"
  s.text _("t(%d) = %0.4f, p=%0.4f (%s tails)") % [df, t,probability, tails]
  s.text _("CI(%d%%): %0.4f - %0.4f") % [confidence_level*100, ci[0],ci[1]]
  
end

#to_fObject



93
94
95
# File 'lib/statsample/test/t.rb', line 93

def to_f
  t
end