Skip to content

Commit

Permalink
Issue 32: T-Test uses incorrect degrees of freedom for one sample tes…
Browse files Browse the repository at this point in the history
…ts (#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.
  • Loading branch information
Esteban Zapata Rojas authored Mar 1, 2020
1 parent 07aed55 commit 27eb853
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ jobs:
gem install bundler
bundle install --jobs 2 --retry 1
bundle exec rake
build_2_7:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Set up Ruby 2.7
uses: actions/setup-ruby@v1
with:
ruby-version: 2.7.x
- name: Build and test with Rake
run: |
gem install bundler
bundle install --jobs 2 --retry 1
bundle exec rake
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ rvm:
- 2.6.0
- 2.6.3
- 2.6.5
- 2.7
before_install: gem update --system && gem install bundler
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Unit test runs under the following ruby versions:
* Ruby 2.6.0.
* Ruby 2.6.3.
* Ruby 2.6.5.
* Ruby 2.7.

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).

Expand Down
2 changes: 1 addition & 1 deletion lib/statistics/statistical_test/t_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.perform(alpha, tails, *args)
raise ZeroStdError, ZeroStdError::STD_ERROR_MSG if data_std == 0

comparison_mean = args[0]
degrees_of_freedom = args[1].size
degrees_of_freedom = args[1].size - 1

(data_mean - comparison_mean)/(data_std / Math.sqrt(args[1].size).to_f).to_f
else
Expand Down
4 changes: 2 additions & 2 deletions ruby-statistics.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_development_dependency "rake", '~> 12.0', '>= 12.0.0'
spec.add_development_dependency "rspec", '~> 3.6', '>= 3.6.0'
spec.add_development_dependency "rspec", '>= 3.6.0'
spec.add_development_dependency "grb", '~> 0.4.1', '>= 0.4.1'
spec.add_development_dependency 'byebug', '~> 9.1.0', '>= 9.1.0'
spec.add_development_dependency 'byebug', '>= 9.1.0'
end
14 changes: 13 additions & 1 deletion spec/statistics/statistical_test/t_test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@
# with a standard deviation of 0.452, whereas marks range from 1 (worst) to 6 (excellent).
# The grade point average (GPA) of all fifth grade pupils of the last five years is 4.7.
# Is the GPA of the 22 pupils different from the populations’ GPA?
it 'performs a t-test with one sample' do
it 'performs a t-test with one sample for one tail' do
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]
alpha = 0.05

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

expect(result[:p_value].round(6)).to eq 0.003114 # R 3.5.1. calculates the p_value as 0.003114
expect(result[:null]).to be false
expect(result[:alternative]).to be true
end

it 'performs a t-test with one sample for two tails' do
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]
alpha = 0.05

result = described_class.perform(alpha, :two_tail, 4.7, student_grades)

expect(result[:p_value].round(6)).to eq 0.006229 # R 3.5.1. calculates the p_value as 0.006229
expect(result[:null]).to be false
expect(result[:alternative]).to be true
end
Expand Down

0 comments on commit 27eb853

Please sign in to comment.