-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path07-STAT_220_InferenceSingleProportionPreLab-tidy.Rmd
More file actions
203 lines (136 loc) · 5.64 KB
/
Copy path07-STAT_220_InferenceSingleProportionPreLab-tidy.Rmd
File metadata and controls
203 lines (136 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
---
title: 'Inference for a single proportion pre-lab: tidy'
author: "Professor McNamara"
output:
html_document:
toc: true
toc_depth: 3
toc_float: true
---
We use three packages in this course: `Lock5Data`, `tidyverse` and `infer`. To load a package, you use the `library()` function, wrapped around the name of a package. I've put the code to load one package into the chunk below. Add the other two you need.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)
library(Lock5Data)
# put in the other two packages you need here
```
## Loading in data
As usual, we'll load the example data, `GSS_clean.csv`. It should be inside the `data` folder in your RStudio Cloud. We'll use the `read_csv()` function to read in the data.
```{r data-load}
GSS <- read_csv("data/GSS_clean.csv")
```
## Missing data
Let's make our lives easier by dropping NA values for the variable we'll be considering today,
```{r}
GSS <- GSS |>
drop_na(self_emp_or_works_for_somebody)
```
## Inference
So far, we've done inference using simulation methods (lab 5 was about the bootstrap and lab 6 was about randomization). Now, we're going to do inference using distributional approximations.
Today, we'll cover inference for a single proportion.
When we're doing inference about a single proportion, we approximate using the standard normal distribution, but we need a formula to approximate the standard error.
Let's review the formulas:
For one proportion, in a hypothesis test
$$
SE_{p_0}=\sqrt{\frac{p_0(1-p_0)}{n}}
$$
For one proportion, in a confidence interval,
$$
SE_{\hat{p}} = \sqrt{\frac{\hat{p}(1-\hat{p})}{n}}
$$
We can only use these formulas (and the Normal distribution) for inference if the conditions for inference are met. What are those conditions?
## One proportion
For this lab, we'll consider the proportion of people who are self-employed. We learned how to compute a proportion way back in lab 2. See if you can remember how to compute the sample proportion (also known as the point estimate).
```{r}
```
Okay, so that's a little more than 10% who say they are self-employed. But, is it significantly more? That's what hypothesis testing can help us answer.
### Hypothesis testing for one proportion
Say we wanted to know if the proportion of people who are self employed is greater than 10\%. We could write out our null and alternative hypotheses,
$$
H_0: p \leq 0.1 \\
H_A:p > 0.1
$$
Then, we could use R to help us perform a hypothesis test.
```{r}
GSS |>
prop_test(response = self_emp_or_works_for_somebody,
alternative = "greater", p = 0.1)
```
Notice that I have to specify the alternative (the options are "two-sided", "greater" or "less") and the null proportion I'm testing against.
What is our generic conclusion here?
What does that mean in context?
### Confidence interval for one proportion
Okay, so it's not significantly great than 10%. But, what are some other reasonable values we could have observed? That's what a confidence interval helps us determine.
Again, I could use `prop_test()` to help me,
```{r}
GSS |>
prop_test(response = self_emp_or_works_for_somebody, conf_int = TRUE)
```
How can we interpret this confidence interval?
## Appendix-- doing it "by hand"
Obviously, the `prop_test()` function helps us do these type of inferential tasks quickly! If we wanted to, we could do the work "by hand," using R as a calculator.
### Hypothesis test by hand
The first thing we would need is a z-score, which is our observed proportion minus our null proportion over the standard error,
$$
z = \frac{\hat{p}-p_0}{SE}
$$
and we'd need to use the appropriate formula for the standard error,
$$
SE_{p_0}=\sqrt{\frac{p_0(1-p_0)}{n}}
$$
We could use R as a calculator to find the standard error
```{r}
sqrt((0.1*0.9)/2261)
```
and then the z-score,
```{r}
(0.103-0.1)/0.006309152
```
Then we'd need to see how extreme 0.475 is in the context of the normal distribution. Based on the facts I know about the normal, I don't think it's going to be significant. But, I could find the p-value using `pnorm()`,
```{r}
1-pnorm(0.4754997)
```
Why did I need to do 1 minus the number?
We could also build this into a long data pipeline,
```{r}
GSS |>
group_by(self_emp_or_works_for_somebody) |>
summarize(n = n()) |>
mutate(prop = n / sum(n), total = sum(n)) |>
filter(self_emp_or_works_for_somebody == "Self-employed") |>
mutate(se = sqrt(prop * (1 - prop) / total)) |>
mutate(z_stat = (prop - 0.1) / se) |>
mutate(pvalue = pnorm(z_stat, lower.tail = FALSE))
```
### Confidence intervals by hand
To do a confidence interval, we need a slightly different standard error. We can refer to our formula,
$$
SE_{\hat{p}} = \sqrt{\frac{\hat{p}(1-\hat{p})}{n}}
$$
and use R as a calculator
```{r}
sqrt((0.103*(1-0.103))/2261)
```
To compute a confidence interval, we need one more thing, which is a critical value. In the case of a proportion, this will be a critical z-value, which you might have memorized (it's 1.96 for a 95\% confidence interval) or you may need to compute. Let's check to make sure I'm right about this 1.96 thing.
```{r}
qnorm(0.975)
```
Why did I use 0.975?
Now, we can compute our confidence interval by hand:
```{r}
0.103 - 1.96 * 0.00639
0.103 + 1.96 * 0.00639
```
We could also do this in a longer (but perhaps more readable?) way in a data pipeline,
```{r}
GSS |>
group_by(self_emp_or_works_for_somebody) |>
summarize(n = n()) |>
mutate(prop = n / sum(n), total = sum(n)) |>
filter(self_emp_or_works_for_somebody == "Self-employed") |>
mutate(
se = sqrt(prop * (1 - prop) / total),
critical_z = 1.96,
me = critical_z * se
) |>
mutate(low = prop - me, high = prop + me)
```