Way ahead of schedule
Since I had a little extra time on my hands, I've been able to dedicate a lot of my time to working through these assignments. I'm thankful for that.
Moderating variable
I've shown an association between grade retention in school and the propensity for violence in previous assignments. Today, I'm looking at the age a respondent first starts drinking alcoholic beverages as a possible moderating variable. I took the data and broke it down into four categories, Age 0-6, Elementary, Secondary, and Never.
I looked at each of the variables and determined that only Never drinking or beginning to drink at Secondary levels has any effect on violence among students have been retained.
Preschool drinking
- sub1=clean[(clean['FIRSTAGE']=='Preschool')]
- ctPS=pd.crosstab(sub1['VIOLENT'], sub1['RETAINED'])
- print("\nChi-Square of Retention and Violence among students who first drank\
- alcohol before age 6")
- print(ctPS)
- print("\n\nChi-square value, p-value, expected counts")
- xsquare1=sps.chi2_contingency(ctPS)
- print(xsquare1)
- seaborn.factorplot(x="RETAINED", y="VIOLENT", data=sub1, kind="bar", ci=None)
- plt.xlabel('Retained at least one grade')
- plt.ylabel('Percentage Violent')
- plt.title("Association of retention and violence among respondents who \
- first drank alcohol before age 6")
Chi-Square of Retention and Violence among students who first drankalcohol before age 6
RETAINED No Yes
VIOLENT
0 4 0
1 6 3
Chi-square value, p-value, expected counts
(0.36412037037037032, 0.54622674479725641, 1, array([[ 3.07692308, 0.92307692],
[ 6.92307692, 2.07692308]]))
The Chi-Square is low and the p-value is far too high. The null hypothesis would be accepted in this case.
Drinking in Elementary School
- sub2=clean[(clean['FIRSTAGE']=='Elementary')]
- seaborn.factorplot(x="RETAINED", y="VIOLENT", data=sub2, kind="bar", ci=None)
- plt.xlabel('Retained at least one grade')
- plt.ylabel('Percentage Violent')
- plt.title("Association of retention and violence among respondents who \
- first drank alcohol 6-12")
- ctElem=pd.crosstab(sub2['VIOLENT'], sub2['RETAINED'])
- print("\nChi-Square of Retention and Violence among students who first drank \
- alcohol ages 6-12")
- print(ctElem)
- print("\n\nChi-square value, p-value, expected counts")
- xsquare2=sps.chi2_contingency(ctElem)
- print(xsquare2)
Chi-Square of Retention and Violence among students who first drank alcohol ages 6-12
RETAINED No Yes
VIOLENT
0 94 38
1 107 53
Chi-square value, p-value, expected counts
(0.44815984181655855, 0.50321011393827875, 1, array([[ 90.8630137, 41.1369863],
[ 110.1369863, 49.8630137]]))
The Chi-Square is low again and the p-value is again too high. The null hypothesis is accepted in this case as well.
Drinking in Middle and High School
- sub3=clean[(clean['FIRSTAGE']=='Secondary')]
- seaborn.factorplot(x="RETAINED", y="VIOLENT", data=sub3, kind="bar", ci=None)
- plt.xlabel('Retained at least one grade')
- plt.ylabel('Percentage Violent')
- plt.title("Association of retention and violence among respondents who \
- first drank alcohol 12-18")
- ctSec=pd.crosstab(sub3['VIOLENT'], sub3['RETAINED'])
- print("\nChi-Square of Retention and Violence among students who first drank \
- alcohol ages 12-18")
- print(ctSec)
- print("\n\nChi-square value, p-value, expected counts")
- xsquare3=sps.chi2_contingency(ctSec)
- print(xsquare3)
Chi-Square of Retention and Violence among students who first drank alcohol ages 12-18
RETAINED No Yes
VIOLENT
0 470 111
1 252 132
Chi-square value, p-value, expected counts
(27.808663811593675, 1.3392503595490095e-07, 1, array([[ 434.69637306, 146.30362694],
[ 287.30362694, 96.69637306]]))
The null hypothesis is rejected for students who begin drinking at the secondary level. The Chi-Square is 27.8 and the p-value is very low.
Never drank alcohol
- sub4=clean[(clean['FIRSTAGE']=='Never')]
- seaborn.factorplot(x="RETAINED", y="VIOLENT", data=sub4, kind="bar", ci=None,
- x_order=['Yes', 'No'])
- plt.xlabel('Retained at least one grade')
- plt.ylabel('Percentage Violent')
- plt.title("Association of retention and violence among respondents who \
- never drank alcohol")
- ctnever=pd.crosstab(sub4['VIOLENT'], sub4['RETAINED'])
- print("\nChi-Square of Retention and Violence among students who never drank \
- alcohol")
- print(ctnever)
- print("\n\nChi-square value, p-value, expected counts")
- xsquare4=sps.chi2_contingency(ctnever)
- print(xsquare4)
Chi-Square of Retention and Violence among students who never drank alcohol
RETAINED No Yes
VIOLENT
0 3034 626
1 1119 427
Chi-square value, p-value, expected counts
(73.838722844073715, 8.4767300358856875e-18, 1, array([[ 2919.70418748, 740.29581252],
[ 1233.29581252, 312.70418748]]))
This final section showed the highest Chi-Square and the lowest p-value. The null hypothesis is rejected for this group.
The whole code as it was written
- import pandas as pd
- import numpy as np
- import scipy.stats as sps
- import seaborn
- import matplotlib.pyplot as plt
- filename="addhealth_pds.csv"
- data = pd.read_csv(filename, low_memory=False)
- data['H1ED5']=data['H1ED5'].replace(6, np.nan)
- data['H1ED5']=data['H1ED5'].replace(8, np.nan)
- data['H1TO4']=data['H1TO4'].replace(96, np.nan)
- data['H1TO4']=data['H1TO4'].replace(98, np.nan)
- def violent (row):
- if row["H1FV5"] >= 1 and row["H1FV5"] <= 2:
- return 1
- if row["H1FV7"] >= 1 and row["H1FV7"] <= 2:
- return 1
- if row["H1FV8"] >= 1 and row["H1FV8"] <= 2:
- return 1
- else:
- return 0
- data['VIOLENT'] = data.apply(lambda row: violent (row), axis = 1)
- drinkage=('Preschool', 'Elementary', 'Secondary', 'Never')
- data['FIRSTAGE']=pd.cut(data['H1TO4'],(0, 6, 12, 18,100), labels=drinkage)
- data['RETAINED']=data['H1ED5']
- clean=data[['RETAINED', 'FIRSTAGE', 'VIOLENT']].dropna()
- clean['RETAINED']=clean['RETAINED'].astype(int)
- remap1={1:'Yes', 0:'No',}
- clean['RETAINED']=clean['RETAINED'].map(remap1)
- countlevels=clean['FIRSTAGE'].value_counts()
- print(countlevels)
- sub1=clean[(clean['FIRSTAGE']=='Preschool')]
- ctPS=pd.crosstab(sub1['VIOLENT'], sub1['RETAINED'])
- print("\nChi-Square of Retention and Violence among students who first drank\
- alcohol before age 6")
- print(ctPS)
- print("\n\nChi-square value, p-value, expected counts")
- xsquare1=sps.chi2_contingency(ctPS)
- print(xsquare1)
- seaborn.factorplot(x="RETAINED", y="VIOLENT", data=sub1, kind="bar", ci=None)
- plt.xlabel('Retained at least one grade')
- plt.ylabel('Percentage Violent')
- plt.title("Association of retention and violence among respondents who \
- first drank alcohol before age 6")
- sub2=clean[(clean['FIRSTAGE']=='Elementary')]
- seaborn.factorplot(x="RETAINED", y="VIOLENT", data=sub2, kind="bar", ci=None)
- plt.xlabel('Retained at least one grade')
- plt.ylabel('Percentage Violent')
- plt.title("Association of retention and violence among respondents who \
- first drank alcohol 6-12")
- ctElem=pd.crosstab(sub2['VIOLENT'], sub2['RETAINED'])
- print("\nChi-Square of Retention and Violence among students who first drank \
- alcohol ages 6-12")
- print(ctElem)
- print("\n\nChi-square value, p-value, expected counts")
- xsquare2=sps.chi2_contingency(ctElem)
- print(xsquare2)
- sub3=clean[(clean['FIRSTAGE']=='Secondary')]
- seaborn.factorplot(x="RETAINED", y="VIOLENT", data=sub3, kind="bar", ci=None)
- plt.xlabel('Retained at least one grade')
- plt.ylabel('Percentage Violent')
- plt.title("Association of retention and violence among respondents who \
- first drank alcohol 12-18")
- ctSec=pd.crosstab(sub3['VIOLENT'], sub3['RETAINED'])
- print("\nChi-Square of Retention and Violence among students who first drank \
- alcohol ages 12-18")
- print(ctSec)
- print("\n\nChi-square value, p-value, expected counts")
- xsquare3=sps.chi2_contingency(ctSec)
- print(xsquare3)
- sub4=clean[(clean['FIRSTAGE']=='Never')]
- seaborn.factorplot(x="RETAINED", y="VIOLENT", data=sub4, kind="bar", ci=None,
- x_order=['Yes', 'No'])
- plt.xlabel('Retained at least one grade')
- plt.ylabel('Percentage Violent')
- plt.title("Association of retention and violence among respondents who \
- never drank alcohol")
- ctnever=pd.crosstab(sub4['VIOLENT'], sub4['RETAINED'])
- print("\nChi-Square of Retention and Violence among students who never drank \
- alcohol")
- print(ctnever)
- print("\n\nChi-square value, p-value, expected counts")
- xsquare4=sps.chi2_contingency(ctnever)
- print(xsquare4)




No comments:
Post a Comment