forked from saundersg/Statistics-Notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PermutationTests.html
789 lines (719 loc) · 56 KB
/
PermutationTests.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Permutation Tests</title>
<script src="site_libs/jquery-1.11.3/jquery.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>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<script src="site_libs/accessible-code-block-0.0.1/empty-anchor.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
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 {
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 */
</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>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<style type="text/css">
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;
}
.table th:not([align]) {
text-align: left;
}
</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;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
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;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
.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: #ffffff;
}
.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>
// 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.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
background: white;
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-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">Statistics Notebook</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" 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" 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" 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="ANOVA.html">ANOVA</a>
</li>
<li>
<a href="Kruskal.html">Kruskal-Wallis</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 Testing</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Analyses
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="./Analyses/AnalysisRubric.html">Analysis Rubric</a>
</li>
<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/DayCare.html">Day Care</a>
</li>
<li>
<a href="./Analyses/Kruskal-Wallis/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 class="fluid-row" id="header">
<h1 class="title toc-ignore">Permutation 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>A nonparametric approach to computing the p-value for any test statistic in just about any scenario.</p>
<hr />
<div id="section" class="section level3 tabset tabset-pills tabset-fade">
<h3></h3>
<div id="overview" class="section level4">
<h4>Overview</h4>
<div style="padding-left:30px;">
<p>In almost all hypothesis testing scenarios, the null hypothesis can be interpreted as follows.</p>
<div style="padding-left:15px;padding-right:15px;">
<p><span class="math inline">\(H_0\)</span>: Any pattern that has been witnessed in the sampled data is simply due to random chance.</p>
</div>
<p>Permutation Tests depend completely on this single idea. If all patterns in the data really are simply due to random chance, then the null hypothesis is true. Further, random <strong>re</strong>-samples of the data should show similar lack of patterns. However, if the pattern in the data is <em>real</em>, then random <strong>re</strong>-samples of the data will show very different patterns from the original.</p>
<p>Consider the following image. In that image, the toy blocks on the left show a clear pattern or structure. They are nicely organized into colored piles. This suggests a <em>real</em> pattern that is not random. Someone certainly organized those blocks into that pattern. The blocks didn’t land that way by random chance. On the other hand, the pile of toy blocks shown on the right is certainly a random pattern. This is a pattern that would result if the toy blocks were put into a bag, shaken up, and dumped out. This is the idea of the permutation test. If there is structure in the data, then “mixing up the data and dumping it out again” will show very different patterns from the original. However, if the data was just random to begin with, then we would see a similar pattern by “mixing up the data and dumping it out again.”</p>
<center>
<img src="Images/legoPermutationExample.png" width=600px;>
</center>
<p>The process of a permutation test is:</p>
<ol style="list-style-type: decimal">
<li>Compute a test statistic for the original data.</li>
<li>Re-sample the data (“shake it up and dump it out”) thousands of times, computing a new test statistic each time, to create a sampling distribution of the test statistic.</li>
<li>Compute the p-value of the permutation test as the percentage of test statistics that are as extreme or more extreme than the one originally observed.</li>
</ol>
<p>In review, the sampling distribution is created by permuting (randomly rearranging) the data thousands of times and calculating a test statistic on each permuted version of the data. A histogram of the test statistics then provides the sampling distribution of the test statistic needed to compute the p-value of the original test statistic.</p>
</div>
<hr />
</div>
<div id="r-instructions" class="section level4">
<h4>R Instructions</h4>
<div style="padding-left:50px;">
<p>Any permutation test can be performed in R with a <code>for</code> loop.</p>
<div style="padding-left:80px;">
<a href="javascript:showhide('perm1')">
<div class="hoverchunk">
<p><span class="tooltipr"> #Step 1 <span class="tooltiprtext">Compute a test statistic for the original data.</span> </span><br/><span class="tooltipr"> myTest <- …perform the initial test… <span class="tooltiprtext">This could be a <code>t.test</code>, <code>wilcox.test</code>, <code>aov</code>, <code>kruskal.test</code>, <code>lm</code>, <code>glm</code>, <code>chisq.test</code>, or any other R code that results in a test statistic. It could even simply be the mean or standard deviation of the data.</span> </span><br/><span class="tooltipr"> observedTestStat <- …get the test statistic… <span class="tooltiprtext">Save the test statistic of your test into the object called <code>observedTestStat</code>. For tests that always result in a single test statistic like a <code>t.test</code>, <code>wilcox.test</code>, <code>kruskal.test</code>, and <code>chisq.test</code> it is <code>myTest$statistic</code>. For an <code>aov</code>, <code>lm</code>, or <code>glm</code> try printing <code>summary(myTest)[]</code> to see what values you are interested in using.</span> </span><br/><br/><span class="tooltipr"> #Step 2 <span class="tooltiprtext">Re-sample the data (“shake it up and dump it out”) thousands of times, computing a new test statistic each time, to create a sampling distribution of the test statistic.</span> </span><br/><span class="tooltipr"> N <- 2000 <span class="tooltiprtext">N is the number of times you will reuse the data to create the sampling distribution of the test statistic. A typical choice is 2000, but sometimes 10000, or 100000 reuses are needed before useful answers can be obtained.</span> </span><br/><span class="tooltipr"> permutedTestStats <- <span class="tooltiprtext">This is a storage container that will be used to store the test statistics from each of the thousands of reuses of the data.</span> </span><span class="tooltipr"> rep(NA, N) <span class="tooltiprtext">The rep() function repeats a given value N times. This particular statement repeats NA’s or “missing values” N times. This gives us N “empty” storage spots inside of <code>permutedTestStats</code> that we can use to store the N test statistics from the N reuses of the data we will make in our <code>for</code> loop.</span> </span><br/><span class="tooltipr"> for <span class="tooltiprtext">The <code>for</code> loop is a programming tool that lets us tell R to run a certain code over and over again for a certain number of times.</span> </span><span class="tooltipr"> (i in <span class="tooltiprtext">In R, the <code>for</code> loop must be followed by a space, then an opening parenthesis, then a variable name (in this case the variable is called “i”), then the word “in” then a list of values.</span> </span><span class="tooltipr"> 1:N <span class="tooltiprtext">The 1:N gives R the list of values 1, 2, 3, … and so on all the way up to N. These values are passed into <code>i</code> one at a time and the code inside the <code>for</code> loop is performed first for <code>i=1</code>, then again for <code>i=2</code>, then again for <code>i=3</code> and so on until finally <code>i=N</code>. At that point, the <code>for</code> loop ends.</span> </span><span class="tooltipr"> ) <span class="tooltiprtext">Required closing parenthesis on the <code>for (i in 1:N)</code> statement.</span> </span><span class="tooltipr"> { <span class="tooltiprtext">This bracket opens the code section of the <code>for</code> loop. Any code placed between the opening { and closing } brackets will be performed over and over again for each value of <code>i=1</code>, <code>i=2</code>, … up through <code>i=N</code>.</span> </span><br/><span class="tooltipr"> <span class="tooltiprtext">Two spaces in front of every line inside of the opening { and closing } brackets helps keep your code organized.</span> </span><span class="tooltipr"> permutedData <- …randomly permute the data… <span class="tooltiprtext">This is the most important part of the permutation test and takes some thinking. The data must be randomly reorganized in a way consistent with the null hypothesis. What that means exactly is specific to each scenario. Read the Explanation tab for further details on the logic you should use here.</span> </span><br/><span class="tooltipr"> <span class="tooltiprtext">Two spaces in front of every line inside of the opening { and closing } brackets helps keep your code organized.</span> </span><span class="tooltipr"> permutedTest <- …perform test with permutedData… <span class="tooltiprtext">The same test that was performed on the original data, should be performed again on the randomly permuted data.</span> </span><br/><span class="tooltipr"> <span class="tooltiprtext">Two spaces in front of every line inside of the opening { and closing } brackets helps keep your code organized.</span> </span><span class="tooltipr"> permutedTestStats <span class="tooltiprtext">This is the storage container that was built prior to the <code>for (i in 1:N)</code> code. Inside the <code>for</code> loop, this container is filled value by value using the square brackets <code>[i]</code>.</span> </span><span class="tooltipr"> [i] <span class="tooltiprtext">The square brackets <code>[i]</code> allows us to access the “i”th position of <code>permutedTestStats</code>. Remember, since this code is inside of the <code>for</code> loop, <code>i=1</code> the first time through the code, then <code>i=2</code> the second time through the code, <code>i=3</code> the third time through, and so on up until <code>i=N</code> the <code>N</code>th time through the code.</span> </span><span class="tooltipr"> <- …get test statistic… <span class="tooltiprtext">The test statistic from <code>permutedTest</code> is accessed here and stored into <code>permutedTestStats[i]</code>.</span> </span><br/><span class="tooltipr"> } <span class="tooltiprtext">The closing } bracket ends the code that is repeated over and over again inside the <code>for</code> loop.</span> </span><br/><span class="tooltipr"> hist(permutedTestStats) <span class="tooltiprtext">Creating a histogram of the sampling distribution of the test statistics obtained from the reused and permuted data allows us to visually compare the <code>observedTestStat</code> to the distribution of test statistics to visually see the percentage of test statistics that are as extreme or more extreme than the observed test statistic value. This is the p-value.</span> </span><br/><span class="tooltipr"> abline(v=observedTestStat) <span class="tooltiprtext">This adds the <code>observedTestStat</code> to the distribution of test statistics to visually see the percentage of test statistics that are as extreme or more extreme than the observed test statistic value. This is the p-value.</span> </span><br/><br/><span class="tooltipr"> #Step 3 <span class="tooltiprtext">Compute the p-value of the permutation test as the percentage of test statistics that are as extreme or more extreme than the one originally observed.</span> </span><br/><span class="tooltipr"> sum(permutedTestStats >= observedTestStat)/N <span class="tooltiprtext">This computes a “greater than” p-value. A two-sided p-value could be obtained by multiplying this value by 2 if the observed test statistic was on the right hand side of the histogram.</span> </span><br/><span class="tooltipr"> sum(permutedTestStats <= observedTestStat)/N <span class="tooltiprtext">This computes a “less than” p-value. A two-sided p-value could be obtained by multiplying this value by 2 if the observed test statistic was on the left hand side of the histogram.</span> </span></p>
</div>
<p></a></p>
<div id="perm1" style="display:none;">
<ul>
<li><p><code>myTest <- ...perform the initial test...</code></p>
<p>This could be a <code>t.test</code>, <code>wilcox.test</code>, <code>aov</code>, <code>kruskal.test</code>, <code>lm</code>, <code>glm</code>, <code>chisq.test</code>, or any other R code that results in a test statistic. It could even simply be the mean or standard deviation of the data.</p></li>
<li><p><code>observedTestStat <- ...get the test statistic...</code></p>
<p>Save the test statistic of your test into the object called <code>observedTestStat</code>. For tests that always result in a single test statistic like a <code>t.test</code>, <code>wilcox.test</code>, <code>kruskal.test</code>, and <code>chisq.test</code> it is <code>myTest$statistic</code>. For an <code>aov</code>, <code>lm</code>, or <code>glm</code> try printing <code>summary(myTest)[]</code> to see what values you are interested in using.</p></li>
<li><p><code>N <- 2000</code> N is the number of times you will reuse the data to create the sampling distribution of the test statistic. A typical choice is 2000, but sometimes 10000, or 100000 reuses are needed before useful answers can be obtained.</p></li>
<li><p><code>permutedTestStats <- rep(NA, N)</code> This is a storage container that will be used to store the test statistics from each of the thousands of reuses of the data. The <code>rep()</code> function repeats a given value N times. This particular statement repeats NA’s or “missing values” N times. This gives us N “empty” storage spots inside of <code>permutedTestStats</code> that we can use to store the N test statistics from the N reuses of the data we will make in our <code>for</code> loop.</p></li>
<li><p><code>for (i in 1:N)\{</code> The <code>for</code> loop is a programming tool that lets us tell R to run a certain code over and over again for a certain number of times. In R, the <code>for</code> loop must be followed by a space, then an opening parenthesis, then a variable name (in this case the variable is called “i”), then the word “in” then a list of values. The <code>1:N</code> gives R the list of values 1, 2, 3, … and so on all the way up to N. These values are passed into <code>i</code> one at a time and the code inside the <code>for</code> loop is performed first for <code>i=1</code>, then again for <code>i=2</code>, then again for <code>i=3</code> and so on until finally <code>i=N</code>. At that point, the <code>for</code> loop ends. There is a required closing parenthesis on the <code>for (i in 1:N)</code> statement. Any code placed between the opening { and closing } brackets will be performed over and over again for each value of <code>i=1</code>, <code>i=2</code>, … up through <code>i=N</code>.</p></li>
<li><p>Two spaces in front of every line inside of the opening { and closing } brackets helps keep your code organized.</p></li>
<li><p><code>permutedData <- ...randomly permute the data...</code> This is the most important part of the permutation test and takes some thinking. The data must be randomly reorganized in a way consistent with the null hypothesis. What that means exactly is specific to each scenario. Read the Explanation tab for further details on the logic you should use here.</p></li>
<li><p><code>permutedTest <- ...perform test with permutedData...</code> The same test that was performed on the original data, should be performed again on the randomly permuted data.</p></li>
<li><p><code>permutedTestStats[i] <- ...get test statistic...</code> This is the storage container that was built prior to the <code>for (i in 1:N)</code> code. Inside the <code>for</code> loop, this container is filled value by value using the square brackets <code>[i]</code>. The square brackets <code>[i]</code> allows us to access the “i”th position of <code>permutedTestStats</code>. Remember, since this code is inside of the <code>for</code> loop, <code>i=1</code> the first time through the code, then <code>i=2</code> the second time through the code, <code>i=3</code> the third time through, and so on up until <code>i=N</code> the <code>N</code>th time through the code. The test statistic from <code>permutedTest</code> is accessed here and stored into <code>permutedTestStats[i]</code>.</p></li>
<li><p><code>}</code> The closing } bracket ends the code that is repeated over and over again inside the <code>for</code> loop.</p></li>
<li><p><code>hist(permutedTestStats)</code> Creating a histogram of the sampling distribution of the test statistics obtained from the reused and permuted data allows us to visually compare the <code>observedTestStat</code> to the distribution of test statistics to visually see the percentage of test statistics that are as extreme or more extreme than the observed test statistic value. This is the p-value.</p></li>
<li><p><code>abline(v=observedTestStat)</code> This adds the <code>observedTestStat</code> to the distribution of test statistics to visually see the percentage of test statistics that are as extreme or more extreme than the observed test statistic value. This is the p-value.</p></li>
<li><p><code>sum(permutedTestStats >= observedTestStat)/N</code> This computes a “greater than” p-value. A two-sided p-value could be obtained by multiplying this value by 2 if the observed test statistic was on the right hand side of the histogram.</p></li>
<li><p><code>sum(permutedTestStats <= observedTestStat)/N</code> This computes a “less than” p-value. A two-sided p-value could be obtained by multiplying this value by 2 if the observed test statistic was on the left hand side of the histogram.</p></li>
</ul>
</div>
</div>
</div>
<hr />
</div>
<div id="explanation" class="section level4">
<h4>Explanation</h4>
<div style="padding-left:30px;">
<p>The most difficult part of a permutation test is in the random permuting of the data. How the permuting is performed depends on the type of hypothesis test being performed. It is important to remember that the permutation test only changes the way the p-value is calculated. Everything else about the original test is unchanged when switching to a permutation test.</p>
<h5 id="paired-data-example">Paired Data Example</h5>
<p>See the <a href="./Analyses/SleepPairedt.html">Sleep Paired t Test</a> example for the background and context of the study. Here is how to perform the test as a permutation test instead of a t test.</p>
<p>The question that this <code>sleep</code> data can answer concerns which drug is more effective at increasing the amount of extra sleep an individual receives. The associated hypotheses would be <span class="math display">\[
H_0: \mu_d = 0
\]</span> <span class="math display">\[
H_a: \mu_d \neq 0
\]</span> where <span class="math inline">\(\mu_d\)</span> denotes the true mean of the differences between the observations for each drug obtained from each individual. Differences would be obtained by <span class="math inline">\(d_i = \text{extra}_{1i} - \text{extra}_{2i}\)</span>.</p>
<p>To perform a permutation test of the hypothesis that the drugs are equally effective, we use the following code.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1"></a><span class="co"># Perform the initial test:</span></span>
<span id="cb1-2"><a href="#cb1-2"></a>myTest <-<span class="st"> </span><span class="kw">with</span>(sleep, <span class="kw">t.test</span>(extra[group<span class="op">==</span><span class="dv">1</span>], extra[group<span class="op">==</span><span class="dv">2</span>], <span class="dt">paired =</span> <span class="ot">TRUE</span>, <span class="dt">mu =</span> <span class="dv">0</span>))</span>
<span id="cb1-3"><a href="#cb1-3"></a><span class="co"># Get the test statistic from the test:</span></span>
<span id="cb1-4"><a href="#cb1-4"></a>observedTestStat <-<span class="st"> </span>myTest<span class="op">$</span>statistic</span>
<span id="cb1-5"><a href="#cb1-5"></a></span>
<span id="cb1-6"><a href="#cb1-6"></a></span>
<span id="cb1-7"><a href="#cb1-7"></a><span class="co"># Obtain the permutation sampling distribution </span></span>
<span id="cb1-8"><a href="#cb1-8"></a>N <-<span class="st"> </span><span class="dv">2000</span></span>
<span id="cb1-9"><a href="#cb1-9"></a>permutedTestStats <-<span class="st"> </span><span class="kw">rep</span>(<span class="ot">NA</span>, N)</span>
<span id="cb1-10"><a href="#cb1-10"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span>N){</span>
<span id="cb1-11"><a href="#cb1-11"></a> permuteData <-<span class="st"> </span><span class="kw">sample</span>(<span class="dt">x=</span><span class="kw">c</span>(<span class="op">-</span><span class="dv">1</span>,<span class="dv">1</span>), <span class="dt">size=</span><span class="dv">10</span>, <span class="dt">replace=</span><span class="ot">TRUE</span>) </span>
<span id="cb1-12"><a href="#cb1-12"></a> permutedTest <-<span class="st"> </span><span class="kw">with</span>(sleep, <span class="kw">t.test</span>(permuteData<span class="op">*</span>(extra[group<span class="op">==</span><span class="dv">1</span>] <span class="op">-</span><span class="st"> </span>extra[group<span class="op">==</span><span class="dv">2</span>]), <span class="dt">mu =</span> <span class="dv">0</span>))</span>
<span id="cb1-13"><a href="#cb1-13"></a> <span class="co">#Note, t.test(group1 - group2) is the same as t.test(group1, group2, paired=TRUE).</span></span>
<span id="cb1-14"><a href="#cb1-14"></a> permutedTestStats[i] <-<span class="st"> </span>permutedTest<span class="op">$</span>statistic</span>
<span id="cb1-15"><a href="#cb1-15"></a>}</span>
<span id="cb1-16"><a href="#cb1-16"></a><span class="kw">hist</span>(permutedTestStats)</span>
<span id="cb1-17"><a href="#cb1-17"></a><span class="kw">abline</span>(<span class="dt">v=</span>observedTestStat, <span class="dt">col=</span><span class="st">'skyblue'</span>, <span class="dt">lwd=</span><span class="dv">3</span>)</span></code></pre></div>
<p><img src="PermutationTests_files/figure-html/unnamed-chunk-2-1.png" width="672" /></p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1"></a><span class="co"># Greater than p-value: (not what we want here)</span></span>
<span id="cb2-2"><a href="#cb2-2"></a><span class="kw">sum</span>(permutedTestStats <span class="op">>=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1"></a><span class="co"># Less than p-value:</span></span>
<span id="cb4-2"><a href="#cb4-2"></a><span class="kw">sum</span>(permutedTestStats <span class="op"><=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span></code></pre></div>
<pre><code>## [1] 0.004</code></pre>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1"></a><span class="co"># Correct two sided p-value for this study:</span></span>
<span id="cb6-2"><a href="#cb6-2"></a><span class="dv">2</span><span class="op">*</span><span class="kw">sum</span>(permutedTestStats <span class="op"><=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span></code></pre></div>
<pre><code>## [1] 0.008</code></pre>
<h5 id="other-examples-from-students">Other Examples from Students</h5>
<p><a href="javascript:showhide('ItTest')">Independent Samples t Test (and Wilcoxon) <span style="font-size:8pt;">(click to show/hide)</span></a></p>
<div id="ItTest" style="display:none;">
<p>For the independent sample t Test, we will use the data from the <a href="Analyses/SleepIndependentt.html">independent sleep</a> analysis.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1"></a><span class="co"># First run the initial test and gain the test statistic:</span></span>
<span id="cb8-2"><a href="#cb8-2"></a>myTest <-<span class="st"> </span><span class="kw">t.test</span>(extra <span class="op">~</span><span class="st"> </span>group, <span class="dt">data =</span> sleep, <span class="dt">mu =</span> <span class="dv">0</span>)</span>
<span id="cb8-3"><a href="#cb8-3"></a>observedTestStat <-<span class="st"> </span>myTest<span class="op">$</span>statistic</span>
<span id="cb8-4"><a href="#cb8-4"></a></span>
<span id="cb8-5"><a href="#cb8-5"></a><span class="co"># Now we run the permutation for a distribution of test statistics</span></span>
<span id="cb8-6"><a href="#cb8-6"></a>N <-<span class="st"> </span><span class="dv">2000</span></span>
<span id="cb8-7"><a href="#cb8-7"></a>permutedTestStats <-<span class="st"> </span><span class="kw">rep</span>(<span class="ot">NA</span>, N)</span>
<span id="cb8-8"><a href="#cb8-8"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span>N){</span>
<span id="cb8-9"><a href="#cb8-9"></a> permutedData <-<span class="st"> </span><span class="kw">sample</span>(sleep<span class="op">$</span>group)</span>
<span id="cb8-10"><a href="#cb8-10"></a> permutedTest <-<span class="st"> </span><span class="kw">t.test</span>(extra <span class="op">~</span><span class="st"> </span>permutedData, <span class="dt">data =</span> sleep, <span class="dt">mu =</span> <span class="dv">0</span>)</span>
<span id="cb8-11"><a href="#cb8-11"></a> permutedTestStats[i] <-<span class="st"> </span>permutedTest<span class="op">$</span>statistic</span>
<span id="cb8-12"><a href="#cb8-12"></a>}</span>
<span id="cb8-13"><a href="#cb8-13"></a></span>
<span id="cb8-14"><a href="#cb8-14"></a><span class="co"># Now we create a histogram of that distribution</span></span>
<span id="cb8-15"><a href="#cb8-15"></a><span class="kw">hist</span>(permutedTestStats, <span class="dt">col =</span> <span class="st">"skyblue"</span>)</span>
<span id="cb8-16"><a href="#cb8-16"></a><span class="kw">abline</span>(<span class="dt">v =</span> observedTestStat, <span class="dt">col =</span> <span class="st">"red"</span>, <span class="dt">lwd =</span> <span class="dv">3</span>)</span>
<span id="cb8-17"><a href="#cb8-17"></a></span>
<span id="cb8-18"><a href="#cb8-18"></a><span class="co">#Greater-Than p-value: Not the correct one in this case</span></span>
<span id="cb8-19"><a href="#cb8-19"></a><span class="kw">sum</span>(permutedTestStats <span class="op">>=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span>
<span id="cb8-20"><a href="#cb8-20"></a></span>
<span id="cb8-21"><a href="#cb8-21"></a><span class="co"># Less-Than p-value: This is the corret one</span></span>
<span id="cb8-22"><a href="#cb8-22"></a><span class="kw">sum</span>(permutedTestStats <span class="op"><=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span>
<span id="cb8-23"><a href="#cb8-23"></a></span>
<span id="cb8-24"><a href="#cb8-24"></a><span class="co"># Two-Sided p-value</span></span>
<span id="cb8-25"><a href="#cb8-25"></a><span class="dv">2</span><span class="op">*</span><span class="kw">sum</span>(permutedTestStats <span class="op"><=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span></code></pre></div>
<p><strong>Note</strong> The Kruskal-Wallis Rank Sum test is ran using the same code except with <code>myTest <- kruskal.test(y ~ X, data)</code> instead.</p>
</div>
<p><a href="javascript:showhide('ANOVA')">ANOVA <span style="font-size:8pt;">(click to show/hide)</span></a></p>
<div id="ANOVA" style="display:none;">
<h5 id="one-way-anova">One-Way ANOVA</h5>
<p>For this example, we will use the data from the <a href="Analyses/chickwtsOneWayANOVA.html">chick weights</a> analysis.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1"></a><span class="co"># Again, we run the initial test and find the test statistic</span></span>
<span id="cb9-2"><a href="#cb9-2"></a>myTest <-<span class="st"> </span><span class="kw">aov</span>(weight <span class="op">~</span><span class="st"> </span>feed, <span class="dt">data =</span> chickwts)</span>
<span id="cb9-3"><a href="#cb9-3"></a>observedTestStat <-<span class="st"> </span><span class="kw">summary</span>(myTest)[[<span class="dv">1</span>]]<span class="op">$</span><span class="st">`</span><span class="dt">F value</span><span class="st">`</span>[<span class="dv">1</span>]</span>
<span id="cb9-4"><a href="#cb9-4"></a></span>
<span id="cb9-5"><a href="#cb9-5"></a><span class="co"># For this permutation, we need to shake up the groups similar to the Independent Sample example</span></span>
<span id="cb9-6"><a href="#cb9-6"></a>N <-<span class="st"> </span><span class="dv">2000</span></span>
<span id="cb9-7"><a href="#cb9-7"></a>permutedTestStats <-<span class="st"> </span><span class="kw">rep</span>(<span class="ot">NA</span>, N)</span>
<span id="cb9-8"><a href="#cb9-8"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span>N){</span>
<span id="cb9-9"><a href="#cb9-9"></a> permutedData <-<span class="st"> </span><span class="kw">sample</span>(chickwts<span class="op">$</span>weight)</span>
<span id="cb9-10"><a href="#cb9-10"></a> permutedTest <-<span class="st"> </span><span class="kw">aov</span>(permutedData <span class="op">~</span><span class="st"> </span>feed, <span class="dt">data =</span> chickwts)</span>
<span id="cb9-11"><a href="#cb9-11"></a> permutedTestStats[i] <-<span class="st"> </span><span class="kw">summary</span>(permutedTest)[[<span class="dv">1</span>]]<span class="op">$</span><span class="st">`</span><span class="dt">F value</span><span class="st">`</span>[<span class="dv">1</span>]</span>
<span id="cb9-12"><a href="#cb9-12"></a>}</span>
<span id="cb9-13"><a href="#cb9-13"></a></span>
<span id="cb9-14"><a href="#cb9-14"></a><span class="co"># The histogram of this distribution gives an interesting insight into the results</span></span>
<span id="cb9-15"><a href="#cb9-15"></a><span class="kw">hist</span>(permutedTestStats, <span class="dt">col =</span> <span class="st">"skyblue"</span>, <span class="dt">xlim =</span> <span class="kw">c</span>(<span class="dv">0</span>,<span class="dv">16</span>))</span>
<span id="cb9-16"><a href="#cb9-16"></a><span class="kw">abline</span>(<span class="dt">v =</span> observedTestStat, <span class="dt">col =</span> <span class="st">"red"</span>, <span class="dt">lwd =</span> <span class="dv">3</span>)</span>
<span id="cb9-17"><a href="#cb9-17"></a></span>
<span id="cb9-18"><a href="#cb9-18"></a><span class="co"># Here is the greater-than p-value (since the F-distribution is right skewed</span></span>
<span id="cb9-19"><a href="#cb9-19"></a><span class="co"># this is the only p-value of interest.)</span></span>
<span id="cb9-20"><a href="#cb9-20"></a><span class="kw">sum</span>(permutedTestStats <span class="op">>=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span></code></pre></div>
<h5 id="two-way-anova">Two-Way ANOVA</h5>
<p>For the two-way ANOVA, I will use the data from the <a href="Analyses/warpbreaksTwoWayANOVA.html">warpbreaks</a> analysis.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1"></a><span class="co"># The initial test is done in the same way as one-way ANOVA but there is a little more to find the test statistic</span></span>
<span id="cb10-2"><a href="#cb10-2"></a>myTest <-<span class="st"> </span><span class="kw">aov</span>(breaks <span class="op">~</span><span class="st"> </span>wool <span class="op">+</span><span class="st"> </span>tension <span class="op">+</span><span class="st"> </span>wool<span class="op">:</span>tension, <span class="dt">data=</span>warpbreaks)</span>
<span id="cb10-3"><a href="#cb10-3"></a></span>
<span id="cb10-4"><a href="#cb10-4"></a><span class="co"># This first test statistic is the comparison between the two types of wool</span></span>
<span id="cb10-5"><a href="#cb10-5"></a>observedTestStatW <-<span class="st"> </span><span class="kw">summary</span>(myTest)[[<span class="dv">1</span>]]<span class="op">$</span><span class="st">`</span><span class="dt">F value</span><span class="st">`</span>[<span class="dv">1</span>]</span>
<span id="cb10-6"><a href="#cb10-6"></a></span>
<span id="cb10-7"><a href="#cb10-7"></a><span class="co"># This second test statistic is the comparison between the three types of tension</span></span>
<span id="cb10-8"><a href="#cb10-8"></a>observedTestStatT <-<span class="st"> </span><span class="kw">summary</span>(myTest)[[<span class="dv">1</span>]]<span class="op">$</span><span class="st">`</span><span class="dt">F value</span><span class="st">`</span>[<span class="dv">2</span>]</span>
<span id="cb10-9"><a href="#cb10-9"></a></span>
<span id="cb10-10"><a href="#cb10-10"></a><span class="co"># The third test statistic is the comparison of the interaction of wool types and tension</span></span>
<span id="cb10-11"><a href="#cb10-11"></a>observedTestStatWT <-<span class="st"> </span><span class="kw">summary</span>(myTest)[[<span class="dv">1</span>]]<span class="op">$</span><span class="st">`</span><span class="dt">F value</span><span class="st">`</span>[<span class="dv">3</span>]</span>
<span id="cb10-12"><a href="#cb10-12"></a></span>
<span id="cb10-13"><a href="#cb10-13"></a><span class="co"># Now comes three different permutations for the test. First is for wool, second is for tension, and third is the interaction</span></span>
<span id="cb10-14"><a href="#cb10-14"></a>N <-<span class="st"> </span><span class="dv">2000</span></span>
<span id="cb10-15"><a href="#cb10-15"></a>permutedTestStatsW <-<span class="st"> </span><span class="kw">rep</span>(<span class="ot">NA</span>, N)</span>
<span id="cb10-16"><a href="#cb10-16"></a>permutedTestStatsT <-<span class="st"> </span><span class="kw">rep</span>(<span class="ot">NA</span>, N)</span>
<span id="cb10-17"><a href="#cb10-17"></a>permutedTestStatsWT <-<span class="st"> </span><span class="kw">rep</span>(<span class="ot">NA</span>, N)</span>
<span id="cb10-18"><a href="#cb10-18"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span>N){</span>
<span id="cb10-19"><a href="#cb10-19"></a> permuteData <-<span class="st"> </span><span class="kw">sample</span>(warpbreaks<span class="op">$</span>breaks)</span>
<span id="cb10-20"><a href="#cb10-20"></a> permutedTest <-<span class="st"> </span><span class="kw">aov</span>(permuteData <span class="op">~</span><span class="st"> </span>wool <span class="op">+</span><span class="st"> </span>tension <span class="op">+</span><span class="st"> </span>wool<span class="op">:</span>tension, <span class="dt">data=</span>warpbreaks)</span>
<span id="cb10-21"><a href="#cb10-21"></a> permutedTestStatsW[i] <-<span class="st"> </span><span class="kw">summary</span>(permutedTest)[[<span class="dv">1</span>]]<span class="op">$</span><span class="st">`</span><span class="dt">F value</span><span class="st">`</span>[<span class="dv">1</span>]</span>
<span id="cb10-22"><a href="#cb10-22"></a> permutedTestStatsT[i] <-<span class="st"> </span><span class="kw">summary</span>(permutedTest)[[<span class="dv">1</span>]]<span class="op">$</span><span class="st">`</span><span class="dt">F value</span><span class="st">`</span>[<span class="dv">2</span>]</span>
<span id="cb10-23"><a href="#cb10-23"></a> permutedTestStatsWT[i] <-<span class="st"> </span><span class="kw">summary</span>(permutedTest)[[<span class="dv">1</span>]]<span class="op">$</span><span class="st">`</span><span class="dt">F value</span><span class="st">`</span>[<span class="dv">3</span>]</span>
<span id="cb10-24"><a href="#cb10-24"></a>}</span>
<span id="cb10-25"><a href="#cb10-25"></a></span>
<span id="cb10-26"><a href="#cb10-26"></a><span class="co"># We likewise need three differenct plots to show the distribution. First is wool, second is tension, and third is the interaction</span></span>
<span id="cb10-27"><a href="#cb10-27"></a><span class="kw">hist</span>(permutedTestStatsW, <span class="dt">col =</span> <span class="st">"skyblue"</span>, <span class="dt">xlim =</span> <span class="kw">c</span>(<span class="dv">3</span>,<span class="dv">14</span>))</span>
<span id="cb10-28"><a href="#cb10-28"></a><span class="kw">abline</span>(<span class="dt">v =</span> observedTestStatW, <span class="dt">col =</span> <span class="st">"red"</span>, <span class="dt">lwd =</span> <span class="dv">3</span>)</span>
<span id="cb10-29"><a href="#cb10-29"></a></span>
<span id="cb10-30"><a href="#cb10-30"></a><span class="kw">hist</span>(permutedTestStatsT, <span class="dt">col =</span> <span class="st">"skyblue"</span>)</span>
<span id="cb10-31"><a href="#cb10-31"></a><span class="kw">abline</span>(<span class="dt">v =</span> observedTestStatT, <span class="dt">col =</span> <span class="st">"red"</span>, <span class="dt">lwd =</span> <span class="dv">3</span>)</span>
<span id="cb10-32"><a href="#cb10-32"></a></span>
<span id="cb10-33"><a href="#cb10-33"></a><span class="kw">hist</span>(permutedTestStatsWT, <span class="dt">col =</span> <span class="st">"skyblue"</span>)</span>
<span id="cb10-34"><a href="#cb10-34"></a><span class="kw">abline</span>(<span class="dt">v =</span> observedTestStatWT, <span class="dt">col =</span> <span class="st">"red"</span>, <span class="dt">lwd =</span> <span class="dv">3</span>)</span>
<span id="cb10-35"><a href="#cb10-35"></a></span>
<span id="cb10-36"><a href="#cb10-36"></a><span class="co"># Greater-than p-value: the three situations are in order</span></span>
<span id="cb10-37"><a href="#cb10-37"></a><span class="kw">sum</span>(permutedTestStatsW <span class="op">>=</span><span class="st"> </span>observedTestStatW)<span class="op">/</span>N</span>
<span id="cb10-38"><a href="#cb10-38"></a></span>
<span id="cb10-39"><a href="#cb10-39"></a><span class="kw">sum</span>(permutedTestStatsT <span class="op">>=</span><span class="st"> </span>observedTestStatT)<span class="op">/</span>N</span>
<span id="cb10-40"><a href="#cb10-40"></a></span>
<span id="cb10-41"><a href="#cb10-41"></a><span class="kw">sum</span>(permutedTestStatsWT <span class="op">>=</span><span class="st"> </span>observedTestStatWT)<span class="op">/</span>N</span>
<span id="cb10-42"><a href="#cb10-42"></a></span>
<span id="cb10-43"><a href="#cb10-43"></a><span class="co"># Less-than p-value: again, they are in order</span></span>
<span id="cb10-44"><a href="#cb10-44"></a><span class="kw">sum</span>(permutedTestStatsW <span class="op"><=</span><span class="st"> </span>observedTestStatW)<span class="op">/</span>N</span>
<span id="cb10-45"><a href="#cb10-45"></a></span>
<span id="cb10-46"><a href="#cb10-46"></a><span class="kw">sum</span>(permutedTestStatsT <span class="op"><=</span><span class="st"> </span>observedTestStatT)<span class="op">/</span>N</span>
<span id="cb10-47"><a href="#cb10-47"></a></span>
<span id="cb10-48"><a href="#cb10-48"></a><span class="kw">sum</span>(permutedTestStatsWT <span class="op"><=</span><span class="st"> </span>observedTestStatWT)<span class="op">/</span>N</span>
<span id="cb10-49"><a href="#cb10-49"></a></span>
<span id="cb10-50"><a href="#cb10-50"></a><span class="co"># Two-sided p-values:</span></span>
<span id="cb10-51"><a href="#cb10-51"></a><span class="dv">2</span><span class="op">*</span><span class="kw">sum</span>(permutedTestStatsW <span class="op">>=</span><span class="st"> </span>observedTestStatW)<span class="op">/</span>N</span>
<span id="cb10-52"><a href="#cb10-52"></a></span>
<span id="cb10-53"><a href="#cb10-53"></a><span class="dv">2</span><span class="op">*</span><span class="kw">sum</span>(permutedTestStatsT <span class="op">>=</span><span class="st"> </span>observedTestStatT)<span class="op">/</span>N</span>
<span id="cb10-54"><a href="#cb10-54"></a></span>
<span id="cb10-55"><a href="#cb10-55"></a><span class="dv">2</span><span class="op">*</span><span class="kw">sum</span>(permutedTestStatsWT <span class="op">>=</span><span class="st"> </span>observedTestStatWT)<span class="op">/</span>N</span></code></pre></div>
</div>
<p><a href="javascript:showhide('LinR')">Simple Linear Regression <span style="font-size:8pt;">(click to show/hide)</span></a></p>
<div id="LinR" style="display:none;">
<p>For this example, I will use the <code>trees</code> dataset to compare the <code>Girth</code> and <code>Height</code> of black cherry trees.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1"></a><span class="co"># The test and then the test statistic is found in a similar way to that of an ANOVA (this is the t statistic)</span></span>
<span id="cb11-2"><a href="#cb11-2"></a>myTest <-<span class="st"> </span><span class="kw">lm</span>(Height <span class="op">~</span><span class="st"> </span>Girth, <span class="dt">data =</span> trees)</span>
<span id="cb11-3"><a href="#cb11-3"></a>observedTestStat <-<span class="st"> </span><span class="kw">summary</span>(myTest)[[<span class="dv">4</span>]][<span class="dv">2</span>,<span class="dv">3</span>]</span>
<span id="cb11-4"><a href="#cb11-4"></a></span>
<span id="cb11-5"><a href="#cb11-5"></a><span class="co"># The permutation part is set up in this way</span></span>
<span id="cb11-6"><a href="#cb11-6"></a>N <-<span class="st"> </span><span class="dv">2000</span></span>
<span id="cb11-7"><a href="#cb11-7"></a>permutedTestStats <-<span class="st"> </span><span class="kw">rep</span>(<span class="ot">NA</span>, N)</span>
<span id="cb11-8"><a href="#cb11-8"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span>N){</span>
<span id="cb11-9"><a href="#cb11-9"></a> permutedData <-<span class="st"> </span><span class="kw">sample</span>(trees<span class="op">$</span>Height)</span>
<span id="cb11-10"><a href="#cb11-10"></a> permutedTest <-<span class="st"> </span><span class="kw">lm</span>(permutedData <span class="op">~</span><span class="st"> </span>Girth, <span class="dt">data =</span> trees)</span>
<span id="cb11-11"><a href="#cb11-11"></a> permutedTestStats[i] <-<span class="st"> </span><span class="kw">summary</span>(permutedTest)[[<span class="dv">4</span>]][<span class="dv">2</span>,<span class="dv">3</span>]</span>
<span id="cb11-12"><a href="#cb11-12"></a>}</span>
<span id="cb11-13"><a href="#cb11-13"></a></span>
<span id="cb11-14"><a href="#cb11-14"></a><span class="co"># Here, as before, is the histogram of the distribution of the test statistics</span></span>
<span id="cb11-15"><a href="#cb11-15"></a><span class="kw">hist</span>(permutedTestStats, <span class="dt">col =</span> <span class="st">"skyblue"</span>)</span>
<span id="cb11-16"><a href="#cb11-16"></a><span class="kw">abline</span>(<span class="dt">v =</span> observedTestStat, <span class="dt">col =</span> <span class="st">"red"</span>, <span class="dt">lwd =</span> <span class="dv">3</span>)</span>
<span id="cb11-17"><a href="#cb11-17"></a></span>
<span id="cb11-18"><a href="#cb11-18"></a><span class="co"># Less-than p-value:</span></span>
<span id="cb11-19"><a href="#cb11-19"></a><span class="kw">sum</span>(permutedTestStats <span class="op"><=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span>
<span id="cb11-20"><a href="#cb11-20"></a></span>
<span id="cb11-21"><a href="#cb11-21"></a><span class="co"># Greater-than p-value:</span></span>
<span id="cb11-22"><a href="#cb11-22"></a><span class="kw">sum</span>(permutedTestStats <span class="op">>=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span>
<span id="cb11-23"><a href="#cb11-23"></a></span>
<span id="cb11-24"><a href="#cb11-24"></a><span class="co"># Two-Sided p-value:</span></span>
<span id="cb11-25"><a href="#cb11-25"></a><span class="dv">2</span><span class="op">*</span><span class="kw">sum</span>(permutedTestStats <span class="op">>=</span><span class="st"> </span>observedTestStat)<span class="op">/</span>N</span></code></pre></div>
</div>
</div>
<hr />
<footer>
</footer>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open')
});
});
</script>
<!-- code folding -->
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>