
%matplotlib inline

import chap01soln
resp = chap01soln.ReadFemResp()
resp

"""
Explanation: Exercise from Think Stats, 2nd Edition (thinkstats2.com)<br>
Allen Downey
Read the female respondent file and display the variables names.
End of explanation
"""


import thinkstats2
hist = thinkstats2.Hist(resp.totincr)
print resp.totincr

"""
Explanation: Make a histogram of <tt>totincr</tt> the total income for the respondent's family.  To interpret the codes see the codebook.
End of explanation
"""


import thinkplot
thinkplot.Hist(hist, label='totincr')
thinkplot.Show()

"""
Explanation: Display the histogram.
End of explanation
"""


import matplotlib.pyplot as plt
age_r = resp.age_r.value_counts().sort_index()

age_r.plot(kind='bar', label = 'age_r', legend = True)

resp.age_r.plot(kind='hist',label='age_r', range =(0,44), bins = 45, ylim=(0, 300), legend='True')

"""
Explanation: Make a histogram of <tt>age_r</tt>, the respondent's age at the time of interview.
End of explanation
"""


df_numfmhh = resp.numfmhh
df = df_numfmhh.value_counts().sort_index()
df

df.plot(kind='bar', label='numfmhh', legend = True)

df_numfmhh.plot(kind='hist', label='numfmhh', legend = True)

"""
Explanation: Make a histogram of <tt>numfmhh</tt>, the number of people in the respondent's household.
End of explanation
"""


import pandas as pd
parity_vc = resp.parity.value_counts().sort_index()
df_parity_vc = pd.DataFrame(df_parity, columns=['frequency'])

parity_vc.plot(kind='bar',label='parity',legend=True)

resp.parity.plot(kind='hist', label = 'parity', legend = True, range=(-5,25), ylim= (0,3500), bins = 30)

"""
Explanation: Make a histogram of <tt>parity</tt>, the number children the respondent has borne.  How would you describe this distribution?
End of explanation
"""


resp.parity.describe()

"""
Explanation: Use Hist.Largest to find the largest values of <tt>parity</tt>.
End of explanation
"""


resp_highest_income = resp[resp.totincr == resp.totincr.max()]
resp_other_income = resp[ resp.totincr != resp.totincr.max()]
resp_highest_income_parity = resp_highest_income.parity
resp_highest_income_parity_vc = resp_highest_income_parity.value_counts().sort_index()
resp_highest_income_parity_vc.plot(kind = 'bar', title = 'parity of those respondents with highest income')

"""
Explanation: Use <tt>totincr</tt> to select the respondents with the highest income.  Compute the distribution of <tt>parity</tt> for just the high income respondents.
End of explanation
"""


resp_highest_income_parity.max()

"""
Explanation: Find the largest parities for high income respondents.
End of explanation
"""


print resp_highest_income_parity.mean()
print resp_other_income.parity.mean()

"""
Explanation: Compare the mean <tt>parity</tt> for high income respondents and others
End of explanation
"""


resp_hadsex = resp[resp.hadsex == 1]
resp_hadsex_age_r = resp_hadsex.age_r
ax1 = resp_hadsex_age_r.plot(kind = 'hist')

"""
Explanation: Investigate any other variables that look interesting.
Histogram of the age of respondents who had sex
End of explanation
"""


resp_nosex = resp[ resp.hadsex != 1]
resp_nosex_age_r = resp_nosex.age_r
resp_nosex_age_r.plot(kind = 'hist')

df1 = pd.DataFrame(resp_hadsex.age_r.value_counts().sort_index(), columns = ['hadsex'])
df2 = pd.DataFrame(resp_nosex.age_r.value_counts().sort_index(), columns = ['nosex'])
df1['nosex'] = df2.nosex
df1

df1.plot(kind = 'bar', alpha = 0.5, xlim = (0,15))

"""
Explanation: Histogram of the age of respondents who never had sex
End of explanation
"""


