Week-2 assignment: Running your first Program:
In this week I will continue with NERSAC dataset and alcohol dependence variables I selected in week1 assignment, and try to refine my research question by examining the frequency distribution of the selected variables, below is a python program to produce frequency tables.
for this week assignment:
1-Run a python code:(Run successfully without errors):
#import pandas,Numpy library import pandas as pd import numpy data=pd.read_csv('nesarc_pds.csv',low_memory=False,encoding='utf-8',na_values=' ') print('number of rows:') print(len(data)) # number of rows print('number of variables (columns):') print(len(data.columns)) # number of variables (columns) #counts and percentages (i.e. frequency distributions) for each variable alco_vars=['CONSUMER','S2AQ1','S2AQ2','S2AQ3','S2AQ8A','S2AQ8B'] for var in alco_vars: #data[var]=pd.to_numeric(data[var],errors='coerce') if var==alco_vars[0]: print("{}-Drinking status,Current drinker=1, EX-drinker=2, Lifetime Abstainer=3".format(var)) elif var==alco_vars[1] : print("{}-Drank at least 1 alcoholic drink in life ,yes=1, No=2".format(var)) elif var==alco_vars[2]: print("{}-Drank at least 12 alcoholic drinks in last 12 months ,yes=1, No=2,Unknown=9".format(var)) # elif var==alco_vars[3]: print("{}-Drank at least 1 alcoholic drink in last 12 months ,yes=1, No=2,Unknown=9".format(var)) elif var==alco_vars[4]: print("{}-How often drank any alcohol in last 12 months".format(var)) else: print("{}- drinks of any alcohol consumed on days when drank alcohol in last 12 months ".format(var)) c = data[var].value_counts(sort=False,dropna=False).sort_index(ascending=False) p=data[ var].value_counts(sort=False,normalize=True,dropna=False).sort_index(ascending=True).astype(float).mul(100).round(2).astype(str)+'%' ferq_table=pd.DataFrame({'Frequency':c,' ':' ', 'Percent':p}) print(ferq_table) ##subset data to young adults age 18 to 25 who have drank at least 1 alcoholic drink in the last 12 months sub1=data[(data['AGE']>=18) & (data['AGE']<=25) & (data['S2AQ2']==1)] #make a copy of new subsetted data alco_dep_a =sub1.copy() # frequency distritions on new alco_dep_a data frame print("Frequency and percentages for subset data( young adults age 18 to 25 who have drank at least 12 alcoholic drink in the last 12 months):") alco_a_vars=['AGE','S2AQ2','S2AQ8A','S2AQ8B'] for var in alco_a_vars: if var==alco_a_vars[0]: print("adults age 18-25 by-{} tab".format(var)) elif var==alco_a_vars[1]: print("{} adults who drank at least 12 alcoholic drink in the last 12 months".format(var)) elif var==alco_a_vars[2]: print("{}- How often drank any alcohol in last 12 months".format(var)) else: print("{}-Number of drinks of any alcohol consumed on days when drank alcohol in last 12 months".format(var)) c=alco_dep_a[var].value_counts(sort=False,dropna=False).sort_index(ascending=True) p=alco_dep_a[var].value_counts(sort=False,normalize=True,dropna=False).sort_index(ascending=True).mul(100).round(2).astype(str) + '%' ferq_table=pd.DataFrame({'Frequency':c,' ':' ', 'Percent':p}) print(ferq_table) #upper-case all DataFrame column names - place afer code for loading data aboave data.columns = map(str.upper, data.columns) # bug fix for display formats to avoid run time errors - put after code for loading data above pd.set_option('display.float_format', lambda x:'%f'%x)
2- the results:
number of rows:
43093
number of variables (columns):
3010
CONSUMER-Drinking status,Current drinker=1, EX-drinker=2, Lifetime Abstainer=3
Frequency Percent
1 26946 62.53%
2 7881 18.29%
3 8266 19.18%
S2AQ1-Drank at least 1 alcoholic drink in life ,yes=1, No=2
Frequency Percent
1 34827 80.82%
2 8266 19.18%
S2AQ2-Drank at least 12 alcoholic drinks in last 12 months ,yes=1, No=2,Unknown=9
Frequency Percent
1 20836 48.35%
2 22225 51.57%
9 32 0.07%
S2AQ3-Drank at least 1 alcoholic drink in last 12 months ,yes=1, No=2,Unknown=9
Frequency Percent
1 26946 62.53%
2 16116 37.4%
9 31 0.07%
S2AQ8A-How often drank any alcohol in last 12 months
Frequency Percent
1.0 1865 4.33%
2.0 1210 2.81%
3.0 2619 6.08%
4.0 2914 6.76%
5.0 3261 7.57%
6.0 3557 8.25%
7.0 2663 6.18%
8.0 1805 4.19%
9.0 3210 7.45%
10.0 3637 8.44%
99.0 205 0.48%
NaN 16147 37.47%
S2AQ8B- drinks of any alcohol consumed on days when drank alcohol in last 12 months
Frequency Percent
1.0 10350 24.02%
2.0 7880 18.29%
3.0 3678 8.54%
4.0 1724 4.0%
5.0 759 1.76%
6.0 1247 2.89%
7.0 189 0.44%
8.0 275 0.64%
9.0 45 0.1%
10.0 163 0.38%
11.0 7 0.02%
12.0 246 0.57%
13.0 11 0.03%
14.0 7 0.02%
15.0 40 0.09%
16.0 3 0.01%
17.0 4 0.01%
18.0 24 0.06%
19.0 1 0.0%
20.0 14 0.03%
24.0 21 0.05%
25.0 6 0.01%
30.0 8 0.02%
32.0 1 0.0%
36.0 1 0.0%
42.0 1 0.0%
45.0 1 0.0%
48.0 1 0.0%
98.0 1 0.0%
99.0 238 0.55%
NaN 16147 37.47%
After examining the frequency distributions for the selected variables I decide to subset the data to examine the dependence based on adults who are 25 or younger , and focus on the recent drinkers , So the research question will be refined to include the association between drinking and alcohol dependence only for the young adults who have drank at least 12 drinks in the last 12 months, to answer my hypothesis that more drinking lead to alcohol dependence. So, the research question now:
Are alcohol consumption and alcohol dependence associated among the young adults who have drank in the last 12 Months?
To include only the adult observations who drank last 12 months I will add the following logic code:
sub1=data[(data['AGE']>=18) & (data['AGE']<=25) & (data['S2AQ2']==1)]
where S2AQ2 is variable for observations who drank at least 12 drinks in the last 12 months.
AGE is age variable for the observations.
The complete program above.
After executing the code, the results are below:
Frequency and percentages for subset data (young adults age 18 to 25 who drank at least 12 alcoholic drink in the last 12 months):
adults age 18-25 by-AGE tab
Frequency Percent
18 266 8.38%
19 358 11.28%
20 348 10.97%
21 453 14.28%
22 436 13.74%
23 443 13.96%
24 471 14.84%
25 398 12.54%
S2AQ2 adults who drank at least 12 alcoholic drinks in the last 12 months
Frequency Percent
1 3173 100.0%
S2AQ8A- How often drank any alcohol in last 12 months
Frequency Percent
1.0 134 4.22%
2.0 128 4.03%
3.0 360 11.35%
4.0 475 14.97%
5.0 528 16.64%
6.0 660 20.8%
7.0 436 13.74%
8.0 221 6.97%
9.0 161 5.07%
10.0 53 1.67%
99.0 17 0.54%
S2AQ8B-Number of drinks of any alcohol consumed on days when drank alcohol in last 12 months
Frequency Percent
1.0 385 12.13%
2.0 802 25.28%
3.0 599 18.88%
4.0 397 12.51%
5.0 243 7.66%
6.0 326 10.27%
7.0 81 2.55%
8.0 89 2.8%
9.0 21 0.66%
10.0 59 1.86%
11.0 4 0.13%
12.0 66 2.08%
13.0 7 0.22%
14.0 3 0.09%
15.0 22 0.69%
16.0 1 0.03%
17.0 3 0.09%
18.0 8 0.25%
20.0 7 0.22%
24.0 8 0.25%
25.0 3 0.09%
30.0 2 0.06%
36.0 1 0.03%
42.0 1 0.03%
99.0 35 1.1%
Interpretation:
In the subset data observations were those aged 18-25 answered yes to the drank at least 12 drinks in the last 12 months?
The frequency table for the age shows that adult observations nearly distributed equally among different ages from 18-25.
In the first column in (S2AQ8A- How often drank any alcohol in last 12 months) mean:
1. Every day
2. Nearly every day
3. 3 to 4 times a week
4. 2 times a week
5. Once a week
6. 2 to 3 times a month
7. Once a month
8. 7 to 11 times in the last year
9. 3 to 6 times in the last year
10. 1 or 2 times in the last year
99. Unknown
From the frequency table for this variable we notice that most of adults 20.8% drink 2-3 times a month and 16.64% once a week.
In the first column in (S2AQ8B-NUMBER OF DRINKS OF ANY ALCOHOL USUALLY CONSUMED ON DAYS WHEN DRANK ALCOHOL):
1-98 Number of drinks
99. Unknown
A quick look at the frequency table for this variable we notice that most of adults 18.88% take 3 drinks.














