-
Notifications
You must be signed in to change notification settings - Fork 7
/
20-Tools-DistributionsAndModels.Rmd
2205 lines (1769 loc) · 72 KB
/
20-Tools-DistributionsAndModels.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
# Models and normal distributions {#SamplingDistributions}
```{r, child = if (knitr::is_html_output()) {'./introductions/20-Tools-DistributionsAndModels-HTML.Rmd'} else {'./introductions/20-Tools-DistributionsAndModels-LaTeX.Rmd'}}
```
<!-- Define colours as appropriate -->
```{r, child = if (knitr::is_html_output()) {'./children/coloursHTML.Rmd'} else {'./children/coloursLaTeX.Rmd'}}
```
## Introduction {#DistributionsModelsIntro}
As seen in Chap.\ \@ref(SamplingVariation), many different samples could be drawn from a population, and the value of the statistic varies from sample to sample.
The challenge of research is that only one of these countless possible samples is observed.
The distribution of possible values of the statistic that could be observed from all possible samples is a *sampling distribution*.
::: {.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 statistic are possible, the possible values of the statistic vary (called *sampling variation*) and have a *distribution* (called a *sampling distribution*).
:::
As seen in Chap.\ \@ref(SamplingVariation), sampling distributions often have a *normal distribution* (or bell-shaped distribution).\index{Distributions}\index{Normal distribution}
That is, the normal model is often used to describe the *sampling distribution*.\index{Sampling distribution}
We now study normal distributions, as they appear in many places in research.
## Normal distributions: examples {#DistributionsExample}
\index{Normal distribution!examples for data}
In Chap.\ \@ref(SamplingVariation), we saw that the proportion of odd spins in $15$\ spins of a roulette wheel could vary; similarly, the mean spin from $15$\ spins could vary (Fig.\ \@ref(fig:RouletteWheelHistPropMean)).
In both cases, these sampling distributions had a rough *normal distribution* shape.
This is true for larger numbers of spins also (Figs.\ \@ref(fig:RouletteWheelHist) and\ \@ref(fig:RouletteWheelHistx)).
```{r RouletteWheelHistPropMean, results='hide', fig.width=8.5, fig.height=2.5, fig.cap="Sampling distributions for the proportion of odd spins (left), and the mean of the numbers after $15$ roulette wheel spins (right) are approximate normal distributions. The solid lines are theoretical normal distributions.", fig.align="center", out.width="90%"}
p <- 18/37
spins <- c(15, 25, 100, 200)
se <- sqrt( p * (1 - p) / spins )
num.sims <- 5000
par( mfrow = c(1, 2))
### Spin the wheel spins[1] * num.sims. times
### and grab each sim set from there
xNorm <- seq(0, 1,
length = 100)
propOdd <- function(x){
sum( (x%%2 != 0 ) ) / length(x)
}
set.seed(37945000)
spinNumbersAll <- sample( 0:36,
spins[1] * num.sims,
replace = TRUE)
spinNumbers <- array( spinNumbersAll,
dim = c(spins[1], num.sims) )
sampleP <- apply( spinNumbers,
MARGIN = 2,
FUN = propOdd )
break.list <- seq(0, 1,
by = 1/15) + 1/30
out <- hist( sampleP,
breaks = break.list,
xlim = c(0, 1),
axes = FALSE,
col = plot.colour,
xlab = "Sample proportions",
ylab = "",
main = paste("Proportion odd spins, from\n", spins[1], " spins of the wheel") )
yNorm <- dnorm(xNorm,
mean = p,
sd = se[1])
lines( xNorm,
(yNorm) / max(yNorm) * max(out$count),
col = "black",
lwd = 2)
axis(side = 1)
points(x = 18/37,
y = 0,
pch = 19,
cex = 0.8)
mtext(text = expression(italic(p)),
side = 1,
line = 0,
at = 18/37,
padj = 0.5,
cex = 0.8)
###
mu <- sum( 0:36 )/ 37
sigma <- sqrt( sum( ( (0:36) - mu )^2 )/37 )
spins <- c(15, 50, 100, 250)
se <- sigma / sqrt( spins )
num.sims <- 5000
### Spin the wheel spins[1] * num.sims. times
### and grab each sim set from there
set.seed(37389457)
xNorm <- seq(0, 37,
length = 100)
spinNumbersAll <- sample( 0:36,
spins[1] * num.sims,
replace = TRUE)
spinNumbers <- array( spinNumbersAll,
dim = c(spins[1], num.sims) )
sampleMeans <- colMeans(spinNumbers)
break.list <- seq(0, 37,
by = 1)
out <- hist( sampleMeans,
breaks = break.list,
xlim = c(5, 30),
axes = FALSE,
col = plot.colour,
xlab = "Sample means",
ylab = "",
main = paste("Mean number, from\n", spins[1], " spins of the wheel") )
yNorm <- dnorm(xNorm,
mean = mu,
sd = se[1])
lines( xNorm,
(yNorm) / max(yNorm) * max(out$count),
col = "black",
lwd = 2)
axis(side = 1)
points(x = 18,
y = 0,
pch = 19,
cex = 0.8)
mtext(text = expression(mu),
side = 1,
line = 0,
at = 18,
padj = 0.5,
cex = 0.8)
```
The *histograms* in Fig.\ \@ref(fig:RouletteWheelHistPropMean) are based on results from a limited number of simulations.
The solid lines shown in Fig.\ \@ref(fig:RouletteWheelHistPropMean) are actual *normal distributions*, and represent how the histogram might appear theoretically if we used an infinite number of simulations.
The normal distributions are *models* for what might occur in the *population*, so normal distributions are also called *normal models*.
Since the models represent *populations*, the mean of the model is denoted\ $\mu$ and the standard deviation is denoted\ $\sigma$.
A *model* is a theoretical or ideal concept.
A model skeleton isn't $100$%\ accurate and certainly not exactly like *your* skeleton; nonetheless, it suitably approximates reality.
None of us probably have a skeleton *exactly* like the model, but the model is still useful and helpful.
Likewise, a distribution may not have *exactly* a normal shape, but the model is still useful and helpful.
The model is a way of describing a *theoretical* distribution in the population.
A model is a simple (but not overly simple) approximation to reality.
<!-- ; it does not represent any particular sample of data. -->
The histograms of the data in Fig.\ \@ref(fig:RouletteWheelHistPropMean) are not *exactly* normal distributions, but are very close to normal distributions, and certainly close enough for most purposes.
Many, but not all, sampling distributions have approximate normal distributions.
Sampling distributions represent theoretical distributions of sample *statistics*, not the distribution of sample *data*.
When the sampling distribution is a normal distribution, the mean of the distribution is called the *sampling mean* and the standard deviation is called the *standard error*.
<!-- (These values may be *guided* by sample values; e.g., suggesting a mean Leadbeater's possum weight of $1000$\gs based on Fig.\ \@ref(fig:HistogramDBPPossums) (right panel) would be silly.) -->
Apart from their use in modelling theoretical sampling distributions, some quantitative variables have approximate normal distributions too, when the distribution of the data in the *population* can be approximately modelled by a normal distribution.
```{example NormalExamples, name="Normal distributions of data"}
Some quantitative variables have approximate normal distributions.
Figure\ \@ref(fig:HistogramDBPPossums) (left panel) shows the diastolic blood pressure of $398$\ Americans [@data:Willems1997:CHD; @data:Schorling1997:smoking].
Figure\ \@ref(fig:HistogramDBPPossums) (right panel) shows the weight of $83$\ male Leadbeater's possums [@data:Williams2022:Possums].
```
```{r HistogramDBPPossums, fig.align="center", fig.height = 3, fig.width=8.5, out.width='100%', fig.cap="Two normal distributions. Left: diastolic blood pressure of\ $398$ Americans. Right: the weight of\ $83$ male Leadbeater's possums. The solid lines are the approximate model for the variable in the population."}
par( mfrow = c(1, 2))
data(Diabetes)
out <- hist(Diabetes$DBPfirst,
col = plot.colour,
las = 1,
breaks = seq(40, 130, by = 10),
ylim = c(0, 120),
xlim = c(40, 130),
xlab = "Diastolic blood pressure (in mm Hg)",
ylab = "Number of people",
main = "Diastolic blood pressure of Americans")
x <- seq(40, 130,
length = 200)
y <- dnorm(x,
mean = mean(Diabetes$DBPfirst, na.rm = TRUE),
sd = sd(Diabetes$DBPfirst, na.rm = TRUE) )
y <- max(out$counts) * y / max(y)
lines( y ~ x,
col = "black",
lwd = 2)
#########
data(Possums)
out <- hist( Possums$Wgt[Possums$Sex=="Male"],
col = plot.colour,
las = 1,
xlab = "Weight (in g)",
ylab = "Number of possums",
xlim = c(100, 170),
ylim = c(0, 35),
main = "Weight of male Leadbeater's possums")
x <- seq(100, 170,
length = 200)
y <- dnorm(x,
mean = mean(Possums$Wgt[Possums$Sex=="Male"], na.rm = TRUE),
sd = sd(Possums$Wgt[Possums$Sex=="Male"], na.rm = TRUE) )
y <- max(out$counts) * y / max(y)
lines( y ~ x,
col = "black",
lwd = 2)
```
<!-- The histogram of the proportion of odd spins in Fig.\ \@ref(fig:RouletteWheelHistPropMean) (left panel) is from one of the countless possible samples of odd spins in $15$ spins. -->
<!-- The histogram of the mean of a set of spins in Fig.\ \@ref(fig:RouletteWheelHistPropMean) (right panel) is from one of the countless possible samples of sets of $15$ spins. -->
<!-- The normal distributions represent the unknown population sampling distributions that could reasonably have produced the histograms of the sample statistics. -->
<!-- The histograms in Fig.\ \@ref(fig:HistogramDBPPossums) are from one of the countless possible samples of Americans (left panel) or Leadbeater's possums (right panel). -->
<!-- The normal distributions represent the unknown population distributions that could reasonably have produced the sample histograms. -->
## Normal distributions and the 68--95--99.7 rule {#NormalDistribution}
Normal distributions have a shape that is symmetric about the mean, with a bell shape.
Half the values are greater than the mean, and half the values are less than the mean.
The total probability represented by a normal distribution is one (or\ $100$%).
For example, every sample will produce a sample proportion between\ $0$ and\ $1$ and so is represented somewhere in Fig.\ \@ref(fig:RouletteWheelHistPropMean) (left panel).
For example, every America has a diastolic blood pressure and so is represented somewhere in Fig.\ \@ref(fig:HistogramDBPPossums) (left panel); every male Leadbeater's possum has a weight and so is represented somewhere in Fig.\ \@ref(fig:HistogramDBPPossums) (right panel).
In theory, no upper limits or lower limits exists for a variable modelled using a normal distribution.
In practice, this is rarely true, but usually never presents a problem.
Consider the normal distributions in Fig.\ \@ref(fig:HistogramDBPPossums), for example.
The normal distribution shown for the diastolic blood pressure (left panel) has no lower or upper limit in theory, but all practical values of diastolic blood pressure are captured by that part of the normal distribution shown.
The normal distribution implies almost no-one has a diastolic blood pressure below\ $40\mms$\ Hg or above\ $130\mms$\ Hg.
One of the most important properties of normal distributions is the *68--95--99.7 rule* (sometimes called the *empirical rule*).
`r if (knitr::is_html_output()) '<!--'`
:::{.definition #EmpiricalRule name="The $68$--$95$--$99.7$ rule"}
`r if (knitr::is_html_output()) '-->'`
`r if (knitr::is_latex_output()) '<!--'`
:::{.definition #EmpiricalRule name="The 68--95--99.7 rule"}
`r if (knitr::is_latex_output()) '-->'`
For any quantity modelled by a normal distribution:\index{68@$68$--$95$--$99.7$ rule}
* *approximately*\ $68$% of values lie within\ $1$ standard deviation of the mean;
* *approximately*\ $95$% of values lie within\ $2$ standard deviations of the mean; and
* *approximately*\ $99.7$% of values lie within\ $3$ standard deviations of the mean.
These properties are true for *all* normal distributions, whatever the quantity, whatever the value of the mean, and whatever the value of the standard deviation (Fig.\ \@ref(fig:EmpiricalRuleDiagram)).
:::
```{r EmpiricalRuleDiagram, fig.width=6, fig.height=2.1, out.width='100%', fig.align="center", fig.cap="The $68$--$95$--$99.7$ rule."}
par(mfrow = c(1, 3))
out <- plotNormal(mu = 0,
sd = 1,
xlim.hi = 3.75,
xlim.lo = -3.75,
main = "68% of observations within\none std dev of the mean",
xlab = "Number of std deviations\n from the mean")
shadeNormal(out$x,
out$y,
lo = -1,
hi = 1,
col = plot.colour)
#
out <- plotNormal(mu = 0,
sd = 1,
xlim.hi = 3.75,
xlim.lo = -3.75,
main = "95% of observations within\ntwo std devs of the mean",
xlab = "Number of std deviations\n from the mean")
shadeNormal(out$x,
out$y,
lo = -2,
hi = 2,
col = plot.colour)
#
out <- plotNormal(mu = 0,
sd = 1,
xlim.hi = 3.75,
xlim.lo = -3.75,
main = "99.7% of observations within\n three std devs of the mean",
xlab = "Number of std deviations\n from the mean")
shadeNormal(out$x,
out$y,
lo = -3,
hi = 3,
col = plot.colour)
```
:::{.example #HeightsFemales name="Heights of females"}
Suppose the heights of Australian adult females can be *modelled* with a normal distribution having a mean of $\mu = 162\cms$, and a standard deviation of $\sigma = 7\cms$, and follow a normal distribution.
<!-- (based on the -->
<!-- r if (knitr::is_latex_output()) { -->
<!-- 'Australian Health Survey, 2011--2012).' -->
<!-- } else { -->
<!-- '[Australian Health Survey](https://www.abs.gov.au/AUSSTATS/[email protected]/DetailsPage/4364.0.55.0012011-12?OpenDocument)).' -->
<!-- } -->
Using the $68$--$95$--$99.7$ rule, approximately\ $68$% of Australian women will be between $162 - 7 = 155\cms$ and $162 + 7 = 169\cms$ tall using this model.
Similarly, approximately\ $95$% of Australian women will be between $162 - (2\times 7) = 148\cms$ and $162 + (2\times 7) = 176\cms$ tall using this model.
:::
These regions under the normal curve are probabilities, are often called areas, and are sometimes expressed as percentages.
## Standardising ($z$-scores) {#zScores}
\index{z@$z$-score|(}
<!-- Since many statistics have a normal distribution (under certain circumstances), the $68$--$95$--$99.7$ rule can be used to understand the distribution of sample statistics. -->
Since the $68$--$95$--$99.7$ rule (Def.\ \@ref(def:EmpiricalRule)) applies for all normal distributions, the percentages in the rule only depend on how many standard deviations\ ($\sigma$) a value\ ($x$) is from the mean\ ($\mu$).
This information can be used to learn more about how values are distributed in a normal distribution.
For example, suppose heights of Australian adult females can be modelled with a normal distribution having a mean of $\mu = 162\cms$, and a standard deviation of $\sigma = 7\cms$ (Example\ \@ref(exm:HeightsFemales)).
Using this model, the proportion of Australian adult women *taller* than\ $169\cms$ can be determined.
From a picture (Fig.\ \@ref(fig:HtsExer1), left panel), $162 + 7 = 169\cms$ is one standard deviation *above* the mean.
Since\ $68$% of values are within one standard deviation of the mean,\ $32$% are outside that range (some shorter; some taller).
Hence,\ $16$% are taller than one standard deviation above the mean, so the answer is about\ $16$%.
(Another\ $16$% are shorter than one standard deviation *below* the mean, or less than $162 - 7 = 155\cms$ in height.)
Again, the percentages only depend on how many standard deviations\ ($\sigma$) the value\ ($x$) is from the mean\ ($\mu$), and not the actual values of\ $\mu$ and\ $\sigma$.
```{r HtsExer1, fig.cap="Left: what proportion of Australian adult females are taller than $169\\cms$? Right: what proportion of Australian adult females are shorter than $148$\\cms?", fig.align="center", fig.width=7.5, fig.height=3, out.width='90%'}
HT.mn <- 162
HT.sd <- 7
plus1 <- HT.mn + HT.sd
minus1 <- HT.mn - HT.sd
plus2 <- HT.mn + (2 * HT.sd)
minus2 <- HT.mn - (2 * HT.sd)
plus3 <- HT.mn + (3 * HT.sd)
minus3 <- HT.mn - (3 * HT.sd)
plus4 <- HT.mn + (4 * HT.sd)
minus4 <- HT.mn - (4 * HT.sd)
gap <- 0.5
shrinkDotted <- 3
####################
par( mfrow = c(1, 2),
mar = c(5, 0.5, 5, 0.5))
out <- plotNormal(mu = HT.mn,
sd = HT.sd,
ylim = c(0, 0.075),
xlab = "Height (in cm)")
shadeNormal(out$x,
out$y,
lo = 0,
hi = minus1,
col = plot.colour)
shadeNormal(out$x,
out$y,
lo = plus1,
hi = 400,
col = plot.colour)
abline( v = c(minus1, plus1),
col = "grey")
arrows(x0 = minus1 + gap,
y0 = 0.06,
x1 = plus1 - gap,
y1 = 0.06,
code = 3, # Arrow both ends
length = 0.10,
angle = 15)
text(x = HT.mn,
y = 0.06,
labels = "Area: 68%",
cex = 0.9,
pos = 3)
arrows(x0 = plus1 + gap,
y0 = 0.06,
x1 = plus3 - shrinkDotted,
y1 = 0.06,
code = 1,
length = 0.10,
angle = 15)
lines( x = c(plus3 - shrinkDotted, plus3 + gap),
y = c(0.06, 0.06),
lty = 2)
text(x = plus2,
y = 0.06,
labels = "Area: 16%",
cex = 0.9,
pos = 3)
arrows(x0 = minus1 - gap,
y0 = 0.06,
x1 = minus3 + shrinkDotted,
y1 = 0.06,
code = 1,
length = 0.10,
angle = 15)
lines( x = c(minus3 + shrinkDotted, minus3 - gap),
y = c(0.06, 0.06),
lty = 2)
text(x = minus2,
y = 0.06,
labels = "Area: 16%",
cex = 0.9,
pos = 3)
###########################
out <- plotNormal(mu = HT.mn,
sd = HT.sd,
ylim = c(0, 0.075),
xlab = "Height (in cm)")
shadeNormal(out$x,
out$y,
lo = 0,
hi = minus2,
col = plot.colour)
shadeNormal(out$x,
out$y,
lo = plus2,
hi = 200,
col = plot.colour)
abline( v = c(minus2, plus2),
col = "grey")
arrows(x0 = minus2 + gap,
y0 = 0.06,
x1 = plus2 - gap,
y1 = 0.06,
code = 3, # Arrow both ends
length = 0.10,
angle = 15)
text(x = HT.mn,
y = 0.06,
labels = "Area: 95%",
cex = 0.9,
pos = 3)
arrows(x0 = plus2 + gap,
y0 = 0.06,
x1 = plus3 - shrinkDotted,
y1 = 0.06,
code = 1,
length = 0.10,
angle = 15)
lines( x = c(plus3 - shrinkDotted, plus3 + gap),
y = c(0.06, 0.06),
lty = 2)
text(x = mean( c(plus2, plus3) ) + 2.5,
y = 0.06,
labels = "Area: 2.5%",
cex = 0.9,
pos = 3)
arrows(x0 = minus2 - gap,
y0 = 0.06,
x1 = minus3 + shrinkDotted,
y1 = 0.06,
code = 1,
length = 0.10,
angle = 15)
lines( x = c(minus3 + shrinkDotted, minus3 - gap),
y = c(0.06, 0.06),
lty = 2)
text(x = mean(c(minus2, minus3)) - 2.5,
y = 0.06,
labels = "Area: 2.5%",
cex = 0.9,
pos = 3)
```
`r if (knitr::is_html_output()) '<!--'`
::: {.example #HeightsExer2 name="The $68$--$95$--$99.7$ rule"}
`r if (knitr::is_html_output()) '-->'`
`r if (knitr::is_latex_output()) '<!--'`
::: {.example #HeightsExer2 name="The 68--95--99.7 rule"}
`r if (knitr::is_latex_output()) '-->'`
Consider again the heights of Australian adult females.
Using this model, what proportion are *shorter* than\ $148\cms$?
Again, drawing a picture is helpful (Fig.\ \@ref(fig:HtsExer1), right panel).
Since $162 - (2\times 7) = 148$, $148\cms$ is two standard deviation *below* the mean.
Since $95$% of values are within two standard deviation of the mean,\ $5$% are outside that range (half smaller, half larger; see Fig.\ \@ref(fig:HtsExer1), right panel), so that\ $2.5$% are *shorter* than\ $148\cms$.
(Another\ $2.5$% are *taller* than $162 + 14 = 176\cms$.)
:::
Again, the percentages only depend on how many standard deviations\ ($\sigma$) the value\ ($x$) is from the mean\ ($\mu$).
The number of standard deviations that an observation is from the mean is called a *$z$-score*.
A $z$-score is computed using
$$
z = \frac{ x - \mu}{\sigma},
$$
where\ $\sigma$ is the standard deviation quantifying the variation in the $x$-values.
Converting values to $z$-scores is called *standardising*.
`r if (knitr::is_html_output()) '<!--'`
::: {.definition #zScore name="$z$-score"}
`r if (knitr::is_html_output()) '-->'`
`r if (knitr::is_latex_output()) '<!--'`
::: {.definition #zScore name="z-score"}
`r if (knitr::is_latex_output()) '-->'`
A *$z$-score* measures how many standard deviations a value\ $x$ is from the mean.
In symbols:
\begin{equation}
z = \frac{x - \mu}{\sigma},
(\#eq:zscores)
\end{equation}
where\ $\mu$ is the mean of the distribution, and\ $\sigma$ is the standard deviation of the distribution (measuring the variation in the $x$-values).
:::
The $z$-score is also called the *standardised value* or *standard score*.
Note that:
* $z$-scores are negative for observations *below* the mean.
* $z$-scores are positive for observations *above* the mean.
* $z$-scores have no units (that is, not measured in kg, or cm, etc.).
`r if (knitr::is_html_output()) '<!--'`
::: {.example #HeightsExer3 name="$z$-scores"}
`r if (knitr::is_html_output()) '-->'`
`r if (knitr::is_latex_output()) '<!--'`
::: {.example #HeightsExer3 name="z-scores"}
`r if (knitr::is_latex_output()) '-->'`
Consider the model for the heights of Australian adult females again.
From earlier, the $z$-score for a height of\ $169\cms$ is
$$
z = \frac{x-\mu}{\sigma} = \frac{169 - 162}{7} = 1,
$$
one standard deviation *above* the mean.
Similarly, the $z$-score for a height of\ $148\cms$ is
$$
z = \frac{x-\mu}{\sigma} = \frac{148 - 162}{7} = -2,
$$
two standard deviations *below* the mean.
:::
`r if (knitr::is_html_output()) '<!--'`
::: {.example #EmpiricalRuleZ name="The $68$--$95$--$99.7$ rule"}
`r if (knitr::is_html_output()) '-->'`
`r if (knitr::is_latex_output()) '<!--'`
::: {.example #EmpiricalRuleZ name="The 68--95--99.7 rule"}
`r if (knitr::is_latex_output()) '-->'`
Consider the model for the heights of Australian adult females: a normal distribution, mean $\mu = 162\cms$, standard deviation $\sigma = 7\cms$ (Fig.\ \@ref(fig:HtsEmpirical)).
Using this model:
* A height of $162\cms$ is zero standard deviations from the mean: $z = 0$.
* $155\cms$ is one standard deviation *below* the mean: $z = -1$.
* $169\cms$ is one standard deviation *above* the mean: $z = 1$.
* $148\cms$ and $176\cms$ correspond to $z = -2$ and $z = 2$ respectively.
* $141\cms$ and $183\cms$ correspond to $z = -3$ and $z = 3$ respectively.
:::
```{r HtsEmpirical, fig.cap="The $68$--$95$--$99.7$ rule and the heights of Australian adult females.", fig.align="center", fig.width=7.15, fig.height=2.75, out.width='75%'}
#par( mar = c() )
out <- plotNormal(HT.mn,
HT.sd,
main = "Heights of Australian adult females",
xlab = "Heights (in cm)")
mtext(expression( "("*italic(z)==0*")"),
side = 1,
line = 2,
cex = 0.9,
at = HT.mn)
mtext(expression( "("*italic(z)==1*")"),
side = 1,
line = 2,
cex = 0.9,
at = HT.mn + HT.sd)
mtext(expression( "("*italic(z)==2*")"),
side = 1,
line = 2,
cex = 0.9,
at = HT.mn + HT.sd*2)
mtext(expression( "("*italic(z)==3*")"),
side = 1,
line = 2,
cex = 0.9,
at = HT.mn + HT.sd*3)
mtext(expression( "("*italic(z)==-1*")"),
side = 1,
line = 2,
cex = 0.9,
at = HT.mn - HT.sd)
mtext(expression( "("*italic(z)==-2*")"),
side = 1,
line = 2,
cex = 0.9,
at = HT.mn - HT.sd*2)
mtext(expression( "("*italic(z)==-3*")"),
side = 1,
line = 2,
cex = 0.9,
at = HT.mn - HT.sd*3)
```
## Approximating areas (percentages) using the $68$--$95$--$99.7$ rule {#ApproxProbs}
\index{Normal distribution!approximating percentages}
As seen above, the $68$--$95$--$99.7$ rule can be used to approximate percentages under normal distributions.
The rule can even be used for values that do not exactly align with\ $1$,\ $2$ or\ $3$ standard deviations from the mean.
Suppose again that heights of Australian adult females can be modelled with a normal distribution with a mean of $\mu = 162\cms$, and a standard deviation of $\sigma = 7\cms$ (Fig.\ \@ref(fig:HtsEmpirical)).
To find the proportion of women *shorter* than $145\cms$, first draw the situation (Fig.\ \@ref(fig:HtsExer3)).
Proceeding as before, we ask 'How many standard deviations from the mean is\ $145\cms$?'
Using Equation\ \@ref(eq:zscores), $145\cms$ corresponds to a $z$-score of
\begin{equation}
z = \frac{145 - 162}{7} = -2.4285...
(\#eq:zscore214)
\end{equation}
which is about\ $2.43$ standard deviations *below* the mean.
```{r HtsExer3, fig.cap="What proportion of Australian adult females are shorter than\ $145\\cms$?", fig.align="center", fig.width=7.0, fig.height=3.00, out.width='85%'}
HtInterest <- 145
par( mar = c(5.1, 4.1, 1.1, 2.1) )
out <- plotNormal(HT.mn,
HT.sd,
ylim = c(0, 0.085),
xlab = "Heights (in cm)")
shadeNormal(out$x,
out$y,
lo = 120,
hi = 148,
col = plot.colour)
mtext(expression( "("*italic(z)==0*")"),
side = 1,
line = 2,
cex = 0.8,
at = HT.mn)
mtext(expression( "("*italic(z)==1*")"),
side = 1,
line = 2,
cex = 0.8,
at = plus1)
mtext(expression( "("*italic(z)==2*")"),
side = 1,
line = 2,
cex = 0.8,
at = plus2)
mtext(expression( "("*italic(z)==3*")"),
side = 1,
line = 2,
cex = 0.8,
at = plus3)
mtext(expression( "("*italic(z)==-1*")"),
side = 1,
line = 2,
cex = 0.8,
at = minus1)
mtext(expression( "("*italic(z)==-2*")"),
side = 1,
line = 2,
cex = 0.8,
at = minus2)
mtext(expression( "("*italic(z)==-3*")"),
side = 1,
line = 2,
cex = 0.8,
at = minus3)
zInterest <- (HtInterest - HT.mn) / HT.sd
lines( x = c(HtInterest, HtInterest),
y = c(0, max(out$y) * 0.7),
col = "black")
text(x = HtInterest,
y = max(out$y) * 0.7,
pos = 3,
cex = 0.9,
labels = "145 cm")
text(x = HT.mn,
y = 0.070,
pos = 3,
cex = 0.9,
labels = "Area: 95%")
text(x = mean(c(minus2, minus3)) - 2,
y = 0.070,
pos = 3,
cex = 0.9,
labels = "Area: 2.5%")
text(x = mean(c(plus2, plus3)) + 2,
y = 0.070,
pos = 3,
cex = 0.9,
labels = "Area: 2.5%")
abline(v = c(minus2, plus2),
col = "grey")
shrinkDotted <- 3
minus4 <- HT.mn - (4 * HT.sd)
plus4 <- HT.mn + (4 * HT.sd)
# Arrows for areas
arrows( x0 = minus3 + shrinkDotted,
x1 = minus2 - gap,
y0 = 0.070,
y1 = 0.070,
length = 0.10,
angle = 15,
lwd = 1)
lines( x = c(minus4, minus3 + shrinkDotted),
y = c(0.070, 0.070),
lwd = 1,
lty = 2)
arrows( x0 = plus3 - shrinkDotted,
x1 = plus2 + gap,
y0 = 0.070,
y1 = 0.070,
length = 0.10,
angle = 15,
lwd = 1)
lines( x = c(plus3 - shrinkDotted, plus4),
y = c(0.070, 0.070),
lwd = 1,
lty = 2)
arrows( x0 = minus2 + gap,
x1 = plus2 - gap,
y0 = 0.070,
y1 = 0.070,
code = 3,
length = 0.10,
angle = 15,
lwd = 1)
# Arrow to small area sought
arrows(x0 = 138,
x1 = 145,
y0 = 0.018,
y1 = 0.018,
angle = 15,
lwd = 1,
length = 0.1)
lines( x = c(135, 138),
y = c(0.018, 0.018),
lwd = 1,
lty = 2)
text(x = 140,
y = 0.018,
pos = 3,
cex = 0.9,
labels = "Area smaller")
text(x = 140,
y = 0.018,
pos = 1,
cex = 0.9,
labels = "than 2.5%")
```
What percentage of observations are less than this $z$-score?
This case is not covered by the $68$--$95$--$99.7$ rule, though the rule can be used to make *rough estimates*.
About\ $2.5$% of observations are less than\ $2$ standard deviations below the mean; that is, about\ $2.5$% of women are shorter than\ $148\cms$.
So the percentage of females shorter than\ $145$\cms (that is, even shorter than\ $148\cms$ and so further into the tail of the distribution) will be *smaller* than\ $2.5$%.
While we don't know the probability exactly, it will be smaller than\ $2.5$%.
Percentages found this way are very approximate, but often sufficient.
However, more accurate percentages are found using tables compiled for this very purpose
`r if ( knitr::is_html_output()) {
'(Appendix\\ \\@ref(ZTablesOnline)).'
} else {
'(Appendices\\ \\@ref(ZTablesNEG) and \\@ref(ZTablesPOS)).'
}`
We now learn how to use these tables.
## Exact areas (percentages) using tables {#ExactAreasUsingTables}
\index{Normal distribution!using tables}
Areas under normal distributions can be found using online tables, or hard copy tables.
The online tables are easier to use,
`r if (knitr::is_latex_output()) {
'but only the hard-copy tables are explained in this book (see the online version of this book for the online tables, and instructions for using the online tables).'
} else {
'but only the online tables are explained in this online book (see the hard-copy version for the hard-copy tables, and instruction for using use the hard-copy tables).'
}`
The tables
`r if (knitr::is_latex_output()) {
'(Appendices\\ \\@ref(ZTablesNEG) and\\ \\@ref(ZTablesPOS))'
} else {
'(Appendix\\ \\@ref(ZTablesOnline))'
}`
work with $z$-scores to two decimal places, so consider the $z$-score from Sect.\ \@ref(ApproxProbs) as $z = -2.43$.
```{r, child = if (knitr::is_latex_output()) './Tables/Ztables-Using-Hardcopy.Rmd'}
```
```{r, child = if (knitr::is_html_output()) './Tables/Ztables-Using-Online.Rmd'}
```
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
The tables always give the area to the *left* of the $z$-score.
:::
Either the hard-copy or online tables gives an answer of\ $0.75$%.
This is consistent with the rough answer using the $68$--$95$--$99.7$ rule: a value less than\ $2.5$%.
<iframe src="https://learningapps.org/watch?v=ppievv9gc22" style="border:0px;width:100%;height:800px" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
## Examples using $z$-scores {#ZScoreForestry}
The general approach to computing probabilities from normal distributions is:
* *Draw a diagram*, and mark on the value(s) of interest.
* *Shade* the required region of interest.
* *Compute* the $z$-score(s) using Equation\ \@ref(eq:zscores).
* *Use* the tables in `r if ( knitr::is_html_output()) { 'Appendix\\ \\@ref(ZTablesOnline)'} else {'Appendices\\ \\@ref(ZTablesNEG) and\\ \\@ref(ZTablesPOS)'}` to compute corresponding areas (percentages).
* *Deduce* the answer.
Using this approach, more complicated questions can be answered
::: {.example #NormalTrees name="Normal distributions"}
Mechanized forest harvesting systems were simulated by @data:Aedo1997:softwood, and the diameters of a specific type of trees were modelled using:
* a normal distribution; with
* a mean of $\mu = 8.8$ inches; and
* a standard deviation of $\sigma = 2.7$ inches.
Using this model, what is the probability that a randomly-chosen tree has a diameter *greater* than\ $5$\ inches?
Following the steps identified earlier:
* *Draw* a normal curve, and mark on\ $5$\ inches (Fig.\ \@ref(fig:ZDBH1), left panel).
* *Shade* the region 'greater than\ $5$\ inches' (Fig.\ \@ref(fig:ZDBH1), centre panel).
* *Compute* the $z$-score using Eq.\ \@ref(eq:zscores):
$\displaystyle z = (5 - 8.8)/2.7 = -1.41$ to two decimal places.
* *Use* tables:
The probability of a tree diameter *shorter* than $5$\ inches is\ $0.0793$.
(Remember: the tables always give area *less* than the value of\ $z$.)
* *Deduce* the answer (Fig.\ \@ref(fig:ZDBH1), right panel):
since the *total* area under the normal distribution is one (or\ $100$%), the probability of a tree diameter *greater* than\ $5$\ inches is $1 - 0.0793 = 0.9207$, or about\ $92$%.
A randomly-chosen tree has a probability of\ $92$% of having a diameter *greater*\ $5$\ inches.
:::
```{r ZDBH1, fig.cap="What proportion of tree diameters are greater than\ $5$ inches?", fig.align="center", fig.width=7.0, fig.height=1.75, out.width='100%'}
DBH.mn <- 8.8
DBH.sd <- 2.7
DBH.x <- 5
par(mfrow = c(1, 3),
mar = c(3, 0.25, 4, 0.25))
z <- seq( -3.5, 3.5,
length = 250)
zy <- dnorm( z,
mean = 0,
sd = 1)
mu <- DBH.mn
sigma <- DBH.sd
x <- z * sigma + mu
out <- plotNormal(mu,
sigma,
xlab = "Tree diameters (in inches)",
main = "Draw",
ylim = c(0, 0.16),
round.dec = 1)
segments(x0 = DBH.x,
x1 = DBH.x,
y0 = 0,
y1 = max(out$y) * 0.75,
lwd = 2)
text(x = DBH.x,
y = max(out$y) * 0.75,
pos = 3,
labels = "5 inches")
####
out <- plotNormal(mu,
sigma,
xlab = "Tree diameters (in inches)",
main = "Shade",
ylim = c(0, 0.16),
round.dec = 1)
shadeNormal(out$x,
out$y,
col = plot.colour,
lo = DBH.x,
hi = 20)
abline(v = DBH.x,
lwd = 2)
text(x = 14,
y = max(out$y) * 0.7,
pos = 3,
labels = "Area of\ninterest")
arrows(x0 = 14,
y0 = max(out$y) * 0.7,
x1 = 10,
y1 = max(out$y) * 0.2,
lwd = 2,
angle = 15,
length = 0.1)
####
out <- plotNormal(mu,
sigma,
xlab = "Tree diameters (in inches)",
main = "Compute the answer",
round.dec = 1,
ylim = c(0, 0.16),
showZ = FALSE)
shadeNormal(out$x,
out$y,
col = plot.colour,