-
Notifications
You must be signed in to change notification settings - Fork 0
/
anova.html
3775 lines (3694 loc) · 253 KB
/
anova.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" />
<meta name="author" content="May" />
<title>ANOVA</title>
<script src="anova_files/header-attrs-2.1/header-attrs.js"></script>
<script src="anova_files/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="anova_files/bootstrap-3.3.5/css/united.min.css" rel="stylesheet" />
<script src="anova_files/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="anova_files/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="anova_files/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="anova_files/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="anova_files/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="anova_files/tocify-1.9.1/jquery.tocify.js"></script>
<script src="anova_files/navigation-1.1/tabsets.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ background-color: #f8f8f8; }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ef2929; } /* Alert */
code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #c4a000; } /* Attribute */
code span.bn { color: #0000cf; } /* BaseN */
code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4e9a06; } /* Char */
code span.cn { color: #000000; } /* Constant */
code span.co { color: #8f5902; font-style: italic; } /* Comment */
code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code span.dt { color: #204a87; } /* DataType */
code span.dv { color: #0000cf; } /* DecVal */
code span.er { color: #a40000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #0000cf; } /* Float */
code span.fu { color: #000000; } /* Function */
code span.im { } /* Import */
code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code span.ot { color: #8f5902; } /* Other */
code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code span.sc { color: #000000; } /* SpecialChar */
code span.ss { color: #4e9a06; } /* SpecialString */
code span.st { color: #4e9a06; } /* String */
code span.va { color: #000000; } /* Variable */
code span.vs { color: #4e9a06; } /* VerbatimString */
code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
</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>
<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>
<!-- 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 -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">ANOVA</h1>
<h4 class="author">May</h4>
<h4 class="date">2/20/2021</h4>
</div>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span></code></pre></div>
<div id="diet-data-set" class="section level1">
<h1>Diet Data Set</h1>
<p>This data set contains information on 76 people using one of three diets.</p>
<p><strong>Source</strong> <a href="https://www.sheffield.ac.uk/mash/statistics/datasets">link</a></p>
<p><strong>Format</strong></p>
<ul>
<li><code>gender</code>: 1 = male, 0 = female</li>
<li><code>Age</code>: Age (years)</li>
<li><code>Height</code>: Height (cm)</li>
<li><code>preweight</code>: Weight before the diet (kg)</li>
<li><code>Diet</code>: Diet Type</li>
<li><code>weight6weeks</code> - Weight after 6 weeks (kg)</li>
</ul>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>diet <span class="ot">=</span> <span class="fu">read.csv</span>(<span class="st">"datasets/Diet_R.csv"</span>)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>diet<span class="sc">$</span>weight_loss <span class="ot">=</span> diet<span class="sc">$</span>pre.weight <span class="sc">-</span> diet<span class="sc">$</span>weight6weeks</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>diet<span class="sc">$</span>Diet <span class="ot">=</span> <span class="fu">factor</span>(diet<span class="sc">$</span>Diet)</span></code></pre></div>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(diet)</span></code></pre></div>
<pre><code>## 'data.frame': 78 obs. of 8 variables:
## $ Person : int 25 26 1 2 3 4 5 6 7 8 ...
## $ gender : int NA NA 0 0 0 0 0 0 0 0 ...
## $ Age : int 41 32 22 46 55 33 50 50 37 28 ...
## $ Height : int 171 174 159 192 170 171 170 201 174 176 ...
## $ pre.weight : int 60 103 58 60 64 64 65 66 67 69 ...
## $ Diet : Factor w/ 3 levels "1","2","3": 2 2 1 1 1 1 1 1 1 1 ...
## $ weight6weeks: num 60 103 54.2 54 63.3 61.1 62.2 64 65 60.5 ...
## $ weight_loss : num 0 0 3.8 6 0.7 ...</code></pre>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>diet <span class="sc">%>%</span> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>weight_loss)) <span class="sc">+</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_histogram</span>(<span class="at">bins =</span> <span class="dv">25</span>, <span class="at">color =</span> <span class="st">'white'</span>, <span class="at">fill =</span> <span class="st">'darkblue'</span>) <span class="sc">+</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span> <span class="st">"Weight Lost"</span>, <span class="at">title =</span> <span class="st">"Histogram of Weight Lost"</span>)</span></code></pre></div>
<p><img src="anova_files/figure-html/Histogram%20of%20Weight%20Lost-1.png" width="672" /></p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>diet <span class="sc">%>%</span> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>weight_loss, <span class="at">fill =</span> Diet)) <span class="sc">+</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_histogram</span>(<span class="at">alpha=</span> .<span class="dv">5</span>, <span class="at">color =</span> <span class="st">'white'</span>, <span class="at">position =</span> <span class="st">'identity'</span>) <span class="sc">+</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span> <span class="st">"Weight Lost"</span>, <span class="at">title =</span> <span class="st">"Histogram of Weight Lost by Diet"</span>)</span></code></pre></div>
<pre><code>## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.</code></pre>
<p><img src="anova_files/figure-html/Histogram%20of%20Weight%20Lost%20by%20Diet%20-1.png" width="672" /></p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>diet <span class="sc">%>%</span> <span class="fu">group_by</span>(Diet) <span class="sc">%>%</span> <span class="fu">summarise</span>(<span class="at">mean=</span> <span class="fu">mean</span>(weight_loss),</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="at">sd =</span> <span class="fu">sd</span>(weight_loss))</span></code></pre></div>
<pre><code>## # A tibble: 3 x 3
## Diet mean sd
## * <fct> <dbl> <dbl>
## 1 1 3.3 2.24
## 2 2 3.03 2.52
## 3 3 5.15 2.40</code></pre>
<div id="one-way-anova" class="section level2">
<h2>One-way ANOVA</h2>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> diet, <span class="fu">aes</span>(<span class="at">x=</span>Diet, <span class="at">y=</span> weight_loss, <span class="at">color=</span>Diet)) <span class="sc">+</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">outlier.colour=</span><span class="st">"red"</span>, <span class="at">outlier.shape=</span><span class="dv">8</span>,<span class="at">outlier.size=</span><span class="dv">3</span>) <span class="sc">+</span></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span><span class="st">"Diet Type"</span>, <span class="at">y=</span> <span class="st">"Weight Loss"</span>) <span class="sc">+</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">""</span>)</span></code></pre></div>
<p><img src="anova_files/figure-html/Box%20plot%20weight%20loss-1.png" width="672" /></p>
<div id="analysis-of-variance" class="section level3">
<h3>Analysis of Variance</h3>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>diet.aov <span class="ot">=</span> <span class="fu">aov</span>(weight_loss<span class="sc">~</span>Diet,<span class="at">data=</span>diet)</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(diet.aov)</span></code></pre></div>
<pre><code>## Df Sum Sq Mean Sq F value Pr(>F)
## Diet 2 71.1 35.55 6.197 0.00323 **
## Residuals 75 430.2 5.74
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div id="checking-assumptions" class="section level3">
<h3>Checking Assumptions</h3>
<div id="levins-test" class="section level4">
<h4>Levin’s test</h4>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(lawstat)</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="fu">levene.test</span>(diet<span class="sc">$</span>weight_loss, diet<span class="sc">$</span>Diet, <span class="at">location =</span> <span class="st">"mean"</span>)</span></code></pre></div>
<pre><code>##
## Classical Levene's test based on the absolute deviations from the mean
## ( none not applied because the location is not set to median )
##
## data: diet$weight_loss
## Test Statistic = 0.6589, p-value = 0.5204</code></pre>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="co"># manually</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a><span class="co"># adding column of the group mean of weight loss </span></span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a>diet <span class="ot">=</span> diet <span class="sc">%>%</span> <span class="fu">group_by</span>(Diet) <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">group_mean =</span> <span class="fu">mean</span>(weight_loss))</span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a><span class="co"># adding Z column: </span></span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a>diet<span class="sc">$</span>z <span class="ot">=</span> <span class="fu">abs</span>(diet<span class="sc">$</span>weight_loss <span class="sc">-</span> diet<span class="sc">$</span>group_mean)</span>
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb15-8"><a href="#cb15-8" aria-hidden="true" tabindex="-1"></a>aov_equal_var <span class="ot">=</span> <span class="fu">aov</span>(z<span class="sc">~</span>Diet,<span class="at">data=</span>diet)</span>
<span id="cb15-9"><a href="#cb15-9" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(aov_equal_var)</span></code></pre></div>
<pre><code>## Df Sum Sq Mean Sq F value Pr(>F)
## Diet 2 2.68 1.341 0.659 0.52
## Residuals 75 152.61 2.035</code></pre>
</div>
<div id="diagnostic-plots" class="section level4">
<h4>Diagnostic plots</h4>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(diet.aov)</span></code></pre></div>
<p><img src="anova_files/figure-html/Checking%20Assumptions%20r-1.png" width="672" /><img src="anova_files/figure-html/Checking%20Assumptions%20r-2.png" width="672" /><img src="anova_files/figure-html/Checking%20Assumptions%20r-3.png" width="672" /><img src="anova_files/figure-html/Checking%20Assumptions%20r-4.png" width="672" /></p>
<p>We can also create these plot manually:</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="co"># residudal vs fitted plot:</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>diet.resid <span class="ot">=</span> <span class="fu">resid</span>(diet.aov)</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>diet.predict <span class="ot">=</span> <span class="fu">predict</span>(diet.aov)</span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(diet.resid<span class="sc">~</span>diet.predict)</span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a><span class="fu">abline</span>(<span class="dv">0</span>,<span class="dv">0</span>, <span class="at">col=</span><span class="st">"red"</span>)</span></code></pre></div>
<p><img src="anova_files/figure-html/Checking%20Assumptions-1.png" width="672" /></p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="do">## qqplot:</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a><span class="fu">qqnorm</span>(diet.resid)</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="fu">qqline</span>(diet.resid)</span></code></pre></div>
<p><img src="anova_files/figure-html/Checking%20Assumptions-2.png" width="672" /></p>
</div>
</div>
<div id="post-hoc-analysis" class="section level3">
<h3>Post-hoc analysis</h3>
<div id="estimated-expectations" class="section level4">
<h4>Estimated Expectations</h4>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">tapply</span>(<span class="at">X =</span> diet<span class="sc">$</span>weight_loss, <span class="at">INDEX =</span> diet<span class="sc">$</span>Diet, <span class="at">FUN =</span> mean )</span></code></pre></div>
<pre><code>## 1 2 3
## 3.300000 3.025926 5.148148</code></pre>
</div>
<div id="finding-which-group-is-different-from-all-other-groups" class="section level4">
<h4>Finding which group is different from all other groups</h4>
<p><span style="color: red;">????? </span></p>
</div>
<div id="pairwise-comparisons" class="section level4">
<h4>Pairwise Comparisons</h4>
<div id="regular-no-multiple-correction" class="section level5">
<h5>Regular (No multiple correction)</h5>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(multcomp)</span></code></pre></div>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>contr <span class="ot">=</span> <span class="fu">rbind</span>(</span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a> <span class="st">"2 - 1"</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">0</span>),</span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"3 - 1"</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>,<span class="dv">0</span>,<span class="dv">1</span>),</span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"3 - 2"</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="dv">0</span>,<span class="sc">-</span><span class="dv">1</span>,<span class="dv">1</span>)</span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a><span class="do">### set up general linear hypothesis</span></span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a>pairwise_res <span class="ot">=</span> <span class="fu">glht</span>(diet.aov, <span class="at">linfct =</span> <span class="fu">mcp</span>(<span class="at">Diet =</span> contr))</span>
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(pairwise_res,<span class="at">test =</span> <span class="fu">adjusted</span>(<span class="at">type=</span><span class="st">"none"</span>))</span></code></pre></div>
<pre><code>##
## Simultaneous Tests for General Linear Hypotheses
##
## Multiple Comparisons of Means: User-defined Contrasts
##
##
## Fit: aov(formula = weight_loss ~ Diet, data = diet)
##
## Linear Hypotheses:
## Estimate Std. Error t value Pr(>|t|)
## 2 - 1 == 0 -0.2741 0.6719 -0.408 0.68449
## 3 - 1 == 0 1.8481 0.6719 2.751 0.00745 **
## 3 - 2 == 0 2.1222 0.6518 3.256 0.00170 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- none method)</code></pre>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="do">### t.test</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="fu">pairwise.t.test</span>(diet<span class="sc">$</span>weight_loss, </span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> diet<span class="sc">$</span>Diet,</span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a> <span class="at">p.adjust.method =</span> <span class="st">"none"</span>)</span></code></pre></div>
<pre><code>##
## Pairwise comparisons using t tests with pooled SD
##
## data: diet$weight_loss and diet$Diet
##
## 1 2
## 2 0.6845 -
## 3 0.0075 0.0017
##
## P value adjustment method: none</code></pre>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="co"># calpha = univariate_calpha() - without correction </span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>pairwise_ci <span class="ot">=</span> <span class="fu">confint</span>(pairwise_res, <span class="at">calpha =</span> <span class="fu">univariate_calpha</span>())</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a>pairwise_ci</span></code></pre></div>
<pre><code>##
## Simultaneous Confidence Intervals
##
## Multiple Comparisons of Means: User-defined Contrasts
##
##
## Fit: aov(formula = weight_loss ~ Diet, data = diet)
##
## Quantile = 1.9921
## 95% confidence level
##
##
## Linear Hypotheses:
## Estimate lwr upr
## 2 - 1 == 0 -0.2741 -1.6125 1.0644
## 3 - 1 == 0 1.8481 0.5097 3.1866
## 3 - 2 == 0 2.1222 0.8237 3.4207</code></pre>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(pairwise_ci, <span class="at">xlab =</span> <span class="st">"Weight loss diff"</span>)</span></code></pre></div>
<p><img src="anova_files/figure-html/Pairwise%20Comparisons-1.png" width="672" /></p>
</div>
<div id="bonferroni-correction" class="section level5">
<h5>Bonferroni Correction</h5>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(pairwise_res, <span class="at">test =</span> <span class="fu">adjusted</span>(<span class="st">"bonferroni"</span>))</span></code></pre></div>
<pre><code>##
## Simultaneous Tests for General Linear Hypotheses
##
## Multiple Comparisons of Means: User-defined Contrasts
##
##
## Fit: aov(formula = weight_loss ~ Diet, data = diet)
##
## Linear Hypotheses:
## Estimate Std. Error t value Pr(>|t|)
## 2 - 1 == 0 -0.2741 0.6719 -0.408 1.00000
## 3 - 1 == 0 1.8481 0.6719 2.751 0.02235 *
## 3 - 2 == 0 2.1222 0.6518 3.256 0.00509 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- bonferroni method)</code></pre>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="do">### t.test</span></span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a><span class="fu">pairwise.t.test</span>(diet<span class="sc">$</span>weight_loss, </span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a> diet<span class="sc">$</span>Diet,</span>
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a> <span class="at">p.adjust.method =</span> <span class="st">"bonferroni"</span>)</span></code></pre></div>
<pre><code>##
## Pairwise comparisons using t tests with pooled SD
##
## data: diet$weight_loss and diet$Diet
##
## 1 2
## 2 1.0000 -
## 3 0.0224 0.0051
##
## P value adjustment method: bonferroni</code></pre>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>alpha <span class="ot">=</span> <span class="fl">0.05</span> ; m <span class="ot">=</span> <span class="dv">3</span> </span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a>pairwise_ci_bonferroni <span class="ot">=</span> <span class="fu">confint</span>(pairwise_res, <span class="at">level =</span> <span class="dv">1</span><span class="sc">-</span> (alpha<span class="sc">/</span>m) , <span class="at">calpha =</span> <span class="fu">univariate_calpha</span>())</span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a>pairwise_ci_bonferroni</span></code></pre></div>
<pre><code>##
## Simultaneous Confidence Intervals
##
## Multiple Comparisons of Means: User-defined Contrasts
##
##
## Fit: aov(formula = weight_loss ~ Diet, data = diet)
##
## Quantile = 2.4489
## 98.3333333333333% confidence level
##
##
## Linear Hypotheses:
## Estimate lwr upr
## 2 - 1 == 0 -0.2741 -1.9194 1.3713
## 3 - 1 == 0 1.8481 0.2028 3.4935
## 3 - 2 == 0 2.1222 0.5260 3.7184</code></pre>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(pairwise_ci_bonferroni)</span></code></pre></div>
<p><img src="anova_files/figure-html/Bonferroni%20Correction-1.png" width="672" /></p>
</div>
<div id="tukeys-method" class="section level5">
<h5>Tukey’s Method</h5>
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a>pairwise_res_tukey <span class="ot">=</span> <span class="fu">TukeyHSD</span>(diet.aov)</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a>pairwise_res_tukey</span></code></pre></div>
<pre><code>## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = weight_loss ~ Diet, data = diet)
##
## $Diet
## diff lwr upr p adj
## 2-1 -0.2740741 -1.8806155 1.332467 0.9124737
## 3-1 1.8481481 0.2416067 3.454690 0.0201413
## 3-2 2.1222222 0.5636481 3.680796 0.0047819</code></pre>
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(pairwise_res_tukey)</span></code></pre></div>
<p><img src="anova_files/figure-html/unnamed-chunk-11-1.png" width="672" /></p>
</div>
</div>
<div id="multiple-comparisons" class="section level4">
<h4>Multiple Comparisons</h4>
<div id="prespecified-comparison" class="section level5">
<h5>Prespecified Comparison</h5>
<p><span style="color: red;">Any Other Idea???</span></p>
<p><span class="math display">\[
H_0: \frac{\alpha_1+\alpha_2}{2}-\alpha_3=0 \leftrightarrow \frac{\mu_1 + \mu_2}{2} - \mu_3 = 0
\]</span></p>
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>contr1 <span class="ot">=</span> (<span class="fu">c</span>(<span class="dv">1</span><span class="sc">/</span><span class="dv">2</span>,<span class="dv">1</span><span class="sc">/</span><span class="dv">2</span>,<span class="sc">-</span><span class="dv">1</span>))</span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a>glht_test <span class="ot">=</span> <span class="fu">glht</span>(diet.aov, <span class="at">linfct =</span> <span class="fu">mcp</span>(<span class="at">Diet =</span> contr1))</span>
<span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(glht_test)</span></code></pre></div>
<pre><code>##
## Simultaneous Tests for General Linear Hypotheses
##
## Multiple Comparisons of Means: User-defined Contrasts
##
##
## Fit: aov(formula = weight_loss ~ Diet, data = diet)
##
## Linear Hypotheses:
## Estimate Std. Error t value Pr(>|t|)
## 1 == 0 -1.9852 0.5703 -3.481 0.000837 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- single-step method)</code></pre>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="co"># other option:</span></span>
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a>lvls <span class="ot">=</span> <span class="fu">data.frame</span>(<span class="at">diet_type =</span> <span class="fu">unique</span>(diet<span class="sc">$</span>Diet))</span>
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a>X <span class="ot">=</span> <span class="fu">model.matrix</span>(<span class="sc">~</span> diet_type , <span class="at">data =</span> lvls)</span>
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>( <span class="fu">glht</span>(diet.aov, <span class="at">linfct =</span> contr1 <span class="sc">%*%</span> X ))</span></code></pre></div>
<pre><code>##
## Simultaneous Tests for General Linear Hypotheses
##
## Fit: aov(formula = weight_loss ~ Diet, data = diet)
##
## Linear Hypotheses:
## Estimate Std. Error t value Pr(>|t|)
## 1 == 0 -1.9852 0.5703 -3.481 0.000837 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- single-step method)</code></pre>
</div>
<div id="scheffe-method" class="section level5">
<h5>Scheffe Method</h5>
<p><span style="color: red;">Any Other Idea???</span></p>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(DescTools)</span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ScheffeTest</span>(diet.aov , <span class="at">contrasts =</span> contr1, </span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a> <span class="at">conf.level =</span> <span class="fl">0.95</span>) </span></code></pre></div>
<pre><code>##
## Posthoc multiple comparisons of means: Scheffe Test
## 95% family-wise confidence level
##
## $Diet
## diff lwr.ci upr.ci pval
## 1,2-3 -1.985185 -3.409589 -0.5607817 0.0036 **
##
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
</div>
</div>
<div id="two-way-anova" class="section level2">
<h2>Two-way ANOVA</h2>
<p><strong>Effect of diet and gender on weight lost</strong></p>
<p>Gender:<br />
1- male, 0 - female</p>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a>diet<span class="sc">$</span>gender <span class="ot">=</span> <span class="fu">factor</span>(diet<span class="sc">$</span>gender)</span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> diet, <span class="fu">aes</span>(<span class="at">x=</span>Diet, <span class="at">y=</span> weight_loss, <span class="at">color=</span>gender)) <span class="sc">+</span></span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">outlier.colour=</span><span class="st">"red"</span>, <span class="at">outlier.shape=</span><span class="dv">8</span>,<span class="at">outlier.size=</span><span class="dv">2</span>) <span class="sc">+</span></span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span><span class="st">"Diet Type"</span>, <span class="at">y=</span> <span class="st">"Weight Loss"</span>) <span class="sc">+</span></span>
<span id="cb46-5"><a href="#cb46-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_discrete</span>(<span class="at">name =</span> <span class="st">"Gender"</span>, <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"Female"</span>, <span class="st">"Male"</span>))</span></code></pre></div>
<p><img src="anova_files/figure-html/diet%20Boxplot%20of%20weight%20lost%20by%20gender-1.png" width="672" /></p>
<div class="sourceCode" id="cb47"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>diet.aov2 <span class="ot">=</span> <span class="fu">aov</span>( weight_loss<span class="sc">~</span>Diet <span class="sc">+</span> gender ,<span class="at">data=</span>diet)</span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(diet.aov2) </span></code></pre></div>
<pre><code>## Df Sum Sq Mean Sq F value Pr(>F)
## Diet 2 60.5 30.264 5.312 0.00705 **
## gender 1 0.2 0.169 0.030 0.86387
## Residuals 72 410.2 5.698
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness</code></pre>
<div class="sourceCode" id="cb49"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(diet.aov2)</span></code></pre></div>
<p><img src="anova_files/figure-html/diet%20two-way%20checking%20assumptions-1.png" width="672" /><img src="anova_files/figure-html/diet%20two-way%20checking%20assumptions-2.png" width="672" /><img src="anova_files/figure-html/diet%20two-way%20checking%20assumptions-3.png" width="672" /><img src="anova_files/figure-html/diet%20two-way%20checking%20assumptions-4.png" width="672" /></p>
<p><strong>F tests:</strong></p>
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>diet.aov <span class="ot">=</span> <span class="fu">aov</span>(weight_loss<span class="sc">~</span>Diet,<span class="at">data=</span>diet[<span class="sc">!</span><span class="fu">is.na</span>(diet<span class="sc">$</span>gender),] )</span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a>results <span class="ot">=</span> <span class="fu">anova</span>( diet.aov, diet.aov2)</span>
<span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(results)</span></code></pre></div>
<pre><code>## Analysis of Variance Table
##
## Model 1: weight_loss ~ Diet
## Model 2: weight_loss ~ Diet + gender
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 73 410.40
## 2 72 410.23 1 0.1687 0.0296 0.8639</code></pre>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="co"># note it is equivalent to aov() even if the data is unbalanced</span></span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a>results2 <span class="ot">=</span> <span class="fu">summary</span>(diet.aov2)</span>
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a>results<span class="sc">$</span><span class="st">`</span><span class="at">Pr(>F)</span><span class="st">`</span>[<span class="dv">2</span>]</span></code></pre></div>
<pre><code>## [1] 0.8638659</code></pre>
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>results2[[<span class="dv">1</span>]]<span class="sc">$</span><span class="st">`</span><span class="at">Pr(>F)</span><span class="st">`</span>[<span class="dv">2</span>] </span></code></pre></div>
<pre><code>## [1] 0.8638659</code></pre>
</div>
<div id="intercations" class="section level2">
<h2>Intercations</h2>
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a>diet.aov2_intr <span class="ot">=</span> <span class="fu">aov</span>( weight_loss<span class="sc">~</span>Diet <span class="sc">*</span> gender ,<span class="at">data=</span>diet)</span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(diet.aov2_intr) </span></code></pre></div>
<pre><code>## Df Sum Sq Mean Sq F value Pr(>F)
## Diet 2 60.5 30.264 5.629 0.00541 **
## gender 1 0.2 0.169 0.031 0.85991
## Diet:gender 2 33.9 16.952 3.153 0.04884 *
## Residuals 70 376.3 5.376
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness</code></pre>
<p><strong>Interaction Plot</strong></p>
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="fu">interaction.plot</span>(<span class="at">x.factor =</span> diet<span class="sc">$</span>Diet,</span>
<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.factor =</span> diet<span class="sc">$</span>gender, </span>
<span id="cb58-3"><a href="#cb58-3" aria-hidden="true" tabindex="-1"></a> <span class="at">response =</span> diet<span class="sc">$</span>weight_loss,</span>
<span id="cb58-4"><a href="#cb58-4" aria-hidden="true" tabindex="-1"></a> <span class="at">fun =</span> mean, </span>
<span id="cb58-5"><a href="#cb58-5" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"b"</span>, <span class="at">legend =</span> <span class="cn">TRUE</span>, </span>
<span id="cb58-6"><a href="#cb58-6" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"Diet"</span>, <span class="at">ylab=</span><span class="st">"Weight Loss"</span>,</span>
<span id="cb58-7"><a href="#cb58-7" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.label =</span> <span class="st">"Gender"</span>,</span>
<span id="cb58-8"><a href="#cb58-8" aria-hidden="true" tabindex="-1"></a> <span class="at">pch=</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">8</span>), <span class="at">col =</span> <span class="fu">c</span>(<span class="st">"#FF6347"</span>, <span class="st">"#3CB371"</span>))</span></code></pre></div>
<p><img src="anova_files/figure-html/interatcion%20plot%20diet-1.png" width="672" /></p>
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(diet.aov2_intr)</span></code></pre></div>
<p><img src="anova_files/figure-html/unnamed-chunk-16-1.png" width="672" /><img src="anova_files/figure-html/unnamed-chunk-16-2.png" width="672" /><img src="anova_files/figure-html/unnamed-chunk-16-3.png" width="672" /><img src="anova_files/figure-html/unnamed-chunk-16-4.png" width="672" /></p>
<p><strong>Levin’s test</strong></p>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a>diet <span class="ot">=</span> diet[<span class="sc">!</span><span class="fu">is.na</span>(diet<span class="sc">$</span>gender),]</span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a>groups_df <span class="ot">=</span> <span class="fu">cbind</span> ( <span class="fu">expand.grid</span>(<span class="at">Diet =</span> <span class="fu">unique</span>(diet<span class="sc">$</span>Diet),<span class="at">gender =</span> <span class="fu">unique</span>(diet<span class="sc">$</span>gender)), <span class="at">group =</span> <span class="fu">factor</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">6</span>))</span>
<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a>diet2 <span class="ot">=</span> <span class="fu">inner_join</span>(diet ,groups_df, <span class="at">by =</span> <span class="fu">c</span>(<span class="st">"Diet"</span>, <span class="st">"gender"</span>) )</span>
<span id="cb60-4"><a href="#cb60-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-5"><a href="#cb60-5" aria-hidden="true" tabindex="-1"></a><span class="co"># option 1 </span></span>
<span id="cb60-6"><a href="#cb60-6" aria-hidden="true" tabindex="-1"></a>lawstat<span class="sc">::</span><span class="fu">levene.test</span>(<span class="at">y =</span> diet2<span class="sc">$</span>weight_loss, <span class="at">group =</span> diet2<span class="sc">$</span>group , <span class="at">location =</span> <span class="st">"mean"</span> )</span></code></pre></div>
<pre><code>##
## Classical Levene's test based on the absolute deviations from the mean
## ( none not applied because the location is not set to median )
##
## data: diet2$weight_loss
## Test Statistic = 0.38158, p-value = 0.8598</code></pre>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="co"># option 2 </span></span>
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a>DescTools<span class="sc">::</span><span class="fu">LeveneTest</span>(weight_loss <span class="sc">~</span> Diet <span class="sc">*</span> <span class="fu">factor</span>(gender) ,<span class="at">data=</span>diet )</span></code></pre></div>
<pre><code>## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 5 0.3867 0.8563
## 70</code></pre>
</div>
</div>
<div id="interaction-plots-examples" class="section level1">
<h1>Interaction Plots Examples</h1>
<p><strong>Invented example:</strong><br />
Ranking of movies from different genres by women and men.</p>
<div id="no-interaction" class="section level2">
<h2>No interaction</h2>
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">22</span>)</span>
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a>group <span class="ot"><-</span> <span class="fu">gl</span>(<span class="at">n =</span> <span class="dv">2</span>, <span class="at">k =</span> <span class="dv">20</span>, <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"Female"</span>,<span class="st">"Male"</span>))</span>
<span id="cb64-3"><a href="#cb64-3" aria-hidden="true" tabindex="-1"></a>trt <span class="ot"><-</span> <span class="fu">rep</span>(<span class="fu">rep</span>(<span class="fu">c</span>(<span class="st">"Adventure"</span>,<span class="st">"Comedy"</span>), <span class="at">each=</span><span class="dv">10</span>),<span class="dv">2</span>)</span>
<span id="cb64-4"><a href="#cb64-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-5"><a href="#cb64-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-6"><a href="#cb64-6" aria-hidden="true" tabindex="-1"></a>resp <span class="ot"><-</span> <span class="fu">c</span>(</span>
<span id="cb64-7"><a href="#cb64-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">rnorm</span>(<span class="at">n =</span> <span class="dv">20</span>, <span class="at">mean =</span> <span class="fu">rep</span>(<span class="fu">c</span>(<span class="fl">3.60</span>,<span class="fl">3.65</span>), <span class="at">each =</span> <span class="dv">10</span>), <span class="at">sd =</span> .<span class="dv">01</span>),</span>
<span id="cb64-8"><a href="#cb64-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">rnorm</span>(<span class="at">n =</span> <span class="dv">20</span>, <span class="at">mean =</span> <span class="fu">rep</span>(<span class="fu">c</span>(<span class="fl">3.62</span>,<span class="fl">3.67</span>), <span class="at">each =</span> <span class="dv">10</span>), <span class="at">sd =</span> .<span class="dv">01</span>)</span>
<span id="cb64-9"><a href="#cb64-9" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb64-10"><a href="#cb64-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-11"><a href="#cb64-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-12"><a href="#cb64-12" aria-hidden="true" tabindex="-1"></a>df1 <span class="ot"><-</span> <span class="fu">data.frame</span>(group, trt, resp)</span>
<span id="cb64-13"><a href="#cb64-13" aria-hidden="true" tabindex="-1"></a><span class="fu">interaction.plot</span>(<span class="at">x.factor =</span> df1<span class="sc">$</span>trt, </span>
<span id="cb64-14"><a href="#cb64-14" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.factor =</span> df1<span class="sc">$</span>group,</span>
<span id="cb64-15"><a href="#cb64-15" aria-hidden="true" tabindex="-1"></a> <span class="at">response =</span> df1<span class="sc">$</span>resp,</span>
<span id="cb64-16"><a href="#cb64-16" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"b"</span>, <span class="at">legend =</span> <span class="cn">TRUE</span>,</span>
<span id="cb64-17"><a href="#cb64-17" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"Genre"</span>, <span class="at">ylab=</span><span class="st">"Movie Rating"</span>,</span>
<span id="cb64-18"><a href="#cb64-18" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.label =</span> <span class="st">"Gender"</span>,</span>
<span id="cb64-19"><a href="#cb64-19" aria-hidden="true" tabindex="-1"></a> <span class="at">pch=</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">8</span>), <span class="at">col =</span> <span class="fu">c</span>(<span class="st">"#FF6347"</span>, <span class="st">"#3CB371"</span>) )</span></code></pre></div>
<p><img src="anova_files/figure-html/rating%20interaction%20plot%201-1.png" width="672" /></p>
</div>
<div id="with-interaction" class="section level2">
<h2>with interaction</h2>
<div class="sourceCode" id="cb65"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">22</span>)</span>
<span id="cb65-2"><a href="#cb65-2" aria-hidden="true" tabindex="-1"></a>group <span class="ot"><-</span> <span class="fu">gl</span>(<span class="at">n =</span> <span class="dv">2</span>, <span class="at">k =</span> <span class="dv">20</span>, <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"Female"</span>,<span class="st">"Male"</span>))</span>
<span id="cb65-3"><a href="#cb65-3" aria-hidden="true" tabindex="-1"></a>trt <span class="ot"><-</span> <span class="fu">rep</span>(<span class="fu">rep</span>(<span class="fu">c</span>(<span class="st">"Sci-fi"</span>,<span class="st">"Romance"</span>), <span class="at">each=</span><span class="dv">10</span>),<span class="dv">2</span>)</span>
<span id="cb65-4"><a href="#cb65-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb65-5"><a href="#cb65-5" aria-hidden="true" tabindex="-1"></a>resp <span class="ot"><-</span> <span class="fu">c</span>(</span>
<span id="cb65-6"><a href="#cb65-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">rnorm</span>(<span class="at">n =</span> <span class="dv">20</span>, <span class="at">mean =</span> <span class="fu">rep</span>(<span class="fu">c</span>(<span class="fl">3.60</span>,<span class="fl">3.65</span>), <span class="at">each =</span> <span class="dv">10</span>), <span class="at">sd =</span> .<span class="dv">01</span>),</span>
<span id="cb65-7"><a href="#cb65-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">rnorm</span>(<span class="at">n =</span> <span class="dv">20</span>, <span class="at">mean =</span> <span class="fu">rep</span>(<span class="fu">c</span>(<span class="fl">3.65</span>,<span class="fl">3.60</span>), <span class="at">each =</span> <span class="dv">10</span>), <span class="at">sd =</span> .<span class="dv">01</span>)</span>
<span id="cb65-8"><a href="#cb65-8" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb65-9"><a href="#cb65-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb65-10"><a href="#cb65-10" aria-hidden="true" tabindex="-1"></a>df2 <span class="ot"><-</span> <span class="fu">data.frame</span>(group, trt, resp)</span>
<span id="cb65-11"><a href="#cb65-11" aria-hidden="true" tabindex="-1"></a><span class="fu">interaction.plot</span>(<span class="at">x.factor =</span> df2<span class="sc">$</span>trt, </span>
<span id="cb65-12"><a href="#cb65-12" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.factor =</span> df2<span class="sc">$</span>group,</span>
<span id="cb65-13"><a href="#cb65-13" aria-hidden="true" tabindex="-1"></a> <span class="at">response =</span> df2<span class="sc">$</span>resp,</span>
<span id="cb65-14"><a href="#cb65-14" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"b"</span>, <span class="at">legend =</span> <span class="cn">TRUE</span>,</span>
<span id="cb65-15"><a href="#cb65-15" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"Genre"</span>, <span class="at">ylab=</span><span class="st">"Movie Rating"</span>,</span>
<span id="cb65-16"><a href="#cb65-16" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.label =</span> <span class="st">"Gender"</span>,</span>
<span id="cb65-17"><a href="#cb65-17" aria-hidden="true" tabindex="-1"></a> <span class="at">pch=</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">8</span>), <span class="at">col =</span> <span class="fu">c</span>(<span class="st">"#FF6347"</span>, <span class="st">"#3CB371"</span>) )</span></code></pre></div>
<p><img src="anova_files/figure-html/rating%20interaction%20plot%202-1.png" width="672" /></p>
<div class="sourceCode" id="cb66"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">22</span>)</span>
<span id="cb66-2"><a href="#cb66-2" aria-hidden="true" tabindex="-1"></a>trt <span class="ot"><-</span> <span class="fu">rep</span>(<span class="fu">rep</span>(<span class="fu">c</span>(<span class="st">"Romantic Comedy"</span>, <span class="st">"Musicals"</span>), <span class="at">each=</span><span class="dv">10</span>),<span class="dv">2</span>)</span>
<span id="cb66-3"><a href="#cb66-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb66-4"><a href="#cb66-4" aria-hidden="true" tabindex="-1"></a>resp <span class="ot"><-</span> <span class="fu">c</span>(</span>
<span id="cb66-5"><a href="#cb66-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">rnorm</span>(<span class="at">n =</span> <span class="dv">20</span>, <span class="at">mean =</span> <span class="fu">rep</span>(<span class="fu">c</span>(<span class="fl">3.25</span>,<span class="fl">3.17</span>), <span class="at">each =</span> <span class="dv">10</span>), <span class="at">sd =</span> .<span class="dv">01</span>),</span>
<span id="cb66-6"><a href="#cb66-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">rnorm</span>(<span class="at">n =</span> <span class="dv">20</span>, <span class="at">mean =</span> <span class="fu">rep</span>(<span class="fu">c</span>(<span class="fl">3.10</span>,<span class="fl">3.13</span>), <span class="at">each =</span> <span class="dv">10</span>), <span class="at">sd =</span> .<span class="dv">01</span>)</span>
<span id="cb66-7"><a href="#cb66-7" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb66-8"><a href="#cb66-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb66-9"><a href="#cb66-9" aria-hidden="true" tabindex="-1"></a>df3 <span class="ot"><-</span> <span class="fu">data.frame</span>(group, trt, resp)</span>
<span id="cb66-10"><a href="#cb66-10" aria-hidden="true" tabindex="-1"></a><span class="fu">interaction.plot</span>(<span class="at">x.factor =</span> df3<span class="sc">$</span>trt, </span>
<span id="cb66-11"><a href="#cb66-11" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.factor =</span> df3<span class="sc">$</span>group,</span>
<span id="cb66-12"><a href="#cb66-12" aria-hidden="true" tabindex="-1"></a> <span class="at">response =</span> df3<span class="sc">$</span>resp,</span>
<span id="cb66-13"><a href="#cb66-13" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"b"</span>, <span class="at">legend =</span> <span class="cn">TRUE</span>,</span>
<span id="cb66-14"><a href="#cb66-14" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"Genre"</span>, <span class="at">ylab=</span><span class="st">"Movie Rating"</span>,</span>
<span id="cb66-15"><a href="#cb66-15" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.label =</span> <span class="st">"Gender"</span>,</span>
<span id="cb66-16"><a href="#cb66-16" aria-hidden="true" tabindex="-1"></a> <span class="at">pch=</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">8</span>), <span class="at">col =</span> <span class="fu">c</span>(<span class="st">"#FF6347"</span>, <span class="st">"#3CB371"</span>) )</span></code></pre></div>
<p><img src="anova_files/figure-html/rating%20interaction%20plot%203-1.png" width="672" /></p>
</div>
</div>
<div id="ricci-firefighter-promotion-exam-scores-dataset" class="section level1">
<h1>Ricci: Firefighter Promotion Exam Scores Dataset</h1>
<p><strong>Details</strong></p>
<p>The city of New Haven, Connecticut administered exams (both written and oral) in November and December of 2003 to firefighters hoping to qualify for promotion to either Lieutenant or Captain in the city fire department. A final score consisting of a 60% weight for the written exam and a 40% weight for the oral exam was computed for each person who took the exam. For each person who took the exams, there are measurements on their race (black, white, or Hispanic), which position they were trying for (Lieutenant, Captain), scores on the oral and written exams, and the combined score. These data were used as part of a court case (Ricci v.DeStefano) dealing with racial discrimination</p>
<p><strong>Source</strong></p>
<p>An article on using these data: Miao, W. (2011) “Did the Results of Promotion Exams Have a Disparate Impact on Minorities? Using Statistical Evidence in Ricci v. DeStefano,” JSE 19:1 at <a href="http://jse.amstat.org/v18n3/miao.pdf">link</a></p>
<p><strong>Format</strong></p>
<ul>
<li><code>Race</code>: Race of firefighter (B=black, H=Hispanic, or W=white)</li>
<li><code>Position</code>: Promotion desired (Captain or Lieutenant)</li>
<li><code>Oral</code>: Oral exam score</li>
<li><code>Written</code>: Written exam score</li>
<li><code>Combine</code>: Combined score (written exam gets 60% weight)</li>
</ul>
<div class="sourceCode" id="cb67"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(Stat2Data)</span>
<span id="cb67-2"><a href="#cb67-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb67-3"><a href="#cb67-3" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(Ricci)</span>
<span id="cb67-4"><a href="#cb67-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb67-5"><a href="#cb67-5" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(Ricci)</span></code></pre></div>
<pre><code>## 'data.frame': 118 obs. of 5 variables:
## $ Race : Factor w/ 3 levels "B","H","W": 3 3 3 3 3 2 3 2 3 3 ...
## $ Position: Factor w/ 2 levels "Captain","Lieutenant": 1 1 1 1 1 1 1 1 1 1 ...
## $ Oral : num 89.5 80 82.4 88.6 76.2 ...
## $ Written : int 95 95 87 76 84 82 82 84 81 72 ...
## $ Combine : num 92.8 89 85.2 81 80.9 ...</code></pre>
<div class="sourceCode" id="cb69"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> Ricci, </span>
<span id="cb69-2"><a href="#cb69-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x=</span>Race, <span class="at">y=</span> Combine, <span class="at">color=</span>Position)) <span class="sc">+</span></span>
<span id="cb69-3"><a href="#cb69-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">outlier.colour=</span><span class="st">"red"</span>, <span class="at">outlier.shape=</span><span class="dv">8</span>,<span class="at">outlier.size=</span><span class="dv">3</span>) <span class="sc">+</span></span>
<span id="cb69-4"><a href="#cb69-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span><span class="st">"Race"</span>, <span class="at">y=</span> <span class="st">"Combined score"</span>) </span></code></pre></div>
<p><img src="anova_files/figure-html/ricci%20boxplot%20of%20score%20by%20race-1.png" width="672" /></p>
<div id="one-way-anova-1" class="section level2">
<h2>One-way ANOVA</h2>
<div id="option-1" class="section level3">
<h3>Option 1</h3>
<div class="sourceCode" id="cb70"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb70-1"><a href="#cb70-1" aria-hidden="true" tabindex="-1"></a>aov_captain <span class="ot">=</span> <span class="fu">aov</span>(Combine <span class="sc">~</span> Race , <span class="at">data =</span> Ricci <span class="sc">%>%</span> <span class="fu">filter</span>(Position <span class="sc">==</span> <span class="st">"Captain"</span>))</span>
<span id="cb70-2"><a href="#cb70-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(aov_captain)</span></code></pre></div>
<pre><code>## Df Sum Sq Mean Sq F value Pr(>F)
## Race 2 707.2 353.6 5.034 0.0115 *
## Residuals 38 2669.1 70.2
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<div id="tukes-95-cis" class="section level4">
<h4>Tuke’s 95% CI’s</h4>
<div class="sourceCode" id="cb72"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb72-1"><a href="#cb72-1" aria-hidden="true" tabindex="-1"></a>ricci_capt_tukey <span class="ot">=</span> <span class="fu">TukeyHSD</span>(aov_captain)</span>
<span id="cb72-2"><a href="#cb72-2" aria-hidden="true" tabindex="-1"></a>ricci_capt_tukey</span></code></pre></div>
<pre><code>## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Combine ~ Race, data = Ricci %>% filter(Position == "Captain"))
##
## $Race
## diff lwr upr p adj
## H-B 4.7645 -5.455364 14.98436 0.4976777
## W-B 10.3308 2.028143 18.63346 0.0117824
## W-H 5.5663 -2.736357 13.86896 0.2435959</code></pre>
<div class="sourceCode" id="cb74"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb74-1"><a href="#cb74-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(ricci_capt_tukey)</span></code></pre></div>
<p><img src="anova_files/figure-html/unnamed-chunk-21-1.png" width="672" /></p>
</div>
</div>
<div id="option-2" class="section level3">
<h3>Option 2</h3>
<div class="sourceCode" id="cb75"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a>aov_lieutenant <span class="ot">=</span> <span class="fu">aov</span>(Combine <span class="sc">~</span> Race , <span class="at">data =</span> Ricci <span class="sc">%>%</span> <span class="fu">filter</span>(Position <span class="sc">==</span> <span class="st">"Lieutenant"</span>))</span>
<span id="cb75-2"><a href="#cb75-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(aov_lieutenant)</span></code></pre></div>
<pre><code>## Df Sum Sq Mean Sq F value Pr(>F)
## Race 2 1266 633.2 8.579 0.000446 ***
## Residuals 74 5462 73.8
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<div class="sourceCode" id="cb77"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a>ricci_lie_tukey <span class="ot">=</span> <span class="fu">TukeyHSD</span>(aov_lieutenant)</span>
<span id="cb77-2"><a href="#cb77-2" aria-hidden="true" tabindex="-1"></a>ricci_lie_tukey</span></code></pre></div>
<pre><code>## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Combine ~ Race, data = Ricci %>% filter(Position == "Lieutenant"))
##
## $Race
## diff lwr upr p adj
## H-B -0.09272982 -7.190168 7.004708 0.9994618
## W-B 8.12599266 2.465302 13.786683 0.0027861
## W-H 8.21872248 2.056765 14.380680 0.0058494</code></pre>
<div class="sourceCode" id="cb79"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb79-1"><a href="#cb79-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(ricci_lie_tukey)</span></code></pre></div>
<p><img src="anova_files/figure-html/unnamed-chunk-24-1.png" width="672" /></p>
</div>
</div>
<div id="two-way-anova-1" class="section level2">
<h2>Two-way ANOVA</h2>
<div class="sourceCode" id="cb80"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb80-1"><a href="#cb80-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(<span class="fu">aov</span>(Combine <span class="sc">~</span> Race<span class="sc">*</span>Position , <span class="at">data =</span> Ricci))</span></code></pre></div>
<pre><code>## Df Sum Sq Mean Sq F value Pr(>F)
## Race 2 1972 985.8 13.579 5.25e-06 ***
## Position 1 144 143.9 1.981 0.162
## Race:Position 2 64 32.0 0.441 0.645
## Residuals 112 8131 72.6
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<div class="sourceCode" id="cb82"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb82-1"><a href="#cb82-1" aria-hidden="true" tabindex="-1"></a><span class="co"># interaction plot </span></span>
<span id="cb82-2"><a href="#cb82-2" aria-hidden="true" tabindex="-1"></a><span class="fu">interaction.plot</span>(<span class="at">x.factor =</span> Ricci<span class="sc">$</span>Race,</span>
<span id="cb82-3"><a href="#cb82-3" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.factor =</span> Ricci<span class="sc">$</span>Position, </span>
<span id="cb82-4"><a href="#cb82-4" aria-hidden="true" tabindex="-1"></a> <span class="at">response =</span> Ricci<span class="sc">$</span>Combine,</span>
<span id="cb82-5"><a href="#cb82-5" aria-hidden="true" tabindex="-1"></a> <span class="at">fun =</span> mean, </span>
<span id="cb82-6"><a href="#cb82-6" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"b"</span>, <span class="at">legend =</span> <span class="cn">TRUE</span>, </span>
<span id="cb82-7"><a href="#cb82-7" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"Race"</span>, <span class="at">ylab=</span><span class="st">"Combine Score"</span>,</span>
<span id="cb82-8"><a href="#cb82-8" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.label =</span> <span class="st">"Position"</span>,</span>
<span id="cb82-9"><a href="#cb82-9" aria-hidden="true" tabindex="-1"></a> <span class="at">pch=</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">8</span>), <span class="at">col =</span> <span class="fu">c</span>(<span class="st">"#FF6347"</span>, <span class="st">"#3CB371"</span>))</span></code></pre></div>
<p><img src="anova_files/figure-html/ricci%20two%20way%20anova%20interaction%20plot-1.png" width="672" /></p>
<div class="sourceCode" id="cb83"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb83-1"><a href="#cb83-1" aria-hidden="true" tabindex="-1"></a><span class="fu">interaction.plot</span>(<span class="at">x.factor =</span> Ricci<span class="sc">$</span>Position,</span>
<span id="cb83-2"><a href="#cb83-2" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.factor =</span> Ricci<span class="sc">$</span>Race, </span>
<span id="cb83-3"><a href="#cb83-3" aria-hidden="true" tabindex="-1"></a> <span class="at">response =</span> Ricci<span class="sc">$</span>Combine,</span>
<span id="cb83-4"><a href="#cb83-4" aria-hidden="true" tabindex="-1"></a> <span class="at">fun =</span> mean, </span>
<span id="cb83-5"><a href="#cb83-5" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"b"</span>, <span class="at">legend =</span> <span class="cn">TRUE</span>, </span>
<span id="cb83-6"><a href="#cb83-6" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"Position"</span>, <span class="at">ylab=</span><span class="st">"Combine Score"</span>,</span>
<span id="cb83-7"><a href="#cb83-7" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.label =</span> <span class="st">"Race"</span>,</span>
<span id="cb83-8"><a href="#cb83-8" aria-hidden="true" tabindex="-1"></a> <span class="at">pch=</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">8</span>, <span class="dv">3</span>), <span class="at">col =</span> <span class="fu">c</span>(<span class="st">"#FF6347"</span>, <span class="st">"#3CB371"</span>,<span class="st">"#2980B9"</span> ))</span></code></pre></div>
<p><img src="anova_files/figure-html/ricci%20two%20way%20anova%20interaction%20plot2-1.png" width="672" /></p>
</div>
</div>
<div id="one-factor-at-a-time" class="section level1">
<h1>One-factor-at-a-time</h1>
<p>Example:</p>
<div class="sourceCode" id="cb84"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb84-1"><a href="#cb84-1" aria-hidden="true" tabindex="-1"></a>res_s <span class="ot">=</span> <span class="fu">c</span>(<span class="dv">13</span>, <span class="dv">24</span>, <span class="dv">18</span>, <span class="dv">16</span> ,<span class="dv">14</span>, <span class="dv">27</span>, <span class="dv">30</span>, <span class="dv">14</span>, <span class="dv">14</span>)</span>
<span id="cb84-2"><a href="#cb84-2" aria-hidden="true" tabindex="-1"></a>df <span class="ot">=</span> <span class="fu">cbind</span>(<span class="fu">expand.grid</span>(<span class="at">Treatment_1 =</span> <span class="fu">c</span>(<span class="st">"none"</span>, <span class="st">"low"</span>, <span class="st">"high"</span>),</span>
<span id="cb84-3"><a href="#cb84-3" aria-hidden="true" tabindex="-1"></a> <span class="at">Treatment_2 =</span> <span class="fu">c</span>(<span class="st">"none"</span>, <span class="st">"low"</span>, <span class="st">"high"</span>)), <span class="at">y_exp =</span>res_s)</span>
<span id="cb84-4"><a href="#cb84-4" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> df, <span class="fu">aes</span>(<span class="at">x=</span>Treatment_1, <span class="at">y =</span> Treatment_2, <span class="at">size =</span> y_exp, <span class="at">color =</span> <span class="fu">factor</span>(y_exp))) <span class="sc">+</span></span>
<span id="cb84-5"><a href="#cb84-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span> <span class="fu">scale_size</span>(<span class="at">guide =</span> <span class="st">"none"</span>) <span class="sc">+</span> <span class="fu">guides</span>(<span class="at">color=</span><span class="fu">guide_legend</span>(<span class="at">title=</span><span class="st">"E[Y]"</span>)) <span class="sc">+</span></span>
<span id="cb84-6"><a href="#cb84-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span> <span class="st">"Treatment 1"</span>, <span class="at">y=</span> <span class="st">"Treatment 2"</span>) <span class="sc">+</span> <span class="fu">theme_bw</span>() </span></code></pre></div>
<p><img src="anova_files/figure-html/One-factor-at-a-time-1.png" width="672" /></p>
</div>
<div id="mixed-effect-model" class="section level1">
<h1>Mixed Effect Model</h1>
<div id="positive-negative-feedback--" class="section level2">
<h2>Positive Negative Feedback -</h2>
<p><strong>The effects of positive and negative verbal feedback on repeated force production</strong></p>
<div id="arranging-the-data-in-a-long-format" class="section level4">
<h4>Arranging the data in a long format</h4>
<div class="sourceCode" id="cb85"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb85-1"><a href="#cb85-1" aria-hidden="true" tabindex="-1"></a>mvc <span class="ot">=</span> <span class="fu">read.csv</span>(<span class="st">"datasets/pos_neg_feedback.csv"</span>)</span>
<span id="cb85-2"><a href="#cb85-2" aria-hidden="true" tabindex="-1"></a>emg_bi <span class="ot">=</span> <span class="fu">read.csv</span>(<span class="st">"datasets/BICEPS.csv"</span>)</span>
<span id="cb85-3"><a href="#cb85-3" aria-hidden="true" tabindex="-1"></a>emg_tr <span class="ot">=</span> <span class="fu">read.csv</span>(<span class="st">"datasets/TRICEPS.csv"</span>)</span>
<span id="cb85-4"><a href="#cb85-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb85-5"><a href="#cb85-5" aria-hidden="true" tabindex="-1"></a>emg_bi <span class="ot">=</span> emg_bi <span class="sc">%>%</span> <span class="fu">rename_all</span>(janitor<span class="sc">::</span>make_clean_names) <span class="sc">%>%</span></span>
<span id="cb85-6"><a href="#cb85-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">rename_all</span>(<span class="sc">~</span><span class="fu">str_remove</span>(., <span class="st">"_"</span>)) <span class="sc">%>%</span> <span class="fu">rename</span>( <span class="st">"gender"</span> <span class="ot">=</span> <span class="st">"a"</span>)</span>
<span id="cb85-7"><a href="#cb85-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb85-8"><a href="#cb85-8" aria-hidden="true" tabindex="-1"></a>emg_tr <span class="ot">=</span> emg_tr <span class="sc">%>%</span> <span class="fu">rename_all</span>(janitor<span class="sc">::</span>make_clean_names) <span class="sc">%>%</span></span>
<span id="cb85-9"><a href="#cb85-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">rename_all</span>(<span class="sc">~</span><span class="fu">str_remove</span>(., <span class="st">"_"</span>)) <span class="sc">%>%</span> <span class="fu">rename</span>( <span class="st">"gender"</span> <span class="ot">=</span> <span class="st">"a"</span>)</span></code></pre></div>
<div class="sourceCode" id="cb86"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb86-1"><a href="#cb86-1" aria-hidden="true" tabindex="-1"></a>to_long <span class="ot">=</span> <span class="cf">function</span>(data, activity) {</span>
<span id="cb86-2"><a href="#cb86-2" aria-hidden="true" tabindex="-1"></a> neg_long <span class="ot">=</span> data <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="st">"id"</span><span class="ot">=</span> <span class="fu">row_number</span>()) <span class="sc">%>%</span></span>
<span id="cb86-3"><a href="#cb86-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>( neg1<span class="sc">:</span>neg12, id, gender) <span class="sc">%>%</span> </span>
<span id="cb86-4"><a href="#cb86-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">cols =</span> neg1<span class="sc">:</span>neg12, <span class="at">values_to =</span> <span class="st">"performance"</span>, <span class="at">names_to =</span> <span class="st">"time"</span>) <span class="sc">%>%</span></span>
<span id="cb86-5"><a href="#cb86-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate_at</span>(<span class="st">"time"</span>, <span class="sc">~</span>(<span class="fu">str_extract</span>(., <span class="at">pattern =</span> <span class="st">"[0-9]+"</span>))) <span class="sc">%>%</span></span>
<span id="cb86-6"><a href="#cb86-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">feedback =</span> <span class="st">"negative"</span>, <span class="at">activity =</span> activity)</span>
<span id="cb86-7"><a href="#cb86-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb86-8"><a href="#cb86-8" aria-hidden="true" tabindex="-1"></a> pos_long <span class="ot">=</span> data <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="st">"id"</span><span class="ot">=</span> <span class="fu">row_number</span>()) <span class="sc">%>%</span></span>
<span id="cb86-9"><a href="#cb86-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>( pos1<span class="sc">:</span>pos12, id, gender) <span class="sc">%>%</span> </span>
<span id="cb86-10"><a href="#cb86-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">cols =</span> pos1<span class="sc">:</span>pos12, <span class="at">values_to =</span> <span class="st">"performance"</span>, <span class="at">names_to =</span> <span class="st">"time"</span>) <span class="sc">%>%</span></span>
<span id="cb86-11"><a href="#cb86-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate_at</span>(<span class="st">"time"</span>, <span class="sc">~</span>(<span class="fu">str_extract</span>(., <span class="at">pattern =</span> <span class="st">"[0-9]+"</span>))) <span class="sc">%>%</span></span>
<span id="cb86-12"><a href="#cb86-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">feedback =</span> <span class="st">"positive"</span>, <span class="at">activity =</span> activity)</span>
<span id="cb86-13"><a href="#cb86-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb86-14"><a href="#cb86-14" aria-hidden="true" tabindex="-1"></a> con_long <span class="ot">=</span> data <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="st">"id"</span><span class="ot">=</span> <span class="fu">row_number</span>()) <span class="sc">%>%</span></span>
<span id="cb86-15"><a href="#cb86-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>( con1<span class="sc">:</span>con12, id, gender) <span class="sc">%>%</span> </span>
<span id="cb86-16"><a href="#cb86-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">cols =</span> con1<span class="sc">:</span>con12, <span class="at">values_to =</span> <span class="st">"performance"</span>, <span class="at">names_to =</span> <span class="st">"time"</span>) <span class="sc">%>%</span></span>
<span id="cb86-17"><a href="#cb86-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate_at</span>(<span class="st">"time"</span>, <span class="sc">~</span>(<span class="fu">str_extract</span>(., <span class="at">pattern =</span> <span class="st">"[0-9]+"</span>))) <span class="sc">%>%</span></span>
<span id="cb86-18"><a href="#cb86-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">feedback =</span> <span class="st">"no feedback"</span>, <span class="at">activity =</span> activity)</span>
<span id="cb86-19"><a href="#cb86-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb86-20"><a href="#cb86-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>( neg_long <span class="sc">%>%</span> <span class="fu">bind_rows</span>(pos_long) <span class="sc">%>%</span> <span class="fu">bind_rows</span>(con_long) )</span>
<span id="cb86-21"><a href="#cb86-21" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb86-22"><a href="#cb86-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb86-23"><a href="#cb86-23" aria-hidden="true" tabindex="-1"></a>mvc_long <span class="ot">=</span> <span class="fu">to_long</span>(mvc, <span class="st">"mvc"</span>)</span>
<span id="cb86-24"><a href="#cb86-24" aria-hidden="true" tabindex="-1"></a>emg_bi_long <span class="ot">=</span> <span class="fu">to_long</span>(emg_bi, <span class="st">"emg_bi"</span>)</span>
<span id="cb86-25"><a href="#cb86-25" aria-hidden="true" tabindex="-1"></a>emg_tr_long <span class="ot">=</span> <span class="fu">to_long</span>(emg_tr, <span class="st">"emg_tr"</span>)</span>
<span id="cb86-26"><a href="#cb86-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb86-27"><a href="#cb86-27" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb86-28"><a href="#cb86-28" aria-hidden="true" tabindex="-1"></a>feedback_long_df <span class="ot">=</span> mvc_long <span class="sc">%>%</span> <span class="fu">bind_rows</span>(emg_bi_long) <span class="sc">%>%</span> <span class="fu">bind_rows</span>(emg_tr_long)</span>
<span id="cb86-29"><a href="#cb86-29" aria-hidden="true" tabindex="-1"></a><span class="co"># write_csv(feedback_long_df, "datasets/feedback_long_df.csv")</span></span></code></pre></div>
<p>let’s filter only mvc performance:</p>
<div class="sourceCode" id="cb87"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb87-1"><a href="#cb87-1" aria-hidden="true" tabindex="-1"></a>feedback_long_df <span class="ot">=</span> <span class="fu">read.csv</span>(<span class="st">"datasets/feedback_long_df.csv"</span>)</span>
<span id="cb87-2"><a href="#cb87-2" aria-hidden="true" tabindex="-1"></a>feedback_long_df_mvc <span class="ot">=</span> feedback_long_df <span class="sc">%>%</span> <span class="fu">filter</span>(activity <span class="sc">==</span> <span class="st">"mvc"</span>)</span>
<span id="cb87-3"><a href="#cb87-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb87-4"><a href="#cb87-4" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(feedback_long_df_mvc)</span></code></pre></div>
<pre><code>## 'data.frame': 792 obs. of 6 variables:
## $ id : int 1 1 1 1 1 1 1 1 1 1 ...
## $ gender : int 1 1 1 1 1 1 1 1 1 1 ...
## $ time : int 1 2 3 4 5 6 7 8 9 10 ...
## $ performance: num 92.4 93.2 93.6 90.5 87.1 ...
## $ feedback : Factor w/ 3 levels "negative","no feedback",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ activity : Factor w/ 3 levels "emg_bi","emg_tr",..: 3 3 3 3 3 3 3 3 3 3 ...</code></pre>
<div class="sourceCode" id="cb89"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb89-1"><a href="#cb89-1" aria-hidden="true" tabindex="-1"></a>feedback_long_df_mvc <span class="ot">=</span> feedback_long_df_mvc <span class="sc">%>%</span> <span class="fu">mutate_at</span>(<span class="fu">vars</span>(id, gender, feedback), factor) <span class="sc">%>%</span> <span class="fu">mutate_at</span>(<span class="st">"time"</span>, as.integer)</span></code></pre></div>
<p>let’s take a look:</p>
<div class="sourceCode" id="cb90"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb90-1"><a href="#cb90-1" aria-hidden="true" tabindex="-1"></a>feedback_long_df_mvc <span class="sc">%>%</span> <span class="fu">group_by</span>(id, feedback) <span class="sc">%>%</span></span>
<span id="cb90-2"><a href="#cb90-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">mean_per =</span> <span class="fu">mean</span>(performance)) <span class="sc">%>%</span></span>
<span id="cb90-3"><a href="#cb90-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>feedback , <span class="at">y=</span>mean_per, <span class="at">color =</span> feedback)) <span class="sc">+</span></span>
<span id="cb90-4"><a href="#cb90-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>() <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">""</span>) <span class="sc">+</span></span>
<span id="cb90-5"><a href="#cb90-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Boxplots of Average Relative Performance by Feedback"</span> , <span class="at">y =</span> <span class="st">"Average Relative Performance"</span>)</span></code></pre></div>
<pre><code>## `summarise()` has grouped output by 'id'. You can override using the `.groups` argument.</code></pre>
<p><img src="anova_files/figure-html/unnamed-chunk-30-1.png" width="672" /></p>
<div class="sourceCode" id="cb92"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb92-1"><a href="#cb92-1" aria-hidden="true" tabindex="-1"></a>feedback_long_df_mvc <span class="sc">%>%</span> </span>