Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Question 4
- Partition the sample based on 10-year age bands, i.e. the resulting groups will consist of people with ages from 18-28, 29-38, etc. Question 4 Partition the sample based on 10-year age bands, i.e. the resulting groups will consist of people with ages from 18-28, 29-38, etc. Construct 95% confidence intervals for the difference between the mean BMI for females and for males within each age band.
- df = da.copy()
- df['agegrp'] = pd.cut(x=df.RIDAGEYR, bins=[18,28,38,48,58,68,78,88])
- df['gender'] = df.RIAGENDR.map({1: 1, 2: 2})
- dk = df[['agegrp', 'gender', 'BMXBMI']].dropna()
- dk
- condition = dk['gender'] == 1 # only for males
- dk_male = dk[condition]
- dk_male
- dz_male = dk_male.groupby('agegrp').agg({'BMXBMI': [np.mean,np.std]})
- dz_male # means and std for males
- dk_male.groupby('agegrp').size() # sample size n for each males groups
- condition = dk['gender'] == 2 # only for females
- dk_female = dk[condition]
- dk_female
- dz_female = dk_female.groupby('agegrp').agg({'BMXBMI': [np.mean,np.std]})
- dz_female # means and std for females
- dk_female.groupby(['agegrp']).size() # sample size n for each females groups
- # Function for means difference
- def find_limits_male_female (mean1, mean2, std1, std2, n1, n2):
- diff = 1.984 * np.sqrt((std1*std1 / n1) + (std2 * std2 / n2))
- low = (mean1 - mean2) - diff
- upper = (mean1 - mean2) + diff
- return (low, upper)
- group1 = find_limits_male_female(27.058186,28.01943,6.679515,8.048854, 452,494)
- group1
- group2 = find_limits_male_female(29.697180,29.943443,6.726690,7.959097,461,488)
- group2
- group3 = find_limits_male_female(29.514646,31.003733,6.104950,8.044642,396,509)
- group3
- group4 = find_limits_male_female(29.385132,30.787361,6.151534,7.647590,417,451)
- group4
- group5 = find_limits_male_female(29.232462,31.054664,5.959024,7.779502,459,461)
- group5
- group6 = find_limits_male_female(28.720270,30.537818,5.336652, 6.780588,296,275)
- group6
- group7 = find_limits_male_female(27.464368,27.850000,4.695650, 5.483781,174,198)
- group7
- CI for 18 to 28 (0.0, 1.8)
- CI for 28 to 38 (-0.7, 1.1)
- CI for 38 to 48 (0.6, 2.4)
- CI for 48 to 58 (0.4, 2.4)
- CI for 58 to 68 (0.9, 2.7)
- CI for 68 to 78 (0.8, 2.8)
- CI for 78 to 88 (-0.6, 1.4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement