In this chapter you will learn how to use R to compute a confidence interval.
20.1 The confint() Function
To compute a confidence interval (CI), we fit a two-sided t-test and then call the confint() function on the results. For example, to compute a CI to estimate the average price of a single-family house near the University of Minnesota we can use the following syntax.
# Carry out two-sided t-testmy_t <-t_test(~price, data = zillow, mu =322.46, alternative ="two.sided")# Compute CIconfint(my_t)
mean of x lower upper level
1 404.966 348.2415 461.6905 0.95
The lower and upper values of the output give the lower and upper bounds of the CI, respectively. From this output, our CI is computed as .
20.1.1 Some Computational Shortcuts for Computing a CI
In the house prices case study, we had initially tested a two-sided hypothesis, so we already had written the syntax for the t_test() function. This will be true in most applications. If, however, you haven’t carried out a t-test, and you want to compute a CI, you can shorten the syntax in the t_test() function by omitting the arguments mu= and alternative=. For example, if we hadn’t previously carried out a t-test on our Zillow data, we could use the syntax:
# Carry out two-sided t-testmy_t <-t_test(~price, data = zillow)# Compute CIconfint(my_t)
mean of x lower upper level
1 404.966 348.2415 461.6905 0.95
Notice that the CI is the same as we found earlier. This is because the computation of the CI does not use the argument mu=. That is only necessary when computing output for a hypothesis test. (If you looked at the results of the t-test, the t- and p-values would be different!) Thus, the value for mu= can be whatever you want or can be omitted completely. (If it is omitted, the t_test() function defaults to mu=0.) The default value for the argument alternative= is alternative=two.sided, so omitting the alternative= argument will automatically fit a two-sided t-test.
20.2 Computing a CI for a Proportion
We can also use the confint() function to compute the CI for a proportion. In a similar fashion, we would need to carry out a two-sided z-test (using prop_test()) and then call confint() on the results of that z-test. For example to compute a CI to estimate the proportion of all Minnesota children under 6 years of age who are above the MDH reference level in 2018, we will use the following syntax:
# A tibble: 91,706 × 2
lead_level ebll
<dbl> <chr>
1 2.39 No
2 2.42 No
3 3.73 No
4 3.53 No
5 2.6 No
6 1.8 No
7 4.46 No
8 3.77 No
9 2.03 No
10 3.73 No
# ℹ 91,696 more rows
# One-sample z-testmy_z <-prop_test(~ebll =="Yes", data = mn_lead, p = .029, alternative ="two.sided",correct =FALSE )# Compute CIconfint(my_z)
p lower upper level
1 0.01391403 0.01317602 0.01469276 0.95
From this output, our CI is computed as which suggests that based on the data, we think the proportion of all Minnesota children under 6 years of age who are above the MDH reference level in 2018 is between .013 and .015.
20.3 Computing a CI for the Difference in Means or Proportions
You can also use the confint() function on the results of a two-sided t- or z-test in the same way. For example, to estimate the difference in the proportion of Bachelor and Bachelorette contestants that are still together with the person they chose, we would use:
# A tibble: 50 × 10
show season run_dates name age winner runner_up proposal still_together
<chr> <dbl> <chr> <chr> <dbl> <chr> <chr> <chr> <chr>
1 Bachel… 1 March 25… Alex… 31 Amand… Trista R… No No
2 Bachel… 2 Septembe… Aaro… 28 Helen… Brooke S… Yes No
3 Bachel… 3 March 24… Andr… 27 Jen S… Kirsten … Yes No
4 Bachel… 4 Septembe… Bob … 32 Estel… Kelly Jo… No No
5 Bachel… 5 April 7 … Jess… 25 Jessi… Tara Huc… No No
6 Bachel… 6 Septembe… Byro… 40 Mary … Tanya Mi… Yes No
7 Bachel… 7 March 28… Char… 29 Sarah… Krisily … No No
8 Bachel… 8 January … Trav… 33 Sarah… Moana Di… No No
9 Bachel… 9 October … Lore… 34 Jenni… Sadie Mu… No No
10 Bachel… 10 April 2 … Andy… 30 Tessa… Bevin Po… Yes No
# ℹ 40 more rows
# ℹ 1 more variable: relationship_status <chr>
From this output, our CI is computed as which suggests that based on the data, we think the the difference in the proportion of Bachelor and Bachelorette contestants that are still together with the person they chose is between and 0.12.
How can we have a negative value when we are dealing with proportions you might ask. Remember, that this represents our estimate of a difference between two proportions. When you compute the difference between two things you can get a negative value if you are subtracting a bigger value from a smaller value. To interpret this, recall that R subtracts in alphabetical order based on the category names; thus it is computing:
The lower bound of tells us that the proportion of Bacheorette contestants that are still together with the person they chose may be higher than the proportion of Bachelor contestants that are still together with the person they chose by as much as .28. While the positive upper bound of .13 suggests that it may be that the proportion of Bachelor contestants still together with the person they chose is higher than the proportion of Bachelorette contestants still together with the person they chose by as much as .13.
Because of this, we have uncertainty about which group is more likely to stay together with the person they chose. It may be Bachelor contestants, or it may be Bachelorette contestants, we just can’t tell based on these data—there is too much uncertainty. Another possibility is that the proportions are the same. If the proportions are the same, the difference between them is 0—which falls in the range of the CI. Remember, any value in the range is a plausible value for the difference in population proportions!
Exercises: Your Turn
In Section 13, we carried out a two-sample t-test to evaluate whether the effects on IQ were different for users who started using marijuana as a teen. The evidence from this test suggested that the difference in average IQ score change is likely different for participants who became persistent marijuana users as adults than those that became persistent marijuana users as teens. Compute a CI for the difference in the average amount of change in IQ scores between these groups.
# Import datacannabis <-read_csv("https://raw.githubusercontent.com/zief0002/epsy-5261/main/data/cannabis.csv")# One-sample t-testmy_t <-t_test(~ iq_change | cannabis_dep, data = cannabis, mu =0, alternative ="two.sided", var.equal =TRUE )# Compute CIconfint(my_t)
mean in group Adult mean in group Teen lower upper level
1 -2.071429 -8.26087 1.111976 11.26691 0.95
The CI is
Interpret the CI you computed.
Based on the data, the difference in the average amount of change in IQ scores between people who became persistent marijuana users as adults than those that became persistent marijuana users as teens is between 1.11 and 11.26.
Based on the CI, is it more detrimental on IQ for people who became persistent marijuana users as adults or those that became persistent marijuana users as teens? Explain.
The CI is computed alphabetically by category level, which means it is computed as . We can compute this difference using the sample means. In the output we see the average IQ change for adults () is a smaller decrease than it is for teens (). The difference in sample means is . So a positive difference between means suggests that the adult group has LESS of a decrease in IQ than the teen group.
Because both the lower and upper bounds of the CI are positive, it means that the data suggest that people who become persistent marijuana users as an adult have, on average, LESS of a decrease in IQ than people who become persistent marijuana users as a teen. And this difference might be as little as 1.11 IQ points or as large as 11.27 IQ points.
BE CAREFUL
Be very careful when the means are negative and you are computing or estimating a difference. Interpretations can be tricky and it may take some thought in how you interpret findings.
In Section 10, we carried out a one-sample t-test to evaluate whether Ethiopian primary teachers are measuring students’ prior knowledge. The evidence from this test suggested that the empirical data were consistent with the hypothesis that the average response for all Ethiopian primary school teachers is 2.5 suggesting we were uncertain about whether, on average, they were measuring students’ prior knowledge. Compute a CI for the average response score for Ethiopian primary teachers.
# Import datacontinuous_assessment <-read_csv("https://raw.githubusercontent.com/zief0002/epsy-5261/main/data/continuous-assessment.csv")# One-sample t-testmy_t <-t_test(~prior_knowledge, data = continuous_assessment, mu =2.5, alternative ="two.sided")# Compute CIconfint(my_t)
mean of x lower upper level
1 2.433333 2.098151 2.768515 0.95
Based on the output, the CI for the average response score is .
Use the CI to compute the margin of error. Remember a CI is computed as .
The sample estimate () is 2.43. Computing the distance from this value to the upper bound (orto the lower bound) will give us the margin of error.