forked from saundersg/Statistics-Notebook
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLinearRegression.html
8334 lines (8253 loc) · 675 KB
/
LinearRegression.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Linear Regression</title>
<script src="site_libs/header-attrs-2.14/header-attrs.js"></script>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cerulean.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<script src="site_libs/navigation-1.1/codefolding.js"></script>
<script src="site_libs/htmlwidgets-1.5.4/htmlwidgets.js"></script>
<script src="site_libs/plotly-binding-4.10.0/plotly.js"></script>
<script src="site_libs/typedarray-0.1/typedarray.min.js"></script>
<link href="site_libs/crosstalk-1.2.0/css/crosstalk.min.css" rel="stylesheet" />
<script src="site_libs/crosstalk-1.2.0/js/crosstalk.min.js"></script>
<link href="site_libs/plotly-htmlwidgets-css-2.5.1/plotly-htmlwidgets.css" rel="stylesheet" />
<script src="site_libs/plotly-main-2.5.1/plotly-latest.min.js"></script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ background-color: #f8f8f8; }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ef2929; } /* Alert */
code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #c4a000; } /* Attribute */
code span.bn { color: #0000cf; } /* BaseN */
code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4e9a06; } /* Char */
code span.cn { color: #000000; } /* Constant */
code span.co { color: #8f5902; font-style: italic; } /* Comment */
code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code span.dt { color: #204a87; } /* DataType */
code span.dv { color: #0000cf; } /* DecVal */
code span.er { color: #a40000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #0000cf; } /* Float */
code span.fu { color: #000000; } /* Function */
code span.im { } /* Import */
code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code span.ot { color: #8f5902; } /* Other */
code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code span.sc { color: #000000; } /* SpecialChar */
code span.ss { color: #4e9a06; } /* SpecialString */
code span.st { color: #4e9a06; } /* String */
code span.va { color: #000000; } /* Variable */
code span.vs { color: #4e9a06; } /* VerbatimString */
code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
.sourceCode .row {
width: 100%;
}
.sourceCode {
overflow-x: auto;
}
.code-folding-btn {
margin-right: -30px;
}
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
for (var j = 0; j < rules.length; j++) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') continue;
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<link rel="stylesheet" href="styles.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.tab('show');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
.code-folding-btn { margin-bottom: 4px; }
</style>
</head>
<body>
<div class="container-fluid main-container">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-bs-toggle="collapse" data-target="#navbar" data-bs-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html"><img src='Images/snlogo.png' alt='Statistics Notebook Logo' style='height: 30px; margin: -5px 0px'></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
R Help
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="RCommands.html">R Commands</a>
</li>
<li>
<a href="RMarkdownHints.html">R Markdown Hints</a>
</li>
<li>
<a href="RCheatSheetsAndNotes.html">R Cheatsheets & Notes</a>
</li>
<li>
<a href="DataSources.html">Data Sources</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Describing Data
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="GraphicalSummaries.html">Graphical Summaries</a>
</li>
<li>
<a href="NumericalSummaries.html">Numerical Summaries</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Making Inference
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="MakingInference.html">Making Inference</a>
</li>
<li>
<a href="tTests.html">t Tests</a>
</li>
<li>
<a href="WilcoxonTests.html">Wilcoxon Tests</a>
</li>
<li>
<a href="Kruskal.html">Kruskal-Wallis Test</a>
</li>
<li>
<a href="ANOVA.html">ANOVA</a>
</li>
<li>
<a href="LinearRegression.html">Linear Regression</a>
</li>
<li>
<a href="LogisticRegression.html">Logistic Regression</a>
</li>
<li>
<a href="ChiSquaredTests.html">Chi Squared Tests</a>
</li>
<li>
<a href="PermutationTests.html">Randomization</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Analyses
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="./Analyses/StudentHousing.html">Good Example Analysis</a>
</li>
<li>
<a href="./Analyses/StudentHousingPOOR.html">Poor Example Analysis</a>
</li>
<li>
<a href="./Analyses/Rent.html">Rent</a>
</li>
<li>
<a href="./Analyses/Stephanie.html">Stephanie</a>
</li>
<li>
<a href="./Analyses/t Tests/HighSchoolSeniors.html">High School Seniors</a>
</li>
<li>
<a href="./Analyses/Wilcoxon Tests/RecallingWords.html">Recalling Words</a>
</li>
<li>
<a href="./Analyses/ANOVA/MyTwoWayANOVA.html">My Two-way ANOVA</a>
</li>
<li>
<a href="./Analyses/Kruskal-Wallis Test/Food.html">Food</a>
</li>
<li>
<a href="./Analyses/Linear Regression/MySimpleLinearRegression.html">My Simple Linear Regression</a>
</li>
<li>
<a href="./Analyses/Linear Regression/CarPrices.html">Car Prices</a>
</li>
<li>
<a href="./Analyses/Logistic Regression/MyLogisticRegression.html">My Logistic Regression</a>
</li>
<li>
<a href="./Analyses/Chi Squared Tests/MyChiSquaredTest.html">My Chi-sqaured Test</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<div class="btn-group pull-right float-right">
<button type="button" class="btn btn-default btn-xs btn-secondary btn-sm dropdown-toggle" data-toggle="dropdown" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span>Code</span> <span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right" style="min-width: 50px;">
<li><a id="rmd-show-all-code" href="#">Show All Code</a></li>
<li><a id="rmd-hide-all-code" href="#">Hide All Code</a></li>
</ul>
</div>
<h1 class="title toc-ignore">Linear Regression</h1>
</div>
<script type="text/javascript">
function showhide(id) {
var e = document.getElementById(id);
e.style.display = (e.style.display == 'block') ? 'none' : 'block';
}
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
<hr />
<p>Determine which explanatory variables have a significant effect on
the mean of the quantitative response variable.</p>
<hr />
<div id="simple-linear-regression"
class="section level2 tabset tabset-fade tabset-pills">
<h2 class="tabset tabset-fade tabset-pills">Simple Linear
Regression</h2>
<div style="float:left;width:125px;" align="center">
<p><img src="Images/QuantYQuantX.png" width=58px;></p>
</div>
<p>Simple linear regression is a good analysis technique when the data
consists of a single quantitative response variable <span
class="math inline">\(Y\)</span> and a single quantitative explanatory
variable <span class="math inline">\(X\)</span>.</p>
<div id="overview" class="section level3 tabset">
<h3 class="tabset">Overview</h3>
<div style="padding-left:125px;">
<p><strong>Mathematical Model</strong></p>
<p>The true regression model assumed by a regression analysis is given
by</p>
<div
style="float:right;font-size:.8em;background-color:lightgray;padding:5px;border-radius:4px;">
<a style="color:darkgray;" href="javascript:showhide('simplelinearlatexrcode')">Math
Code</a>
</div>
<div id="simplelinearlatexrcode" style="display:none;">
<pre><code>$$
\underbrace{Y_i}_\text{Some Label} = \overbrace{\beta_0}^\text{y-int} + \overbrace{\beta_1}^\text{slope} \underbrace{X_i}_\text{Some Label} + \epsilon_i \quad \text{where} \ \epsilon_i \sim N(0, \sigma^2)
$$</code></pre>
</div>
<center>
<span class="tooltipr"> <span class="math inline">\(Y_i\)</span> <span
class="tooltiprtext">The response variable. The “i” denotes that this is
the y-value for individual “i”, where “i” is 1, 2, 3,… and so on up to
<span class="math inline">\(n\)</span>, the sample size.</span>
</span><span class="tooltipr"> <span class="math inline">\(=\)</span>
<span class="tooltiprtext">This states that we are assuming <span
class="math inline">\(Y_i\)</span> was created, or is “equal to” the
formula that will follow on the right-hand-side of the equation.</span>
</span><span class="tooltipr"> <span
class="math inline">\(\underbrace{\overbrace{\beta_0}^\text{y-intercept}
+ \overbrace{\beta_1}^\text{slope} X_i \ }_\text{true regression
relation}\)</span> <span class="tooltiprtext">The true regression
relation is a line, a line that is typically unknown in real life. It
can be likened to “God’s Law” or “Natural Law”. Something that governs
the way the data behaves, but is unkown to us.</span> </span><span
class="tooltipr"> <span class="math inline">\(+\)</span> <span
class="tooltiprtext">This plus sign emphasizes that the actual data, the
<span class="math inline">\(Y_i\)</span>, is created by adding together
the value from the true line <span class="math inline">\(\beta_0 +
\beta_1 X_i\)</span> and an individual error term <span
class="math inline">\(\epsilon_i\)</span>, which allows each dot in the
regression to be off of the line by a certain amount called <span
class="math inline">\(\epsilon_i\)</span>.</span> </span><span
class="tooltipr"> <span
class="math inline">\(\overbrace{\epsilon_i}^\text{error term}\)</span>
<span class="tooltiprtext">Error term for each individual <span
class="math inline">\(i\)</span>. The error terms are “random” and
unique for each individual. This provides the statistical relationship
of the regression. It is what allows each dot to be different, while
still coming from the same line, or underlying law.</span> </span><span
class="tooltipr"> <span class="math inline">\(\quad
\text{where}\)</span> <span class="tooltiprtext">Some extra comments are
needed about <span class="math inline">\(\epsilon_i\)</span>…</span>
</span><span class="tooltipr"> <span class="math inline">\(\
\overbrace{\epsilon_i \sim N(0, \sigma^2)}^\text{error term normally
distributed}\)</span> <span class="tooltiprtext">The error terms <span
class="math inline">\(\epsilon_i\)</span> are assumed to be normally
distributed with constant variance. Pay special note that the <span
class="math inline">\(\sigma\)</span> does not have an <span
class="math inline">\(i\)</span> in it, so it is the same for each
individual. In other words, the variance is constant. The mean of the
errors is zero, which causes the dots to be spread out symmetrically
both above and below the line.</span> </span>
</center>
<p><br/></p>
<p>The estimated regression line obtained from a regression analysis,
pronounced “y-hat”, is written as</p>
<div
style="float:right;font-size:.8em;background-color:lightgray;padding:5px;border-radius:4px;">
<a style="color:darkgray;" href="javascript:showhide('simplelinearlatexrcodeyhat')">Math
Code</a>
</div>
<div id="simplelinearlatexrcodeyhat" style="display:none;">
<pre><code>$$
\underbrace{\hat{Y}_i}_\text{Some Label} = \overbrace{b_0}^\text{est. y-int} + \overbrace{b_1}^\text{est. slope} \underbrace{X_i}_\text{Some Label}
$$</code></pre>
</div>
<center>
<span class="tooltipr"> <span class="math inline">\(\hat{Y}_i\)</span>
<span class="tooltiprtext">The estimated average y-value for individual
<span class="math inline">\(i\)</span> is denoted by <span
class="math inline">\(\hat{Y}_i\)</span>. It is important to recognize
that <span class="math inline">\(Y_i\)</span> is the actual value for
individual <span class="math inline">\(i\)</span>, and <span
class="math inline">\(\hat{Y}_i\)</span> is the average y-value for all
individuals with the same <span class="math inline">\(X_i\)</span>
value.</span> </span><span class="tooltipr"> <span
class="math inline">\(=\)</span> <span class="tooltiprtext">The formula
for the average y-value, <span class="math inline">\(\hat{Y}_i\)</span>
is equal to what follows…</span> </span><span class="tooltipr"> <span
class="math inline">\(\underbrace{\overbrace{\ b_0 \
}^\text{y-intercept} + \overbrace{b_1}^\text{slope} X_i \
}_\text{estimated regression relation}\)</span> <span
class="tooltiprtext">Two things are important to notice about this
equation. First, it uses <span class="math inline">\(b_0\)</span> and
<span class="math inline">\(b_1\)</span> instead of <span
class="math inline">\(\beta_0\)</span> and <span
class="math inline">\(\beta_1\)</span>. This is because <span
class="math inline">\(b_0\)</span> and <span
class="math inline">\(b_1\)</span> are the estimated y-intercept and
slope, respectively, not the true y-intercept <span
class="math inline">\(\beta_0\)</span> and true slope <span
class="math inline">\(\beta_1\)</span>. Second, this equation does not
include <span class="math inline">\(\epsilon_i\)</span>. In other words,
it is the estimated regression line, so it only describes the average
y-values, not the actual y-values.</span> </span>
</center>
<p><br/></p>
<div style="font-size:0.8em;">
<p>Note: see the <strong>Explanation</strong> tab <strong>The
Mathematical Model</strong> for details about these equations.</p>
</div>
<p><strong>Hypotheses</strong></p>
<div
style="float:right;font-size:.8em;background-color:lightgray;padding:5px;border-radius:4px;">
<a style="color:darkgray;" href="javascript:showhide('simplelinearhypecodeslope')">Math
Code</a>
</div>
<div id="simplelinearhypecodeslope" style="display:none;">
<pre><code>$$
\left.\begin{array}{ll}
H_0: \beta_1 = 0 \\
H_a: \beta_1 \neq 0
\end{array}
\right\} \ \text{Slope Hypotheses}
$$
$$
\left.\begin{array}{ll}
H_0: \beta_0 = 0 \\
H_a: \beta_0 \neq 0
\end{array}
\right\} \ \text{Intercept Hypotheses}
$$</code></pre>
</div>
<div style="clear:right;">
</div>
<p><span class="math display">\[
\left.\begin{array}{ll}
H_0: \beta_1 = 0 \\
H_a: \beta_1 \neq 0
\end{array}
\right\} \ \text{Slope Hypotheses}^{\quad \text{(most
common)}}\quad\quad
\]</span></p>
<p><span class="math display">\[
\left.\begin{array}{ll}
H_0: \beta_0 = 0 \\
H_a: \beta_0 \neq 0
\end{array}
\right\} \ \text{Intercept Hypotheses}^{\quad\text{(sometimes useful)}}
\]</span></p>
<p><br/></p>
<p>If <span class="math inline">\(\beta_1 = 0\)</span>, then the model
reduces to <span class="math inline">\(Y_i = \beta_0 +
\epsilon_i\)</span>, which is a flat line. This means <span
class="math inline">\(X\)</span> does not improve our understanding of
the mean of <span class="math inline">\(Y\)</span> if the null
hypothesis is true.</p>
<p>If <span class="math inline">\(\beta_0 = 0\)</span>, then the model
reduces to <span class="math inline">\(Y_i = \beta_1 X +
\epsilon_i\)</span>, a line going through the origin. This means the
average <span class="math inline">\(Y\)</span>-value is <span
class="math inline">\(0\)</span> when <span
class="math inline">\(X=0\)</span> if the null hypothesis is true.</p>
<p><strong>Assumptions</strong></p>
<p>This regression model is appropriate for the data when five
assumptions can be made.</p>
<ol style="list-style-type: decimal">
<li><p><strong>Linear Relation</strong>: the true regression relation
between <span class="math inline">\(Y\)</span> and <span
class="math inline">\(X\)</span> is linear.</p></li>
<li><p><strong>Normal Errors</strong>: the error terms <span
class="math inline">\(\epsilon_i\)</span> are normally distributed with
a mean of zero.</p></li>
<li><p><strong>Constant Variance</strong>: the variance <span
class="math inline">\(\sigma^2\)</span> of the error terms is constant
(the same) over all <span class="math inline">\(X_i\)</span>
values.</p></li>
<li><p><strong>Fixed X</strong>: the <span
class="math inline">\(X_i\)</span> values can be considered fixed and
measured without error.</p></li>
<li><p><strong>Independent Errors</strong>: the error terms <span
class="math inline">\(\epsilon_i\)</span> are independent.</p></li>
</ol>
<div style="font-size:0.8em;">
<p>Note: see the <strong>Explanation</strong> tab <strong>Residual Plots
& Regression Assumptions</strong> for details about checking the
regression assumptions.</p>
</div>
<p><strong>Interpretation</strong></p>
<p>The slope is interpreted as, “the change in the average y-value for a
one unit change in the x-value.” It <strong>is not</strong> the average
change in y. <strong>It is</strong> the change in the average
y-value.</p>
<p>The y-intercept is interpreted as, “the average y-value when x is
zero.” It is often not meaningful, but is sometimes useful. It just
depends if x being zero is meaningful or not within the context of your
analysis. For example, knowing the average price of a car with zero
miles is useful. However, pretending to know the average height of adult
males that weigh zero pounds, is not useful.</p>
<hr />
</div>
</div>
<div id="r-instructions" class="section level3">
<h3>R Instructions</h3>
<div style="padding-left:125px;">
<p><strong>Console</strong> Help Command: <code>?lm()</code></p>
<p><strong>Perform the Regression</strong></p>
<a href="javascript:showhide('simplelinearrcode')">
<div class="hoverchunk">
<p><span class="tooltipr"> mylm <span class="tooltiprtext">This is some
name you come up with that will become the R object that stores the
results of your linear regression <code>lm(...)</code> command.</span>
</span><span class="tooltipr"> <- <span class="tooltiprtext">This
is the “left arrow” assignment operator that stores the results of your
<code>lm()</code> code into <code>mylm</code> name.</span> </span><span
class="tooltipr"> lm( <span class="tooltiprtext">lm(…) is an R function
that stands for “Linear Model”. It performs a linear regression analysis
for Y ~ X.</span> </span><span class="tooltipr"> Y <span
class="tooltiprtext">Y is your quantitative response variable. It is the
name of one of the columns in your data set.</span> </span><span
class="tooltipr"> ~ <span class="tooltiprtext">The tilde symbol ~ is
used to tell R that Y should be treated as the response variable that is
being explained by the explanatory variable X.</span> </span><span
class="tooltipr"> X, <span class="tooltiprtext">X is the quantitative
explanatory variable (at least it is typically quantitative but could be
qualitative) that will be used to explain the average Y-value.</span>
</span><span class="tooltipr"> data = NameOfYourDataset <span
class="tooltiprtext">NameOfYourDataset is the name of the dataset that
contains Y and X. In other words, one column of your dataset would be
your response variable Y and another column would be your explanatory
variable X.</span> </span><span class="tooltipr"> ) <span
class="tooltiprtext">Closing parenthesis for the lm(…) function.</span>
</span><br/><span class="tooltipr"> summary(mylm) <span
class="tooltiprtext">The <code>summary</code> command allows you to
print the results of your linear regression that were previously saved
in <code>mylm</code> name.</span> </span><span class="tooltipr"
style="float:right;font-size:.8em;"> Click to Show Output <span
class="tooltiprtext">Click to View Output.</span> </span></p>
</div>
<p></a></p>
<div id="simplelinearrcode" style="display:none;">
<p>Example output from a regression. Hover each piece to learn more.</p>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> Call:<br/> lm(formula = dist ~ speed, data =
cars) <span class="tooltiprouttext">This is simply a statement of your
original lm(…) “call” that you made when performing your regression. It
allows you to verify that you ran what you thought you ran in the
lm(…).</span> </span>
</td>
</tr>
</table>
<p><br/></p>
<table class="rconsole">
<tr>
<td colspan="2">
<span class="tooltiprout"> Residuals: <span
class="tooltiprouttext">Residuals are the vertical difference between
each point and the line, <span class="math inline">\(Y_i -
\hat{Y}_i\)</span>. The residuals are supposed to be normally
distributed, so a quick glance at their five-number summary can give us
insight about any skew present in the residuals. </span>
</td>
</tr>
<tr>
<td align="right">
<span class="tooltiprout"> min<br/> -29.069 <span
class="tooltiprouttext">“min” gives the value of the residual that is
furthest below the regression line. Ideally, the magnitude of this value
would be about equal to the magnitude of the largest positive residual
(the max) because the hope is that the residuals are normally
distributed around the line.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> 1Q<br/> -9.525 <span
class="tooltiprouttext">“1Q” gives the first quartile of the residuals,
which will always be negative, and ideally would be about equal in
magnitude to the third quartile.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> Median<br/> -2.272 <span
class="tooltiprouttext">“Median” gives the median of the residuals,
which would ideally would be about equal to zero. Note that because the
regression line is the least squares line, the mean of the residuals
will ALWAYS be zero, so it is never included in the output summary. This
particular median value of -2.272 is a little smaller than zero than we
would hope for and suggests a right skew in the data because the mean
(0) is greater than the median (-2.272) witnessing the residuals are
right skewed. This can also be seen in the maximum being much larger in
magnitude than the minimum.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> 3Q<br/> 9.215 <span
class="tooltiprouttext">“3Q” gives the third quartile of the residuals,
which would ideally would be about equal in magnitude to the first
quartile. In this case, it is pretty close, which helps us see that the
first quartile of residuals on either side of the line is behaving
fairly normally.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> Max</br> 43.201 <span
class="tooltiprouttext">“Max” gives the maximum positive residuals,
which would ideally would be about equal in magnitude to the minimum
residual. In this case, it is much larger than the minimum, which helps
us see that the residuals are likely right skewed.</span> </span>
</td>
</tr>
</table>
<p><br/></p>
<table class="rconsole">
<tr>
<td colspan="2">
<span class="tooltiprout"> Coefficients: <span
class="tooltiprouttext">Notice that in your lm(…) you used only <span
class="math inline">\(Y\)</span> and <span
class="math inline">\(X\)</span>. You did type out any coefficients,
i.e., the <span class="math inline">\(\beta_0\)</span> or <span
class="math inline">\(\beta_1\)</span> of the regression model. These
coefficients are estimated by the lm(…) function and displayed in this
part of the output along with standard errors, t-values, and
p-values.</span> </span>
</td>
</tr>
<tr>
<td align="left">
</td>
<td align="right">
<span class="tooltiprout"> Estimate <span class="tooltiprouttext">To
learn more about the “Estimates” of the “Coefficients” see the
“Explanation” tab, “Estimating the Model Parameters” section for
details.</span>
</td>
<td align="right">
<span class="tooltiprout"> Std. Error <span class="tooltiprouttext">To
learn more about the “Standard Errors” of the “Coefficients” see the
“Explanation” tab, “Inference for the Model Parameters” section.</span>
</span>
</td>
<td align="right">
<span class="tooltiprout"> t value <span class="tooltiprouttext">To
learn more about the “t value” of the “Coefficients” see the
“Explanation” tab, “Inference for the Model Parameters” section.</span>
</span>
</td>
<td align="right">
<span class="tooltiprout"> Pr(>|t|) <span
class="tooltiprouttext">The “Pr” stands for “Probability” and the “(>
|t|)” stands for “more extreme than the observed t-value”. Thus, this is
the p-value for the hypothesis test of each coefficient being zero.<br/>
To learn more about the “p-value” of the “Coefficients” see the
“Explanation” tab, “Inference for the Model Parameters” section. </span>
</span>
</td>
</tr>
<tr>
<td align="left">
<span class="tooltiprout"> (Intercept) <span
class="tooltiprouttext">This always says “Intercept” for any lm(…) you
run in R. That is because R always assumes there is a y-intercept for
your regression function.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> -17.5791 <span class="tooltiprouttext">This
is the estimate of the y-intercept, <span
class="math inline">\(\beta_0\)</span>. It is called <span
class="math inline">\(b_0\)</span>. It is the average y-value when X is
zero.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> 6.7584 <span class="tooltiprouttext">This
is the standard error of <span class="math inline">\(b_0\)</span>. It
tells you how much <span class="math inline">\(b_0\)</span> varies from
sample to sample. The closer to zero, the better.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> -2.601 <span class="tooltiprouttext">This is
the test statistic t for the test of <span class="math inline">\(\beta_0
= 0\)</span>. It is calculated by dividing the “Estimate” of the
intercept (-17.5791) by its standard error (6.7584). It gives the
“number of standard errors” away from zero that the “estimate” has
landed. In this case, the estimate of -17.5791 is -2.601 standard errors
(6.7584) from zero, which is a fairly surprising distance as shown by
the p-value.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> 0.0123 <span class="tooltiprouttext">This is
the p-value of the test of the hypothesis that <span
class="math inline">\(\beta_0 = 0\)</span>. It measures the probability
of observing a t-value as extreme as the one observed. To compute it
yourself in R, use
<code>pt(-abs(your t-value), df of your regression)*2</code>.</span>
</span>
</td>
<td align="left">
<span class="tooltiprout"> * <span class="tooltiprouttext">This is
called a “star”. One star means significant at the 0.1 level of <span
class="math inline">\(\alpha\)</span>.</span> </span>
</td>
</tr>
<tr>
<td align="left">
<span class="tooltiprout"> speed <span class="tooltiprouttext">This is
always the name of your X-variable in your lm(Y ~ X, …).</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> 3.9324 <span class="tooltiprouttext">This
is the estimate of the slope, <span
class="math inline">\(\beta_1\)</span>. It is called <span
class="math inline">\(b_1\)</span>. It is the change in the average
y-value as X is increased by 1 unit.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> 0.4155 <span class="tooltiprouttext">This
is the standard error of <span class="math inline">\(b_1\)</span>. It
tells you how much <span class="math inline">\(b_1\)</span> varies from
sample to sample. The closer to zero, the better.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> 9.464 <span class="tooltiprouttext">This is
the test statistic t for the test of <span class="math inline">\(\beta_1
= 0\)</span>. It is calculated by dividing the “Estimate” of the slope
(3.9324) by its standard error (0.4155). It gives the “number of
standard errors” away from zero that the “estimate” has landed. In this
case, the estimate of 3.9324 is 9.464 standard errors (0.4155) from
zero, which is a really surprising distance as shown by the smallness of
the p-value.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> 1.49e-12 <span class="tooltiprouttext">This
is the p-value of the test of the hypothesis that <span
class="math inline">\(\beta_1 = 0\)</span>. To compute it yourself in R,
use <code>pt(-abs(your t-value), df of your regression)*2</code></span>
</span>
</td>
<td align="left">
<span class="tooltiprout"> *** <span class="tooltiprouttext">This is
called a “star”. Three stars means significant at the 0.01 level of
<span class="math inline">\(\alpha\)</span>.</span> </span>
</td>
</tr>
</table>
<table class="rconsole">
<tr>
<td>
<span> --- </span>
</td>
</tr>
</table>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ’*’
0.05 ‘.’ 0.1 ‘ ’ 1 <span class="tooltiprouttext">These “codes” explain
what significance level the p-value is smaller than based on how many
“stars” * the p-value is labeled with in the Coefficients table
above.</span> </span>
</td>
</tr>
</table>
<p><br/></p>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> Residual standard error: <span
class="tooltiprouttext">This is the estimate of <span
class="math inline">\(\sigma\)</span> in the regression model <span
class="math inline">\(Y_i = \beta_0 + \beta_1 X_i + \epsilon_i\)</span>
where <span class="math inline">\(\epsilon_i \sim
N(0,\sigma^2)\)</span>. It is the square root of the MSE.</span> </span>
</td>
<td align="right">
<span class="tooltiprout"> 15.38 <span class="tooltiprouttext">For this