stat 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 arvicco
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Stat
2
+
3
+ Support for doing stats in Ruby with a little help from R and Octave.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'stat'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install stat
18
+
19
+ ## Usage
20
+
21
+ No documentation, this gem is pre-alpa as of now.
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative 'helper'
4
+
5
+ output_file = ARGV[0] || "#{DATA_DIR}/preg.csv"
6
+
7
+ preg = Pregnancies.new.read #(100)
8
+
9
+ File.open(output_file, 'w') do |output|
10
+ output.puts preg.to_csv
11
+ end
data/bin/preg/first.rb ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative 'helper'
4
+
5
+ resp = Respondents.new.read.recode #(10)
6
+ puts 'Number of respondents', resp.length
7
+
8
+ preg = Pregnancies.new.read.recode #(10)
9
+ puts 'Number of pregnancies', preg.length
10
+
11
+ #p preg.records[1]
12
+
13
+ live_births = preg.records.find_all { |rec| rec.outcome == 1 }
14
+ puts "Live births: #{live_births.size}"
15
+
16
+ first_births = live_births.find_all { |rec| rec.birthord == 1 }
17
+ first_avg_length = first_births.inject(0.0) { |sum, rec| sum + rec.prglength } / first_births.size
18
+ puts "First births: #{first_births.size} total, #{first_avg_length} average length"
19
+
20
+ other_births = live_births.find_all { |rec| rec.birthord != 1 }
21
+ other_avg_length = other_births.inject(0.0) { |sum, rec| sum + rec.prglength } / other_births.size
22
+ puts "Other births: #{other_births.size} total, #{other_avg_length} average length"
23
+
24
+ puts 'Difference first-others (weeks)'
25
+ p (first_avg_length - other_avg_length)
26
+
27
+ puts 'Difference (hours)'
28
+ p (first_avg_length - other_avg_length) * 7 * 24
29
+
30
+ puts 'Difference %'
31
+
32
+ p (first_avg_length - other_avg_length) / other_avg_length * 100
@@ -0,0 +1,6 @@
1
+ require_relative '../stat'
2
+ require 'preg'
3
+ include Preg
4
+
5
+ DATA_DIR = 'data/preg'
6
+
data/bin/stat.rb ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # This loads bundler and sets loadpath so that you don't have to.
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
7
+
8
+ require 'stat'
data/lib/preg.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'stat'
2
+
3
+ # This module contains code for use with "Think Stats" pregnancy examples
4
+ module Preg
5
+
6
+ class Respondent < Stat::Record
7
+ end
8
+
9
+ class Pregnancy < Stat::Record
10
+ end
11
+
12
+ class Respondents < Stat::Table
13
+ def read n=nil
14
+ super 'data/preg/2002FemResp.dat', Respondent, n
15
+ end
16
+
17
+ def fields
18
+ [['caseid', 1, 12, :i]
19
+ ]
20
+ end
21
+ end
22
+
23
+ class Pregnancies < Stat::Table
24
+ def read n=nil
25
+ super 'data/preg/2002FemPreg.dat', Pregnancy, n
26
+ end
27
+
28
+ def fields
29
+ [['caseid', 1, 12, :i],
30
+ ['nbrnaliv', 22, 22, :i],
31
+ ['babysex', 56, 56, :i],
32
+ ['birthwgt_lb', 57, 58, :i],
33
+ ['birthwgt_oz', 59, 60, :i],
34
+ ['prglength', 275, 276, :i],
35
+ ['outcome', 277, 277, :i],
36
+ ['birthord', 278, 279, :i],
37
+ ['agepreg', 284, 287, :i],
38
+ ['finalwgt', 423, 440, :f],
39
+ ]
40
+ end
41
+
42
+ def recode
43
+ @records.each do |rec|
44
+ rec.agepreg /= 100.0 if rec.agepreg
45
+ if rec.birthwgt_lb && rec.birthwgt_oz &&
46
+ rec.birthwgt_lb < 20 && rec.birthwgt_oz <= 16
47
+ rec.totalwgt_oz = rec.birthwgt_lb * 16 + rec.birthwgt_oz
48
+ end
49
+ end
50
+
51
+ super
52
+ end
53
+ end # class Pregnancies
54
+ end # module Preg
@@ -0,0 +1,18 @@
1
+ require 'ostruct'
2
+
3
+ module Stat
4
+
5
+ ### Record is a single row in a data table
6
+ class Record < OpenStruct
7
+
8
+ def initialize fields, line
9
+ super()
10
+ fields.each do |(name, start, finish, type)|
11
+ val = line[start-1...finish].strip.send("to_#{type}") rescue nil
12
+ send "#{name}=", val
13
+ end
14
+ end
15
+
16
+ end # class Record
17
+ end # module Stat
18
+
data/lib/stat/table.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'pathname'
2
+ require 'stat/record'
3
+
4
+ module Stat
5
+ ### Table represents a table data file (actual file format may differ)
6
+ class Table
7
+ attr_reader :records
8
+
9
+ def initialize
10
+ @records = []
11
+ end
12
+
13
+ def length
14
+ @records.length
15
+ end
16
+
17
+ # Use it to define fields in subclasses
18
+ def fields
19
+ []
20
+ end
21
+
22
+ # Reads in data file
23
+ def read filename, constructor, n=nil
24
+ file = Pathname.new(filename)
25
+ file.open('r') do |file|
26
+ file.readlines.each_with_index do |line, i|
27
+ break if i == n
28
+ @records << constructor.new(fields, line)
29
+ end
30
+ end
31
+ self
32
+ end
33
+
34
+ # Child classes can override this to recode values
35
+ def recode
36
+ self
37
+ end
38
+
39
+ # Turn Table into CSV format
40
+ def to_csv
41
+ fields.map(&:first).join(',') + "\n" +
42
+ records.map do |rec|
43
+ fields.map { |(name, _, _, _)| rec.send(name) || '' }.join(',')
44
+ end.join("\n")
45
+ end
46
+
47
+ end # class Table
48
+ end # module Stat
49
+
@@ -0,0 +1,3 @@
1
+ module Stat
2
+ VERSION = "0.0.1"
3
+ end
data/lib/stat.rb ADDED
@@ -0,0 +1,8 @@
1
+ # This file contains code library for use with "Think Stats"
2
+
3
+ module Stat
4
+ end
5
+
6
+ require "stat/version"
7
+ require "stat/table"
8
+
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stat
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - arvicco
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: 1.1.3
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.3
27
+ none: false
28
+ prerelease: false
29
+ type: :runtime
30
+ description: Support for doing stats in Ruby
31
+ email:
32
+ - arvicco@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - !binary |-
38
+ YmluL3N0YXQucmI=
39
+ - !binary |-
40
+ YmluL3ByZWcvY29udmVydC5yYg==
41
+ - !binary |-
42
+ YmluL3ByZWcvZmlyc3QucmI=
43
+ - !binary |-
44
+ YmluL3ByZWcvaGVscGVyLnJi
45
+ - !binary |-
46
+ bGliL3ByZWcucmI=
47
+ - !binary |-
48
+ bGliL3N0YXQucmI=
49
+ - !binary |-
50
+ bGliL3N0YXQvcmVjb3JkLnJi
51
+ - !binary |-
52
+ bGliL3N0YXQvdGFibGUucmI=
53
+ - !binary |-
54
+ bGliL3N0YXQvdmVyc2lvbi5yYg==
55
+ - !binary |-
56
+ UmFrZWZpbGU=
57
+ - !binary |-
58
+ UkVBRE1FLm1k
59
+ - !binary |-
60
+ TElDRU5TRQ==
61
+ homepage: ''
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: !binary |-
72
+ MA==
73
+ none: false
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: !binary |-
79
+ MA==
80
+ none: false
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.23
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Support for doing stats in Ruby with a little help from R and Octave
87
+ test_files: []
88
+ ...