means47 <- replicate(10000, mean(rnorm(n = 47, mean = 15, sd = 25)))W9: Confidence Intervals
Pick someone new to be the driver this week. If everyone in the group has already had a week as the driver, start going round again!
The driver will type the code. The rest of you are navigators, you need to tell the driver where to go (i.e., what to type!).
Driver instructions:
- Log in to the bigger screen at your table
- Open RStudio Server. See the first drop-down in the FAQs page for instructions
- Open a new .R script, and load the tidyverse package.
- Listen to your navigators
Navigator instructions:
- Tell your driver where to go (i.e., what to type)!
This workshop starts off conceptual again. The goal of the first part of this workshop is to show you where we get the \(\pm\) 1.96 multipliers that appear in the formula for a 95% Confidence Interval. Later in the workshop, things get more practical: you’ll use these multipliers to calculate a 95% Confidence Interval for your observed outlook scores.
Last week, you used replicate(), mean() and rnorm() to get estimated means from 1000 random samples from the population of outlook scores that we defined. This week, our exploration will start in a similar way, with a couple modifications.
First, our estimates will be more precise if we have more than 1000 samples, so we’re going to up this to 10,000. Second, we will set a sample size of \(n = 47\), the number of responses to the DAPR1 class survey. Each of the 10,000 means in means47 below is the mean of a different hypothetical sample of 47 students.
Copy and run this code.
Let’s start with the innermost term:
rnorm(n = 47, mean = 15, sd = 25)This bit of code takes a sample of size 47 from a normal distribution that has a mean of 15 and a standard deviation of 25.
(These are the parameters that we defined our population to have last week.) The result of rnorm() is a vector of 47 numbers. In our example, each number represents one person’s outlook score.
Next, by wrapping that whole bit inside mean(), we ask R to take the mean of those 47 numbers:
mean( rnorm(n = 47, mean = 15, sd = 25) )Finally, by wrapping that whole bit inside replicate(), we repeat all of that however many times we want. In this case, we’ll repeat it 10,000 times. Each time, it takes a sample of 47 people’s scores (the rnorm() bit) and then finds their mean (the mean() bit). The result is a vector of 10,000 means.
replicate(10000, mean( rnorm(n = 47, mean = 15, sd = 25) ) )Finally, means47 <- stores those 10,000 means into an object called means47.
Again, like last week, we want to use this list of numbers as a column of a dataframe. The code below accomplishes this, and does an extra step too: it sorts the values in the estimate column.
Specifically, the arrange() function in Line 4 takes the dataframe created in Lines 1–3 and sorts it in ascending order by the estimate column. After sorting, the first row contains the smallest estimate (i.e., the smallest sample mean), and the final row contains the largest estimate (i.e., the largest sample mean).
Copy and run this code.
Now, answer the following questions:
- What is the estimate in the 5000th row of
data47? - Bonus conceptual challenge: Which measure of central tendency is given by the estimate in the 5000th row?
🗂️ See Subset a dataframe > Extract row(s) flash card.
Add a new column to data47 that contains the z-scored version of estimate. Name that column est_z.
Check that the mean of the new column is zero (or at least a very very small number close to zero) and that the SD is 1.
Copy and run the following code to create a density plot of your distribution of z-scores.
data47 |>
ggplot(aes(x = est_z)) +
geom_density()🗂️ See Calculate z-scores flash card.
A 95% confidence interval (CI) contains the middle 95% of a standard normal distribution. In other words, in a standard normal distribution:
- 2.5% of the probability distribution is below the 95% CI’s lower bound, and
- 2.5% of the probability distribution is above the 95 CI’s upper bound.
We have approximated a standard normal distribution using these 10,000 z-scores. And we can approximate its 95% CI by taking the middle 9500 z-scores.
Extract two rows from data47:
- the first row of the middle 9500 estimates, and
- the last row of the middle 9500 estimates.
Answer: What are the z-scores in those two rows? (Are they close to the 1.96 multipliers you’ve seen before?)
If we have a list of 10,000 things, then the middle 9500 are between 250 and 9750.
🗂️ See Subset a dataframe > Extract row(s) flash card.
The goal of Q1–Q3 was to show you, using simulations, where the 1.96 multipliers come from: the z-scores that define the edges of a 95% CI in a standard normal distribution. The actual z-scores you got in Q3 might be a bit different from 1.96, and that’s because of random sampling. If you did even more than 10,000 replications, you’d get values even closer to 1.96 (but don’t try it right now, it’d take too long to run).
Now things are getting practical again!
We’ll switch gears and use the \(\pm\) 1.96 multiplier to actually calculate the 95% CI for your outlook scores. For this calculation, we’ll use the sample mean and the known population SD. In real life, you wouldn’t know the population SD, so you would use the sample mean and the sample SD. But here, we have the population SD, so we will use it.
The formula for calculating the upper and lower bounds of the 95% confidence interval is
\[\bar{x} \pm 1.96 * SE\]
where \(\bar{x}\) is the sample mean and \(SE\) is the standard error of the mean. \(SE\) is computed as:
- \(SE = \frac{\sigma}{\sqrt{n}}\) if you know the population standard deviation \(\sigma\), or
- \(SE = \frac{s}{\sqrt{n}}\) if you only know the sample standard deviation \(s\).
In this case, we know the population SD: it’s \(\sigma = 25\).
First: read the data from the DAPR1 class survey (https://uoepsy.github.io/data/dapr1_2627_survey.csv).
🗂️ See Read in and inspect data flash card.
Next, using the formula above and our knowledge that the population standard deviation of outlook scores is \(\sigma = 25\): calculate the upper and lower bounds of the 95% confidence interval for the mean outlook score.
Write a sentence interpreting the 95% confidence interval you just calculated.
Check the lecture slides for how to interpret a confidence interval.
Finishing up
Once you’ve finished, don’t forget to:
- Save your R script
- Post the R script to the group discussion space on Learn, to ensure that everyone has access to it! (For step-by-step instructions on how to do this, click here)
