The picture (Graph)
This is a graph produced by my Python code to show the relationship between the age of a person's first alcoholic drink without a parent or adult family member and their weekly income from a summer job. Red denotes individual data points while the blue is a trendline.
The code output and analysis
Association between age of first drink and Summer Wages
(0.17822749887260816, 1.615966418594485e-10)
The P-value is extremely low which indicates that the null hypothesis (there is no relationship between the age of first drink and summer income) can be rejected. The correlation coefficient of 0.18 indicates a weak relationship.
Prepping the data
I converted skips, refused, and other non-answers to NA for both variables. I used the "Age of first drink" as the explanatory variable and "Weekly summer income" as the result. For weekly summer income, I converted "Legitimate Skip" to 0 since that type of skip meant the respondent did not have a job and, therefore, had an income of 0. Once that data was cleaned up, I made a new dataframe "sub1" without the NA data. I used that to plot my data and run the R-square test.
Different code than in the lesson
Rather than use the "regplot" from Seaborn, I chose to use Pylab and plot the trendline as a seperate feature. I will admit that this takes more code and time but I wanted to learn multiple ways of getting the same thing done. The result is that I was able to plot the data in red and the trendline in blue.
I included other attempts at plotting the data in the commented-out sections of the code below
The code
- # -*- coding: utf-8 -*-
- """
- Created on Tue Dec 13 20:11:43 2016
- @author: broth
- """
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- import matplotlib.pylab as pylab
- import seaborn
- import scipy.stats as sps
- filename ='addhealth_pds.csv'
- data = pd.read_csv(filename, low_memory=False)
- #H1TO4 is age first tried alcohol without parent or adult family member
- data['H1TO4']=data['H1TO4'].replace(96, np.nan)
- data['H1TO4']=data['H1TO4'].replace(97, np.nan)
- data['H1TO4']=data['H1TO4'].replace(98, np.nan)
- #H1EE5 is how much you earn each week in the summer
- data['H1EE5']=data['H1EE5'].replace(996, np.nan)
- data['H1EE5']=data['H1EE5'].replace(997, 0)
- data['H1EE5']=data['H1EE5'].replace(998, np.nan)
- data['H1EE5']=data['H1EE5'].replace(999, np.nan)
- sub1=data[['H1TO4', 'H1EE5']].dropna()
- x=sub1['H1TO4']
- y=sub1['H1EE5']
- z = np.polyfit(x,y,1)
- p = np.poly1d(z)
- plt.plot(x,y,'ro',x,p(x),'o--')
- #scat1 = seaborn.regplot(x="H1TO4", y="H1EE5", fit_reg=True, data=sub1)
- #plt.scatter(sub1['H1TO4'], sub1['H1EE5'])
- #pylab.plot(sub1['H1TO4'], sub1['H1EE5'],"ro")
- plt.xlabel('Age of First Drink')
- plt.ylabel('Weekly Summer Income')
- plt.title('Scatterplot for the Association Between Age of first alcoholic \
- drink and Weekly Summer income')
- print ('Association between age of first drink and Summer Wages')
- print (sps.stats.pearsonr(sub1['H1TO4'], sub1['H1EE5']))

No comments:
Post a Comment