Wednesday, December 14, 2016

Data Anlalysis Week 3 - Correlation Coefficient

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

  1. # -*- coding: utf-8 -*-  
  2. """ 
  3. Created on Tue Dec 13 20:11:43 2016 
  4.  
  5. @author: broth 
  6. """  
  7.   
  8. import numpy as np  
  9. import pandas as pd  
  10. import matplotlib.pyplot as plt  
  11. import matplotlib.pylab as pylab  
  12. import seaborn  
  13. import scipy.stats as sps  
  14.   
  15. filename ='addhealth_pds.csv'  
  16. data = pd.read_csv(filename, low_memory=False)  
  17.   
  18. #H1TO4 is age first tried alcohol without parent or adult family member  
  19.   
  20. data['H1TO4']=data['H1TO4'].replace(96, np.nan)  
  21. data['H1TO4']=data['H1TO4'].replace(97, np.nan)  
  22. data['H1TO4']=data['H1TO4'].replace(98, np.nan)  
  23.   
  24.   
  25. #H1EE5 is how much you earn each week in the summer  
  26.   
  27. data['H1EE5']=data['H1EE5'].replace(996, np.nan)  
  28. data['H1EE5']=data['H1EE5'].replace(997, 0)  
  29. data['H1EE5']=data['H1EE5'].replace(998, np.nan)  
  30. data['H1EE5']=data['H1EE5'].replace(999, np.nan)  
  31.   
  32.   
  33. sub1=data[['H1TO4', 'H1EE5']].dropna()  
  34.   
  35. x=sub1['H1TO4']  
  36. y=sub1['H1EE5']  
  37. z = np.polyfit(x,y,1)  
  38. p = np.poly1d(z)  
  39. plt.plot(x,y,'ro',x,p(x),'o--')  

  40. #scat1 = seaborn.regplot(x="H1TO4", y="H1EE5", fit_reg=True, data=sub1)  
  41. #plt.scatter(sub1['H1TO4'], sub1['H1EE5'])  
  42. #pylab.plot(sub1['H1TO4'], sub1['H1EE5'],"ro") 
  43.  
  44. plt.xlabel('Age of First Drink')  
  45. plt.ylabel('Weekly Summer Income')  
  46. plt.title('Scatterplot for the Association Between Age of first alcoholic \  
  47. drink and Weekly Summer income')  
  48.   
  49. print ('Association between age of first drink and Summer Wages')  
  50. print (sps.stats.pearsonr(sub1['H1TO4'], sub1['H1EE5']))  


No comments:

Post a Comment