lib/statsample/test/t.rb in statsample-0.9.0 vs lib/statsample/test/t.rb in statsample-0.10.0
- old
+ new
@@ -29,11 +29,11 @@
else
den=sqrt((s1**2).quo(n1) + (s2**2).quo(n2))
end
num.quo(den)
end
- # Degrees of freedom for equal variance
+ # Degrees of freedom for equal variance on t test
def df_equal_variance(n1,n2)
n1+n2-2
end
# Degrees of freedom for unequal variance
# * <tt>s1</tt>: sample 1 standard deviation
@@ -65,28 +65,21 @@
# t = -1.1573, p=0.2474, d.f=999
class OneSample
include Math
include Statsample::Test
- include DirtyMemoize
# Options
attr_accessor :opts
# Name of test
attr_accessor :name
# Population mean to contrast
attr_accessor :u
# Degress of freedom
attr_reader :df
- # Value of t
- attr_reader :t
- # Probability
- attr_reader :probability
# Tails for probability (:both, :left or :right)
attr_accessor :tails
- dirty_writer :u, :tails
- dirty_memoize :t, :probability
# Create a One Sample T Test
# Options:
# * :u = Mean to compare. Default= 0
# * :name = Name of the analysis
# * :tails = Tail for probability. Could be :both, :left, :right
@@ -98,18 +91,18 @@
@u=@opts[:u]
@tails=@opts[:tails]
@df= @vector.n_valid-1
@t=nil
end
-
+ def t
+ T.one_sample(@vector.mean, @u, @vector.sd, @vector.n_valid)
+ end
- # Set t and probability for given u
- def compute
- @t = T.one_sample(@vector.mean, @u, @vector.sd, @vector.n_valid)
- @probability = p_using_cdf(Distribution::T.cdf(@t, @df), tails)
+ def probability
+ p_using_cdf(Distribution::T.cdf(t, @df), tails)
end
- # Presents summary of analysis
+ # Summary of analysis
#
def summary
ReportBuilder.new(:no_title=>true).add(self).to_text
end
def report_building(b) # :nodoc:
@@ -152,10 +145,12 @@
class TwoSamplesIndependent
include Math
include Statsample::Test
include DirtyMemoize
+ include GetText
+ bindtextdomain("statsample")
# Options
attr_accessor :opts
# Name of test
attr_accessor :name
# Degress of freedom (equal variance)
@@ -202,28 +197,39 @@
@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
+ # 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
+ def d
+ n1=@v1.n_valid
+ n2=@v2.n_valid
+ num=@v1.mean-@v2.mean
+ den=Math::sqrt( ((n1-1)*@v1.sd+(n2-1)*@v2.sd).quo(n1+n2))
+ num.quo(den)
+ end
+
# Presents summary of analysis
- #
def summary
ReportBuilder.new(:no_title=>true).add(self).to_text
end
+
def report_building(b) # :nodoc:
b.section(:name=>@name) {|g|
- g.table(:name=>"Mean and standard deviation", :header=>["Variable", "m", "sd","n"]) {|t|
- t.row([1,"%0.4f" % @v1.mean,"%0.4f" % @v1.sd,@v1.n_valid])
- t.row([2,"%0.4f" % @v2.mean,"%0.4f" % @v2.sd, @v2.n_valid])
+ g.table(:name=>_("Mean and standard deviation"), :header=>["Variable", "m", "sd","n"]) {|t|
+ t.row([@v1.name,"%0.4f" % @v1.mean,"%0.4f" % @v1.sd,@v1.n_valid])
+ t.row([@v2.name,"%0.4f" % @v2.mean,"%0.4f" % @v2.sd, @v2.n_valid])
}
- g.section(:name=>"Levene Test") {|g1|
- g1.parse_element(Statsample::Test.levene([@v1,@v2]))
- }
+ 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)"]) {|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=>_("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
end
end
end