Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Think Stats by Allen B. Downey Think Stats is an introduction to Probability and Statistics for Python programmers.

This is the accompanying code for this book.

Website: http://greenteapress.com/wp/think-stats-2e/

Views: 7138
License: GPL3
1
"""This file contains code for use with "Think Stats",
2
by Allen B. Downey, available from greenteapress.com
3
4
Copyright 2014 Allen B. Downey
5
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
6
"""
7
8
from __future__ import print_function, division
9
10
import numpy as np
11
import sys
12
13
import nsfg
14
import thinkstats2
15
16
17
def ReadFemResp(dct_file='2002FemResp.dct',
18
dat_file='2002FemResp.dat.gz',
19
nrows=None):
20
"""Reads the NSFG respondent data.
21
22
dct_file: string file name
23
dat_file: string file name
24
25
returns: DataFrame
26
"""
27
dct = thinkstats2.ReadStataDct(dct_file)
28
df = dct.ReadFixedWidth(dat_file, compression='gzip', nrows=nrows)
29
CleanFemResp(df)
30
return df
31
32
33
def CleanFemResp(df):
34
"""Recodes variables from the respondent frame.
35
36
df: DataFrame
37
"""
38
pass
39
40
41
def ValidatePregnum(resp):
42
"""Validate pregnum in the respondent file.
43
44
resp: respondent DataFrame
45
"""
46
# read the pregnancy frame
47
preg = nsfg.ReadFemPreg()
48
49
# make the map from caseid to list of pregnancy indices
50
preg_map = nsfg.MakePregMap(preg)
51
52
# iterate through the respondent pregnum series
53
for index, pregnum in resp.pregnum.items():
54
caseid = resp.caseid[index]
55
indices = preg_map[caseid]
56
57
# check that pregnum from the respondent file equals
58
# the number of records in the pregnancy file
59
if len(indices) != pregnum:
60
print(caseid, len(indices), pregnum)
61
return False
62
63
return True
64
65
66
def main(script):
67
"""Tests the functions in this module.
68
69
script: string script name
70
"""
71
resp = ReadFemResp()
72
73
assert(len(resp) == 7643)
74
assert(resp.pregnum.value_counts()[1] == 1267)
75
assert(ValidatePregnum(resp))
76
77
print('%s: All tests passed.' % script)
78
79
80
if __name__ == '__main__':
81
main(*sys.argv)
82
83