-
Notifications
You must be signed in to change notification settings - Fork 7
/
33-CorrelationRegression.Rmd
2764 lines (2140 loc) · 104 KB
/
33-CorrelationRegression.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
<!-- # (PART) Correlational RQs: correlation and regression {-} -->
# Correlation and regression {#CorrelationRegression}
<!-- Introductions; easier to separate by format -->
```{r, child = if (knitr::is_html_output()) {'./introductions/33-CorrelationRegression-HTML.Rmd'} else {'./introductions/33-CorrelationRegression-LaTeX.Rmd'}}
```
<!-- Define colours as appropriate -->
```{r, child = if (knitr::is_html_output()) {'./children/coloursHTML.Rmd'} else {'./children/coloursLaTeX.Rmd'}}
```
## Introduction: lifespan of dog breeds {#CorReg-Intro}
\index{Correlation}
So far, RQs about single variables (descriptive\ RQs) and RQs for comparisons (relational and repeated-measures\ RQs) have been studied.
In this chapter, the relationship between two quantitative variables is studied (correlational\ RQs) *when that relationship is approximately linear*.
The strength of the relationship (correlation) and the nature of that relationship (regression) are discussed.
For this chapter, consider this RQ:
> In dog breeds, is there a relationship between breed-weight and lifespan, in general?
@da2023dog recorded the lifespan and weight of dog breeds, for at least\ $50$ dogs from each breed [@da2023dogDATA];
`r if( knitr::is_latex_output() ) {
'see Table\\ \\@ref(tab:DogsLifeData).'
} else {
'see Fig.\\ \\@ref(fig:dogslifeDataDT).'
}`
The dog breed is the unit of analysis;\index{Units of analysis} the individual dog is the unit of observation.\index{Units of observation}
The data comprises two quantitative variables (Fig.\ \@ref(fig:DogsLifeScatterjamovi2), left panel).\index{Graphs!scatterplot}
```{r DogsLifeData}
data(DogsLife)
row.names(DogsLife) <- NULL
if( knitr::is_latex_output()){
DogsLifeData <- rbind( head(DogsLife[, c(1, 2, 5)], 5),
rep("$\\vdots$", 3),
tail(DogsLife[, c(1, 2, 5)], 5) )
knitr::kable( pad( DogsLifeData,
surroundMaths = TRUE,
targetLength = c(0, 6, 5),
decDigits = c(0, 3, 2)),
format = "latex",
booktabs = TRUE,
longtable = FALSE,
escape = FALSE,
align = c("r", "c", "c"),
linesep = "",
caption = "Information about various dog breeds (from at least $50$\ individuals from each breed were used). The first five and last five breeds only are shown.",
col.names = c( "Breed",
"Breed weight (in kg)",
"Breed lifespan (in years)") ) %>%
kable_styling(font_size = 8) %>%
row_spec(0, bold = TRUE)
}
```
```{r dogslifeDataDT, fig.cap="The dog-breed data.", fig.align="center"}
data(DogsLife)
row.names(DogsLife) <- NULL
if( knitr::is_html_output() ) {
DT::datatable(DogsLife,
list(scrollX = TRUE,
scrollY = TRUE,
ordering = FALSE),
caption = "The dog-breed data.")
}
```
```{r DogsLifeScatterjamovi2, fig.width=5.25, out.width=c('54%', '45%'), fig.align='center', fig.height=3.75, fig.cap="Lifespan of dog breeds against breed-weight. Left: scatterplot. Right: correlation output.", fig.show='hold'}
plot(Lifespan ~ Weight,
data = DogsLife,
las = 1,
pch = 19,
ylim = c(4, 16),
xlim = c(0, 80),
cex = 0.8,
main = "Lifespan and weight\nof dog breeds",
xlab = "Weight (in kg)",
ylab = "Lifespan (in years)")
# with(DogsLife,
# cor(Lifespan , Weight) )
knitr::include_graphics("jamovi/DogsLife/DogsLifeCorrelationCI.png")
```
Knowing the breed-weight provides some information about the lifespan: a moderate relationship between the variables seems evident.
The relationship also seems somewhat *linear*.
The *Pearson correlation coefficient* (Fig.\ \@ref(fig:DogsLifeScatterjamovi2), right panel)\index{Correlation coefficient (Pearson)} is $r = -0.712$, so $R^2 = (-0.712) ^2 = 50.7$%.
This means that the unexplained variation in lifespan reduces by $50.7$% by knowing the breed-weight.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
Recall that the *sample* correlation coefficient is denoted by\ $r$, and the *population* correlation coefficient is denoted by\ $\rho$,
:::
## Correlation: CIs and tests for $\rho$ {#CorrelationCITest}
### Correlation: CIs for $\rho$ {#CorrelationCI}
\index{Confidence intervals!correlation coefficient|(}
In the dog-breed study, only one of the countless possible samples of dogs has been studied, and the value of\ $r$ (an estimate of $\rho$, the *parameter*) will vary from sample to sample.
That is, the value of\ $r$ has a sampling distribution, and sampling variation exists.\index{Sampling variation}
The sampling distribution of\ $r$, however, does *not* have a normal distribution, so CIs for\ $\rho$ will be taken directly from software output (Fig.\ \@ref(fig:DogsLifeScatterjamovi2), right panel).
For the dog-breed data, the $95$% CI for\ $\rho$ is from $-0.576$ to\ $-0.809$.
Note that this CI is not symmetrical: the value of\ $r$ is not halfway between these limits.
We write:
> For dog breeds, the correlation coefficient between breed lifespan and breed-weight is\ $-0.712$, with a $95$% CI from $-0.576$ to\ $-0.809$ ($n = 73$).
In other words, a population with a correlation coefficient\ $\rho$ between $-0.576$ and\ $-0.809$ could reasonably have produced a sample of size $n = 73$ with a sample correlation coefficient of $r = -0.712$.
::: {.example #CycloneCorrelationCI name="Correlation"}
The relationship between the number of cyclones\ $y$ in the Australian region each year from\ 1969 to\ 2005, and a unitless climatological index called the *Ocean `r readr::parse_character( c("Niño"), locale=locale(encoding="UTF-8"))` Index* (ONI,\ $x$), averaged over October, November and December, is shown in Fig.\ \@ref(fig:ONIcyclonesCorrelation) (left panel) [@mypapers:dunnsmyth:glms].
The relationship has a *negative* direction, so the value of\ $r$ is *negative*.
From the software output, $r = -0.683$ with a $95$% CI from $-0.460 $ to\ $-0.824$.
:::
\index{Confidence intervals!correlation coefficient|)}
```{r ONIcyclonesCorrelation, fig.width=5.25, fig.align="center", fig.cap="The number of cyclones in the Australian region each year from 1969 to 2005, and the ONI averaged over October, November, December. Left: scatterplot. Right: software output.", fig.height=3.75, out.width=c('54%', '45%'), fig.show='hold'}
data(Cyclones)
plot(Total ~ OND,
data = Cyclones,
xlim = c(-2.5, 3),
ylim = c(0, 20),
pch = 19,
ylab = "Total number of cyclones",
xlab = "ONI averaged over Oct., Nov., Dec.",
main = "Cyclone numbers vs ONI",
las = 1)
knitr::include_graphics("jamovi/Cyclones/Cyclones-CorrelationCI.png")
```
### Correlation: hypothesis test for $\rho$ {#CorrelationTesting}
\index{Hypothesis testing!correlation coefficient|(}
A hypothesis test can also be conducted regarding\ $\rho$, the Pearson correlation coefficient in the *population*.\index{Correlation}
The null hypothesis is, as always, the 'no difference, no change, no relationship' position which is, in this context, is:
* $H_0$: $\rho = 0$.
Clearly, the *sample* correlation coefficient $r$ for the data is not zero, and the RQ is effectively asking if sampling variation is the reason for this discrepancy between\ $r$ and the parameter\ $\rho$.
The RQ is (as given in Sect.\ \@ref(CorReg-Intro)) is a two-tailed RQ, so the alternative hypothesis is:
* $H_1$: $\rho \ne 0$ \quad (*two-tailed* test, based on the RQ).
As usual, initially *assume* that $\rho = 0$ (from\ $H_0$), then describe what values of\ $r$ could be *expected* using the *sampling distribution*, under that assumption, across all possible samples (sampling variation).
Then the *observed* value of\ $r$ is compared to the values expected through sampling variation to determine if the value of\ $r$ supports or contradicts the assumption.
For a correlation coefficient, the sampling distribution of\ $r$ does not have a normal distribution^[For those interested: the value of\ $r$ only varies between $-1$ and $1$, so cannot have a normal distribution. A transformation of\ $r$ *does* exist that has an approximate normal distribution and *standard error*.].
However, the output (Fig.\ \@ref(fig:DogsLifeScatterjamovi2), right panel)\index{Software output!correlation} contains the relevant $P$-value for the test: less than\ $0.0001$.
*Very strong evidence* exists to support\ $H_1$ (that the correlation in the population is not zero).
We write:
> The sample presents very strong evidence (two-tailed $P < 0.0001$) that the average lifespan is associated with the breed-weight ($r = -0.712$ with $95$% CI from $-0.576$ to $-0.809$; $n = 73$) in the population.
Notice the three features of writing conclusions again: an *answer to the RQ*, evidence to support the conclusion, and some *sample summary information*.
::: {.tipBox .tip data-latex="{iconmonstr-info-6-240.png}"}
If the evidence suggests that the correlation coefficient is *not zero* (in the population), this does *not* necessarily mean a *strong* correlation exists.
The correlation may be weak in the population (as estimated by the value of\ $r$), but evidence exists that the correlation is *not zero* in the *population*.
That is, the test is about statistical significance, not practical importance.\index{Practical importance}
:::
::: {.example #CycloneCorrelationTest name="Correlation"}
The relationship between the number of cyclones\ $y$ in the Australian region each year from\ 1969 to\ 2005, and the ONI\ $x$ is shown in Fig.\ \@ref(fig:ONIcyclonesCorrelation) (left panel).
To test
$$
\text{$H_0$: $\rho = 0$} \qquad\text{ against \qquad} \text{$H_0$: $\rho \ne 0$},
$$
software reports that $P < 0.0001$ (Fig.\ \@ref(fig:ONIcyclonesCorrelation), right panel).
There is very strong evidence of a relationship between the number of cyclones in the Australian region and the ONI (averaged over October, November and December).
:::
\index{Hypothesis testing!correlation coefficient|)}
## Regression
### Introducting regression {#Chap35-Intro}
*Correlation* measures the *strength* and *direction* of the *linear* relationship between two quantitative variables\ $x$ (an explanatory variable) and\ $y$ (a response variable).
Sometimes, however, *describing* the relationship is also useful.
This is called *regression*.
The regression relationship is described mathematically using an *equation*, and allows us to:
1. *Predict* the mean value of\ $y$ from a given value of\ $x$ (Sect.\ \@ref(RegressionForPrediction)); and
2. *Understand* the relationship between\ $x$ and\ $y$ (Sect.\ \@ref(RegressionForUnderstanding)).
<!-- Of course, any prediction will not be a perfect prediction, due to natural variation in the data.\index{Natural variation} -->
<!-- In other words, for any given value of $x$, many possible values of $y$ could be observed. -->
<!-- This means that the prediction is actually a prediction of the *mean* of all the possible values of $y$ that could be observed for that given value of $x$. -->
An example of a *linear* regression equation, describing the linear relationship between the observed values of an explanatory variable\ $x$ and the observed values of a response variable\ $y$, is\index{Linear equations}
\begin{equation}
\hat{y} = -4 + (2\times x), \qquad\text{usually written}\qquad \hat{y} = -4 + 2x.
(\#eq:ExampleRegressionEqn)
\end{equation}
The notation\ $\hat{y}$ refers to the mean of all the $y$-values that could be observed for some given value of\ $x$.
That is, for some value of\ $x$, many values of\ $y$ could be observed, and\ $\hat{y}$ is the value that regression equation predicts as the *mean* of all those possible values.
This equation describes the connection between the values of\ $x$ and the corresponding average values of\ $y$.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
$y$\ refers to the values of the response variable *observed* from individuals.
$\hat{y}$ refers to *predicted mean* value of\ $y$ for given values of\ $x$.
:::
<!-- Equation\ \@ref(eq:ExampleRegressionEqn) can be used to explore the relationship between the variables. -->
<!-- For instance, suppose $x = 7$; then the equation gives -->
<!-- $$ -->
<!-- \hat{y} = -4 + (2\times 7) = -4 + 14 = 10. -->
<!-- $$ -->
<!-- This means that, when the value of\ $x$ is\ $7$, the observed values of\ $y$ will vary around a predicted mean of $\hat{y} = 10$. -->
:::: {.pronounceBox .pronounce data-latex="{iconmonstr-microphone-7-240.png}"}
$\hat{y}$ is pronounced as 'why-hat'; the 'caret' above the\ $y$ is called a 'hat'.
:::
More generally, the equation of a straight line is
$$
\hat{y} = b_0 + (b_1 \times x), \qquad\text{usually written}\qquad \hat{y} = b_0 + b_1 x,
$$
where\ $b_0$ and\ $b_1$ are (unknown) numbers estimated from sample data.
Notice that\ $b_1$ is the number multiplied by\ $x$.
In Eq.\ \@ref(eq:ExampleRegressionEqn), $b_0 = -4$ and $b_1 = 2$.
::: {.example #RegressionGirlsHt name="Regression equations"}
A report on the growth of Australian children [@data:AustralianGirls] found an approximate linear relationship between the age of girls in years $x$ and their height (in\ cm)\ $y$ aged between four and seven.
The regression equation was approximately
$$
\hat{y} = 73 + 7x.
$$
The regression equation is the same if written as
$$
\hat{y} = 7x + 73.
$$
In both cases, $b_0 = 73$ and $b_1 = 7$.
This regression equation describes the connection between ages\ $x$ and heights\ $y$ for girls between four and seven years-of-age.
<!-- The regression equation can be used to *predict* the *mean* height for any given age\ $x$. -->
<!-- For example, girls aged five years-of-age (i.e., $x = 5$) are predicted to have a mean height of -->
<!-- $$ -->
<!-- \hat{y} = 73 + (7\times 5) = 73 + 35 = 108. -->
<!-- $$ -->
<!-- The heights of girls will vary around a *mean* height of\ $108$\cms; some individual girls aged five will be taller than\ $108$\cms, and some will be shorter than\ $108$\cms. -->
:::
### Reviewing linear equations {#RegressionEquationsReview}
\index{Linear equations}\index{Regression}
To introduce, or revise, the idea of a linear equation, consider the (artificial) data in
`r if (knitr::is_latex_output()) {
'Fig.\\ \\@ref(fig:ExampleScatterLATEX) (left panel),'
} else {
'Fig.\\ \\@ref(fig:ExampleScatterHTML),'
}`
with an explanatory variable\ $x$ and a response variable\ $y$.\index{Response variable}\index{Explanatory variable}\index{Graphs!scatterplot}
In the graph, a sensible line is drawn on the graph that seems to capture the relationship between\ $x$ and\ $y$.
(You may have drawn a slightly different, but similar, line.)
The line describes the predicted mean values of\ $y$ for various values of\ $x$.
The relationship is *positive* and *linear*.
A *regression equation* specifies the line.\index{Regression!equation}
In the regression equation $\hat{y} = b_0 + b_1 x$, the numbers\ $b_0$ and\ $b_1$ are called *regression coefficients*,\index{Regression!coefficients} where
* $b_0$ is the *intercept* (or the *$y$-intercept*),\index{Regression!equation!intercept}\index{Linear equations!intercept}
whose value corresponds to the *predicted* mean value of\ $y$ when $x = 0$.
* $b_1$ is the *slope*,\index{Regression!equation!slope}\index{Linear equations!slope}
whose value measures how much the value of\ $y$ changes, on average, when the value of\ $x$ *increases* by\ $1$.
We will use software to find the values of\ $b_0$ and\ $b_1$ (as the formulas are tedious to use).
However, a rough approximation of the values of\ $b_0$ and\ $b_1$ can be obtained using the rough straight line drawn on the scatterplot
`r if (knitr::is_latex_output()) {
'(Fig.\\ \\@ref(fig:ExampleScatterLATEX)).'
} else {
'(Fig.\\ \\@ref(fig:ExampleScatterHTML)).'
}`
A rough approximation of the value of the *intercept*\ $b_0$ is the value of\ $\hat{y}$ when $x = 0$, from the drawn line.
When $x = 0$, the regression line suggests the value of\ $\hat{y}$ is about\ $2$
`r if (knitr::is_latex_output()) {
'(left panel)'
}`;
that is, the value of\ $b_0$ is approximately\ $2$.
A rough approximation of the *slope*\ $b_1$ is found using\index{Regression!equation!rise over run}
\begin{equation}
\frac{\text{Change in $y$}}{\text{Corresponding \emph{increase} in $x$}}
= \frac{\text{rise}}{\text{run}}
(\#eq:RiseOverRun)
\end{equation}
from the drawn line.
This approximation of the slope is the *change* in the value of\ $y$ (the 'rise') divided by the corresponding *increase* in the value of\ $x$ (the 'run').
<!-- FOR LaTeX: Put this example data plot and the static rise/run in the same plot -->
<!-- FOT HTML, separate plots -->
```{r ExampleScatterHTML, fig.cap="An example scatterplot.", fig.align="center", fig.width=5, fig.height=4}
if (knitr::is_html_output()){
set.seed(5000)
x <- seq(0.5, 5,
length = 10)
mu <- 2 + 3 * x
y <- mu + rnorm(length(x), 0, 0.5)
m1 <- lm(y ~ x)
plot(y ~ x,
xlim = c(-0.5, 5),
ylim = c(0, 20),
las = 1,
xlab = expression(paste("Explanatory, ", italic(x)) ),
ylab = expression(paste("Response, ", italic(y)) ),
pch = 19)
grid()
abline( coef(m1),
col = "grey",
lwd = 2)
}
```
```{r AnimateRiseRun, animation.hook="gifski", interval=0.85, dev=if (is_latex_output()){"pdf"}else{"png"}}
if (knitr::is_html_output()){
NumPlots <- 9
set.seed(5000)
x <- seq(0.5, 5,
length = 10)
b0 <- 2
b1 <- 3
mu <- b0 + b1 * x
y <- mu + rnorm(length(x), 0, 0.5)
x1 <- 1
x2 <- 5
y1 <- b0 + (b1 * x1)
y2 <- b0 + (b1 * x2)
for (i in (1:NumPlots)){
par( mar = c(5, 4, 4, 2) + 0.1)
# DATA
plot(y ~ x,
xlim = c(-0.5, 5),
ylim = c(0, 20),
type = "n",
las = 1,
xlab = expression(paste("Explanatory, ", italic(x)) ),
ylab = expression(paste("Response, ", italic(y)) ),
pch = 19)
rug(1:20,
side = 2,
ticksize = -0.02)
grid()
points(y ~ x,
pch = 19)
if ( i >=2 ){ # DATA + LINE
abline(b0, b1,
col = "black",
lwd = 2)
}
if (i >= 3) { # DATA + LINE + x1
lines( c(x1, x1),
c(0, y1),
lwd = 2,
col = "darkgrey",
lty = 2)
}
if ( i >= 4){ # DATA + LINE + x1 + y1
lines( c(0, x1),
c(y1, y1),
lwd = 2,
col = "darkgrey",
lty = 2)
}
if ( i >= 5 ){ # DATA + LINE + x1 + y1 + x2
lines( c(x2, x2),
c(0, y2),
lwd = 2,
col = "darkgrey",
lty = 2)
}
if ( i >= 6) { # DATA + LINE + x1 + y1 + x2 + y2
lines( c(0, x2),
c(y2, y2),
lwd = 2,
col = "darkgrey",
lty = 2)
}
if (i >= 7 ) { # DATA + LINE + x1 + y1 + x2 + y2 + RUN
run <- x2 - x1
arrows(x1, 2,
x2, 2,
lwd = 2,
length = 0.15,
col = "darkgrey",
code = 2)
text( mean( c(x1, x2)), 2.5,
labels = bquote("Run: "~ .(x2)-.(x1) == .(run)),
pos = 2 )
}
if (i >= 8 ){ # DATA + LINE + x1 + y1 + x2 + y2 + RUN + RISE
rise <- y2 - y1
arrows(0.5, y1,
0.5, y2,
lwd = 2,
length = 0.15,
col = "darkgrey",
code = 2)
text( 0.5,
mean( c(y1, y2)),
labels = bquote("Rise: "~ .(y2)-.(y1) == .(rise)),
pos = 4 )
}
if (i >= 9 ) { # DATA + LINE + x1 + y1 + x2 + y2 + RUN + RISE + SLOPE
slope <- rise / run
polygon( c(2.15, 2.15, 4.85, 4.85),
c(6, 8, 8, 6),
border = NA, # No border
col = "antiquewhite1")
text(3.5,
7,
labels = bquote("Slope: "~ .(rise) %/% .(run) == .(slope)) )
}
}
}
```
<!-- FOR LaTeX: Put this example data plot and the static rise/run in the same plot -->
```{r ExampleScatterLATEX, fig.cap="Estimating the regression equation from a line using an example scatterplot. Left: approximating $b_0$. Right: approximating $b_1$ using rise-over-run.", fig.align="center", fig.width=9, fig.height=4, out.width='100%'}
set.seed(5000)
x <- seq(0.5, 5,
length = 10)
b0 <- 2
b1 <- 3
mu <- 2 + 3 * x
y <- mu + rnorm(length(x), 0, 0.5)
if (knitr::is_latex_output()){
par(mfrow = c(1, 2))
plot(y ~ x,
xlim = c(-0.5, 5),
ylim = c(0, 20),
las = 1,
main = expression(Approximating~italic(b)[0]),
xlab = expression(Explanatory~italic(x) ),
ylab = expression(Response~italic(y) ),
pch = 19,
axes = FALSE,
col = "grey")
box()
axis(side = 2,
las = 2)
grid()
rug(1:20,
side = 2,
ticksize = -0.02)
axis(side = 1,
at = 0:5,
labels = c( expression( italic(x) == 0),
1 : 5))
abline( c(b0, b1),
col = "black",
lwd = 2)
text(x = 2,
y = 15,
pos = 3,
label = "Regression line")
arrows(x0 = 2,
x1 = 2.75,
y0 = 15,
y1 = b0 + (b1 * 2.75),
lwd = 2,
angle = 15,
length = 0.25,
col = "darkgrey")
# Beta 0
abline(v = 0,
lwd = 2,
col = "darkgrey")
lines( x = c(0, 0),
y = c(0, b0),
lty = 1,
lwd = 2,
col = "darkgrey")
lines( x = c(-1, 1.5),
y = c(b0, b0),
lty = 1,
lwd = 2,
col = "darkgrey")
arrows(x0 = 3,
x1 = 1.75,
y0 = 4,
y1 = b0,
angle = 10,
length = 0.125,
lwd = 2)
text(x = 4,
y = 2.5,
#pos = 4,
labels = expression( atop(Approximate,
value~of~italic(b)[0])) )
######
x1 <- 1
x2 <- 5
y1 <- b0 + (b1 * x1)
y2 <- b0 + (b1 * x2)
NumPlots <- i <- 9
# for (i in (NumPlots:NumPlots)){
# DATA
plot(y ~ x,
xlim = c(-0.5, 5),
ylim = c(0, 20),
type = "n",
las = 1,
main = expression(Approximating~italic(b)[1]),
xlab = expression(Explanatory~italic(x) ),
ylab = expression(Response~italic(y) ),
pch = 19)
rug(1:20,
side = 2,
ticksize = -0.02)
grid()
points(y ~ x,
pch = 19,
col = "grey")
if ( i >=2 ){ # DATA + LINE
abline(b0, b1,
col = "black",
lwd = 2)
}
if (i >= 3) { # DATA + LINE + x1
lines( c(x1, x1),
c(0, y1),
lwd = 2,
col = "darkgrey",
lty = 2)
}
if ( i >= 4){ # DATA + LINE + x1 + y1
lines( c(-1, x1),
c(y1, y1),
lwd = 2,
col = "darkgrey",
lty = 2)
}
if ( i >= 5 ){ # DATA + LINE + x1 + y1 + x2
lines( c(x2, x2),
c(0, y2),
lwd = 2,
col = "darkgrey",
lty = 2)
}
if ( i >= 6) { # DATA + LINE + x1 + y1 + x2 + y2
lines( c(-1, x2),
c(y2, y2),
lwd = 2,
col = "darkgrey",
lty = 2)
}
if (i >= 7 ) { # DATA + LINE + x1 + y1 + x2 + y2 + RUN
run <- x2 - x1
arrows(x1, 2,
x2, 2,
lwd = 2,
length = 0.15,
angle = 15,
col = "darkgrey",
code = 2)
text( mean( c(x1, x2)),
2,
labels = bquote("Run: "~ .(x2)-.(x1) == .(run)),
pos = 3 )
}
if (i >= 8 ){ # DATA + LINE + x1 + y1 + x2 + y2 + RUN + RISE
arrows(0.5, y1,
0.5, y2,
lwd = 2,
length = 0.15,
angle = 15,
col = "darkgrey",
code = 2)
rise <- y2 - y1
text( 0.5,
mean( c(y1, y2)) + 1.5,
labels = bquote("Rise: "~ .(y2)-.(y1) == .(rise)),
pos = 4 )
}
# }
}
```
Consider what happens in
`r if (knitr::is_latex_output()) {
'Fig.\\ \\@ref(fig:ExampleScatterLATEX) (right panel)'
} else {
'the animation below'
}`
when the value of\ $x$ increases from\ $1$ to\ $5$ (an *increase* of $5 - 1 = 4$).
The corresponding value of\ $y$ changes from about\ $5$ to about\ $17$, a change (specifically, an *increase*) of $17 - 5 = 12$.
So, using Eq.\ \@ref(eq:RiseOverRun),
$$
\frac{\text{rise}}{\text{run}}
= \frac{17 - 5}{5 - 1}
= \frac{12}{4}
= 3.
$$
The value of\ $b_1$ is about\ $3$.
When using rise-over-run, better guesses for\ $b_1$ are found when using one value of\ $x$ near the left-side of the scatterplot, and another value of\ $x$ near the right-side of the scatterplot, but any two values can be used.
Combing the rough guesses for $b_0$ and $b_1$, the regression line is approximately $\hat{y} = 2 + (3\times x)$, usually written
$$
\hat{y} = 2 + 3x.
$$
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
The regression equation has\ $\hat{y}$ (not\ $y$) on the left-hand side.
That is, the equation *predicts* the *mean* values of\ $y$, which may not be equal to any of the observed values (which are denoted by\ $y$).
A 'good' regression equation would produce predicted values\ $\hat{y}$ close to the observed values\ $y$; that is, the line passes close to each point on the scatterplot.\index{Graphs!scatterplot}
:::
The *intercept*\ $b_0$ has the same measurement units as the response variable.\index{Regression!equation!intercept}
The measurement units for the *slope*\ $b_1$ is the 'measurement units of the response variable', per 'measurement units of the explanatory variable'.\index{Regression!equation!intercept}
:::{.example name="Measurement units of regression parameters"}
In Example\ \@ref(exm:RegressionGirlsHt), the regression line for the relationship between the age of Australian girls in years\ $x$ and their height (in\ cm)\ $y$ was $\hat{y} = 73 + 7x$ (for girls aged between four and seven years).
In the equation, the intercept is $b_0 = 73\cms$ and the slope is $b_1 = 73\cms$/y (the growth *rate*).
:::
:::{.example name="A rough approximation of the regression equation"}
For the dog-breed data (Fig.\ \@ref(fig:DogsLifeScatterjamovi2), left panel), a rough estimate of the regression line can be drawn on a scatterplot to estimate\ $b_0$ and\ $b_1$ (Fig.\ \@ref(fig:DogBreedRiseRun)).
The estimate of\ $b_0$ (the value of\ $y$ when $x = 0$) is roughly\ $13.5\ys$.
The estimate of\ $b_1$ can be found using the rise-over-run idea.
When $x = 0$, the value of\ $\hat{y}$ (according to the drawn line) is about\ $13.5$.
At the other extreme of the plot, where $x = 80$, the value of\ $y$ is about\ $5.5$.
(Any two points on the line can be used, but using two points at each end gives better guesses of the slope.)
So, as\ $x$ increases from\ $0$ to about\ $80$, the value of\ $\hat{y}$ *reduces* from about\ $13.5$ to about\ $5.5$, a change of about\ $-8$.
That is, for a 'run' of $80 - 0 = 80$, the 'rise' is $5.5 - 13.5 = -8$ (i.e., a drop of\ $8$), and so a rough estimate of the slope is $-8/80 = -0.1\ys$/kg.\index{Regression!equation!rise over run}
(The relationship is *negative*, so the slope is *negative*.)
The rough guess of the regression line is therefore
$$
\hat{y} = 13.5 - 0.1x,
$$
where\ $x$ is the breed-weight (in\ kgs), and\ $y$ is lifespan (in\ years).
The rough guess of the intercept\ $b_0$ is\ $13.5\ys$, while the rough guess of the slope\ $b_1$ is\ $-0.1\ys$/kg.
:::
```{r DogBreedRiseRun, fig.cap="Obtaining rough guesses for the regression equation for the dog-breed data. Left: approximating $b_0$. Right: approximating $b_1$ using rise-over-run. The plus signs on the right plot indicate the points used to estimate the slope.", fig.align="center", fig.width=9, fig.height=4, out.width='100%'}
mLifespan <- lm( Lifespan ~ Weight,
data = DogsLife)
b0 <- 13.5
b1 <- -0.1
par(mfrow = c(1, 2))
plot(Lifespan ~ Weight,
data = DogsLife,
col = "darkgrey",
xlim = c(-5, 80),
ylim = c(4, 16),
las = 1,
axes = FALSE,
main = expression(Dog~breeds*":"~approximating~italic(b)[0]),
xlab = expression(Breed~weight~italic(x) ),
ylab = expression(Breed~lifespan~italic(y) ),
pch = 19)
box()
grid()
axis(side = 1,
las = 1,
at = c(0, 20, 40, 60, 80),
labels = c(expression(italic(x)==0),
20, 40, 60, 80))
axis(side = 2,
las = 1,
at = seq(4, 16, by = 4) )
axis(side = 2,
at = 13.5,
las = 1,
cex.axis = 0.8)
abline(b0, b1,
col = "black",
lwd = 2)
# Beta 0
abline(v = 0,
lwd = 2,
col = "darkgrey")
lines( x = c(-15, 12),
y = c(13.5, 13.5),
lty = 1,
lwd = 2,
col = "darkgrey")
arrows(x0 = 20,
x1 = 10,
y0 = 15,
y1 = 13.5,
#angle = 15,
length = 0.1,
lwd = 2)
text(x = 20,
y = 15,
pos = 4,
labels = expression(Approximate~value~of~italic(b)[0]))
######
x1 <- 0
x2 <- 80
y1 <- b0 + (b1 * x1)
y2 <- b0 + (b1 * x2)
# DATA
plot(Lifespan ~ Weight,
data = DogsLife,
xlim = c(-5, 80),
ylim = c(4, 16),
las = 1,
axes = FALSE,
main = expression(Dog~breeds*":"~approximating~italic(b)[1]),
xlab = expression(Breed~weight~italic(x) ),
ylab = expression(Breed~lifespan~italic(y) ),
pch = 19)
box()
grid()
axis(side = 1,
las = 1,
at = c(0, 20, 40, 60, 80) )
axis(side = 2,
las = 1,
at = seq(4, 16, by = 4) )
axis(side = 2,
at = 13.5,
las = 1,
cex.axis = 0.8)
axis(side = 2,
at = 5.5,
las = 1,
cex.axis = 0.8)
# rug(1:20,
# side = 2,
# ticksize = -0.02)
grid()
points(Lifespan ~ Weight,
data = DogsLife,
col = "darkgrey",
pch = 19)
abline(b0, b1,
col = "black",
lwd = 2)
# DATA + LINE + x1
# lines( c(x1, x1),
# c(-2, y1),
# lwd = 2,
# col = "black",
# lty = 2)
#
# # # DATA + LINE + x1 + y1
# lines( c(-2, x1),
# c(y1, y1),
# lwd = 2,
# col = "black",
# lty = 2)
#
# # DATA + LINE + x1 + y1 + x2
# lines( c(x2, x2),
# c(0, y2),
# lwd = 2,
# col = "black",
# lty = 2)
#
# # DATA + LINE + x1 + y1 + x2 + y2
# lines( c(-2, x2),
# c(y2, y2),
# lwd = 2,
# col = "black",
# lty = 2)
#
# DATA + LINE + x1 + y1 + x2 + y2 + RUN
# RUN
run <- x2 - x1
arrows(x0 = x1 + 1,
y0 = 5.5,
x1 = x2 - 3,
y1 = 5.5,
lwd = 2,
length = 0.1,
angle = 10,
col = "darkgrey",
code = 2)
text( mean( c(x1, x2)),
4.5,
labels = bquote("Run: "~ .(x2)-.(x1) == .(run)) ) # Get proper minus sign, not dash, for run
# DATA + LINE + x1 + y1 + x2 + y2 + RUN + RISE
# RISE
arrows(x0 = 0,
y0 = y1 - 0.5,
x1 = 0,
y1 = y2 + 0.25,
lwd = 2,
length = 0.1,
angle = 10,
col = "darkgrey",
code = 2)
rise <- y2 - y1
text( x = 1,
y = 8.75,
labels = "Rise: ",
pos = 4)
text( x = 1,
y = 7.5,
labels = bquote( .(y2)-.(y1) == .("\u2212") * .(abs(rise))), # To get a proper negative sign
pos = 4 )
# MARK THE POINTS from which rise and run are determined
points(x1, y1,
cex = 1.5,
pch = 3)
points(x2, y2,
cex = 1.5,
pch = 3)
```
`r if (knitr::is_html_output()){
'You may like the following interactive activity, which explores slopes and intercepts.'}`
<iframe src="https://phet.colorado.edu/sims/html/graphing-slope-intercept/latest/graphing-slope-intercept_en.html" width="600" height="450" scrolling="no" allowfullscreen="allowfullscreen"></iframe>
::: {.example #CycloneRegressionGuesses name="Estimating regression parameters"}
The relationship between the number of cyclones\ $y$ in the Australian region each year from\ 1969 to\ 2005, and the ONI\ $x$ is shown in Fig.\ \@ref(fig:ONIcyclonesCorrelation) (left panel).
To make a guess of the regression coefficients, a sensible line can be drawn through the data (Fig.\ \@ref(fig:ONIcyclones)).
When the value of\ $x$ is zero, the predicted value of\ $y$ is about\ $12$, so\ $b_0$ is about\ $12$\ cyclones.
Recall: the intercept is the predicted value of\ $y$ when $x = 0$, which is *not* at the left of the graph in Fig.\ \@ref(fig:ONIcyclones).
To approximate the value of\ $b_1$, use the rise-over-run idea.\index{Regression!equation!rise over run}
When\ $x = -2$, the predicted mean value of\ $y$ is about\ $17$; when\ $x = 2$, the predicted mean value of\ $y$ is about\ $8$.
The value of\ $x$ increases by $2 - (-2) = 4$, while the value of\ $y$ changes by $7.5 - 17 = -9.5$ (a *decrease* of about $9.5$).
Hence, $b_1$ is approximately $-9.5/4 = -2.375$ cyclones per unit change in ONI.
(You may get a slightly different value from a slightly different line.)
The relationship has a *negative* direction, so the slope must be *negative*.
The regression line is approximately $\hat{y} = 12 - 2.375x$.
:::
```{r ONIcyclones, out.width="70%", fig.align="center", fig.cap="The number of cyclones in the Australian region each year from 1969 to 2005, and the ONI averaged over October, November, December. The plus sign ${}+{}$ is located on the line where $x = 0$. The crosses ${}\\times{}$ are located to find rise-over-run.", fig.width=6, fig.height=4}
plot(Total ~ OND,
data = Cyclones,
xlim = c(-2.65, 3),
ylim = c(0, 20),
pch = 19,
col = "darkgrey",
ylab = "Total number of cyclones",
xlab = "ONI averaged over Oct., Nov., Dec.",
main = "Cyclone numbers vs ONI",
las = 1)
clm <- lm( Total ~ OND,
data = Cyclones)
abline( coef(clm),
lwd = 2)
grid(lty = 2)
axis(side = 2,
at = c(7.5, 17),
las = 1,
cex.axis = 0.8)
rise1 <- predict( clm,
newdata = data.frame(OND = -2))
lines(x = c(-2, -2),
y = c(1, rise1),
lty = 5,
lwd = 2,
col = plotDark)
rise2 <- predict( clm,
newdata = data.frame(OND = 2))
lines(x = c(2, 2),
y = c(1, rise2),
lty = 5,
lwd = 2,
col = plotDark)
text(x = 0,
y = 1,
pos = 3,
labels = "'Run'")
points(x = 0,
y = coef(clm)[1],
pch = 3,
cex = 1.75)
arrows(x0 = -2,
x1 = 2,
y0 = 1,
y1 = 1,