-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathall.html
executable file
·976 lines (884 loc) · 58.8 KB
/
all.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
<!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="Leo Lahti, Sudarshan Shetty et al." />
<meta name="date" content="2022-02-04" />
<title>Differential abundance testing</title>
<script src="site_libs/header-attrs-2.11/header-attrs.js"></script>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
</style>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; } /* Alert */
code span.an { color: #008000; } /* Annotation */
code span.at { } /* Attribute */
code span.bu { } /* BuiltIn */
code span.cf { color: #0000ff; } /* ControlFlow */
code span.ch { color: #008080; } /* Char */
code span.cn { } /* Constant */
code span.co { color: #008000; } /* Comment */
code span.cv { color: #008000; } /* CommentVar */
code span.do { color: #008000; } /* Documentation */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.im { } /* Import */
code span.in { color: #008000; } /* Information */
code span.kw { color: #0000ff; } /* Keyword */
code span.op { } /* Operator */
code span.ot { color: #ff4000; } /* Other */
code span.pp { color: #ff4000; } /* Preprocessor */
code span.sc { color: #008080; } /* SpecialChar */
code span.ss { color: #008080; } /* SpecialString */
code span.st { color: #008080; } /* String */
code span.va { } /* Variable */
code span.vs { color: #008080; } /* VerbatimString */
code span.wa { color: #008000; font-weight: bold; } /* 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>
<link rel="stylesheet" href="C:/Users/shettys/Documents/R/win-library/4.1/BiocStyle/resources/html/bioconductor.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 828px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.tab('show');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 246px;
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>
<script>
function toggle_visibility(id1) {
var e = document.getElementById(id1);
e.style.display = ((e.style.display!="none") ? "none" : "block");
}
</script>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<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="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">My Website</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Differential abundance testing</h1>
<p class="author-name">Leo Lahti, Sudarshan Shetty et al.</p>
<h4 class="date">4 February 2022</h4>
</div>
<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{microbiome tutorial - atlas}
%\usepackage[utf8]{inputenc}
-->
<div id="differential-abundance-testing-univariate-data" class="section level1">
<h1>Differential abundance testing: univariate data</h1>
<p>This section covers basic univariate tests for two-group comparison,
covering t-test, Wilcoxon test, and multiple testing.</p>
<p>The following example compares the abundance of a selected bug between
two conditions. Let us assume that the data is already properly
normalized.</p>
<p>Let us load example data</p>
<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>(microbiome)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(dietswap)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>d <span class="ot"><-</span> dietswap</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Pick microbial abundances for a given taxonomic group</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>taxa <span class="ot"><-</span> <span class="st">"Dialister"</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Construct a data.frame with the selected</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="co"># taxonomic group and grouping</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">Abundance =</span> <span class="fu">abundances</span>(d)[taxa,],</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> <span class="at">Group =</span> <span class="fu">meta</span>(d)<span class="sc">$</span>nationality)</span></code></pre></div>
<p>Compare the groups visually using a boxplot (left). However, we
observe that the abundances are in absolute scale and therefore the
comparison is not clear. Let us try the log10 transformation. Now,
the data contains many zeros and taking log10 will yield infinite
values. Hence we choose the commonly used, although somewhat
problematic, log10(1+x) transformation (right).</p>
<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>p1 <span class="ot"><-</span> <span class="fu">ggplot</span>(df, <span class="fu">aes</span>(<span class="at">x =</span> Group, <span class="at">y =</span> Abundance)) <span class="sc">+</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>() <span class="sc">+</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Absolute abundances"</span>, <span class="at">y =</span> <span class="st">"Abundance</span><span class="sc">\n</span><span class="st"> (read count)"</span>)<span class="sc">+</span> <span class="fu">theme</span>(<span class="at">plot.title =</span> <span class="fu">element_text</span>(<span class="at">size=</span><span class="dv">18</span>))</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Let us add the log10(1+x) version:</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>Log10_Abundance <span class="ot"><-</span> <span class="fu">log10</span>(<span class="dv">1</span> <span class="sc">+</span> df<span class="sc">$</span>Abundance)</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot"><-</span> <span class="fu">ggplot</span>(df, <span class="fu">aes</span>(<span class="at">x =</span> Group, <span class="at">y =</span> Log10_Abundance)) <span class="sc">+</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>() <span class="sc">+</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Log10 abundances"</span>, <span class="at">y =</span> <span class="st">"Abundance</span><span class="sc">\n</span><span class="st"> (log10(1+x) read count)"</span>)<span class="sc">+</span> <span class="fu">theme</span>(<span class="at">plot.title =</span> <span class="fu">element_text</span>(<span class="at">size=</span><span class="dv">18</span>)) </span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(patchwork)</span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(p1 <span class="sc">+</span> p2)</span></code></pre></div>
<p><img src="figure/univariate_boxplot-1.png" width="100%" /></p>
<p>The groups seem to differ. Let us test the difference statistically.
First, let us perform t-test, which is based on Gaussian assumptions.
Each group is expected to follow Gaussian distribution.</p>
<p>Significance p-value with t-test:</p>
<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">print</span>(<span class="fu">t.test</span>(Log10_Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df)<span class="sc">$</span>p.value)</span></code></pre></div>
<pre><code>## [1] 0.02554997</code></pre>
<p>Now let us investigate the Gaussian assumption in more
detail. Boxplots may not show deviations from Gaussian assumptions
very clearly Let us try another visualization; the density plot.</p>
<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>p <span class="ot"><-</span> <span class="fu">ggplot</span>(df, <span class="fu">aes</span>(<span class="at">fill =</span> Group, <span class="at">x =</span> Log10_Abundance)) <span class="sc">+</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_density</span>(<span class="at">alpha =</span> <span class="fl">0.5</span>)</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(p)</span></code></pre></div>
<p><img src="figure/univariate_densityplot-1.png" width="100%" /></p>
<p>Apparently, the data is not Gaussian distributed. In such cases, a common
procedure is to use non-parametric tests. These do not make
assumptions of the data distribution but instead compare the ordering
of the samples.</p>
<p>So, let us look at the significance p-value with Wilcoxon test (log10 data):</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><span class="fu">print</span>(<span class="fu">wilcox.test</span>(Log10_Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df)<span class="sc">$</span>p.value)</span></code></pre></div>
<pre><code>## [1] 0.02979053</code></pre>
<p>But since the test is non-parametric, we can as well use the original absolute abundances since the
log transformation does not change sample ordering on which the Wilcoxon test is based.</p>
<p>Let us verify that the absolute abundances yield the same p-value for Wilcoxon test:</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><span class="fu">print</span>(<span class="fu">wilcox.test</span>(Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df)<span class="sc">$</span>p.value)</span></code></pre></div>
<pre><code>## [1] 0.02979053</code></pre>
<p>Let us compare how much the results would differ in the whole data
between t-test and Wilcoxon test. To remove non-varying taxa that
would demand extra scripting, let us for demonstration purposes now
focus on core taxa that are observed in more than 20% of the samples
with more than 3 reads.</p>
<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="co"># Core taxa to be tested</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>test.taxa <span class="ot"><-</span> <span class="fu">core_members</span>(d, <span class="at">prevalence =</span> <span class="dv">20</span><span class="sc">/</span><span class="dv">100</span>, <span class="at">detection =</span> <span class="dv">3</span>)</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate p-values with the two different methods for each taxonomic unit</span></span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a>pvalue.ttest <span class="ot"><-</span> <span class="fu">c</span>()</span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a>pvalue.wilcoxon <span class="ot"><-</span> <span class="fu">c</span>()</span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (taxa <span class="cf">in</span> test.taxa) {</span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># Create a new data frame for each taxonomic group</span></span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a> df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">Abundance =</span> <span class="fu">abundances</span>(d)[taxa,],</span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a> <span class="at">Log10_Abundance =</span> <span class="fu">log10</span>(<span class="dv">1</span> <span class="sc">+</span> <span class="fu">abundances</span>(d)[taxa,]), </span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> <span class="at">Group =</span> <span class="fu">meta</span>(d)<span class="sc">$</span>nationality)</span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a> pvalue.ttest[[taxa]] <span class="ot"><-</span> <span class="fu">t.test</span>(Log10_Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df)<span class="sc">$</span>p.value</span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a> pvalue.wilcoxon[[taxa]] <span class="ot"><-</span> <span class="fu">wilcox.test</span>(Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df)<span class="sc">$</span>p.value </span>
<span id="cb10-15"><a href="#cb10-15" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb10-16"><a href="#cb10-16" aria-hidden="true" tabindex="-1"></a><span class="co"># Arrange the results in a data.frame</span></span>
<span id="cb10-17"><a href="#cb10-17" aria-hidden="true" tabindex="-1"></a>pvalues <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">taxon =</span> test.taxa,</span>
<span id="cb10-18"><a href="#cb10-18" aria-hidden="true" tabindex="-1"></a> <span class="at">pvalue.ttest =</span> pvalue.ttest,</span>
<span id="cb10-19"><a href="#cb10-19" aria-hidden="true" tabindex="-1"></a> <span class="at">pvalue.wilcoxon =</span> pvalue.wilcoxon)</span>
<span id="cb10-20"><a href="#cb10-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-21"><a href="#cb10-21" aria-hidden="true" tabindex="-1"></a><span class="co"># Note that multiple testing occurs.</span></span>
<span id="cb10-22"><a href="#cb10-22" aria-hidden="true" tabindex="-1"></a><span class="co"># We must correct the p-values.</span></span>
<span id="cb10-23"><a href="#cb10-23" aria-hidden="true" tabindex="-1"></a><span class="co"># let us apply the standard Benjamini-Hochberg False Discovery Rate (FDR)</span></span>
<span id="cb10-24"><a href="#cb10-24" aria-hidden="true" tabindex="-1"></a><span class="co"># correction</span></span>
<span id="cb10-25"><a href="#cb10-25" aria-hidden="true" tabindex="-1"></a>pvalues<span class="sc">$</span>pvalue.ttest.adjusted <span class="ot"><-</span> <span class="fu">p.adjust</span>(pvalue.ttest)</span>
<span id="cb10-26"><a href="#cb10-26" aria-hidden="true" tabindex="-1"></a><span class="co">#pvalues$pvalue.ttest.adjusted <- p.adjust(pvalues$pvalue.ttest)</span></span>
<span id="cb10-27"><a href="#cb10-27" aria-hidden="true" tabindex="-1"></a>pvalues<span class="sc">$</span>pvalue.wilcoxon.adjusted <span class="ot"><-</span> <span class="fu">p.adjust</span>(pvalue.wilcoxon)</span></code></pre></div>
<p>Compare the p-value histograms between raw and adjusteed p-values.</p>
<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><span class="fu">library</span>(reshape2)</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>pvalues<span class="sc">$</span>pvalue.wilcoxon<span class="ot"><-</span> <span class="fu">as.numeric</span>(pvalue.wilcoxon)</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot"><-</span> <span class="fu">ggplot</span>(pvalues, <span class="fu">aes</span>(<span class="at">x =</span> pvalue.wilcoxon)) <span class="sc">+</span> <span class="fu">geom_histogram</span>(<span class="at">bins =</span> <span class="dv">50</span>, <span class="at">binwidth =</span> .<span class="dv">03</span>)<span class="sc">+</span></span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Raw p-values"</span>) <span class="sc">+</span></span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylim</span>(<span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">80</span>))</span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot"><-</span> <span class="fu">ggplot</span>(pvalues, <span class="fu">aes</span>(<span class="at">x =</span> pvalue.wilcoxon.adjusted)) <span class="sc">+</span></span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_histogram</span>(<span class="at">bins =</span> <span class="dv">50</span>, <span class="at">binwidth =</span> .<span class="dv">03</span>) <span class="sc">+</span></span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Adjusted p-values"</span>) <span class="sc">+</span></span>
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylim</span>(<span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">80</span>)) </span>
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(p1 <span class="sc">+</span> p2)</span></code></pre></div>
<p><img src="figure/univariate5-1.png" width="100%" /></p>
<p>Now compare these adjusted p-values between t-test and Wilcoxon test. Let us
also highlight the p = 0.05 intervals.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>p <span class="ot"><-</span> <span class="fu">ggplot</span>(<span class="at">data =</span> pvalues,</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> pvalue.ttest.adjusted,</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> pvalue.wilcoxon.adjusted)) <span class="sc">+</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(<span class="fu">aes</span>(<span class="at">label =</span> taxon)) <span class="sc">+</span> </span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_abline</span>(<span class="fu">aes</span>(<span class="at">intercept =</span> <span class="dv">0</span>, <span class="at">slope =</span> <span class="dv">1</span>)) <span class="sc">+</span></span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_hline</span>(<span class="fu">aes</span>(<span class="at">yintercept =</span> <span class="fl">0.05</span>), <span class="at">shape =</span> <span class="dv">2</span>) <span class="sc">+</span></span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_vline</span>(<span class="fu">aes</span>(<span class="at">xintercept =</span> <span class="fl">0.05</span>), <span class="at">shape =</span> <span class="dv">2</span>)</span>
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(p)</span></code></pre></div>
<p><img src="figure/univariate6-1.png" width="100%" /></p>
</div>
<div id="linear-models-the-role-of-covariates" class="section level1">
<h1>Linear models: the role of covariates</h1>
<p>This section provides a brief hands-on introduction to the practical motivations
and use linear (and generalized linear) models.</p>
<p>Let us compare two groups with a linear model. We use Log10 abundances
since this is closer to the Gaussian assumptions than the absolute
count data. We can fit a linear model with Gaussian variation as follows:</p>
<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>res <span class="ot"><-</span> <span class="fu">glm</span>(Log10_Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df, <span class="at">family =</span> <span class="st">"gaussian"</span>)</span></code></pre></div>
<p>Let us investigate model coefficients</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">kable</span>(<span class="fu">summary</span>(res)<span class="sc">$</span>coefficients, <span class="at">digits =</span> <span class="dv">5</span>)</span></code></pre></div>
<div class='horizontal-scroll'><table>
<thead>
<tr class="header">
<th align="left"></th>
<th align="right">Estimate</th>
<th align="right">Std. Error</th>
<th align="right">t value</th>
<th align="right">Pr(>|t|)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">(Intercept)</td>
<td align="right">0.64825</td>
<td align="right">0.02877</td>
<td align="right">22.53405</td>
<td align="right">0</td>
</tr>
<tr class="even">
<td align="left">GroupAFR</td>
<td align="right">0.20313</td>
<td align="right">0.04308</td>
<td align="right">4.71530</td>
<td align="right">0</td>
</tr>
</tbody>
</table></div>
<p>The intercept equals to the mean in the first group:</p>
<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="fu">print</span>(<span class="fu">mean</span>(<span class="fu">subset</span>(df, Group <span class="sc">==</span> <span class="st">"AAM"</span>)<span class="sc">$</span>Log10_Abundance))</span></code></pre></div>
<pre><code>## [1] 0.6482493</code></pre>
<p>The group term equals to the difference between group means:</p>
<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">print</span>(<span class="fu">mean</span>(<span class="fu">subset</span>(df, Group <span class="sc">==</span> <span class="st">"AFR"</span>)<span class="sc">$</span>Log10_Abundance) <span class="sc">-</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mean</span>(<span class="fu">subset</span>(df, Group <span class="sc">==</span> <span class="st">"AAM"</span>)<span class="sc">$</span>Log10_Abundance))</span></code></pre></div>
<pre><code>## [1] 0.2031287</code></pre>
<p>Note that the linear model (default) significance equals to t-test assuming equal variances.</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="fu">print</span>(<span class="fu">t.test</span>(Log10_Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df, <span class="at">var.equal=</span><span class="cn">TRUE</span>)<span class="sc">$</span>p.value)</span></code></pre></div>
<pre><code>## [1] 4.284318e-06</code></pre>
<p>An important advantage of linear models, compared to plain t-test is
that they allow incorporating additional variables, such as potential
confounders (age, BMI, gender..):</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Add a covariate:</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>sex <span class="ot"><-</span> <span class="fu">meta</span>(d)<span class="sc">$</span>sex</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Fit the model:</span></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a>res <span class="ot"><-</span> <span class="fu">glm</span>(Log10_Abundance <span class="sc">~</span> Group <span class="sc">+</span> sex, <span class="at">data =</span> df, <span class="at">family =</span> <span class="st">"gaussian"</span>)</span></code></pre></div>
<div id="generalized-linear-models-a-very-brief-overview" class="section level2">
<h2>Generalized linear models: a very brief overview</h2>
<p>Let us briefly discuss the ideas underlying generalized linear models
before proceeding to the next section.</p>
<p>The Generalized linear model (GLM) allows a richer family of
probability distributions to describe the data. Intuitively speaking,
GLMs allow the modeling of nonlinear, nonsymmetric, and nongaussian
associations. GLMs consist of three elements:
- A probability distribution (from exponential family)
- A linear predictor η = Xβ .
- A link function g such that <span class="math inline">\(E(Y) = μ = g^{−1}(η)\)</span>.</p>
<p>We use Poisson with (its natural) log-link. Fit abundance (read
counts) assuming that the data is Poisson distributed, and the
logarithm of its mean, or expectation, is obtained with a linear
model.</p>
<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>res <span class="ot"><-</span> <span class="fu">glm</span>(Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df, <span class="at">family =</span> <span class="st">"poisson"</span>)</span></code></pre></div>
<p>Investigate the model output:</p>
<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><span class="fu">kable</span>(<span class="fu">summary</span>(res)<span class="sc">$</span>coefficients, <span class="at">digits =</span> <span class="dv">5</span>)</span></code></pre></div>
<div class='horizontal-scroll'><table>
<thead>
<tr class="header">
<th align="left"></th>
<th align="right">Estimate</th>
<th align="right">Std. Error</th>
<th align="right">z value</th>
<th align="right">Pr(>|z|)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">(Intercept)</td>
<td align="right">1.49409</td>
<td align="right">0.04272</td>
<td align="right">34.97577</td>
<td align="right">0</td>
</tr>
<tr class="even">
<td align="left">GroupAFR</td>
<td align="right">1.04329</td>
<td align="right">0.05122</td>
<td align="right">20.36855</td>
<td align="right">0</td>
</tr>
</tbody>
</table></div>
</div>
</div>
<div id="advanced-models-of-differential-abundance" class="section level1">
<h1>Advanced models of differential abundance</h1>
<p>GLMs are the basis for advanced testing of differential abundance in
sequencing data. This is necessary, as the sequencing data sets
deviate from symmetric, continuous, Gaussian assumptions in many ways.</p>
<p>Sequencing data consists of discrete counts:</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(<span class="fu">abundances</span>(d)[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>])</span></code></pre></div>
<pre><code>## Sample-1 Sample-2 Sample-3
## Actinomycetaceae 0 1 0
## Aerococcus 0 0 0
## Aeromonas 0 0 0
## Akkermansia 18 97 67
## Alcaligenes faecalis et rel. 1 2 3</code></pre>
<p>The data is sparse:</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hist</span>(<span class="fu">log10</span>(<span class="dv">1</span> <span class="sc">+</span> <span class="fu">abundances</span>(d)), <span class="dv">100</span>)</span></code></pre></div>
<p><img src="figure/pooled3-1.png" width="100%" /></p>
<p>Long tails of rare taxa:</p>
<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>medians <span class="ot"><-</span> <span class="fu">apply</span>(<span class="fu">abundances</span>(d),<span class="dv">1</span>,median)<span class="sc">/</span><span class="fl">1e3</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(reshape2)</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a>A <span class="ot"><-</span> <span class="fu">melt</span>(<span class="fu">otu_tibble</span>(d))</span></code></pre></div>
<pre><code>## Using FeatureID as id variables</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>A<span class="sc">$</span>FeatureID <span class="ot"><-</span> <span class="fu">factor</span>(A<span class="sc">$</span>FeatureID, <span class="at">levels =</span> <span class="fu">rev</span>(<span class="fu">names</span>(<span class="fu">sort</span>(medians))))</span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a>p <span class="ot"><-</span> <span class="fu">ggplot</span>(A, <span class="fu">aes</span>(<span class="at">x =</span> FeatureID, <span class="at">y =</span> value)) <span class="sc">+</span></span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>() <span class="sc">+</span></span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">y =</span> <span class="st">"Abundance (reads)"</span>, <span class="at">x =</span> <span class="st">"Taxonomic Group"</span>) <span class="sc">+</span></span>
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_log10</span>() <span class="sc">+</span></span>
<span id="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb29-7"><a href="#cb29-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">size=</span><span class="dv">6</span>))</span>
<span id="cb29-8"><a href="#cb29-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb29-9"><a href="#cb29-9" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(p)</span></code></pre></div>
<p><img src="figure/tail-1.png" width="100%" /></p>
<p>Overdispersion (variance exceeds the mean):</p>
<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>means <span class="ot"><-</span> <span class="fu">apply</span>(<span class="fu">abundances</span>(d),<span class="dv">1</span>,mean)</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a>variances <span class="ot"><-</span> <span class="fu">apply</span>(<span class="fu">abundances</span>(d),<span class="dv">1</span>,var)</span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate mean and variance over samples for each taxon</span></span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(reshape2)</span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">melt</span>(<span class="fu">otu_tibble</span>(d))</span>
<span id="cb30-8"><a href="#cb30-8" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(df) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Taxon"</span>, <span class="st">"Sample"</span>, <span class="st">"Reads"</span>)</span>
<span id="cb30-9"><a href="#cb30-9" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> df <span class="sc">%>%</span> <span class="fu">group_by</span>(Taxon) <span class="sc">%>%</span></span>
<span id="cb30-10"><a href="#cb30-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">mean =</span> <span class="fu">mean</span>(Reads),</span>
<span id="cb30-11"><a href="#cb30-11" aria-hidden="true" tabindex="-1"></a> <span class="at">variance =</span> <span class="fu">var</span>(Reads))</span>
<span id="cb30-12"><a href="#cb30-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-13"><a href="#cb30-13" aria-hidden="true" tabindex="-1"></a><span class="co"># Illustrate overdispersion</span></span>
<span id="cb30-14"><a href="#cb30-14" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(scales)</span>
<span id="cb30-15"><a href="#cb30-15" aria-hidden="true" tabindex="-1"></a>p <span class="ot"><-</span> <span class="fu">ggplot</span>(df, <span class="fu">aes</span>(<span class="at">x =</span> mean, <span class="at">y =</span> variance)) <span class="sc">+</span></span>
<span id="cb30-16"><a href="#cb30-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb30-17"><a href="#cb30-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_abline</span>(<span class="fu">aes</span>(<span class="at">intercept =</span> <span class="dv">0</span>, <span class="at">slope =</span> <span class="dv">1</span>)) <span class="sc">+</span></span>
<span id="cb30-18"><a href="#cb30-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_log10</span>(<span class="at">labels =</span> scales<span class="sc">::</span>scientific) <span class="sc">+</span></span>
<span id="cb30-19"><a href="#cb30-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_log10</span>(<span class="at">labels =</span> scales<span class="sc">::</span>scientific) <span class="sc">+</span></span>
<span id="cb30-20"><a href="#cb30-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Overdispersion (variance > mean)"</span>)</span>
<span id="cb30-21"><a href="#cb30-21" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(p)</span></code></pre></div>
<p><img src="figure/pooled_overdispersion-1.png" width="100%" /></p>
<div id="deseq2-differential-abundance-testing-for-sequencing-data" class="section level2">
<h2>DESeq2: differential abundance testing for sequencing data</h2>
<p>DESeq2 analysis can accommodate those particular assumptions about
sequencing data.</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Start by converting phyloseq object to deseq2 format</span></span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(DESeq2)</span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a>ds2 <span class="ot"><-</span> <span class="fu">phyloseq_to_deseq2</span>(d, <span class="sc">~</span> group <span class="sc">+</span> nationality)</span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Run DESeq2 analysis (all taxa at once!)</span></span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a>dds <span class="ot"><-</span> <span class="fu">DESeq</span>(ds2)</span>
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-8"><a href="#cb31-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Investigate results</span></span>
<span id="cb31-9"><a href="#cb31-9" aria-hidden="true" tabindex="-1"></a>res <span class="ot"><-</span> <span class="fu">results</span>(dds)</span>
<span id="cb31-10"><a href="#cb31-10" aria-hidden="true" tabindex="-1"></a>deseq.results <span class="ot"><-</span> <span class="fu">as.data.frame</span>(res)</span>
<span id="cb31-11"><a href="#cb31-11" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> deseq.results</span>
<span id="cb31-12"><a href="#cb31-12" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>taxon <span class="ot"><-</span> <span class="fu">rownames</span>(df)</span>
<span id="cb31-13"><a href="#cb31-13" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> df <span class="sc">%>%</span> <span class="fu">arrange</span>(log2FoldChange, padj)</span>
<span id="cb31-14"><a href="#cb31-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-15"><a href="#cb31-15" aria-hidden="true" tabindex="-1"></a><span class="co"># Print the results; flitered and sorted by pvalue and effectsize</span></span>
<span id="cb31-16"><a href="#cb31-16" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(knitr)</span>
<span id="cb31-17"><a href="#cb31-17" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> df <span class="sc">%>%</span> <span class="fu">filter</span>(pvalue <span class="sc"><</span> <span class="fl">0.05</span> <span class="sc">&</span> log2FoldChange <span class="sc">></span> <span class="fl">1.5</span>) <span class="sc">%>%</span></span>
<span id="cb31-18"><a href="#cb31-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(pvalue, log2FoldChange)</span>
<span id="cb31-19"><a href="#cb31-19" aria-hidden="true" tabindex="-1"></a><span class="fu">kable</span>(df, <span class="at">digits =</span> <span class="dv">5</span>)</span></code></pre></div>
<div class='horizontal-scroll'><table>
<colgroup>
<col width="26%" />
<col width="7%" />
<col width="12%" />
<col width="6%" />
<col width="7%" />
<col width="6%" />
<col width="6%" />
<col width="26%" />
</colgroup>
<thead>
<tr class="header">
<th align="left"></th>
<th align="right">baseMean</th>
<th align="right">log2FoldChange</th>
<th align="right">lfcSE</th>
<th align="right">stat</th>
<th align="right">pvalue</th>
<th align="right">padj</th>
<th align="left">taxon</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Clostridium difficile et rel.</td>
<td align="right">29.20535</td>
<td align="right">1.91205</td>
<td align="right">0.13432</td>
<td align="right">14.23457</td>
<td align="right">0.00000</td>
<td align="right">0.00000</td>
<td align="left">Clostridium difficile et rel.</td>
</tr>
<tr class="even">
<td align="left">Mitsuokella multiacida et rel.</td>
<td align="right">51.65152</td>
<td align="right">3.04116</td>
<td align="right">0.28687</td>
<td align="right">10.60107</td>
<td align="right">0.00000</td>
<td align="right">0.00000</td>
<td align="left">Mitsuokella multiacida et rel.</td>
</tr>
<tr class="odd">
<td align="left">Klebisiella pneumoniae et rel.</td>
<td align="right">12.39749</td>
<td align="right">1.83825</td>
<td align="right">0.18531</td>
<td align="right">9.91994</td>
<td align="right">0.00000</td>
<td align="right">0.00000</td>
<td align="left">Klebisiella pneumoniae et rel.</td>
</tr>
<tr class="even">
<td align="left">Megasphaera elsdenii et rel.</td>
<td align="right">44.16494</td>
<td align="right">1.78333</td>
<td align="right">0.23072</td>
<td align="right">7.72937</td>
<td align="right">0.00000</td>
<td align="right">0.00000</td>
<td align="left">Megasphaera elsdenii et rel.</td>
</tr>
<tr class="odd">
<td align="left">Escherichia coli et rel.</td>
<td align="right">66.93783</td>
<td align="right">1.68345</td>
<td align="right">0.25330</td>
<td align="right">6.64609</td>
<td align="right">0.00000</td>
<td align="right">0.00000</td>
<td align="left">Escherichia coli et rel.</td>
</tr>
<tr class="even">
<td align="left">Weissella et rel.</td>
<td align="right">3.63459</td>
<td align="right">1.53142</td>
<td align="right">0.23140</td>
<td align="right">6.61792</td>
<td align="right">0.00000</td>
<td align="right">0.00000</td>
<td align="left">Weissella et rel.</td>
</tr>
<tr class="odd">
<td align="left">Serratia</td>
<td align="right">5.74035</td>
<td align="right">3.07334</td>
<td align="right">0.47848</td>
<td align="right">6.42308</td>
<td align="right">0.00000</td>
<td align="right">0.00000</td>
<td align="left">Serratia</td>
</tr>
<tr class="even">
<td align="left">Moraxellaceae</td>
<td align="right">0.42171</td>
<td align="right">1.70079</td>
<td align="right">0.47147</td>
<td align="right">3.60743</td>
<td align="right">0.00031</td>
<td align="right">0.00075</td>
<td align="left">Moraxellaceae</td>
</tr>
</tbody>
</table></div>
<p>For comparison purposes, assess significances and effect sizes based on Wilcoxon test.</p>
<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>test.taxa <span class="ot"><-</span> <span class="fu">taxa</span>(d)</span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a>pvalue.wilcoxon <span class="ot"><-</span> <span class="fu">c</span>()</span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a>foldchange <span class="ot"><-</span> <span class="fu">c</span>()</span>
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (taxa <span class="cf">in</span> test.taxa) {</span>
<span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># Create a new data frame for each taxonomic group</span></span>
<span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></a> df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">Abundance =</span> <span class="fu">abundances</span>(d)[taxa,],</span>
<span id="cb32-7"><a href="#cb32-7" aria-hidden="true" tabindex="-1"></a> <span class="at">Log10_Abundance =</span> <span class="fu">log10</span>(<span class="dv">1</span> <span class="sc">+</span> <span class="fu">abundances</span>(d)[taxa,]),</span>
<span id="cb32-8"><a href="#cb32-8" aria-hidden="true" tabindex="-1"></a> <span class="at">Group =</span> <span class="fu">meta</span>(d)<span class="sc">$</span>nationality)</span>
<span id="cb32-9"><a href="#cb32-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Calculate pvalue and effect size (difference beween log means) </span></span>
<span id="cb32-10"><a href="#cb32-10" aria-hidden="true" tabindex="-1"></a> pvalue.wilcoxon[[taxa]] <span class="ot"><-</span> <span class="fu">wilcox.test</span>(Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df)<span class="sc">$</span>p.value</span>
<span id="cb32-11"><a href="#cb32-11" aria-hidden="true" tabindex="-1"></a> foldchange[[taxa]] <span class="ot"><-</span> <span class="fu">coef</span>(<span class="fu">lm</span>(Log10_Abundance <span class="sc">~</span> Group, <span class="at">data =</span> df))[[<span class="dv">2</span>]]</span>
<span id="cb32-12"><a href="#cb32-12" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb32-13"><a href="#cb32-13" aria-hidden="true" tabindex="-1"></a><span class="co"># Correct p-values for multiple testing</span></span>
<span id="cb32-14"><a href="#cb32-14" aria-hidden="true" tabindex="-1"></a>pvalue.wilcoxon.adjusted <span class="ot"><-</span> <span class="fu">p.adjust</span>(pvalue.wilcoxon)</span></code></pre></div>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>))</span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(deseq.results<span class="sc">$</span>padj, pvalue.wilcoxon.adjusted,</span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"DESeq2 adjusted p-value"</span>,</span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a> <span class="at">ylab =</span> <span class="st">"Wilcoxon adjusted p-value"</span>,</span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a> <span class="at">main =</span> <span class="st">"P-value comparison"</span>)</span>
<span id="cb33-6"><a href="#cb33-6" aria-hidden="true" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">v =</span> <span class="fl">0.05</span>, <span class="at">h =</span> <span class="fl">0.05</span>, <span class="at">lty =</span> <span class="dv">2</span>)</span>
<span id="cb33-7"><a href="#cb33-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-8"><a href="#cb33-8" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(deseq.results<span class="sc">$</span>log2FoldChange, foldchange, </span>
<span id="cb33-9"><a href="#cb33-9" aria-hidden="true" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">"DESeq2"</span>,</span>
<span id="cb33-10"><a href="#cb33-10" aria-hidden="true" tabindex="-1"></a> <span class="at">ylab =</span> <span class="st">"Linear model"</span>,</span>
<span id="cb33-11"><a href="#cb33-11" aria-hidden="true" tabindex="-1"></a> <span class="at">main =</span> <span class="st">"Effect size comparison"</span>)</span>
<span id="cb33-12"><a href="#cb33-12" aria-hidden="true" tabindex="-1"></a><span class="fu">abline</span>(<span class="dv">0</span>,<span class="dv">1</span>)</span></code></pre></div>
<p><img src="figure/pooled_pcomp-1.png" width="100%" /></p>
</div>
<div id="visualizing-deseq2-results" class="section level2">
<h2>Visualizing DESEQ2 results</h2>
<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>deseq.results <span class="sc">%>%</span> </span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a> tibble<span class="sc">::</span><span class="fu">rownames_to_column</span>(<span class="st">"Taxon"</span>) <span class="sc">%>%</span> </span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(padj <span class="sc"><=</span> <span class="fl">0.05</span> <span class="sc">&</span> <span class="fu">abs</span>(log2FoldChange) <span class="sc">>=</span> <span class="fl">1.5</span>) <span class="sc">%>%</span> </span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(log2FoldChange, <span class="fu">reorder</span>(Taxon, log2FoldChange))) <span class="sc">+</span></span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>() <span class="sc">+</span></span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span><span class="st">"log2FoldChange"</span>, <span class="at">y=</span><span class="st">"Taxon"</span>)</span></code></pre></div>
<p><img src="figure/unnamed-chunk-2-1.png" width="100%" /></p>
</div>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.odd').parent('tbody').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 -->
<script>
$(document).ready(function () {
// temporarily add toc-ignore selector to headers for the consistency with Pandoc
$('.unlisted.unnumbered').addClass('toc-ignore')
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3,h4",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
"HTML-CSS": {
styles: {
".MathJax_Display": {
"text-align": "center",
padding: "0px 150px 0px 65px",
margin: "0px 0px 0.5em"
},
"@media screen and (max-width: 991px)": {
".MathJax_Display": {
"text-align": "center",
padding: "0 0 0 0"
}
}
}
}
});
</script>
<!-- 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>