Method: RubyStatistics::StatisticalTest::ChiSquaredTest.goodness_of_fit

Defined in:
lib/ruby-statistics/statistical_test/chi_squared_test.rb

.goodness_of_fit(alpha, expected, observed) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby-statistics/statistical_test/chi_squared_test.rb', line 23

def self.goodness_of_fit(alpha, expected, observed)
  chi_score, df = *self.chi_statistic(expected, observed) # Splat array result

  return if chi_score.nil? || df.nil?

  probability = Distribution::ChiSquared.new(df).cumulative_function(chi_score)
  p_value = 1 - probability

  # According to https://stats.stackexchange.com/questions/29158/do-you-reject-the-null-hypothesis-when-p-alpha-or-p-leq-alpha
  # We can assume that if p_value <= alpha, we can safely reject the null hypothesis, ie. accept the alternative hypothesis.
  { probability: probability,
    p_value: p_value,
    alpha: alpha,
    null: alpha < p_value,
    alternative: p_value <= alpha,
    confidence_level: 1 - alpha }
end