load('data/nhanes500.RData')Lab 9: Inference for binomial data
With solutions
Fill in the author and calpoly-id fields at the very top of this file. Replace "Your Name Here" with your full name and "yourpolynetid" with your Cal Poly username (the part of your email before @calpoly.edu, e.g. jdoe01). Your submission cannot be matched to your record without this.
Where a “Your turn” asks you to save a result by a specific name (shown in bold), use the exact name given — your work is checked automatically.
This lab covers inference for population proportions from binomial (two-category) data. Examples use the NHANES survey data.
Point estimation
How common is diabetes among U.S. adults?
For categorical data, point estimates of population proportions are computed by tabulating counts and normalizing by the sample size:
# tabulate counts
diabetes.tbl <- table(nhanes$diabetes)
diabetes.tbl
Yes No
57 443
# compute sample proportions
diabetes.tbl |> prop.table()
Yes No
0.114 0.886
Diabetes prevalence among U.S. adults is estimated to be 11.4%.
How common is sleep trouble among U.S. adults?
The sleeptrouble variable records whether the participant experiences sleep trouble. Tabulate the counts and store the table as sleep.tbl. Then compute sample proportions and estimate the prevalence of sleep trouble.
# tabulate and store
sleep.tbl <- table(nhanes$sleeptrouble)
sleep.tbl
Yes No
139 361
# compute sample proportions
sleep.tbl |> prop.table()
Yes No
0.278 0.722
An estimated 27.8% of U.S. adults experience sleep trouble.
Testing a binomial proportion
Is diabetes prevalence really 10%?
To test whether a population proportion equals a hypothesized value, use prop.test(...). Pass a table of counts and specify the hypothesized proportion p:
# test of binomial proportion (table input)
diabetes.tbl |> prop.test(p = 0.1)
1-sample proportions test with continuity correction
data: diabetes.tbl, null probability 0.1
X-squared = 0.93889, df = 1, p-value = 0.3326
alternative hypothesis: true p is not equal to 0.1
95 percent confidence interval:
0.08814952 0.14594579
sample estimates:
p
0.114
The output includes the test statistic, p-value, and a confidence interval.
The data do not provide evidence that diabetes prevalence among U.S. adults differs from 10% (\(\chi^2\) = 0.94 on 1 degree of freedom, p = 0.3326). With 95% confidence, diabetes prevalence is estimated to be between 8.81% and 14.59%.
Is one in five U.S. adults affected by sleep trouble?
Test whether one in five U.S. adults experience sleep trouble. Store the full test result as sleep.rslt. Report the test result and confidence interval in context following conventional style.
# test of binomial proportion
sleep.rslt <- sleep.tbl |> prop.test(p = 0.2)
sleep.rslt
1-sample proportions test with continuity correction
data: sleep.tbl, null probability 0.2
X-squared = 18.528, df = 1, p-value = 1.674e-05
alternative hypothesis: true p is not equal to 0.2
95 percent confidence interval:
0.2395872 0.3198838
sample estimates:
p
0.278
The data provide evidence that the proportion of U.S. adults who experience sleep trouble differs from 0.2 (\(\chi^2\) = 18.53 on 1 degree of freedom, p < 0.0001). With 95% confidence, between 23.96% and 31.99% of U.S. adults experience sleep trouble.
Alternatively, prop.test(...) can accept the count and sample size directly instead of a table. For the diabetes example, 57 out of 500 participants are diabetic:
# test of binomial proportion (count input)
prop.test(x = 57, n = 500, p = 0.1)
1-sample proportions test with continuity correction
data: 57 out of 500, null probability 0.1
X-squared = 0.93889, df = 1, p-value = 0.3326
alternative hypothesis: true p is not equal to 0.1
95 percent confidence interval:
0.08814952 0.14594579
sample estimates:
p
0.114
The results are identical.
Does more than half the population experience no sleep trouble?
Using the count and sample size directly (i.e., prop.test(x = ..., n = ..., p = ...)), test whether 80% of U.S. adults are free of sleep trouble. Store the result as nosleep.rslt. Report the result in context.
# test using count input
nosleep.rslt <- prop.test(x = 361, n = 500, p = 0.8)
nosleep.rslt
1-sample proportions test with continuity correction
data: 361 out of 500, null probability 0.8
X-squared = 18.528, df = 1, p-value = 1.674e-05
alternative hypothesis: true p is not equal to 0.8
95 percent confidence interval:
0.6801162 0.7604128
sample estimates:
p
0.722
The data provide evidence that the proportion of U.S. adults free of sleep trouble differs from 80% (\(\chi^2\) = 18.53 on 1 degree of freedom, p < 0.0001). With 95% confidence, between 68.01% and 76.04% of U.S. adults are free of sleep trouble.
Adjusting the confidence level
The default confidence level is 95%. To change it, add a conf.level argument:
# 99% confidence interval
diabetes.tbl |> prop.test(p = 0.1, conf.level = 0.99)
1-sample proportions test with continuity correction
data: diabetes.tbl, null probability 0.1
X-squared = 0.93889, df = 1, p-value = 0.3326
alternative hypothesis: true p is not equal to 0.1
99 percent confidence interval:
0.0814852 0.1568967
sample estimates:
p
0.114
With 99% confidence, diabetes prevalence among U.S. adults is estimated to be between 8.10% and 15.66%.
How precise is the estimate of sleep trouble prevalence at the 90% level?
Construct a 90% confidence interval for the proportion of U.S. adults who experience sleep trouble by testing against the same hypothesis as Your turn 2 but with a different confidence level. Store the result as sleep90.rslt. Interpret the interval in context.
# 90% confidence interval
sleep90.rslt <- sleep.tbl |> prop.test(p = 0.2, conf.level = 0.90)
sleep90.rslt
1-sample proportions test with continuity correction
data: sleep.tbl, null probability 0.2
X-squared = 18.528, df = 1, p-value = 1.674e-05
alternative hypothesis: true p is not equal to 0.2
90 percent confidence interval:
0.2453476 0.3131143
sample estimates:
p
0.278
With 90% confidence, between 24.60% and 31.27% of U.S. adults experience sleep trouble.
Directional tests
A directional (one-sided) test is performed by specifying alternative = 'greater' or alternative = 'less'. For example, to test whether diabetes prevalence exceeds 10%:
# upper-sided test
diabetes.tbl |> prop.test(p = 0.1, alternative = 'greater')
1-sample proportions test with continuity correction
data: diabetes.tbl, null probability 0.1
X-squared = 0.93889, df = 1, p-value = 0.1663
alternative hypothesis: true p is greater than 0.1
95 percent confidence interval:
0.09176378 1.00000000
sample estimates:
p
0.114
The data do not provide evidence that diabetes prevalence among U.S. adults exceeds 10% (\(\chi^2\) = 0.94 on 1 degree of freedom, p = 0.1663). With 95% confidence, diabetes prevalence is estimated to be at least 9.18%.
Does more than a quarter of U.S. adults experience sleep trouble?
Test whether sleep trouble prevalence exceeds 25%. Store the result as sleep.upper.rslt. Report the test result and lower confidence bound in context.
# upper-sided test
sleep.upper.rslt <- sleep.tbl |>
prop.test(p = 0.25, alternative = 'greater')
sleep.upper.rslt
1-sample proportions test with continuity correction
data: sleep.tbl, null probability 0.25
X-squared = 1.944, df = 1, p-value = 0.08162
alternative hypothesis: true p is greater than 0.25
95 percent confidence interval:
0.2453476 1.0000000
sample estimates:
p
0.278
The data do not provide evidence that sleep trouble prevalence exceeds 25% (\(\chi^2\) = 1.94 on 1 degree of freedom, p = 0.0816). With 95% confidence, sleep trouble prevalence is estimated to be at least 24.53%.
- Save the file (Ctrl+S / Cmd+S).
- Render to PDF: click Render or press Ctrl+Shift+K / Cmd+Shift+K.
- Download both files: in the Files panel, check the
.qmdand PDF, then click More ▾ → Export…. - Upload the PDF to the Gradescope assignment for this lab.
- Upload the
.qmdfile to the course submission link.