-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtTests.html
1552 lines (1476 loc) · 54.8 KB
/
tTests.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>t Tests</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>
<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>
<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 -->
</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">
<h1 class="title toc-ignore">t Tests</h1>
</div>
<script type="text/javascript">
function showhide(id) {
var e = document.getElementById(id);
e.style.display = (e.style.display == 'block') ? 'none' : 'block';
}
</script>
<hr />
<p>Much of statistical inference concerns the location of the population
mean <span class="math inline">\(\mu\)</span> for a given parametric
distribution. Some of the most common approaches to making inference
about <span class="math inline">\(\mu\)</span> utilize a test statistic
that follows a t distribution.</p>
<hr />
<div id="one-sample-t-test"
class="section level3 tabset tabset-fade tabset-pills">
<h3 class="tabset tabset-fade tabset-pills">One Sample t Test</h3>
<div style="float:left;width:125px;" align="center">
<p><a href="index.html#one-quantitative-response-variable-y"><img src="Images/QuantY.png" width=35px;></a></p>
</div>
<p>A one sample t test is used when there is a hypothesized value for
the population mean <span class="math inline">\(\mu\)</span> of a single
quantitative variable.</p>
<div id="overview" class="section level4">
<h4>Overview</h4>
<div style="padding-left:125px;">
<p><strong>Questions</strong></p>
<p>The one sample t test can be used to answer questions like:</p>
<ul>
<li>How long does it take to drive from Rexburg, ID to Salt Lake City,
UT on average?</li>
<li>Is human body temperature really 98.6&deg; F on average?</li>
<li>Do I spend less than $3 a day, on average, purchasing snacks?</li>
</ul>
<p><strong>Requirements</strong></p>
<p>This test is only appropriate when both of the following are
satisfied.</p>
<ol style="list-style-type: decimal">
<li><p>The sample is <strong>representative</strong> of the population.
(Having a simple random sample is the best way to do this.)</p></li>
<li><p>The sampling distribution of the sample mean <span
class="math inline">\(\bar{x}\)</span> <em>can be assumed to be
normal</em>. This is a safe assumption when either (a) the population
data can be assumed to be normally distributed using a Q-Q Plot or (b)
the size of the sample (n) that was taken from the population is large
(at least n > 30, but “large” really depends on how badly the data is
skewed).</p></li>
</ol>
<p>If the requirements listed above are satisfied, then the results of
the test can be trusted to give meaningful inference about the
population. If the requirements are not met, then that doesn’t mean the
results of the test are necessarily bad, but there is no guarantee that
they are good.</p>
<p><strong>Hypotheses</strong></p>
<div style="padding-left:15px;">
<div
style="float:right;font-size:.8em;background-color:lightgray;padding:5px;border-radius:4px;">
<a style="color:darkgray;" href="javascript:showhide('onesampletlatex')">Math
Code</a>
</div>
<div id="onesampletlatex" style="display:none;">
<pre><code>$$
H_0: \mu = 5.2
$$
$$
H_a: \mu \neq 5.2
$$</code></pre>
</div>
<p><span class="math inline">\(H_0: \mu = \text{some
number}\)</span></p>
<p><span class="math inline">\(H_a: \mu \
\left\{\underset{<}{\stackrel{>}{\neq}}\right\} \ \text{some
number}\)</span></p>
</div>
<p><strong>Examples</strong>: <a
href="./Analyses/t%20Tests/Examples/Math325AnalysisResubmits.html">analysis
resubmits</a></p>
</div>
<hr />
</div>
<div id="r-instructions" class="section level4">
<h4>R Instructions</h4>
<div style="padding-left:125px;">
<p><strong>Console</strong> Help Command: <code>?t.test()</code></p>
<p><code>t.test(NameOfYourData$Y, mu = YourNull, alternative = YourAlternative, conf.level = 0.95)</code></p>
<ul>
<li><code>NameOfYourData</code> is the name of your data set, like
<code>mtcars</code> or <code>KidsFeet</code>.</li>
<li><code>Y</code> must be a “numeric” vector of quantitative data.</li>
<li><code>YourNull</code> is the numeric value from your null hypothesis
for <span class="math inline">\(\mu\)</span>.</li>
<li><code>YourAlternative</code> is one of the three options:
<code>"two.sided"</code>, <code>"greater"</code>, <code>"less"</code>
and should correspond to your alternative hypothesis.</li>
<li>The value for <code>conf.level = 0.95</code> can be changed to any
desired confidence level, like 0.90 or 0.99. It should correspond to
<span class="math inline">\(1-\alpha\)</span>.</li>
</ul>
<p>Testing Assumptions</p>
<p><code>library(car)</code></p>
<p><code>qqPlot(NameOfYourData$Y)</code></p>
<p><br></p>
<p><strong>Example Code</strong></p>
<p>Hover your mouse over the example codes to learn more.</p>
<a href="javascript:showhide('tTest')">
<div class="hoverchunk">
<p><span class="tooltipr"> t.test( <span class="tooltiprtext">‘t.test’
is an R function that performs one and two sample t-tests.</span>
</span><span class="tooltipr"> mtcars <span
class="tooltiprtext">‘mtcars’ is a dataset. Type ‘View(mtcars)’ in R to
view the dataset.</span> </span><span class="tooltipr"> $ <span
class="tooltiprtext">The $ allows us to access any variable from the
mtcars dataset.</span> </span><span class="tooltipr"> mpg, <span
class="tooltiprtext">‘mpg’ is Y, a quantitative variable (numeric
vector) from the mtcars dataset.</span> </span><span class="tooltipr">
mu = 20, <span class="tooltiprtext"> The numeric value from the null
hypothesis is 20 meaning <span class="math inline">\(\mu=20\)</span>.
</span> </span><span class="tooltipr"> alternative = “two.sided”, <span
class="tooltiprtext"> The alternative hypothesis is “two.sided” meaning
the alternative hypothesis is <span
class="math inline">\(\mu\neq20\)</span>.</span> </span><span
class="tooltipr"> conf.level = 0.95) <span class="tooltiprtext">This
test has a 0.95 confidence level which corresponds to 1−α. </span>
</span><span class="tooltipr"> <br />
<span class="tooltiprtext">Press Enter to run the code if you have typed
it in yourself. You can also click here to view the output.</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="tTest" style="display:none;">
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> One Sample t-test <span
class="tooltiprouttext">EXPLANATION.</span> </span>
</td>
</tr>
</table>
<p><br/></p>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> data: mtcars$mpg <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> t = 0.08506, <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
<td>
<span class="tooltiprout"> df = 31, <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
<td>
<span class="tooltiprout"> p-value = 0.9328 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> alternative hypothesis: true mean is not
equal to 20 <span class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> 95 percent confidence interval: <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> 17.91768 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
<td align="left">
<span class="tooltiprout"> 22.26357 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> sample estimates: <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> mean of x <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> 20.09062 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
</table>
</div>
<a href="javascript:showhide('onesampleQQplot')">
<div class="hoverchunk">
<p><span class="tooltipr"> qqPlot( <span class="tooltiprtext">‘qqPlot’
is a R function from library(car) that creates a qqPlot.</span>
</span><span class="tooltipr"> mtcars <span
class="tooltiprtext">‘mtcars’ is a dataset. Type ‘View(mtcars)’ in R to
view the dataset.</span> </span><span class="tooltipr"> $ <span
class="tooltiprtext">The $ allows us to access any variable from the
mtcars dataset.</span> </span><span class="tooltipr"> mpg) <span
class="tooltiprtext">‘mpg’ is a quantitative variable (numeric vector)
from the mtcars dataset.</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>
</a>
<div id="onesampleQQplot" style="display:none;">
<p><img src="tTests_files/figure-html/unnamed-chunk-2-1.png" width="672" /></p>
<pre><code>## [1] 20 18</code></pre>
</div>
</div>
<hr />
</div>
<div id="explanation" class="section level4">
<h4>Explanation</h4>
<div style="padding-left:125px;">
<p>In many cases where it is of interest to test a claim about a single
population mean <span class="math inline">\(\mu\)</span>, the one sample
t test is used. This is an appropriate decision whenever the sampling
distribution of the sample mean can be assumed to be normal and the data
represents a simple random sample from the population.</p>
<p>In the figure below, the null hypothesis <span
class="math inline">\(H_0: \mu = \mu_0\)</span> is represented by the
normal distribution (gray) centered at <span
class="math inline">\(\mu_0\)</span>. Note that <span
class="math inline">\(\mu_0\)</span> is just some specified number. This
shows how the null hypothesis represents the assumption about the center
of the distribution of the data.</p>
<p><img src="tTests_files/figure-html/unnamed-chunk-3-1.png" width="672" /></p>
<p>After a hypothesis (null) is established and an alternative
hypothesis similarly declared, a simple random sample of data of size
<span class="math inline">\(n\)</span> is obtained from the population
of interest. In the plot above, this is depicted by the points (blue
dots) which are centered around their sample mean <span
class="math inline">\(\bar{x}\)</span>.</p>
<p>Above the points (blue dots) is shown a second normal distribution
(blue dashed line) which represents the idea that the alternative
hypothesis allows for a normal distribution which is potentially more
consistent with the data than the one specified under the null
hypothesis.</p>
<p>The role of the one sample t test is to measure the probability of a
sample mean being as extreme or more extreme from the hypothesized value
of <span class="math inline">\(\mu_0\)</span> than the one observed
assuming the null hypothesis is true. This probability is of course the
p-value of the test. This works because the sampling distribution of the
sample mean has been assumed to be normal. In this case, the
distribution of the test statistic t, <span class="math display">\[
t = \frac{\bar{x}-\mu}{s/\sqrt{n}}
\]</span><br />
is known to follow a t distribution with <span
class="math inline">\(n-1\)</span> degrees of freedom. (The mathematics
that provide this result are phenominal! You can consult any advanced
statistical textbook for the details.)</p>
<p>The p-value of the one sample t test represents the probability that
the test statistic <span class="math inline">\(t\)</span> is as extreme
or more extreme than the one observed according to a t-distribution with
<span class="math inline">\(n-1\)</span> degrees of freedom.</p>
<p>If the probability (the p-value) is close enough to zero (smaller
than <span class="math inline">\(\alpha\)</span>) then it is determined
that the most <em>plausible</em> hypothesis is the alternative
hypothesis, and thus the null is “rejected” in favor of the
alternative.</p>
</div>
<hr />
</div>
</div>
<div id="paired-samples-t-test"
class="section level3 tabset tabset-fade tabset-pills">
<h3 class="tabset tabset-fade tabset-pills">Paired Samples t Test</h3>
<div style="float:left;width:125px;" align="center">
<p><a href="index.html#one-quantitative-response-variable-y"><img src="Images/QuantY.png" width=35px;></a></p>
</div>
<p>The paired samples t test is used when a value is hypothesized for
the popluation mean of the differences, <span
class="math inline">\(\mu_d\)</span>, obtained from paired
observations.</p>
<div id="overview-1" class="section level4">
<h4>Overview</h4>
<div style="padding-left:125px;">
<p><strong>Questions</strong></p>
<p>The Paired Samples t Test can be used to answer questions like:</p>
<ul>
<li>From pre-test to post-test is there an improvement on average in the
subjects?</li>
<li>How much taller are husbands than their wives, on average?</li>
<li>Do hospital patients that are <em>carefully matched</em> together
according to reason for being in the hospital, age, gender, ethnicity,
height, and weight show increased stay times in the hospital when
infected with a nosocomial infection compared to those who were not
infected?</li>
</ul>
<p><strong>Requirements</strong></p>
<p>The test is only appropriate when both of the following are
satisfied.</p>
<ol style="list-style-type: decimal">
<li><p>The sample of differences is representative of the population
differences.</p></li>
<li><p>The sampling distribution of the sample mean of the differences
<span class="math inline">\(\bar{d}\)</span> (<span
class="math inline">\(\bar{x}\)</span> of the differences) can be
assumed to be normal. (This second requirement can be assumed to be
satisfied when (a) the differences themselves can be assumed to be
normal from a Q-Q Plot, or (b) when the sample size <span
class="math inline">\(n\)</span> of the differences is large.)</p></li>
</ol>
<p><strong>Hypotheses</strong></p>
<div style="padding-left:15px;">
<div
style="float:right;font-size:.8em;background-color:lightgray;padding:5px;border-radius:4px;">
<a style="color:darkgray;" href="javascript:showhide('pairedsampletlatex')">Math
Code</a>
</div>
<div id="pairedsampletlatex" style="display:none;">
<pre><code>$$
H_0: \mu_d = 0
$$
$$
H_a: \mu_d \neq 0
$$</code></pre>
</div>
<p><span class="math inline">\(H_0: \mu_d = \text{some number, but
typically 0}\)</span><br />
<span class="math inline">\(H_a: \mu_d \
\left\{\underset{<}{\stackrel{>}{\neq}}\right\} \ \text{some
number, but typically 0}\)</span></p>
</div>
<p><strong>Examples</strong>: <a
href="./Analyses/t%20Tests/Examples/SleepPairedt.html">sleepPaired</a>
<a
href="./Analyses/t%20Tests/Examples/Student1Paired.html">studentPaired</a></p>
</div>
<hr />
</div>
<div id="r-instructions-1" class="section level4">
<h4>R Instructions</h4>
<div style="padding-left:125px;">
<p><strong>Console</strong> Help Command: <code>?t.test()</code></p>
<p><strong>Option 1:</strong></p>
<p><code>t.test(NameOfYourData$Y1, NameOfYourData$Y2, paired = TRUE, mu = YourNull, alternative = YourAlternative, conf.level = 0.95)</code></p>
<ul>
<li><code>NameOfYourData</code> is the name of your data set like
<code>sleep</code> or <code>mtcars</code> or <code>KidsFeet</code>.</li>
<li><code>Y1</code> must be a “numeric” vector that represents the
quantitative data from the first sample of data.</li>
<li><code>Y2</code> must be a “numeric” vector that represents the
quantitative data from the second sample of data. This vector must be in
the same order as the first sample so that the pairing can take
place.</li>
<li><code>YourNull</code> is the numeric value from your null hypothesis
for <span class="math inline">\(\mu_d\)</span>.</li>
<li><code>YourAlternative</code> is one of the three options:
<code>"two.sided"</code>, <code>"greater"</code>, <code>"less"</code>
and should correspond to your alternative hypothesis.</li>
<li>The value for <code>conf.level = 0.95</code> can be changed to any
desired confidence level, like 0.90 or 0.99. It should correspond to
<span class="math inline">\(1-\alpha\)</span>.</li>
</ul>
<p>Testing Assumptions</p>
<p><code>library(car)</code></p>
<p><code>qqPlot(Y1 - Y2)</code></p>
<p><strong>Example Code</strong></p>
<p>Hover your mouse over the example codes to learn more.</p>
<a href="javascript:showhide('tTestPaired')">
<div class="hoverchunk">
<p><span class="tooltipr"> sleep1 <- filter(sleep, group==1) <span
class="tooltiprtext">This splits out the “group1” data from the sleep
data set.</span> </span><br/><span class="tooltipr"> sleep2 <-
filter(sleep, group==2) <span class="tooltiprtext">This splits out the
“group2” data from the sleep data set</span> </span><br/><span
class="tooltipr"> t.test( <span class="tooltiprtext">‘t.test’ is an R
function that performs one and two sample t-tests.</span> </span><span
class="tooltipr"> sleep2$extra, <span class="tooltiprtext">A numeric
vector that represents the hours of extra sleep that the group had with
drug 2.</span> </span><span class="tooltipr"> sleep1$extra, <span
class="tooltiprtext">A numeric vector that represents the hours of extra
sleep that the same group had with drug 1.</span> </span><br><span
class="tooltipr"> paired=TRUE, <span class="tooltiprtext">Indicates
that this is a paired t-Test. This will cause the subtraction of
sleep2$extra - sleep1$extra to be performed to obtain the paired
differences. To cause the subtraction to occur in the other order,
reverse the order sleep1$extra, sleep2$extra occur in the t.test(…)
function.</span> </span><span class="tooltipr"> mu = 0, <span
class="tooltiprtext">The numeric value from the null hypothesis 0
meaning the null hypothesis is <span
class="math inline">\(\mu_d=0\)</span>.</span> </span><span
class="tooltipr"> alternative = “two.sided”, <span
class="tooltiprtext">The alternative hypothesis is “two.sided” meaning
the alternative hypothesis is <span
class="math inline">\(\mu_d\neq0\)</span>.</span> </span><span
class="tooltipr"> conf.level = 0.95) <span class="tooltiprtext">This
test has a 0.95 confidence level which corresponds to 1 - <span
class="math inline">\(\alpha\)</span>.</span> </span><span
class="tooltipr"> <br />
<span class="tooltiprtext">Press Enter to run the code if you have typed
it in yourself. You can also click here to view the output.</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>
</a>
<div id="tTestPaired" style="display:none;">
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> Paired t-test <span
class="tooltiprouttext">EXPLANATION.</span> </span>
</td>
</tr>
</table>
<p><br/></p>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> data: sleep2$extra and sleep1$extra <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> t = 4.0621, <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
<td>
<span class="tooltiprout"> df = 9, <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
<td>
<span class="tooltiprout"> p-value = 0.002833 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> alternative hypothesis: true mean is not
equal to 0 <span class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> 95 percent confidence interval: <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> 0.7001142 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
<td align="left">
<span class="tooltiprout"> 2.4598858 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> sample estimates: <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> mean of the differences <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td align="right">
<span class="tooltiprout"> 1.58 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
</table>
</div>
<a href="javascript:showhide('pairedQQplot')">
<div class="hoverchunk">
<p><span class="tooltipr"> qqPlot( <span class="tooltiprtext">‘qqPlot’
is a R function from library(car) that creates a qqPlot.</span>
</span><span class="tooltipr"> sleep2$extra <span
class="tooltiprtext">The hours of extra sleep that the group had with
drug 2.</span> </span><span class="tooltipr"> - <span
class="tooltiprtext">Subtract the hours of extra sleep with drug 1 from
the hours of extra sleep with drug 2 to get the difference.</span>
</span><span class="tooltipr"> sleep1$extra <span
class="tooltiprtext">The hours of extra sleep that the same group had
with drug 1.</span> </span><span class="tooltipr"> ) <span
class="tooltiprtext">Closing parenthesis for qqPlot(…) function.</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>
</a>
<div id="pairedQQplot" style="display:none;">
<p><img src="tTests_files/figure-html/unnamed-chunk-5-1.png" width="672" /></p>
<pre><code>## [1] 9 5</code></pre>
</div>
<p><br /></p>
<p><br></p>
<p><strong>Option 2:</strong></p>
<p>Compute the differences yourself instead of using
<code>paired=TRUE</code>.</p>
<p><code>differences = NameOfYourData$Y1 - NameOfYourData$Y2</code></p>
<p><code>t.test(differences, mu = YourNull, alternative = YourAlternative, conf.level = 0.95)</code></p>
<ul>
<li><code>NameOfYourData</code> is the name of your data set.</li>
<li><code>Y1</code> must be a “numeric” vector that represents the
quantitative data from the first sample of data.</li>
<li><code>Y2</code> must be a “numeric” vector that represents the
quantitative data from the second sample of data. This vector must be in
the same order as the first sample so that the pairing can take
place.</li>
<li><code>differences</code> are the resulting differences obtained from
subtracting <code>Y1 - Y2</code>.</li>
<li><code>YourNull</code> is the numeric value from your null hypothesis
for <span class="math inline">\(\mu_d\)</span>.</li>
<li><code>YourAlternative</code> is one of the three options:
<code>"two.sided"</code>, <code>"greater"</code>, <code>"less"</code>
and should correspond to your alternative hypothesis.</li>
<li>The value for <code>conf.level = 0.95</code> can be changed to any
desired confidence level, like 0.90 or 0.99. It should correspond to
<span class="math inline">\(1-\alpha\)</span>.</li>
</ul>
<p>Testing Assumptions</p>
<p><code>library(car)</code></p>
<p><code>qqPlot(differences)</code></p>
<p><strong>Example Code</strong></p>
<p>Hover your mouse over the example codes to learn more.</p>
<a href="javascript:showhide('tTestPaired2')">
<div class="hoverchunk">
<p><span class="tooltipr"> sleep1 <- filter(sleep, group==1) <span
class="tooltiprtext">This splits out the “group1” data from the sleep
data set.</span> </span><br/><span class="tooltipr"> sleep2 <-
filter(sleep, group==2) <span class="tooltiprtext">This splits out the
“group2” data from the sleep data set</span> </span><br/><span
class="tooltipr"> differences <- <span class="tooltiprtext"> Saved
the computed differences to an object called ‘differences’.</span>
</span><span class="tooltipr"> sleep2$extra <span
class="tooltiprtext">The hours of extra sleep that the group had with
drug 2.</span> </span><span class="tooltipr"> - <span
class="tooltiprtext">Subtract the hours of extra sleep with drug 1 from
the hours of extra sleep with drug 2 to get the difference.</span>
</span><span class="tooltipr"> sleep1$extra <span
class="tooltiprtext">The hours of extra sleep that the same group had
with drug 1.</span> </span><br><span class="tooltipr"> t.test( <span
class="tooltiprtext">‘t.test’ is an R function that performs one and two
sample t-tests.</span> </span><span class="tooltipr"> differences,
<span class="tooltiprtext">‘differences’ are the resulting differences
of the hours of extra sleep with drug 1 and the hours of extra sleep
with drug 2.</span> </span><span class="tooltipr"> mu = 0, <span
class="tooltiprtext">The numeric value from the null hypothesis 0
meaning the null hypothesis is <span
class="math inline">\(\mu_d=0\)</span>.</span> </span><span
class="tooltipr"> alternative = “two.sided”, <span
class="tooltiprtext">The alternative hypothesis is “two.sided” meaning
the alternative hypothesis is <span
class="math inline">\(\mu_d\neq0\)</span>.</span> </span><span
class="tooltipr"> conf.level = 0.95) <span class="tooltiprtext">This
test has a 0.95 confidence level which corresponds to 1 - <span
class="math inline">\(\alpha\)</span>.</span> </span><span
class="tooltipr"> <br />
<span class="tooltiprtext">Press Enter to run the code if you have typed
it in yourself. You can also click here to view the output.</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>
</a>
<div id="tTestPaired2" style="display:none;">
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> One Sample t-test <span
class="tooltiprouttext">EXPLANATION.</span> </span>
</td>
</tr>
</table>
<p><br/></p>
<table class="rconsole">
<tr>
<td>
<span class="tooltiprout"> data: differences <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> t = 4.0621, <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
<td>
<span class="tooltiprout"> df = 9, <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
<td>
<span class="tooltiprout"> p-value = 0.002833 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> alternative hypothesis: true mean is not
equal to 0 <span class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> 95 percent confidence interval: <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
</tr>
<tr>
<td>
<span class="tooltiprout"> 0.7001142 <span
class="tooltiprouttext">EXPLANATION.</span>
</td>
<td align="left">
<span class="tooltiprout"> 2.4598858 <span