Class: Statistics::StatisticalTest::TTest
- Inherits:
-
Object
- Object
- Statistics::StatisticalTest::TTest
- Defined in:
- lib/statistics/statistical_test/t_test.rb
Class Method Summary collapse
- .paired_test(alpha, tails, left_group, right_group) ⇒ Object
-
.perform(alpha, tails, *args) ⇒ Object
Perform a T-Test for one or two samples.
Class Method Details
.paired_test(alpha, tails, left_group, right_group) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/statistics/statistical_test/t_test.rb', line 45 def self.paired_test(alpha, tails, left_group, right_group) # Handy snippet grabbed from https://stackoverflow.com/questions/2682411/ruby-sum-corresponding-members-of-two-or-more-arrays differences = [left_group, right_group].transpose.map { |value| value.reduce(:-) } degrees_of_freedom = differences.size - 1 down = differences.standard_deviation/Math.sqrt(differences.size) t_score = (differences.mean - 0)/down.to_f probability = Distribution::TStudent.new(degrees_of_freedom).cumulative_function(t_score) p_value = 1 - probability p_value *= 2 if tails == :two_tail { probability: probability, p_value: p_value, alpha: alpha, null: alpha < p_value, alternative: p_value <= alpha, confidence_level: 1 - alpha } end |
.perform(alpha, tails, *args) ⇒ Object
Perform a T-Test for one or two samples. For the tails param, we need a symbol: :one_tail or :two_tail
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/statistics/statistical_test/t_test.rb', line 6 def self.perform(alpha, tails, *args) return if args.size < 2 degrees_of_freedom = 0 t_score = if args[0].is_a? Numeric data_mean = args[1].mean data_std = args[1].standard_deviation comparison_mean = args[0] degrees_of_freedom = args[1].size (data_mean - comparison_mean)/(data_std / Math.sqrt(args[1].size).to_f).to_f else sample_left_mean = args[0].mean sample_left_variance = args[0].variance sample_right_variance = args[1].variance sample_right_mean = args[1].mean degrees_of_freedom = args.flatten.size - 2 left_root = sample_left_variance/args[0].size.to_f right_root = sample_right_variance/args[1].size.to_f standard_error = Math.sqrt(left_root + right_root) (sample_left_mean - sample_right_mean)/standard_error.to_f end probability = Distribution::TStudent.new(degrees_of_freedom).cumulative_function(t_score) p_value = 1 - probability p_value *= 2 if tails == :two_tail { probability: probability, p_value: p_value, alpha: alpha, null: alpha < p_value, alternative: p_value <= alpha, confidence_level: 1 - alpha } end |