-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path06_Frequencies.Rmd
292 lines (173 loc) · 7.67 KB
/
06_Frequencies.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
# Frequencies
## Binomial Test
### Example
An example from Hays (1974, pp. 190-192):
"Think of a hypothetical study of this question: 'If a human is subjected to a stimulus below his threshold of conscious awareness, can his behavior somehow still be influenced by the presence of the stimulus?' The experimental task is as follows: the subject is seated in a room in front of a square screen divided into four equal parts. He is instructed that his task is to guess in which part of the screen a small, very faint, spot of light is thrown."
Under the null hypothesis H~0~, the number of correct guesses is expected to be 1/4 of the trials N. The alternative hypothesis H~1~ is that the number of correct guesses is larger than 1/4 of the trials N.
The subject obtained 7 correct guesses T out of 10 trials N.
What is the p-value of this result under H~0~?
p = 0.25
N = 10
T = 7
### Results Overview {#ResultsBinom}
```{r echo=F}
ResultsBinomial <- matrix(c(0.0035,0.0035, 0.004, 0.0035, 0.004, 0.0035), ncol=6)
colnames(ResultsBinomial) <- c('By Hand', 'JASP', 'SPSS', 'SAS', 'Minitab', 'R')
rownames(ResultsBinomial) <- c('P')
knitr::kable(head(ResultsBinomial, 20), caption = "Result Overview Binomial Test", booktabs = T)
```
### By Hand {#ByHandBinom}
Calculations by hand can be found in Hays, 1974, pp. 190-192.
Result: P = 0.0035
### JASP {#jaspBinom}
```{r BinomialTestJASP, echo=FALSE, fig.cap="\\label{fig:BinomialTestJASP}JASP Output for Binomial Test"}
knitr::include_graphics('Screenshots/Binomial Test/BinomialTestJASP.PNG')
```
### SPSS {#spssBinom}
```{R eval=F}
DATASET NAME DataSet1 WINDOW=FRONT.
*Nonparametric Tests: One Sample.
NPTESTS
/ONESAMPLE TEST (Guesses) BINOMIAL(TESTVALUE=0.25 SUCCESSCATEGORICAL=FIRST SUCCESSCONTINUOUS=CUTPOINT(MIDPOINT))
/MISSING SCOPE=ANALYSIS USERMISSING=EXCLUDE
/CRITERIA ALPHA=0.05 CILEVEL=95.
```
```{r BinomialTestSPSS, echo=FALSE, fig.cap="\\label{fig:BinomialTestSPSS}SPSS Output for Binomial Test"}
knitr::include_graphics('Screenshots/Binomial Test/BinomialTestSPSS.PNG')
```
### SAS {#sasBinom}
```{R eval=F}
PROC Freq data=WORK.IMPORT;
tables Guesses / binomial(p=.25 level=2);
exact binomial;
run;
```
```{r BinomialTestSAS, echo=FALSE, fig.cap="\\label{fig:BinomialTestSAS}SAS Output for Binomial Test"}
knitr::include_graphics('Screenshots/Binomial Test/BinomialTestSAS.PNG')
```
### Minitab {#minitabBinom}
```{r BinomialTestMinitab, echo=FALSE, fig.cap="\\label{fig:BinomialTestMinitab}Minitab Output for Binomial Test"}
knitr::include_graphics('Screenshots/Binomial Test/BinomialTestMinitab.PNG')
```
### R {#rBinom}
```{r}
binom.test(7, 10, p = 0.25, alternative = "greater")
```
### Remarks {#remarksBinom}
All differences in results between the software and hand calculation are due to rounding.
### References {#refBinom}
Hays, W. L. (1974). *Statistics for the social sciences (2nd Ed.)*. New York, US: Holt, Rinehart and Winston, Inc.
## Multinomial Test / Chi-square Goodness of Fit Test
### Example
Think of colored marbles mixed together in a box, where the following probability distribution holds:
```{r echo=F}
Pdist <- data.frame('Color' = c('Black', 'Red', 'White'), "p" = c(0.40,0.30, 0.30))
knitr::kable(head(Pdist, 20), caption = "Probability Distribution for Multinomial Test Example", booktabs = T)
```
Now suppose that 10 marbles were drawn at random and with replacement. The samples shows 2 black, 3 red, and 5 white.
```{r echo=F}
MNsample <- data.frame('Color' = c('Black', 'Red', 'White'), "Count" = c(2,3,5), "Expected" = c(4,3,3))
knitr::kable(head(MNsample, 20), caption = "Sample for Multinomial Test Example", booktabs = T)
```
### Results Overview {#ResultsMultinom}
```{r echo=F}
ResultsMNT <- matrix(c(rep(2.333,5)), ncol=5, byrow = T)
colnames(ResultsMNT) <- c('JASP', 'SPSS', 'SAS', 'Minitab', 'R')
rownames(ResultsMNT) <- "$\\chi ^2$"
knitr::kable(head(ResultsMNT, 20), caption = "Result Overview Multinomial Test", booktabs = T)
```
### JASP {#jaspMultinom}
```{r mntJASP, echo=FALSE, fig.cap="\\label{fig:mntJASP}JASP Output for Multinomial Test"}
knitr::include_graphics('Screenshots/Multinomial Test/mntJASP.PNG')
```
### SPSS {#spssMultinom}
```{r eval=F}
DATASET ACTIVATE DataSet1.
NPAR TESTS
/CHISQUARE=Numbered
/EXPECTED=4 3 3
/MISSING ANALYSIS.
```
```{r mntSPSS, echo=FALSE, fig.cap="\\label{fig:mntSPSS}SPSS Output for Multinomial Test"}
knitr::include_graphics('Screenshots/Multinomial Test/mntSPSS.PNG')
```
### SAS {#sasMultinom}
```{r eval=F}
PROC FREQ DATA = chisquared;
TABLES Sex*Preference / chisq;
run;
```
```{r mntSAS, echo=FALSE, fig.cap="\\label{fig:mntSAS}SAS Output for Multinomial Test"}
knitr::include_graphics('Screenshots/Multinomial Test/mntSAS.PNG')
```
### Minitab {#minitabMultinom}
```{r mntMinitab, echo=FALSE, fig.cap="\\label{fig:mntMinitab}Minitab Output for Multinomial Test"}
knitr::include_graphics('Screenshots/Multinomial Test/mntMinitab.PNG')
```
### R {#rMultinom}
```{r}
chisq.test(MNsample$Count, p = Pdist$p)
```
### Remarks {#remarksMultinom}
All differences in results between the software are due to rounding.
## Chi-Squared-Test
An example from Hays (1974, pp. 728-731):
"For example, suppose that a random sample of 1-- school children is drawn. Each child is classified in two ways: the first attribute is the sex of the child, with two possible categories: [Male, Female].
The second attribute [...] is the stated preference of a child for two kinds of reading materials: [Fiction, Nonfiction]. [...] The data might, for example, turn out to be:"
```{r echo=F}
chiSquare.data <-matrix(c(19,32,29,20), ncol = 2, byrow = T)
colnames(chiSquare.data) <- c("Male", "Female")
rownames(chiSquare.data) <- c("Fiction", "Nonfiction")
knitr::kable(head(chiSquare.data, 20), caption = "Data for Chi-Squared-Test", booktabs = T)
```
### Results Overview {#ResultsChisquare}
```{r echo=F}
ResultsCST <- matrix(c(4.83, 4.8145, 4.814, 4.8145, 4.814, 4.8145), ncol=6, byrow = T)
colnames(ResultsCST) <- c('By Hand', 'JASP', 'SPSS', 'SAS', 'Minitab', 'R')
rownames(ResultsCST) <- "$\\chi ^2$"
knitr::kable(head(ResultsCST, 20), caption = "Result Overview Chi-Squared-Test", booktabs = T)
```
### By Hand {#ByHandChisquare}
Calculations by hand can be found in Hays, 1974, pp. 728-731.
Result: $\chi ^2$ = 4.83
Significant for $\alpha$ = .05 or less
### JASP {#jaspChisquare}
```{r cstJASP, echo=FALSE, fig.cap="\\label{fig:cstJASP}JASP Output for Chi-Squared-Test"}
knitr::include_graphics('Screenshots/Chi Squared/ChiSquaredJASP.PNG')
```
### SPSS {#spssChisquare}
```{r eval=F}
CROSSTABS
/TABLES=Sex BY Preference
/FORMAT=AVALUE TABLES
/STATISTICS=CHISQ
/CELLS=COUNT
/COUNT ROUND CELL.
```
```{r cstSPSS, echo=FALSE, fig.cap="\\label{fig:cstSPSS}SPSS Output for Chi-Squared-Test"}
knitr::include_graphics('Screenshots/Chi Squared/ChiSquaredSPSS.PNG')
```
### SAS {#sasChisquare}
```{r eval=F}
PROC FREQ DATA = chisquared;
TABLES Sex*Preference / chisq;
run;
```
```{r cstSAS, echo=FALSE, fig.cap="\\label{fig:cstSAS}SAS Output for Chi-Squared-Test"}
knitr::include_graphics('Screenshots/Chi Squared/ChiSquaredSAS.PNG')
```
### Minitab {#minitabChisquare}
```{r cstMinitab, echo=FALSE, fig.cap="\\label{fig:cstMinitab}Minitab Output for Chi-Squared-Test"}
knitr::include_graphics('Screenshots/Chi Squared/ChiSquaredMinitab.PNG')
```
### R {#rChisquare}
```{r echo=F}
chisquare.data2 <- read.csv("Datasets/chisquare2.csv", sep=",")
```
```{r}
chisq.test(chiSquare.data, correct = F)
```
### Remarks {#remarksChisquare}
All differences in results between the software and hand calculation are due to rounding.
### References {#refChisquare}
Hays, W. L. (1974). *Statistics for the social sciences (2nd Ed.)*. New York, US: Holt, Rinehart and Winston, Inc.