class Statsample::Test::T

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

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

Attributes

confidence_level[RW]
df[R]
estimate[R]
estimate_name[RW]
name[RW]

Name of F analysis

se[R]
standard_error[R]
standard_error_name[RW]
t[R]
tails[RW]

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

Public Class Methods

new(estimate, standard_error, df, opts=Hash.new) click to toggle source

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

# File lib/statsample/test/t.rb, line 76
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

Public Instance Methods

ci(cl=nil)
Alias for: confidence_interval
confidence_interval(cl=nil) click to toggle source
# File lib/statsample/test/t.rb, line 104
def confidence_interval(cl=nil)
    cl||=confidence_level
    t_crit = t_critical(cl, df)
    [estimate - se*t_crit, estimate + se*t_crit]
end
Also aliased as: ci, ci
df_equal_variance(n1,n2) click to toggle source

Degrees of freedom for equal variance on t test

# File lib/statsample/test/t.rb, line 41
def df_equal_variance(n1,n2)
  n1+n2-2
end
df_not_equal_variance(s1,s2,n1,n2) click to toggle source

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

# File lib/statsample/test/t.rb, line 51
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) click to toggle source

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

# File lib/statsample/test/t.rb, line 18
def one_sample(x,u,s,n)
  (x-u)*Math::sqrt(n).quo(s)
end
probability() click to toggle source

probability

# File lib/statsample/test/t.rb, line 100
def probability
  p_using_cdf(Distribution::T.cdf(t, df),  tails)
end
report_building_t(s) click to toggle source
# File lib/statsample/test/t.rb, line 118
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_f() click to toggle source
# File lib/statsample/test/t.rb, line 95
def to_f
  t
end
two_sample_independent(x1, x2, s1, s2, n1, n2, equal_variance = false) click to toggle source

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

# File lib/statsample/test/t.rb, line 30
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