Skip to content

Commit 27eb853

Browse files
author
Esteban Zapata Rojas
authored
Issue 32: T-Test uses incorrect degrees of freedom for one sample tests (#33)
* gemspec: Upgrade dev dependencies. * t-test: Use correct degrees of freedom when doing a one sample test. fixes #32 This change fixes an issue where we use the size as the degrees of freedom when perfoming a T test for one sample with comparative mean. * travis: Run tests on Ruby 2.7. * workflows: Run build with Ruby 2.7. * workflows: Run tests on workflows.
1 parent 07aed55 commit 27eb853

File tree

6 files changed

+33
-4
lines changed

6 files changed

+33
-4
lines changed

.github/workflows/ruby.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,18 @@ jobs:
1818
gem install bundler
1919
bundle install --jobs 2 --retry 1
2020
bundle exec rake
21+
build_2_7:
22+
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- uses: actions/checkout@v1
27+
- name: Set up Ruby 2.7
28+
uses: actions/setup-ruby@v1
29+
with:
30+
ruby-version: 2.7.x
31+
- name: Build and test with Rake
32+
run: |
33+
gem install bundler
34+
bundle install --jobs 2 --retry 1
35+
bundle exec rake

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ rvm:
55
- 2.6.0
66
- 2.6.3
77
- 2.6.5
8+
- 2.7
89
before_install: gem update --system && gem install bundler

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Unit test runs under the following ruby versions:
99
* Ruby 2.6.0.
1010
* Ruby 2.6.3.
1111
* Ruby 2.6.5.
12+
* Ruby 2.7.
1213

1314
We got the inspiration from the folks at [JStat](https://github.com/jstat/jstat) and some interesting lectures about [Keystroke dynamics](http://www.biometric-solutions.com/keystroke-dynamics.html).
1415

lib/statistics/statistical_test/t_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def self.perform(alpha, tails, *args)
2121
raise ZeroStdError, ZeroStdError::STD_ERROR_MSG if data_std == 0
2222

2323
comparison_mean = args[0]
24-
degrees_of_freedom = args[1].size
24+
degrees_of_freedom = args[1].size - 1
2525

2626
(data_mean - comparison_mean)/(data_std / Math.sqrt(args[1].size).to_f).to_f
2727
else

ruby-statistics.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
2828
spec.require_paths = ["lib"]
2929

3030
spec.add_development_dependency "rake", '~> 12.0', '>= 12.0.0'
31-
spec.add_development_dependency "rspec", '~> 3.6', '>= 3.6.0'
31+
spec.add_development_dependency "rspec", '>= 3.6.0'
3232
spec.add_development_dependency "grb", '~> 0.4.1', '>= 0.4.1'
33-
spec.add_development_dependency 'byebug', '~> 9.1.0', '>= 9.1.0'
33+
spec.add_development_dependency 'byebug', '>= 9.1.0'
3434
end

spec/statistics/statistical_test/t_test_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,24 @@
2323
# with a standard deviation of 0.452, whereas marks range from 1 (worst) to 6 (excellent).
2424
# The grade point average (GPA) of all fifth grade pupils of the last five years is 4.7.
2525
# Is the GPA of the 22 pupils different from the populations’ GPA?
26-
it 'performs a t-test with one sample' do
26+
it 'performs a t-test with one sample for one tail' do
2727
student_grades = [5, 5.5, 4.5, 5, 5, 6, 5, 5, 4.5, 5, 5, 4.5, 4.5, 5.5, 4, 5, 5, 5.5, 4.5, 5.5, 5, 5.5]
2828
alpha = 0.05
2929

3030
result = described_class.perform(alpha, :one_tail, 4.7, student_grades)
3131

32+
expect(result[:p_value].round(6)).to eq 0.003114 # R 3.5.1. calculates the p_value as 0.003114
33+
expect(result[:null]).to be false
34+
expect(result[:alternative]).to be true
35+
end
36+
37+
it 'performs a t-test with one sample for two tails' do
38+
student_grades = [5, 5.5, 4.5, 5, 5, 6, 5, 5, 4.5, 5, 5, 4.5, 4.5, 5.5, 4, 5, 5, 5.5, 4.5, 5.5, 5, 5.5]
39+
alpha = 0.05
40+
41+
result = described_class.perform(alpha, :two_tail, 4.7, student_grades)
42+
43+
expect(result[:p_value].round(6)).to eq 0.006229 # R 3.5.1. calculates the p_value as 0.006229
3244
expect(result[:null]).to be false
3345
expect(result[:alternative]).to be true
3446
end

0 commit comments

Comments
 (0)