-
Notifications
You must be signed in to change notification settings - Fork 1
/
62_1way_chisq.Rmd
424 lines (290 loc) · 11.1 KB
/
62_1way_chisq.Rmd
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# 1-way Chi Square Test, "Goodness of Fit"
```{r}
library(tidyverse)
library(effectsize)
library(pander)
```
## Chi Squared Distribution
```{r, fig.width=4, fig.height=4}
data.frame(count = 0:50) %>%
dplyr::mutate(prob = dchisq(x = count, df = 1)) %>%
ggplot(aes(x = count,
y = prob)) +
geom_point() +
geom_segment(aes(xend = count,
y = 0,
yend = prob),
size = .1) +
theme_bw() +
scale_x_continuous() +
labs(x = "Count",
y = "Probability") +
theme(legend.position = "none")
```
```{r, fig.width=4, fig.height=4}
data.frame(count = 0:50) %>%
dplyr::mutate(prob = dchisq(x = count, df = 5)) %>%
ggplot(aes(x = count,
y = prob)) +
geom_point() +
geom_segment(aes(xend = count,
y = 0,
yend = prob),
size = .1) +
theme_bw() +
scale_x_continuous() +
labs(x = "Count",
y = "Probability") +
theme(legend.position = "none")
```
```{r, fig.width=4, fig.height=4}
data.frame(count = 0:50) %>%
dplyr::mutate(prob = dchisq(x = count, df = 20)) %>%
ggplot(aes(x = count,
y = prob)) +
geom_point() +
geom_segment(aes(xend = count,
y = 0,
yend = prob),
size = .1) +
theme_bw() +
scale_x_continuous() +
labs(x = "Count",
y = "Probability") +
theme(legend.position = "none")
```
```{r, fig.width=4, fig.height=4}
data.frame(count = 0:150) %>%
dplyr::mutate(prob = dchisq(x = count, df = 100)) %>%
ggplot(aes(x = count,
y = prob)) +
geom_point() +
geom_segment(aes(xend = count,
y = 0,
yend = prob),
size = .1) +
theme_bw() +
scale_x_continuous() +
labs(x = "Count",
y = "Probability") +
theme(legend.position = "none")
```
```{r}
data.frame(count = 0:150) %>%
dplyr::mutate(prob_2 = dchisq(x = count, df = 2)) %>%
dplyr::mutate(prob_5 = dchisq(x = count, df = 5)) %>%
dplyr::mutate(prob_20 = dchisq(x = count, df = 20)) %>%
dplyr::mutate(prob_100 = dchisq(x = count, df = 100)) %>%
tidyr::pivot_longer(cols = starts_with("prob"),
names_to = "df",
names_prefix = "prob_",
names_ptypes = list(df = factor()),
values_to = "prob") %>%
dplyr::mutate(mu = df %>%
as.character() %>%
as.numeric()) %>%
dplyr::mutate(df = paste0("df = ", df) %>%
factor(levels = c("df = 2",
"df = 5",
"df = 20",
"df = 100"))) %>%
ggplot(aes(x = count,
y = prob)) +
geom_point(alpha = .4) +
geom_segment(aes(xend = count,
y = 0,
yend = prob),
alpha = .4) +
theme_bw() +
scale_x_continuous() +
labs(x = "Count",
y = "Probability") +
theme(legend.position = "none") +
facet_wrap(. ~ df, nrow = 2, scales = "free_y") +
geom_vline(aes(xintercept = mu),
color = "red")
```
## Chi Squared Test
The `chisq.test()` function in the base R `stats` package can be used to perform a **Goodnes-of-Fit** or one-way Chi-Squared test. This assesses if the observed counts are significantly different from a given profile. The comparison or expected counts may be based on all levels being **equally likely** or **any other specification**.
**Steps:**
1. Enter the data as a 'concatinated vector' and change it to the class 'table'
2. Perform the test, saving the model fit
3. Extract the observed and expected counts
4. Extract the test output
5. Write up the methods & results
-------
## Null Hypothesis: Equally Likely
Frequently we will to test if there is any difference between the counts in groups. This omnibus test compares the counts in each group to the uniform distribution. This means the expected count in all groups is the same. The expected counts may be found by dividing the total sample by the number of groups.
### Ex. Senator Support
A Senator supports a bill favoring stem cell research. However, she realizes her vote could influence whether or not her constituents endorse her bid for re-election. She decides to vote for the bill only if 50% of her constituents support this type of research.
In a random survey of 200 constituents, 96 are in favor of stem cell research. Will the senator support the bill?
```{r}
tab_vote <- c(support = 96,
against = 104) %>%
as.table()
tab_vote %>%
addmargins()
```
**Defaults**
* Null Hypothesis: "equally likely" `p = c(1/k, 1/k, 1/k, ... 1/k)`
> k = number of categories
```{r}
fit_chisq_vote <- tab_vote %>%
chisq.test()
```
```{r}
fit_chisq_vote$observed
```
```{r}
fit_chisq_vote$expected
```
```{r}
fit_chisq_vote
```
### Ex. Books
**Question:**
Is there a difference in number of books checked out for different days of the week?
#### Enter observed counts
This example is given in the Barry Cohen textbook. The library counts the number of books checked out each day for a week. The counts are entered below.
```{r}
my_book_counts <- c(Monday = 20,
Tuesday = 14,
Wednesday = 18,
Thursday = 17,
Friday = 22,
Saturday = 29) %>%
as.table()
my_book_counts
```
#### Perform the test, saving the model fit
> **NOTE:** You do not need to declare any options inside the `chisq.test()` function, as the default is to use equally likely probabilities.
```{r}
fit_book_chisq_el <- my_book_counts %>%
chisq.test()
```
#### Extract the observed and expected counts
You can ask for the observed counts. These are just the data your entered into the test.
> **NOTE:** The observed counts MUST be whole numbers!
```{r}
fit_book_chisq_el$observed
```
You can also ask for the expected counts. The default is for each group to have the total sample divided equally.
> **NOTE:** The expected counts CAN be decimal values.
```{r}
fit_book_chisq_el$expected
```
You can use the code below to create a single table with both the observed and expected counts, as well as the total for each.
```{r}
rbind(Observed = fit_book_chisq_el$observed,
Expected = fit_book_chisq_el$expected,
Residual = fit_book_chisq_el$observed - fit_book_chisq_el$expected) %>%
as.table() %>%
addmargins(margin = 2) %>%
pander::pander()
```
#### Extract the test output
```{r}
fit_book_chisq_el
```
#### Visualize
```{r}
my_book_counts %>%
data.frame() %>%
ggplot(aes(x = Var1,
y = Freq)) +
geom_col(alpha = .4,
color = "black") +
geom_hline(yintercept = 20,
color = "red",
size = 2) +
theme_bw() +
labs(x = NULL,
y = "Observed Count")
```
#### Write-up
**Methods**
To assess if books are checked out uniformly throughout the week, a 1-way Chi Squared Goodness of Fit test was conducted.
**Results**
This one week provides no evidence that books are checked out more or less on any given day, $\chi^2 (5) = 6.70$, $p = .244$.
### Ex. M & M Colors
#### Enter observed counts
```{r}
tab_color_counts <- c(brown = 4,
yellow = 2,
red = 10,
green = 23,
blue = 14,
orange = 12) %>%
as.table()
tab_color_counts
```
#### Perform the test, saving the model fit
> **NOTE:** You do not need to declare any options inside the `chisq.test()` function, as the default is to use equally likely probabilities.
```{r}
fit_chisq_el <- tab_color_counts %>%
chisq.test()
```
#### Extract the observed and expected counts
You can ask for the observed counts. These are just the data your entered into the test.
> **NOTE:** The observed counts MUST be whole numbers!
```{r}
fit_chisq_el$observed
```
You can also ask for the expected counts. The default is for each group to have the total sample divided equally.
> **NOTE:** The expected counts CAN be decimal values.
```{r}
fit_chisq_el$expected
```
You can use the code below to create a single table with both the observed and expected counts, as well as the total for each.
```{r}
rbind(Observed = fit_chisq_el$observed,
Expected = fit_chisq_el$expected,
Residual = fit_chisq_el$observed - fit_chisq_el$expected) %>%
as.table() %>%
addmargins(margin = 2) %>%
pander::pander()
```
#### Extract the test output
```{r}
fit_chisq_el
```
#### Write-up
**Methods**
To assess if my bag of M & M's supports the hypothesis that all colors are equally produced, a 1-way Chi Squared Goodness of Fit test was conducted.
**Results**
Statistically significant evidence was found that **not all the colors are equally** produced, $\chi^2 (5) = 26.29$, $p < .001$. There were fewer brown and yellow, as well as more green, than would be expected.
## Null Hypothesis: a Specific Distribution
### Ex. M & M Colors
Years ago, the Mars company used to post the 'color breakdown' for each type of M & M it produced on its website. The information has since been removed, but the last time I checked it claim that plain, milk chocolate M & M's consisted of:
* 13% brown
* 14% yellow
* 13% red
* 16% green
* 24% blue
* 20% orange
#### Perform the test, saving the model fit
> **NOTE:** You DO need to declare any options inside the `chisq.test()` function, as the default is to use equally likely probabilities.
To compare observed counts to a specified distribution, you need to add that breakdown of 100% (or probability of 1.0) inside the `chisq.test()`. Make sure that your probabilities add up to EXACTLY ONE! If ther are off by even .00001, it will not run and give and error instead.
```{r}
fit_chisq_hist <- tab_color_counts %>%
chisq.test(p = c(.13, .14, .13, .16, .24, .20))
```
#### Extract the observed and expected counts
You can use the code below to create a single table with both the observed and expected counts, as well as the total for each.
```{r}
rbind(Observed = fit_chisq_hist$observed,
Expected = fit_chisq_hist$expected,
Residual = fit_chisq_hist$observed - fit_chisq_hist$expected) %>%
as.table() %>%
addmargins(margin = 2) %>%
pander::pander()
```
#### Extract the test output
```{r}
fit_chisq_hist
```
#### Write-up
**Methods**
To assess if my bag of M & M's supports the reported color breakdown that used to be listed on teh Mars website, a 1-way Chi Squared Goodness of Fit test was conducted.
**Results**
Statistically significant evidence was found that the c**olor breakdown has changed** since the website posting, $\chi^2 (5) = 23.67$, $p < .001$. There were fewer brown and yellow, as well as more green, than would be expected if it were still the same.