Thursday, December 15, 2016

Data Regression Tools - Week 1

Describing the data

The sample of 6504 students in 7th to 12th grade was taken from 80 high schools and 52 of their feeder schools from across the United States. The ethnic composition was predominantly White (N=4038, 62.3%), Black (N=1483, 22.9%), Asian(N=212, 3.3%), Native American/Indian (N=68, 1.0%), Other(N=362, 5.6%), and Multiple(N=322, 5.0%). The gender composition was Female (N=3356, 51.6%), Male(N=3147, 48.4%) and one respondent was unknown.

The school-based clustered sampling of the 132 school was primarily conducted by Quality Education Data Inc. through the use of in-school questionnaires, in-home interviews, school administrator questionnaires, in-home interviews, and parent questionnaires.
Grade retention was drawn from the question “Were you ever retained or held back a grade?” Students who responded “Yes” were then asked which grade(s) they had been retained. The data was combined into one variable and binned into four categories, “Never held back”, “Elementary School”, “Secondary” and “Multiple”

Violence was drawn from three different questions with the stem “How often the last 12 months have you…”
  • ·         Gotten into a fight?
  • ·         Pulled a knife or gun on someone?
  • ·         Shot or stabbed someone?

Respondents who indicated any level of involvement in any of the three categories were listed as positive for violence while all who had not indicated any involvement were coded as negative for violence.

The age variable was calculated by subtracting the birth year from the year the survey was completed. Since the age variable itself is not used in the calculations, it's exact calculation isn't pertinent. Three respondents had a negative age calculation using this method and that data was disregarded.



Code to extract the above information (except pie chart)

  1. import pandas as pd  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4.   
  5.   
  6. filename="addhealth_pds.csv"  
  7. data = pd.read_csv(filename, low_memory=False)  
  8.   
  9.   
  10. data['H1GI6A']=data['H1GI6A'].replace(6, np.nan)  
  11. data['H1GI6A']=data['H1GI6A'].replace(8, np.nan)  
  12. data['H1GI6B']=data['H1GI6B'].replace(6, np.nan)  
  13. data['H1GI6C']=data['H1GI6C'].replace(6, np.nan)  
  14. data['H1GI6D']=data['H1GI6D'].replace(6, np.nan)  
  15. data['H1GI6E']=data['H1GI6E'].replace(6, np.nan)  
  16.   
  17. data['H1GI6B']=data['H1GI6B'].replace(8, np.nan)  
  18. data['H1GI6C']=data['H1GI6C'].replace(8, np.nan)  
  19. data['H1GI6D']=data['H1GI6D'].replace(8, np.nan)  
  20. data['H1GI6E']=data['H1GI6E'].replace(8, np.nan)  
  21. data['BIO_SEX']=data['BIO_SEX'].replace(6, np.nan)  
  22.   
  23. data['NUMETHNIC']=(data['H1GI6A']+ data['H1GI6B'] + data['H1GI6C'] + data['H1GI6D']  
  24.  + data['H1GI6E'])  
  25.   
  26. data["AGE"]=data['IYEAR'] - data['H1GI1Y']  
  27. #data["AGE"]=data["AGE"].replace(-1, np.nan)  
  28.   
  29. age=data['AGE'].value_counts()  
  30. print(age)  
  31. agedesc=data['AGE'].describe()  
  32. print('\nStatistical Description of Age')  
  33. print(agedesc)  
  34.   
  35.   
  36. genderlabel={1:"Male", 2:'Female', 6:'unknown'}  
  37. data['BIO_SEX']=data['BIO_SEX'].map(genderlabel).astype('category')  
  38. gender = data['BIO_SEX'].value_counts()  
  39.   
  40. genderprc =data['BIO_SEX'].value_counts(normalize=True)  
  41. print("\nCount of Gender")  
  42. print(gender)  
  43. print("\nPercent of each Gender")  
  44. print(genderprc)  
  45.   
  46. def ethnic (row):  
  47.     if row["NUMETHNIC"] > 1:  
  48.         return 6  
  49.     if row['H1GI6A'] == 1:  
  50.         return 1  
  51.     if row['H1GI6B'] == 1:  
  52.         return 2  
  53.     if row['H1GI6C'] == 1:  
  54.         return 3  
  55.     if row['H1GI6D'] == 1:  
  56.         return 4  
  57.     if row['H1GI6E'] == 1:  
  58.         return 5  
  59.     else:  
  60.         return np.nan  
  61.   
  62. ethniclabels = {1:'White', 2:"Black", 3:'Native American', 4:"Asian", 5:'Other',  
  63.                 6:'Multiple'}  
  64. data['ETHNICITY'] = data.apply(lambda row: ethnic (row), axis=1)  
  65.   
  66. data['ETHNICITY']=data['ETHNICITY'].map(ethniclabels)  
  67. subwhite = data[(data['ETHNICITY'] == 'White')]    
  68. whiteage = subwhite['AGE'].tolist()   
  69.   
  70. subblack = data[(data['ETHNICITY'] == 'Black')]    
  71. blackage = subblack['AGE'].tolist()   
  72.   
  73. subnative = data[(data['ETHNICITY'] == 'Native American')]    
  74. nativeage = subnative['AGE'].tolist()  
  75.   
  76. subasian = data[(data['ETHNICITY'] == 'Asian')]    
  77. asianage = subasian['AGE'].tolist()  
  78.   
  79. subother = data[(data['ETHNICITY'] == 'Other')]    
  80. otherage = subother['AGE'].tolist()  
  81.   
  82. submulti = data[(data['ETHNICITY'] == 'Multiple')]    
  83. multiage = submulti['AGE'].tolist()  
  84.    
  85. ethnicities=[whiteage, blackage, nativeage, asianage, otherage, multiage]   
  86.   
  87. ethnicity = data['ETHNICITY'].value_counts()  
  88. ethnicprc =data['ETHNICITY'].value_counts(normalize=True)  
  89. print("\nCounts for Ethnic Groups")  
  90. print(ethnicity)  
  91. print("\nPercentage of each Ethnic Group")  
  92. print(ethnicprc)  
  93.   
  94. #clean=data[['AGE', 'ETHNICITY', 'BIO_SEX']].dropna()   
  95. plt.boxplot(ethnicities, labels = ('White', 'Black', 'Nat. Am.',  
  96.                                    'Asian', 'Other', 'Multiple'))  
  97. plt.xlabel("Ethnicity")  
  98. plt.ylabel("Age")  
  99. plt.title("Age Range of ADDHEALTH Respondents")  
  100. plt.show()  

The output (boxplot is shown above)

Ages (Count)
17    1169
 16    1162
 18    1149
 15    1081
 14     903
 13     578
 19     389
 20      43
 21      18
 12       9
-1        3
Name: AGE, dtype: int64

Statistical Description of Age
count    6504.000000
mean       16.028905
std         1.810004
min        -1.000000
25%        15.000000
50%        16.000000
75%        17.000000
max        21.000000
Name: AGE, dtype: float64

Count of Gender
Female    3356
Male      3147
Name: BIO_SEX, dtype: int64

Percent of each Gender
Female    0.51607
Male      0.48393
Name: BIO_SEX, dtype: float64

Counts for Ethnic Groups
White              4038
Black              1483
Other               362
Multiple            322
Asian               212
Native American      68
Name: ETHNICITY, dtype: int64

No comments:

Post a Comment