-
Notifications
You must be signed in to change notification settings - Fork 7
/
26-Testing-OneProportion.Rmd
1747 lines (1378 loc) · 70.6 KB
/
26-Testing-OneProportion.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Hypothesis tests: one proportion {#TestOneProportion}
<!-- Introductions; easier to separate by format -->
```{r, child = if (knitr::is_html_output()) {'./introductions/26-Testing-OneProportion-HTML.Rmd'} else {'./introductions/26-Testing-OneProportion-LaTeX.Rmd'}}
```
<!-- Define colours as appropriate -->
```{r, child = if (knitr::is_html_output()) {'./children/coloursHTML.Rmd'} else {'./children/coloursLaTeX.Rmd'}}
```
## Introduction: rolling dice {#ProportionTestIntro}
\index{Hypothesis testing!one proportion|(}
<div style="float:right; width: 222x; border: 1px; padding:10px"><img src="OtherImages/SmiffyDice-Rotated.png" width="200px"/></div>
`r if (knitr::is_html_output()) '<!--'`
\begin{wrapfigure}[3]{R}{.30\textwidth} % The first optional input is the number if lines allowed for the inage to be placed in
\centering%
\vspace{-16pt}% This removes some white space
\includegraphics[width=.27\textwidth]{OtherImages/SmiffyDice-Rotated.png}%
\end{wrapfigure}
`r if (knitr::is_html_output()) '-->'`
When in a toy store one day (for my children, of course), I saw 'loaded dice' for sale.
The packaging claimed <span style="font-variant:small-caps;">one loaded \& one normal</span>.
I bought two sets!
However, there was no indication as to *which* die was the loaded die.
How could I determine which of the dice was loaded?
That is, how could I make a *decision* about which die was loaded?
For a die that is *not* loaded, the population proportion of rolling any face of the die is $p = 1/6$.
So, for example, the population proportion of rolls that show a
`r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}`
is $p = 1/6$, using the classical approach to probability.\index{Probability!classical approach}
In any *sample* of rolls, however, the proportion of rolls showing a
`r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}`
would vary due to sampling variation, but would be approximately $\hat{p} = 1/6$ with a fair die.
Suppose I rolled one die a certain number of times (say, $n = 50$\ times), then determined the value of the sample proportion.
The sample proportion of rolls that show a
`r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}`
is unlikely to be *exactly* $1/6$ (the population proportion).
If the observed value of $\hat{p}$ was not exactly $1/6$, two possible reasons could explain this discrepancy:
* I was rolling the *fair* die (with $p = 1/6$), and the discrepancy between the *population* and *sample* proportions was simply due to sampling variation.
* I was rolling the *loaded* die (with $p \ne 1/6$), and the discrepancy between the *population* and *sample* proportion simply reflected this.
If I observed an unusually small or unusually large sample proportion of rolls that showed a
`r if (knitr::is_latex_output()) {
'\\largedice{1},'
} else {
'<span class="larger-die">⚀</span>,'
}`
I would suspect that I had the loaded die: I was observing something unusual from a fair die.
This is exactly the decision-making process seen in Chap.\ \@ref(MakingDecisions).
More formally then, the decision-making process (Chap.\ \@ref(MakingDecisions)) could proceed as follows:
* Make an *assumption* about the parameter (Sect.\ \@ref(Assumption)): assume I have a fair die, so that $p = 1/6$, where $p$ is the population proportion of rolls that show a
`r if (knitr::is_latex_output()) {
'\\largedice{1}.'
} else {
'<span class="larger-die">⚀</span>.'
}`
* Describe the *expectations* of the statistic (Sect.\ \@ref(ExpectationOf)): describe what value of the *sample* proportion $\hat{p}$ could reasonably be expected from a fair die.
* Take the sample *observations* (Sect.\ \@ref(Observation)): roll the die many times to find a value of $\hat{p}$.
* Make a *decision* (Sect.\ \@ref(MakeDecision)) based on what is observed in the sample.
Using this decision-making process (Fig.\ \@ref(fig:DecisionFlowDice)), I could decide if the die I had rolled seemed to be the fair die.
For one specific die, I am asking the decision-making RQ:
> For this die, is the population proportion of rolls that show a
`r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}`
equal to\ $1/6$?
```{r DecisionFlowDice, fig.cap = "A way to make decisions for the dice example.", fig.align="center", out.width='100%', fig.width = 9.5, fig.height = 4}
source("R/showDecisionMaking.R")
showDecisionMaking(populationText = expression( atop(bold(Assume)~the,
die~is~fair)),
expectationText = expression(atop(bold(Expect)~to~find,
about~1/6~rolls~show~a~1)),
oneSampleText = expression( atop(Roll~die,
many~times) ),
oneStatisticText = expression( atop(One~observed,
value~of~hat(italic(p))) ),
showQuestionMark = TRUE
)
```
Answering a decision-making RQ such as this requires a *hypothesis test*.
The process requires being able to describe what value of the *sample* proportion $\hat{p}$ could reasonably be expected from a fair die, with $p = 1/6$ (that is, Step\ 3 of the decision-making process).
::: {.tipBox .tip data-latex="{iconmonstr-info-6-240.png}"}
$p$ refers to the *population* proportion, and\ $\hat{p}$ refers to a *sample* proportion.
:::
## Rolling dice: the sampling distribution of $\hat{p}$ {#SamplingDistributionKnownpHT}
\index{Sampling distribution!one proportion, known\ $p$ (CI)}
<div style="float:right; width: 222x; border: 1px; padding:10px">
<img src="Illustrations/pexels-skitterphoto-705171.jpg" width="200px"/>
</div>
When a fair, six-sided die is rolled $50$\ times, what proportion of the rolls will produce a
`r if (knitr::is_latex_output()) {
'\\largedice{1}?'
} else {
'<span class="larger-die">⚀</span>?'
}`
That is, what will be the value of the *sample proportion* $\hat{p}$?
Of course, no-one knows, because the sample proportion will not be the same for every sample of $50$\ rolls.
The sample proportion *varies* from sample to sample: *sampling variation* exists and is described by the *sampling distribution*.
As seen in Chap.\ \@ref(SamplingVariation), the sample statistic often varies with a normal distribution (whose standard deviation is called the *standard error*).
However, being more specific about the details of this sampling distribution (i.e., the mean and standard deviation describing the normal model) is useful.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
Remember: studying a sample leads to the following observations:
\vspace{-2ex}
* Every sample is likely to be different.
* We observe just one of the many possible samples.
* Every sample is likely to yield a different value for the statistic.
* We observe just one of the many possible values for the statistic.
\vspace{-2ex}
Since many values for the sample proportion are possible, the values of the sample proportion vary (called *sampling variation*) and have a *distribution* (called a *sampling distribution*).
:::
To better understand the sampling distribution for the proportion of rolls that show a
`r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}`
in $50$\ rolls of a die, statistical theory could be used, or thousands of repetitions of a sample of\ $50$ rolls could be performed, or a computer could *simulate* many samples of $50$\ rolls (as for a roulette wheel in Sect.\ \@ref(SamplingDistributionProportions)).
Here, the *population proportion* of rolls showing a
`r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}`
is $p = 1/6$ (using the classical approach to probability).\index{Probability!classical approach}
Each sample of $n = 50$ rolls produces a *sample* proportion, denoted by\ $\hat{p}$, which varies from sample to sample.
<!-- For these ten samples, the proportion of even rolls ranged from $\hat{p} = 0.32$ to $\hat{p} = 0.60$. -->
These sample proportions would be expected to vary around $p = 1/6$ (the *population proportion*): some values of $\hat{p}$ would be larger than\ $p$ and some smaller than\ $p$.
The value of the sample proportion in\ $50$ rolls could be *very* small or *very* high by chance, but we wouldn't expect to see that very often.
The sample proportions exhibit sampling variation, and the *amount* of sampling variation is quantified using a *standard error*.
Suppose a fair die was rolled $50$\ times, and this random procedure \index{Random procedure} was repeated *thousands* of times, and the proportion of rolls that showed a
`r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}`
was recorded for every one of those thousands of sets of $50$\ rolls.
These thousands of sample proportions $\hat{p}$ (one from every sample of $n = 50$\ rolls) could be shown using a
`r if (knitr::is_latex_output()) {
'histogram (Fig.\\ \\@ref(fig:RollDiceHistFigHT)).'
} else {
'histogram; see the animation below.'
}`
<center>
```{r fig.show="animate", RollDiceHistHTML, animation.hook="gifski", interval=0.4, loop=FALSE, dev=if (is_latex_output()){"pdf"}else{"png"}}
if (knitr::is_html_output()){
set.seed(99100991)
num.rolls <- 50
num.sims <- 1000
print_Histo <- rep(FALSE, num.sims)
print_Histo[ c( 1:10,
seq(24, num.sims, 25) + 1,
num.sims) ] <- TRUE
prop.even <- array(dim = num.sims)
p.die <- 1/6
se.die <- sqrt( p.die * (1 - p.die) / num.rolls)
for (i in 1:num.sims){
roll <- sample(1:6, num.rolls,
replace = TRUE)
prop.even[i] <- sum( roll == 1 )/num.rolls ### sum( roll/2 == floor(roll/2)) / num.rolls
#Print every nth histogram only
if (print_Histo[i]){
out <- hist( prop.even,
breaks = seq(0.05, 0.95, by = 0.02) - 0.03,
las = 1,
ylim = c(0, 250),
xlim = c(0, 1),
col = plot.colour,
main = paste("Histogram of sample proportions\nSet number:", i),
xlab = "Proportion of the 50 rolls showing a one",
sub = paste("(For this sample: proportion of rolls showing a one is ",
format(round(prop.even[i], 2), nsmall = 2),
")",
sep = "" ),
ylab = "",
right = FALSE,
axes = FALSE)
axis(side = 1)
#axis(side = 2,
# las = 1)
points(prop.even[i], 0,
pch = 19,
col = plot.colour0)
xx <- seq(0, 1,
length = 500)
yy <- dnorm(xx,
mean = p.die,
sd = se.die )
yy <- yy/max(yy) * max(out$count)
lines(yy ~ xx,
col = "grey",
lwd = 2)
}
}
}
```
</center>
(ref:DieRollSamplingDist) The proportion of rolls that show a `r if (knitr::is_latex_output()) {
'\\largedice{1},'
} else {
'<span class="larger-die">⚀</span>,'
}` $\hat{p}$, is not the same for every sample of $50$ rolls; it varies around a mean of $p = 1/6$ (shown by the dot). The solid line is the normal distribution used to model the sampling distribution. The sampling distribution is an approximate normal distribution; it shows a model of how the proportion of rolls showing a `r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}` varies, when a die is rolled $50$ times.
```{r RollDiceHistFigHT, fig.align="center", fig.height=3, fig.width=7.5, out.width='85%', fig.cap="(ref:DieRollSamplingDist)" }
if (knitr::is_latex_output()){
set.seed(99100991)
num.rolls <- 50
num.sims <- 1000
prop.even <- array(dim = num.sims)
p.die <- 1/6
se.die <- sqrt( p.die * (1 - p.die) / num.rolls)
for (i in 1:num.sims){
roll <- sample(1:6, num.rolls,
replace = TRUE)
prop.even[i] <- sum(roll == 1) / num.rolls
}
par( mar = c(4, 1, 4, 1) )
out <- hist( prop.even,
breaks = seq(0.05, 0.95, by = 0.02) - 2/15,
las = 1,
ylim = c(0, 250),
xlim = c(0, 1),
col = plot.colour,
main = paste("Histogram of sample proportions\nfrom thousands of simulations of",
num.rolls,
"rolls"),
xlab = "Proportion of the 50 rolls showing a one",
ylab = "",
right = FALSE,
axes = FALSE)
axis(side = 1,
at = seq(0, 1, by = 0.1))
#axis(side = 2,
# las = 1)
xx <- seq(0, 1,
length = 500)
yy <- dnorm(xx,
mean = p.die,
sd = se.die )
yy <- yy/max(yy) * max(out$count)
lines(yy ~ xx,
col = grey(0.3),
lwd = 2)
points(x = 1/6,
y = 0,
pch = 19)
}
```
In fact, the sampling distribution of\ $\hat{p}$ was described in Def.\ \@ref(def:SamplingDistPropCI) (and repeated in Def.\ \@ref(def:SamplingDistPropHT)).
The sample proportions are described by
* an approximate normal distribution,
* centred around the *sampling mean*, with a value of $p = 1/6$,
* with a standard deviation, called the *standard error* $\text{s.e.}(\hat{p})$, of
\begin{equation}
\text{s.e.}(\hat{p})
= \sqrt{ \frac{p\times(1 - p)}{n} }
= \sqrt{ \frac{1/6\times(1 - 1/6)}{50} }
= `r round(se.die, 5)`.
(\#eq:StdErrorExampleDieHT)
\end{equation}
`r if (knitr::is_html_output()) '<!--'`
::: {.definition #SamplingDistPropHT name="Sampling distribution of a sample proportion with $p$ known"}
`r if (knitr::is_html_output()) '-->'`
`r if (knitr::is_latex_output()) '<!--'`
::: {.definition #SamplingDistPropHT name="Sampling distribution of a sample proportion with the population proportion known"}
`r if (knitr::is_latex_output()) '-->'`
For a known value of $p$, the *sampling distribution of the sample proportion* is (when certain conditions are met; Sect.\ \@ref(ValidityProportions)) described by
* an approximate normal distribution,
* centred around the sampling mean whose value is\ $p$,
* with a standard deviation (called the *standard error* of\ $\hat{p}$), denoted $\text{s.e.}(\hat{p})$, whose value is
\begin{equation}
\text{s.e.}(\hat{p}) = \sqrt{\frac{ p \times (1 - p)}{n}},
(\#eq:StdErrorPknownHT)
\end{equation}
where\ $n$ is the size of the sample used to compute\ $\hat{p}$, and\ $p$ is the population proportion.
:::
A picture of this normal distribution can be drawn (Fig.\ \@ref(fig:NormalDieTheoryHT)).
The standard error is the standard deviation of the normal distribution in Fig.\ \@ref(fig:NormalDieTheoryHT).
While we still don't know *exactly* what values of $\hat{p}$ the next set of $50$\ rolls will produce, we have some idea of *how* the sample proportion varies in samples of $50$\ rolls.
For instance, values of\ $\hat{p}$ greater than about\ $0.35$ are unlikely to be observed from a fair die (with $p = 1/6$).
(ref:DieRollStdError) The sampling distribution is an approximate normal distribution; it shows a model of how the proportion of rolls showing a `r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}` varies, when a die is rolled $50$ times. The cross represents the observed sample proportion.
```{r NormalDieTheoryHT, fig.cap="(ref:DieRollStdError)", fig.align="center", fig.width=8.25, fig.height=3.0, out.width='100%'}
pop.p <- 1/6
n <- 50
se.p <- sqrt( pop.p * (1 - pop.p) / n )
par( mar = c(5, 0.25, 0.5, 0.25))
out <- plotNormal(mu = pop.p,
sd = se.p,
xlab = expression( Values~of~hat(italic(p))*","~the~sample~proportion~of~even~rolls~out~of~50),
round.dec = 2,
xlim.hi = 0.55,
xlim.lo = 0,
showX = c(0, 0.1, 1/6, 0.2, 0.3, 0.4, 0.5),
showXlabels = c("0",
"0.1",
expression(1/6),
"0.2",
"0.3",
"0.4",
"0.5"),
ylim = c(0, 11), # To allow room for "Sampling mean"
showZ = TRUE) # Vertical lines at z = -3:3
arrows(x0 = pop.p,
x1 = pop.p,
y0 = 1.2 * max(out$y),
y1 = max(out$y),
lwd = 2,
length = 0.15,
angle = 15)
text(x = pop.p,
y = 1.2 * max(out$y),
pos = 3,
labels = expression(Sampling~mean*":"~italic(p)) )
arrows(x0 = pop.p,
x1 = pop.p + se.p,
y0 = 0.3 * max(out$y),
y1 = 0.3 * max(out$y),
lwd = 2,
code = 3,
length = 0.15,
angle = 15)
locateText <- mean( c( pop.p,
pop.p + se.p) )
text(x = locateText,
y = 0.28 * max(out$y),
pos = 3,
labels = expression(Std~error) )
text(x = locateText,
y = 0.265 * max(out$y),
pos = 1,
labels = expression(plain(s.e.)(hat(italic(p)))))
# Explanations at left and right
text(x = 0.05,
y = 1.05 * max(out$y),
pos = 1,
labels = expression( atop(Values~of~hat(italic(p)),
smaller~than~italic(p)) ) )
text(x = 0.35,
y = 1.05 * max(out$y),
pos = 1,
labels = expression(Values~of~hat(italic(p))~larger~than~italic(p)) )
### Show the sample proportion
phat <- 19/50
# arrows( x0 = phat,
# x1 = phat,
# y0 = 0.2 * max(out$y),
# y1 = 0,
# angle = 15,
# length = 0.1)
# text(x = phat,
# y = 0.2 * max(out$y),
# pos = 3,
# cex = 0.9,
# label = expression(hat(p) == 0.38))
points(x = phat,
y = 0,
cex = 1.25,
pch = 4)
```
## Rolling dice: making a decision {#TestpObsDecision}
Figure\ \@ref(fig:NormalDieTheoryHT) show what values of the sample proportion $\hat{p}$ are expected when a fair die is rolled.
Step\ 3 of the decision-making process (Fig.\ \@ref(fig:DecisionFlowDice)) is to now roll the die.
When I rolled the die, a
`r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}`
appeared $19$\ times in my $50$\ rolls, a sample proportion of
$$
\hat{p} = \frac{19}{50} = 0.38.
$$
In this unusual or unexpected?
Locating this value of $\hat{p}$ on the sampling distribution in Fig.\ \@ref(fig:NormalDieTheoryHT) shows that a sample proportion of $\hat{p} = 0.38$ is *highly* unusual from a fair die with $p = 1/6$.
More specifically, since the sampling distribution has a normal distribution, the $z$-score is
$$
z
= \frac{\text{statistic} - \text{mean of the distribution}}{\text{std dev. of the distribution}}
= \frac{0.38 - (1/6)}{0.05270}
= 4.05,
$$
which is a *very* large $z$-score (based on the $68$--$95$--$99.7$ rule).\index{68@$68$--$95$--$99.7$ rule}
Using a fair die, observing $\hat{p} = 0.38$ would almost never occur.
But I *did* observe $\hat{p} = 0.38$, which suggests that the die I was rolling was *not* the fair die.
I concluded that the die I was rolling was loaded (that is, $p \ne 1/6$).
I may be incorrect (after all, it is not *impossible* to observe $\hat{p} = 0.38$), but the evidence is certainly convincing.
Using the decision-making process, a decision has been made about the dice.
The process described above is called *hypothesis testing*.\index{Hypothesis testing}
Hypothesis testing is used to make decisions about a population after observing just one of the countless possible samples.
Formally, the hypothesis test above proceeds as described in the following sections.
## The process of hypotheses testing: assumption {#TestpObsDecisionHypothesis}
\index{Test statistic!z@$z$-score}
**Step\ 1** in the decision-making process is to make an assumption about the parameter.
For the die example, the parameter is\ $p$, the population proportion of rolls that show a
`r if (knitr::is_latex_output()) {
'\\largedice{1}.'
} else {
'<span class="larger-die">⚀</span>.'
}`
The assumption is that $p = 1/6$.
This is called the *null hypothesis*,\index{Hypotheses!null} denoted by $H_0$:
$$
\text{$H_0$:\ } p = 1/6.
$$
The null hypothesis states the value of\ $p$ is $1/6$; in other words, if the sample proportion\ $\hat{p}$ is not equal to $1/6$, the discrepancy is explained by sampling variation.
The null hypothesis is always the 'sampling variation' explanation for the discrepancy between the values of the statistic and the parameter.
The other explanation for why the value of the sample proportion $\hat{p}$ is not equal to $1/6$ is called the *alternative hypothesis* (denoted $H_1$):\index{Hypotheses!alternative} that the population proportion is *not* $1/6$, and this is the cause of the discrepancy:
$$
\text{$H_1$:\ } p \ne 1/6.
$$
These two hypotheses offer different explanations for the discrepancy between the values of the population proportion (the parameter) and the sample proportion (the statistic).
The null hypothesis $H_0$ states that $p = 1/6$ and the discrepancy is due to sampling variation.
The alternative hypothesis $H_1$ states that $p \ne 1/6$, which explains the discrepancy.
Here, the RQ here is open to the value of $p$ being smaller *or* larger than\ $1/6$; that is, two possibilities are considered.
Hence, we write $p\ne 1/6$, which is called a *two-tailed* alternative hypothesis.
Alternative hypotheses like $p > 1/6$ (the population proportion is *larger* than $1/6$) or $p < 1/6$ (the population proportion is *smaller* than $1/6$) are *one-tailed* hypothesis.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
The form of the alternative hypothesis (either one- or two-tailed) depends on what the research question asks, *not the data*.
:::
## The process of hypotheses testing: expectation {#TestpObsDecisionSamplingDist}
**Step\ 2** in the decision-making process is to describe what values of the statistic (i.e., $\hat{p}$) could be expected under the assumption about the parameter (i.e., *when the null hypothesis is true*).
Hypothesis testing *always* begins by assuming the null hypothesis is true.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
The decision-making process begins by assuming the *null hypothesis* is true.
Thus, *the onus is on the data to refute the null hypothesis, the initial assumption*.
That is, the null hypothesis is retained unless persuasive evidence emerges to change our mind.
:::
Effectively, this step requires describing the sampling distribution of the statistic.
For the die example, the sampling distribution for $\hat{p}$ is (see Def.\ \@ref(def:SamplingDistPropHT))
* an approximate normal distribution,
* centred around the sampling mean whose value is\ $p = 1/6$,
* with a standard deviation, whose value is $\text{s.e.}(\hat{p}) = 0.05270\dots$
Drawing the picture of the sampling distribution (like Fig.\ \@ref(fig:NormalDieTheoryHT)) using this information is not necessary, but may be helpful.
## The process of hypotheses testing: observation {#TestpObsDecisiontestStat}
**Step\ 3** in the decision-making process is to make the observations.
As noted above, a
`r if (knitr::is_latex_output()) {
'\\largedice{1}'
} else {
'<span class="larger-die">⚀</span>'
}`
was observed in\ $19$ of the $50$\ rolls, so $\hat{p} = 0.38$.
Since the sampling distribution has a normal distribution, the corresponding $z$-score was computed as $z = 4.05$.
In hypothesis testing, the $z$-score is called the *test statistic*.\index{Test statistic}\index{Test statistic!z@$z$-score}
The test statistic measures how far, in relative terms, the sample proportion is from the assumed value of the parameter.
## The process of hypotheses testing: decision {#TestpObsDecisionPvalues}
**Step\ 4** of the decision-making process is to use the information to make a decision: is the sample statistic *consistent* with what was expected under the assumption that $p = 1/6$, or does it *contradict* what was expected?
For the die example, the decision is reasonably easy: $z = 4.05$ is *very* large and *very* unlikely to be observed if $p = 1/6$.
This means the sample evidence *contradicts* what was expected if the assumption was true: persuasive evidence exists that the die is loaded.
More generally, evidence is evaluated using a $P$-value.\index{P@$P$-values}
$P$-values refer to the area *more extreme* than the calculated test statistic in the sampling distribution.
For this situation, $P$-values refer to the area *more extreme* than the calculated $z$-score (the statistic)\index{Statistic} in the normal distribution (the sampling distribution); that is, the area in the *tails* of the distribution (see Fig.\ \@ref(fig:OnePropTestP)).
This is a way to measure how unusual the calculated $z$-score is.
For *two-tailed* alternative hypotheses, the $P$-value is the combined area in the lower and upper tails that correspond to the positive *and* negative values of the test statistic.
For *one-tailed* alternative hypotheses, the $P$-value is the area in one tail only.
Clearly, since the $P$-value is a probability, its value is always between\ $0$ and\ $1$.
$P$-values can be approximated using the $68$--$95$--$99.7$ rule and a diagram (Sect.\ \@ref(ApproxProbs); Sect.\ \@ref(OnePropTestP6895997)), or more precisely using the $z$-tables
`r if (knitr::is_latex_output()) {
'in Appendices\\ \\@ref(ZTablesNEG) and \\@ref(ZTablesPOS)'
} else {
'in App.\\ \\@ref(ZTablesOnline)'
}`
(Sect.\ \@ref(ZScoreForestry); Sect.\ \@ref(OnePropTestPTables)).
$P$-values are also reported by software for most statistical tests.
### Approximating $P$-values using the $68$--$95$--$99.7$ rule {#OnePropTestP6895997}
\index{68@$68$--$95$--$99.7$ rule}\index{P@$P$-values!using $68$--$95$--$99.7$ rule}
The $68$--$95$--$99.7$ rule can be used to determine *approximate* $P$-values.
To demonstrate, suppose the computed $z$-score was $z = 1$.
Then, the two-tailed $P$-value is the shaded tail-area in Fig.\ \@ref(fig:OnePropTestP) (top left panel): about\ $32$%, based on the $68$--$95$--$99.7$ rule.
The two-tailed $P$-value would be the same if $z = -1$.
The *one-tailed* $P$-value would be the area in one-tail (Fig.\ \@ref(fig:OnePropTestP), bottom left panel): about\ $16$%, based on the $68$--$95$--$99.7$ rule.
As another example, suppose the calculated $z$-score was $z = -2$.
Then, the two-tailed $P$-value is the shaded area shown in Fig.\ \@ref(fig:OnePropTestP) (top right panel): about\ $5$%, based on the $68$--$95$--$99.7$ rule.
The two-tailed $P$-value would be the same if $z = 2$.
The *one-tailed* $P$-value would be the area in one tail only (Fig.\ \@ref(fig:OnePropTestP), bottom right panel): about\ $2.5$%, based on the $68$--$95$--$99.7$ rule.
```{r, OnePropTestP, fig.cap="The two-tailed $P$-value is the combined area in the two tails of the distribution. Top left panel: if $z = 1$ (or $z = -1$), the two-tailed $P$-value is approximately $0.16$. Top right panel: if $z = 2$ (or $z = -2$), the two-tailed $P$-value is approximately $0.05$. The corresponding one-tailed $P$-values are half the two-tailed $P$-values, and are shown in the bottom panels.", fig.width=9.5, fig.height=5.25, out.width='100%', fig.align="center"}
par(mfrow = c(2, 2),
mar = c(4, 1, 4, 1) + 0.1)
######### TWO-TAILED
out <- plotNormal(mu = 0,
sd = 1,
main = expression( atop( The~italic(P)*"-value"~"if"~italic(z)==1~or~italic(z)==-1*":",
approx.~bold(two)*"-"*bold(tailed)~italic(P)*"-"*value*":"~0.32) ),
xlab = expression(italic(z)*"-score")
)
shadeNormal(out$x, out$y,
lo = -5,
hi = -1,
col = plot.colour)
shadeNormal(out$x, out$y,
lo = 1,
hi = 5,
col = plot.colour)
polygon(x = c(-0.9, -0.9, 0.9, 0.9), # White-ish background for above text
y = c(0.05, 0.14, 0.14, 0.05),
border = NA,
col = "white")
arrows(x0 = -1,
x1 = 1,
y0 = 0.04,
y1 = 0.04,
angle = 15,
length = 0.15,
code = 3) # BOTH ENDS
text(0,
y = 0.07,
label = "Area: 68%")
text(x = -1.5,
y = 0.05,
label = "16%")
text(x = 1.5,
y = 0.05,
label = "16%")
###
out <- plotNormal(mu = 0,
sd = 1,
main = expression( atop( The~italic(P)*"-value"~"if"~italic(z)==2~or~italic(z)==-2*":",
approx.~bold(two)*"-"*bold(tailed)~italic(P)*"-"*value*":"~0.05) ),
xlab = expression(italic(z)*"-score")
)
shadeNormal(out$x, out$y,
lo = -5,
hi = -2,
col = plot.colour)
shadeNormal(out$x, out$y,
lo = 2,
hi = 5,
col = plot.colour)
polygon(x = c(-1.4, -1.4, 1.4, 1.4), # White-ish background for above text
y = c(0.05, 0.14, 0.14, 0.05),
border = NA,
col = "white")
arrows(x0 = -2,
x1 = 2,
y0 = 0.04,
y1 = 0.04,
angle = 15,
length = 0.15,
code = 3) # BOTH ENDS
text(0,
y = 0.07,
label = "Area: 95%")
######### ONE-TAILED
out <- plotNormal(mu = 0,
sd = 1,
main = expression( atop( The~italic(P)*"-value"~"if"~italic(z)==1*":",
approx.~bold(one)*"-"*bold(tailed)~italic(P)*"-"*value*":"~0.16) ),
xlab = expression(italic(z)*"-score")
)
shadeNormal(out$x, out$y,
lo = 1,
hi = 5,
col = plot.colour)
polygon(x = c(-0.9, -0.9, 0.9, 0.9), # White-ish background for above text
y = c(0.05, 0.14, 0.14, 0.05),
border = NA,
col = "white")
arrows(x0 = -1,
x1 = 1,
y0 = 0.04,
y1 = 0.04,
angle = 15,
length = 0.15,
code = 3) # BOTH ENDS
text(0,
y = 0.07,
label = "Area: 68%")
text(x = 1.5,
y = 0.05,
label = "16%")
###
out <- plotNormal(mu = 0,
sd = 1,
main = expression( atop( The~italic(P)*"-value"~"if"~italic(z)==-2*":",
approx.~bold(one)*"-"*bold(tailed)~italic(P)*"-"*value*":"~0.025) ),
xlab = expression(italic(z)*"-score")
)
shadeNormal(out$x, out$y,
lo = -5,
hi = -2,
col = plot.colour)
polygon(x = c(-1.4, -1.4, 1.4, 1.4), # White-ish background for above text
y = c(0.05, 0.14, 0.14, 0.05),
border = NA,
col = "white")
arrows(x0 = -2,
x1 = 2,
y0 = 0.04,
y1 = 0.04,
angle = 15,
length = 0.15,
code = 3) # BOTH ENDS
text(0,
y = 0.07,
label = "Area: 95%")
```
Of course, calculated $z$-scores are unlikely to be exactly $z = 1$ or $z = -2$.
Suppose the $z$-score is a little *larger* than $z = 1$; say $z = 1.2$.
Then, the two-tailed area will be a little *smaller* than the tail area when $z = 1$ (Fig.\ \@ref(fig:OnePropTestP2), left panel).
The two-tailed $P$-value is a little *smaller* than\ $0.32$.
Similarly, suppose the $z$-score is not quite equal to $z = -2$; say $z = -1.9$.
Then, the two-tailed area will be a little *larger* than the tail area when $z = -2$ (Fig.\ \@ref(fig:OnePropTestP2), right panel).
The two-tailed $P$-value is a little *larger* than\ $0.05$.
```{r OnePropTestP2, fig.cap="The two-tailed $P$-value for $z$-scores not aligned with the $68$--$95$--$99.7$ rule. Left panel: when $z = 1.2$ (or $z = -1.2$). Right panel: when $z = 1.9$ (or $z = -1.9$).", fig.align="center", fig.width=10, fig.height=2.75, out.width='95%'}
par(mfrow = c(1, 2),
mar = c(4, 1, 4, 1) + 0.1)
out <- plotNormal(mu = 0,
sd = 1,
main = expression( atop(The~two*"-"*tailed~italic(P)*"-value"~when~italic(z)==1.2*".",
italic(P)*"-"*value~a~bit~smaller~than~0.32)),
xlab = expression(italic(z)*"-score")
)
shadeNormal(out$x, out$y,
lo = -5,
hi = -1.2,
col = plot.colour)
shadeNormal(out$x, out$y,
lo = 1.2,
hi = 5,
col = plot.colour)
lines( x = c(-1, -1),
y = c(0, 1.36 * dnorm(-1)),
lwd = 2)
lines( x = c(1, 1),
y = c(0, 1.36 * dnorm(1)),
lwd = 2)
text(x = -1,
y = 1.36 * dnorm(-1),
pos = 3,
label = expression(italic(z) == -1))
text(x = 1,
y = 1.36 * dnorm(1),
pos = 3,
label = expression(italic(z) == 1))
arrows(x0 = -1,
x1 = 1,
y0 = 0.04,
y1 = 0.04,
angle = 15,
length = 0.15,
code = 3) # BOTH ENDS
text(0,
y = 0.07,
label = "Area: 68%")
#Arrows pointing to z = 1.2 and z = -1.2
arrows(x0 = 2.25,
x1 = 1.2,
y0 = 0.15,
y1 = 0.08,
angle = 15,
length = 0.15)
text(x = 2.25,
y = 0.15,
pos = 4,
label = expression(italic(z) == 1.2) )
arrows(x0 = -2.25,
x1 = -1.2,
y0 = 0.15,
y1 = 0.08,
angle = 15,
length = 0.15)
text(x = -2.25,
y = 0.15,
pos = 2,
label = expression(italic(z) == -1.2) )
###
out <- plotNormal(mu = 0,
sd = 1,
main = expression( atop(The~two*"-"*tailed~italic(P)*"-value"~when~italic(z)==1.9*".",
italic(P)*"-"*value~a~bit~larger~than~0.05)),
xlab = expression(italic(z)*"-score")
)
shadeNormal(out$x, out$y,
lo = -5,
hi = -1.9,
col = plot.colour)
shadeNormal(out$x, out$y,
lo = 1.9,
hi = 5,
col = plot.colour)
lines( x = c(-2, -2),
y = c(0, 1.3 * dnorm(-1)),
lwd = 2)
lines( x = c(2, 2),
y = c(0, 1.3 * dnorm(1)),
lwd = 2)
text(x = -2,
y = 1.3 * dnorm(1),
pos = 3,
label = expression(italic(z) == -2))
text(x = 2,
y = 1.3 * dnorm(1),
pos = 3,
label = expression(italic(z) == 2))
arrows(x0 = -2,
x1 = 2,
y0 = 2.5 * dnorm(2),
y1 = 2.5 * dnorm(2),
angle = 15,
length = 0.15,
code = 3) # BOTH ENDS
text(0,
y = 2.5 * dnorm(2),
pos = 3,
label = "Area: 95%")
```
### More precise $P$-values using tables {#OnePropTestPTables}
\index{P@$P$-values!using tables}
Using the tables of areas under normal distributions (`r if ( knitr::is_html_output()) { 'Appendix\\ \\@ref(ZTablesOnline).'} else {'Appendices\\ \\@ref(ZTablesNEG) and \\@ref(ZTablesPOS)'}`), more precise $P$-values can be found using the ideas from Sect.\ \@ref(ExactAreasUsingTables).
For instance (see Fig.\ \@ref(fig:OnePropTestP2)):
* For $z = 1.2$: the area to the *left* of $z = -1.2$ is\ $0.1151$, and the area to the *right* of $z = 1.2$ is\ $0.1151$, so the *two-tailed* $P$-value is $0.1151 + 0.1151 = 0.2302$.
This is a little smaller than\ $0.32$, as estimated above.
* For $z = 1.9$: the area to the *left* of $z = -1.9$ is\ $0.0287$, and the area to the *right* of $z = 1.9$ is\ $0.0287$, so the *two-tailed* $P$-value is $0.0287 + 0.0287 = 0.0574$.
This is a little larger than\ $0.05$, as estimated above.
In this die-rolling example, where $z = 4.05$, the tail area is *very* small (using
`r if ( knitr::is_html_output()) {
'Appendix\\ \\@ref(ZTablesOnline)'} else {
'Appendices\\ \\@ref(ZTablesNEG) and\\ \\@ref(ZTablesPOS)'}`),
and zero to four decimal places.
$P$-values are never exactly zero, so we write $P < 0.0001$ (that is, the $P$-value is *less than*\ $0.0001$).
$P$-values tells us the probability of observing the sample statistic (or a value even more extreme), assuming the null hypothesis is true.
In the die-rolling example, the $P$-value is the probability of observing the value of $\hat{p} = 0.38$ (or more extreme), just through sampling variation if $p = 1/6$.
Then `r if( knitr::is_html_output() ) {
"(see the animation below)."
}`
`r if( knitr::is_latex_output() ) {
"(see Fig.\\ \\@ref(fig:PvaluesBigSmall)):"
}`
* 'Big' $P$-values mean the sample statistic (i.e., $\hat{p}$) could reasonably have occurred through sampling variation in one of the many possible samples, if the assumption made about the parameter (stated in $H_0$) was true:
the data *do not* contradict the assumption in\ $H_0$.
There *is no* persuasive evidence to support the alternative hypothesis.
* 'Small' $P$-values mean the sample statistic (i.e., $\hat{p}$) is *unlikely* to have occurred through sampling variation in one of the many possible samples, if the assumption made about the parameter (stated in\ $H_0$) was true:
the data *do* contradict the assumption in $H_0$.
There *is* persuasive evidence to support the alternative hypothesis.
```{r PvaluesAnimation, animation.hook="gifski", dev=if (is_latex_output()){"pdf"}else{"png"}}
zList <- c( seq(0.5,
1,
by = 0.1),
seq(1, 3.5,
by = 0.05) )
pMeaning <- function(pValue){
if (pValue > 0.10) Meaning <- "Insufficient"
if ( (pValue >= 0.05) & (pValue < 0.10)) Meaning <- "Slight"
if ( (pValue >= 0.01) & (pValue < 0.05)) Meaning <- "Moderate"
if ( (pValue >= 0.001) & (pValue < 0.01)) Meaning <- "Strong"
if (pValue < 0.001) Meaning <- "Very strong"
Meaning
}
pColours <- viridis( length(zList),
begin = 0.5 ,
end = 1,
option = "H")
if (knitr::is_html_output()) {
for (i in (1:length(zList))) {
zScore <- zList[i]
pValue <- pnorm( -zScore )
pValue2 <- ifelse( pValue < 0.001,
"< 0.001",
round(pValue, 4) )
par( mar = c(0.1, 0.1, 2.5, 0.1) ) # Number of margin lines on each side
out <- plotNormal(mu = 0,
sd = 1,
xlab = expression(italic(z)~"-score"),
main = paste("Evidence to support alternative hypothesis:\n",
pMeaning(pValue)),
round.dec = 0)
shadeNormal(out$x,
out$y,
col = pColours[i],
lo = zScore,
hi = 6)
shadeNormal(out$x,
out$y,
col = pColours[i],
hi = -zScore,
lo = -6)
abline(v = zScore,
col = "grey")
abline(v = -zScore,
col = "grey")
polygon(x = c(-1.4, -1.4, 1.4, 1.4), # White-ish background for above text
y = c(0.02, 0.10, 0.10, 0.02),
border = NA,
col = "white")
text(0,
y = 0.06,
label = paste("Two-tailed P-value:", pValue2 ) )
}
}
```
```{r PvaluesBigSmall, fig.cap="The strength of evidence: $P$-values. As the $z$-score becomes larger, the $P$-value becomes smaller, and it is more likely that the evidence contradicts the null hypothesis.", fig.height = 2.75, fig.width=10, out.width='100%', fig.align="center", dev=if (is_latex_output()){"pdf"}else{"png"}}
if (knitr::is_latex_output()) {
par(mfrow = c(1, 2),
mar = c(4.1, 0.25, 4.1, 0.25) )
# par( mar = c(0.1, 0.1, 0.1, 0.1) ) # Number of margin lines on each side
#zList <- c( 1.15, # Two-tailed P-value: 10% -1.645