You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 05-infer/01-lesson/05-01-lesson.Rmd
+26-22Lines changed: 26 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -235,7 +235,7 @@ p_hat_happy
235
235
236
236
We learn that around 77% of our sample is "happy". If this were a simple random sample from the American population, this would be a good estimate of the percent of all Americans that are very happy, but it's not a sure thing since we only asked a small proportion of them.
237
237
238
-
## How is does the GSS select respondents?
238
+
## How does the GSS select respondents?
239
239
240
240
Remember that not all randomly sampled data is the same -- a sample can be a simple random sample, a stratified sample, a cluster sample, or something more complex. The respondents to the GSS come from a complex survey design and appropriate inference for these data would account for this survey design.
To implement this, we start with our gss2016 data and then *specify* that we will focus on the happy column. Next we *generate* 500 replicate data sets through bootstrapping and for each one *calculate* the proportion that are "happy".
344
344
345
-
When we print this new object, we see we now have a data frame that contains 500 p-hats.
345
+
When we print this new object, we see we now have a data frame that contains 500 $\hat{p}\text{s}$ (p-hats).
346
346
347
347
```{r echo = TRUE}
348
348
boot_dist_happy <- gss2016 |>
@@ -512,7 +512,7 @@ boot_1_consci |>
512
512
513
513
### Constructing the CI
514
514
515
-
You've seen one example of how p-hat can vary upon resampling, but we need to do this many many times to get a good estimate of its variability. Here you will compute a full bootstrap distribution to estimate the standard error (SE) that will be used to form a confidence interval. You'll use an additional verb from infer, `calculate()`, to streamline this process of calculating many statistics from many data sets.
515
+
You've seen one example of how $\hat{p}$ can vary upon resampling, but we need to do this many many times to get a good estimate of its variability. Here you will compute a full bootstrap distribution to estimate the standard error (SE) that will be used to form a confidence interval. You'll use an additional verb from infer, `calculate()`, to streamline this process of calculating many statistics from many data sets.
516
516
517
517
Take a moment to inspect the output of calculate. This function reduces your data frame to just two columns: one for the "stat"s and another for the "replicate" they correspond to.
518
518
@@ -665,7 +665,7 @@ Let's look deeper into this by starting with the confidence interval that we've
665
665
666
666
### Happiness in 2016
667
667
668
-
The data from which this interval was constructed is from 2016, and we can plot both p-hat and the resulting interval on a number line here. To understand what is meant by confident, we need to consider how this interval fits into the big picture.
668
+
The data from which this interval was constructed is from 2016, and we can plot both $\hat{p}$ and the resulting interval on a number line here. To understand what is meant by confident, we need to consider how this interval fits into the big picture.
of the same size from that population and come up with a new p-hat and a new interval. It wouldn't be the same as our first, but it'd likely be similar.
716
+
of the same size from that population and come up with a new $\hat{p}$ and a new interval. It wouldn't be the same as our first, but it'd likely be similar.
@@ -853,7 +853,7 @@ In the following exercises you'll get the chance to explore these factors and ho
853
853
854
854
We learned that for a 95% confidence interval (a confidence level of .95), if we were to take many samples of the same size and compute many intervals, we would expect 95% of the resulting intervals to contain the parameter. Based on the set of confidence intervals plotted here, what is your best guess at the confidence level used in these intervals?
855
855
856
-
The population proportion is represented by the p in the cloud and the dotted line and each confidence interval is represented by a segment that extends out from it's p-hat. Intervals that capture the true value are in green; those that miss it are in red.
856
+
The population proportion is represented by the p in the cloud and the dotted line and each confidence interval is represented by a segment that extends out from its $\hat{p}$. Intervals that capture the true value are in green; those that miss it are in red.
You just saw the effect that _sample size_ can have on inference, but that's not the only variable in play here. Let's return now to our full data set and see what happens to the SE when we consider a category that has a different _population proportion_, p.
1092
+
You just saw the effect that _sample size_ can have on inference, but that's not the only variable at play here. Let's return now to our full data set and see what happens to the SE when we consider a category that has a different _population proportion_, p.
1087
1093
1088
1094
Remember that the proportion of "High" confidence in science in 2016 was pretty close to 0.50.
One when there was less data, and the other where p-hat was low.
1165
+
One when there was less data, and the other where $\hat{p}$ was low.
1160
1166
1161
1167
The different values that you observed demonstrate some important properties of standard errors: they will increase when n is small and also when p is close to 0.5.
That approximation is the normal distribution, also known as the bell curve. A useful result in mathematics says that if you have independent observations and a sufficiently large sample size, then p-hat will follow a normal distribution with a known standard deviation. This distribution is called the sampling distribution of p-hat and it's very similar to the bootstrap distribution in that it captures the variability of our estimate across many possible data sets.
1208
+
That approximation is the normal distribution, also known as the bell curve. A useful result in mathematics says that if you have independent observations and a sufficiently large sample size, then $\hat{p}$ will follow a normal distribution with a known standard deviation. This distribution is called the sampling distribution of $\hat{p}$ and it's very similar to the bootstrap distribution in that it captures the variability of our estimate across many possible data sets.
1203
1209
1204
1210
### Standard deviation
1205
1211
@@ -1218,13 +1224,13 @@ What does "n is large" mean?
1218
1224
- $n \times \hat{p} \gt 10$
1219
1225
- $n \times(1 - \hat{p}) \gt 10$
1220
1226
1221
-
When applying this result in practice, it's important to be sure that the assumptions of independence and a large sample aren't wildly off base. To assess independence, you need to consider the method by which the data was collected. A handy rule of thumb to determine if your sample size is large enough is to check that n times p-hat and n times 1 - p-hat are both greater than or equal to 10.
1227
+
When applying this result in practice, it's important to be sure that the assumptions of independence and a large sample aren't wildly off base. To assess independence, you need to consider the method by which the data was collected. A handy rule of thumb to determine if your sample size is large enough is to check that n times $\hat{p}$ and n times 1 - $\hat{p}$ are both greater than or equal to 10.
1222
1228
1223
1229
### Calculating standard error: approximation
1224
1230
1225
-
OK, let's try our hand at using this shortcut to find the standard error for the proportion of people that were happy. Let's recompute p-hat, then ask the number of rows in the `gss2016`. That's the sample size, `n`.
1231
+
OK, let's try our hand at using this shortcut to find the standard error for the proportion of people that were happy. Let's recompute $\hat{p}$, then ask the number of rows in the `gss2016`. That's the sample size, `n`.
1226
1232
1227
-
Let's check the rule-of-thumb to see if our sample size is large enough by multiplying n times p-hat and n times 1 minus p-hat. This gives 116 and 35, so our sample size should be sufficiently large. We also know that the gss uses random sampling to draw these observations, so is safe to assume that one person's answer is independent of the next.
1233
+
Let's check the rule-of-thumb to see if our sample size is large enough by multiplying n times $\hat{p}$ and n times 1 minus $\hat{p}$. This gives 116 and 35, so our sample size should be sufficiently large. We also know that the gss uses random sampling to draw these observations, so is safe to assume that one person's answer is independent of the next.
1228
1234
1229
1235
```{r echo=TRUE}
1230
1236
p_hat_happy <- gss2016 |>
@@ -1245,7 +1251,7 @@ SE_happy_approx
1245
1251
1246
1252
### Calculating standard error: computation
1247
1253
1248
-
How does it compare to our original computational approach using the bootstrap? Well, if we construct the bootstrap distribution for p-hat, then summarize it by finding it's standard deviation, we estimate a standard error of about 0.032. Those are remarkably similar values! Let's go a step farther.
1254
+
How does it compare to our original computational approach using the bootstrap? Well, if we construct the bootstrap distribution for $\hat{p}$, then summarize it by finding it's standard deviation, we estimate a standard error of about 0.035. Those are remarkably similar values! Let's go a step farther.
1249
1255
1250
1256
```{r echo=TRUE}
1251
1257
boot_dist_happy <- gss2016 |>
@@ -1260,18 +1266,16 @@ SE_happy_boot
1260
1266
1261
1267
### Shape of sampling distributions
1262
1268
1263
-
Let's also take a look at the shape of this bootstrap distribution. A density plot suggests that it's unimodal and symmetric. Let's add a layer to this plot that contains the normal curve that's centered at p-hat has uses the equation to find the standard deviation. And yes, let's make that curve purple.
1269
+
Let's also take a look at the shape of this bootstrap distribution. A density plot suggests that it's unimodal and symmetric.
1264
1270
1265
1271
```{r echo=TRUE}
1266
1272
ggplot(boot_dist_happy, aes(x = stat)) +
1267
1273
geom_density()
1268
1274
```
1269
1275
1270
-
We see that the normal approximation looks fairly similar to the density curve of our bootstrap distribution. This will be a recurring theme: that when an approximation method exists, it will tend to give very similar results to the computational method when the assumptions of that approximation are reasonable.
1271
-
1272
1276
###
1273
1277
1274
-
Let's add a layer to this plot that contains the normal curve that's centered at p-hat has uses the equation to find the standard deviation. And yes, let's make that curve purple.
1278
+
Let's add a layer to this plot that contains the normal curve that's centered at $\hat{p}$ has uses the equation to find the standard deviation. And yes, let's make that curve purple.
1275
1279
1276
1280
```{r echo=TRUE}
1277
1281
ggplot(boot_dist_happy, aes(x = stat)) +
@@ -1347,7 +1351,7 @@ p_hat_meta
1347
1351
Now we'll construct the confidence interval around this proportion.
1348
1352
1349
1353
- Check the rules-of-thumb for the normal distribution being a decent approximation.
1350
-
- Calculate the standard error using the approximation formula $\sqrt{\frac{\hat{p} \times (1 - \hat{p})}{n}}$.
1354
+
- Calculate the standard error using the approximation formula $\sqrt{\frac{\hat{p} (1 - \hat{p})}{n}}$.
1351
1355
- Use `SE_meta_approx` to form a confidence interval for `p_hat`. The limits should be two standard errors either side of `p_hat`.
0 commit comments