-
Notifications
You must be signed in to change notification settings - Fork 33
/
PartIIIanalysis.tex
1602 lines (1370 loc) · 85.9 KB
/
PartIIIanalysis.tex
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
At this stage in the workflow, after converting raw reads to
interpretable species abundances, and after filtering and transforming
these abundances to focus attention on scientifically meaningful
quantities, we are in a position to consider more careful statistical
analysis. R is an ideal environment for performing these analyses, as
it has an active community of package developers building simple
interfaces to sophisticated techniques. As a variety of methods are
available, there is no need to commit to any rigid analysis strategy a
priori. Further, the ability to easily call packages without
reimplementing methods frees researchers to iterate rapidly through
alternative analysis ideas. The advantage of performing this full
workflow in R is that this transition from bioinformatics to
statistics is effortless.
We back these claims by illustrating several analyses on the mouse
data prepared above. We experiment with several flavors of exploratory
ordination before shifting to more formal testing and modeling,
explaining the settings in which the different points of view are most
appropriate. Finally, we provide example analyses of multitable data,
using a study in which both metabolomic and microbial abundance
measurements were collected on the same samples, to demonstrate that
the general workflow presented here can be adapted to the multitable
setting.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{.cran_packages} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"knitr"}\hlstd{,} \hlstr{"phyloseqGraphTest"}\hlstd{,} \hlstr{"phyloseq"}\hlstd{,} \hlstr{"shiny"}\hlstd{,}
\hlstr{"miniUI"}\hlstd{,} \hlstr{"caret"}\hlstd{,} \hlstr{"pls"}\hlstd{,} \hlstr{"e1071"}\hlstd{,} \hlstr{"ggplot2"}\hlstd{,} \hlstr{"randomForest"}\hlstd{,}
\hlstr{"vegan"}\hlstd{,} \hlstr{"plyr"}\hlstd{,} \hlstr{"dplyr"}\hlstd{,} \hlstr{"ggrepel"}\hlstd{,} \hlstr{"nlme"}\hlstd{,}
\hlstr{"reshape2"}\hlstd{,} \hlstr{"devtools"}\hlstd{,} \hlstr{"PMA"}\hlstd{,}\hlstr{"structSSI"}\hlstd{,}\hlstr{"ade4"}\hlstd{,}
\hlstr{"igraph"}\hlstd{,} \hlstr{"ggnetwork"}\hlstd{,} \hlstr{"intergraph"}\hlstd{,} \hlstr{"scales"}\hlstd{)}
\hlstd{.github_packages} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"jfukuyama/phyloseqGraphTest"}\hlstd{)}
\hlstd{.bioc_packages} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"phyloseq"}\hlstd{,} \hlstr{"genefilter"}\hlstd{,} \hlstr{"impute"}\hlstd{)}
\hlcom{# Install CRAN packages (if not already installed)}
\hlstd{.inst} \hlkwb{<-} \hlstd{.cran_packages} \hlopt{%in%} \hlkwd{installed.packages}\hlstd{()}
\hlkwa{if} \hlstd{(}\hlkwd{any}\hlstd{(}\hlopt{!}\hlstd{.inst)) \{}
\hlkwd{install.packages}\hlstd{(.cran_packages[}\hlopt{!}\hlstd{.inst],} \hlkwc{repos} \hlstd{=} \hlstr{"http://cran.rstudio.com/"}\hlstd{)}
\hlstd{\}}
\hlstd{.inst} \hlkwb{<-} \hlstd{.github_packages} \hlopt{%in%} \hlkwd{installed.packages}\hlstd{()}
\hlkwa{if} \hlstd{(}\hlkwd{any}\hlstd{(}\hlopt{!}\hlstd{.inst)) \{}
\hlstd{devtools}\hlopt{::}\hlkwd{install_github}\hlstd{(.github_packages[}\hlopt{!}\hlstd{.inst])}
\hlstd{\}}
\hlstd{.inst} \hlkwb{<-} \hlstd{.bioc_packages} \hlopt{%in%} \hlkwd{installed.packages}\hlstd{()}
\hlkwa{if} \hlstd{(}\hlkwd{any}\hlstd{(}\hlopt{!}\hlstd{.inst)) \{}
\hlkwd{source}\hlstd{(}\hlstr{"http://bioconductor.org/biocLite.R"}\hlstd{)}
\hlkwd{biocLite}\hlstd{(.bioc_packages[}\hlopt{!}\hlstd{.inst])}
\hlstd{\}}
\end{alltt}
\end{kframe}
\end{knitrout}
\subsubsection*{Preprocessing}
Before doing the multivariate projections, we will add a few columns
to our sample data, which can then be used to annotate plots. From
Figure \ref{fig:preprocessing-setup}, we see that the ages of the mice
come in a couple of groups, and so we make a categorical variable
corresponding to young, middle-aged, and old mice. We also record the
total number of counts seen in each sample and log-transform the data
as an approximate variance stabilizing transformation.
\begin{figure}[H]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{qplot}\hlstd{(}\hlkwd{sample_data}\hlstd{(ps)}\hlopt{$}\hlstd{age,} \hlkwc{geom} \hlstd{=} \hlstr{"histogram"}\hlstd{)} \hlopt{+} \hlkwd{xlab}\hlstd{(}\hlstr{"age"}\hlstd{)}
\hlkwd{qplot}\hlstd{(}\hlkwd{log10}\hlstd{(}\hlkwd{rowSums}\hlstd{(}\hlkwd{otu_table}\hlstd{(ps))))} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Logged counts-per-sample"}\hlstd{)}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/preprocessing-plot-1}
\includegraphics[width=\maxwidth]{analysisfigure/preprocessing-plot-2}
}
\end{knitrout}
\caption{Preliminary plots suggest certain preprocessing steps. The
histogram on the left motivates the creation of a new categorical
variable, binning age into one of the three peaks. The histogram on
the right suggests that a $\log\left(1 + x\right)$ transformation is
sufficient for normalizing the abundance data.}
\label{fig:preprocessing-setup}
\end{figure}
For a first pass, we look at principal coordinates analysis (PCoA) with
either the Bray-Curtis dissimilarity on the weighted Unifrac
distance. We see immediately that there are six outliers. These turn
out to be the samples from females 5 and 6 on day 165 and the samples
from males 3, 4, 5, and 6 on day 175. We will take them out, since we
are mainly interested in the relationships between the non-outlier
points.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{pslog} \hlkwb{<-} \hlkwd{transform_sample_counts}\hlstd{(ps,} \hlkwa{function}\hlstd{(}\hlkwc{x}\hlstd{)} \hlkwd{log}\hlstd{(}\hlnum{1} \hlopt{+} \hlstd{x))}
\hlkwd{sample_data}\hlstd{(pslog)}\hlopt{$}\hlstd{age_binned} \hlkwb{<-} \hlkwd{cut}\hlstd{(}\hlkwd{sample_data}\hlstd{(pslog)}\hlopt{$}\hlstd{age,}
\hlkwc{breaks} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{0}\hlstd{,} \hlnum{100}\hlstd{,} \hlnum{200}\hlstd{,} \hlnum{400}\hlstd{))}
\hlstd{out.wuf.log} \hlkwb{<-} \hlkwd{ordinate}\hlstd{(pslog,} \hlkwc{method} \hlstd{=} \hlstr{"MDS"}\hlstd{,} \hlkwc{distance} \hlstd{=} \hlstr{"wunifrac"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{evals} \hlkwb{<-} \hlstd{out.wuf.log}\hlopt{$}\hlstd{values}\hlopt{$}\hlstd{Eigenvalues}
\hlkwd{plot_ordination}\hlstd{(pslog, out.wuf.log,} \hlkwc{color} \hlstd{=} \hlstr{"age_binned"}\hlstd{)} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{col} \hlstd{=} \hlstr{"Binned Age"}\hlstd{)} \hlopt{+}
\hlkwd{coord_fixed}\hlstd{(}\hlkwd{sqrt}\hlstd{(evals[}\hlnum{2}\hlstd{]} \hlopt{/} \hlstd{evals[}\hlnum{1}\hlstd{]))}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/outlier-detect-plot-1}
}
\end{knitrout}
\label{fig:outlier-detect}
\caption{An ordination on the logged abundance data reveals a few
outliers.}
\end{figure}
Before we continue, we should check the two female
outliers -- they have been taken over by the same OTU/RSV, which has a
relative abundance of over 90\% in each of them. This is the only time
in the entire data set that this RSV has such a high relative
abundance -- the rest of the time it is below 20\%. In particular, its
diversity is by far the lowest of all the samples.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{rel_abund} \hlkwb{<-} \hlkwd{t}\hlstd{(}\hlkwd{apply}\hlstd{(}\hlkwd{otu_table}\hlstd{(ps),} \hlnum{1}\hlstd{,} \hlkwa{function}\hlstd{(}\hlkwc{x}\hlstd{) x} \hlopt{/} \hlkwd{sum}\hlstd{(x)))}
\hlkwd{qplot}\hlstd{(rel_abund[,} \hlnum{12}\hlstd{],} \hlkwc{geom} \hlstd{=} \hlstr{"histogram"}\hlstd{)} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Relative abundance"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\includegraphics[height=0.2\linewidth]{analysisfigure/outlier-analyze-1}
\caption{The outlier samples are dominated by a single RSV.}
\label{fig:outlier-analysize}
\end{figure}
\subsubsection*{Aspect ratio of ordination plots}
In the ordination plots in Figures 8-14, you may have noticed as did the reviewers of the first version of the paper, that
the maps are not presented as square representations as is often the case in
standard PCoA and PCA plots in the literature.
The reason for this is that as we are trying to represent the distances between samples as faithfully as possible;
we have to take into account that the second eigenvalue is always smaller
than the first, sometimes considerably so, thus we normalize the axis norm ratios to the relevant eigenvalue ratios.
\subsection*{Different Ordination Projections}
As we have seen, an important first step in analyzing microbiome data is to do
unsupervised, exploratory analysis. This is simple to do in
\BioCpkg{phyloseq}, which provides many distances and ordination
methods.
After documenting the outliers, we are going to compute ordinations
with these outliers removed and more carefully study the output. We
see that there is a fairly substantial age effect that is consistent
between all the mice, male and female, and from different
litters. We'll first perform a PCoA using Bray-Curtis dissimilarity.
The first plot shows the ordination of the samples, and we see that
the second axis corresponds to an age effect, with the samples from
the younger and older mice separating fairly well. The first axis
correlates fairly well with library size (this is not shown).
The first axis explains about twice the variability than the first,
this translates into the elongated form of the ordination plot.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{setup_example}\hlstd{(}\hlkwd{c}\hlstd{(}\hlstr{"phyloseq"}\hlstd{,} \hlstr{"ggplot2"}\hlstd{,} \hlstr{"plyr"}\hlstd{,} \hlstr{"dplyr"}\hlstd{,} \hlstr{"reshape2"}\hlstd{,}
\hlstr{"ade4"}\hlstd{,} \hlstr{"ggrepel"}\hlstd{))}
\hlstd{out.bc.log} \hlkwb{<-} \hlkwd{ordinate}\hlstd{(pslog,} \hlkwc{method} \hlstd{=} \hlstr{"MDS"}\hlstd{,} \hlkwc{distance} \hlstd{=} \hlstr{"bray"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{evals} \hlkwb{<-} \hlstd{out.bc.log}\hlopt{$}\hlstd{values}\hlopt{$}\hlstd{Eigenvalues}
\hlkwd{plot_ordination}\hlstd{(pslog, out.bc.log,} \hlkwc{color} \hlstd{=} \hlstr{"age_binned"}\hlstd{)} \hlopt{+}
\hlkwd{coord_fixed}\hlstd{(}\hlkwd{sqrt}\hlstd{(evals[}\hlnum{2}\hlstd{]} \hlopt{/} \hlstd{evals[}\hlnum{1}\hlstd{]))} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{col} \hlstd{=} \hlstr{"Binned Age"}\hlstd{)}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/ordinations-bray-plot-1}
}
\end{knitrout}
\caption{A PCoA plot using Bray-Curtis distance between samples.}
\label{fig:ordinations-bray}
\end{figure}
Next we look at double principal coordinates analysis (DPCoA)
\cite{Pavoine:2004,Purdom2010,Fukuyama:2012}, which is a phylogenetic ordination method and
that provides a biplot representation of both samples and taxonomic categories. We see
again that the second axis corresponds to young vs. old
mice, and the biplot suggests an interpretation of the second
axis: samples that have larger scores on the second axis have more
taxa from Bacteroidetes and one subset of Firmicutes.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{out.dpcoa.log} \hlkwb{<-} \hlkwd{ordinate}\hlstd{(pslog,} \hlkwc{method} \hlstd{=} \hlstr{"DPCoA"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{evals} \hlkwb{<-} \hlstd{out.dpcoa.log}\hlopt{$}\hlstd{eig}
\hlkwd{plot_ordination}\hlstd{(pslog, out.dpcoa.log,} \hlkwc{color} \hlstd{=} \hlstr{"age_binned"}\hlstd{,}
\hlkwc{shape} \hlstd{=} \hlstr{"family_relationship"}\hlstd{)} \hlopt{+}
\hlkwd{coord_fixed}\hlstd{(}\hlkwd{sqrt}\hlstd{(evals[}\hlnum{2}\hlstd{]} \hlopt{/} \hlstd{evals[}\hlnum{1}\hlstd{]))} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{col} \hlstd{=} \hlstr{"Binned Age"}\hlstd{,} \hlkwc{shape} \hlstd{=} \hlstr{"Litter"}\hlstd{)}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/ordinations-dpcoa-plot-1}
}
\end{knitrout}
\caption{A DPCoA plot incorporates phylogenetic information, but is
dominated by the first axis.}
\label{fig:ordinations-dpcoa}
\end{figure}
\begin{figure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot_ordination}\hlstd{(pslog, out.dpcoa.log,} \hlkwc{type} \hlstd{=} \hlstr{"species"}\hlstd{,} \hlkwc{color} \hlstd{=} \hlstr{"Phylum"}\hlstd{)} \hlopt{+}
\hlkwd{coord_fixed}\hlstd{(}\hlkwd{sqrt}\hlstd{(evals[}\hlnum{2}\hlstd{]} \hlopt{/} \hlstd{evals[}\hlnum{1}\hlstd{]))}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/ordinations-dpcoa-species-1}
}
\end{knitrout}
\caption{The DPCoA sample positions can be interpreted with respect to
the species coordinates in this display.}
\label{fig:ordinations-dpcoa-species}
\end{figure}
Finally, we can look at the results of PCoA with weighted Unifrac. As
before, we find that the second axis is associated with an age effect,
which is fairly similar to DPCoA. This is not surprising, because both
are phylogenetic ordination methods taking abundance into
account. However, when we compare biplots, we see that the DPCoA
gave a much cleaner interpretation of the second axis, compared to
weighted Unifrac.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{out.wuf.log} \hlkwb{<-} \hlkwd{ordinate}\hlstd{(pslog,} \hlkwc{method} \hlstd{=} \hlstr{"PCoA"}\hlstd{,} \hlkwc{distance} \hlstd{=}\hlstr{"wunifrac"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{evals} \hlkwb{<-} \hlstd{out.wuf.log}\hlopt{$}\hlstd{values}\hlopt{$}\hlstd{Eigenvalues}
\hlkwd{plot_ordination}\hlstd{(pslog, out.wuf.log,} \hlkwc{color} \hlstd{=} \hlstr{"age_binned"}\hlstd{,}
\hlkwc{shape} \hlstd{=} \hlstr{"family_relationship"}\hlstd{)} \hlopt{+}
\hlkwd{coord_fixed}\hlstd{(}\hlkwd{sqrt}\hlstd{(evals[}\hlnum{2}\hlstd{]} \hlopt{/} \hlstd{evals[}\hlnum{1}\hlstd{]))} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{col} \hlstd{=} \hlstr{"Binned Age"}\hlstd{,} \hlkwc{shape} \hlstd{=} \hlstr{"Litter"}\hlstd{)}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/ordinations-wuf-plot-1}
}
\end{knitrout}
\caption{The sample positions produced by a PCoA using weighted
Unifrac.}
\label{fig:ordinations-wuf}
\end{figure}
\begin{figure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot_ordination}\hlstd{(pslog, out.wuf.log,} \hlkwc{type} \hlstd{=} \hlstr{"species"}\hlstd{,} \hlkwc{color} \hlstd{=} \hlstr{"Phylum"}\hlstd{)} \hlopt{+}
\hlkwd{coord_fixed}\hlstd{(}\hlkwd{sqrt}\hlstd{(evals[}\hlnum{2}\hlstd{]} \hlopt{/} \hlstd{evals[}\hlnum{1}\hlstd{]))}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/ordinations-wuf-species-1}
}
\end{knitrout}
\caption{Species coordinates that can be used to interpret the sample
positions from PCoA with weighted Unifrac. Compared to the
representation in Figure \ref{fig:ordinations-dpcoa-species}, this
display is harder to interpret.}
\label{fig:ordinations-wuf-species}
\end{figure}
\subsubsection*{PCA on ranks}
Microbial abundance data is often heavy-tailed, and sometimes it can
be hard to identify a transformation that brings the data to
normality. In these cases, it can be safer to ignore the raw
abundances altogether, and work instead with ranks. We demonstrate
this idea using a rank-transformed version of the data to perform
PCA. First, we create a new matrix, representing the abundances by
their ranks, where the microbe with the smallest in a sample gets
mapped to rank 1, second smallest rank 2, etc.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{abund} \hlkwb{<-} \hlkwd{otu_table}\hlstd{(pslog)}
\hlstd{abund_ranks} \hlkwb{<-} \hlkwd{t}\hlstd{(}\hlkwd{apply}\hlstd{(abund,} \hlnum{1}\hlstd{, rank))}
\end{alltt}
\end{kframe}
\end{knitrout}
Naively using these ranks could make differences between pairs of low
and high abundance microbes comparable. In the case where many bacteria are absent or present at trace amounts, an artificially large difference in rank could occur\cite{holmes2011} for minimally abundant taxa.
To avoid this, all those
microbes with rank below some threshold are set to be tied at 1. The ranks for
the other microbes are shifted down, so there is no large gap between
ranks. This transformation is illustrated in Figure
\ref{fig:pca-rank-visualize-procedure}.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{abund_ranks} \hlkwb{<-} \hlstd{abund_ranks} \hlopt{-} \hlnum{329}
\hlstd{abund_ranks[abund_ranks} \hlopt{<} \hlnum{1}\hlstd{]} \hlkwb{<-} \hlnum{1}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{abund_df} \hlkwb{<-} \hlkwd{melt}\hlstd{(abund,} \hlkwc{value.name} \hlstd{=} \hlstr{"abund"}\hlstd{)} \hlopt{%>%}
\hlkwd{left_join}\hlstd{(}\hlkwd{melt}\hlstd{(abund_ranks,} \hlkwc{value.name} \hlstd{=} \hlstr{"rank"}\hlstd{))}
\hlkwd{colnames}\hlstd{(abund_df)} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"sample"}\hlstd{,} \hlstr{"seq"}\hlstd{,} \hlstr{"abund"}\hlstd{,} \hlstr{"rank"}\hlstd{)}
\hlstd{abund_df} \hlkwb{<-} \hlkwd{melt}\hlstd{(abund,} \hlkwc{value.name} \hlstd{=} \hlstr{"abund"}\hlstd{)} \hlopt{%>%}
\hlkwd{left_join}\hlstd{(}\hlkwd{melt}\hlstd{(abund_ranks,} \hlkwc{value.name} \hlstd{=} \hlstr{"rank"}\hlstd{))}
\hlkwd{colnames}\hlstd{(abund_df)} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"sample"}\hlstd{,} \hlstr{"seq"}\hlstd{,} \hlstr{"abund"}\hlstd{,} \hlstr{"rank"}\hlstd{)}
\hlstd{sample_ix} \hlkwb{<-} \hlkwd{sample}\hlstd{(}\hlnum{1}\hlopt{:}\hlkwd{nrow}\hlstd{(abund_df),} \hlnum{8}\hlstd{)}
\hlkwd{ggplot}\hlstd{(abund_df} \hlopt{%>%}
\hlkwd{filter}\hlstd{(sample} \hlopt{%in%} \hlstd{abund_df}\hlopt{$}\hlstd{sample[sample_ix]))} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= abund,} \hlkwc{y} \hlstd{= rank,} \hlkwc{col} \hlstd{= sample),}
\hlkwc{position} \hlstd{=} \hlkwd{position_jitter}\hlstd{(}\hlkwc{width} \hlstd{=} \hlnum{0.2}\hlstd{),} \hlkwc{size} \hlstd{=} \hlnum{.7}\hlstd{)} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{x} \hlstd{=} \hlstr{"Abundance"}\hlstd{,} \hlkwc{y} \hlstd{=} \hlstr{"Thresholded rank"}\hlstd{)} \hlopt{+}
\hlkwd{scale_color_brewer}\hlstd{(}\hlkwc{palette} \hlstd{=} \hlstr{"Set2"}\hlstd{)}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/pca-rank-visualize-procedure-1}
}
\end{knitrout}
\caption{The association between abundance and rank, for a few
randomly selected samples. The numbers of the $y$-axis are those
supplied to PCA.}
\label{fig:pca-rank-visualize-procedure}
\end{figure}
We can now perform PCA and study the resulting biplot, given
in Figure \ref{fig:pca-rank-pca-plot}. To produce annotation for this
figure, we used the following block.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{ranks_pca} \hlkwb{<-} \hlkwd{dudi.pca}\hlstd{(abund_ranks,} \hlkwc{scannf} \hlstd{= F,} \hlkwc{nf} \hlstd{=} \hlnum{3}\hlstd{)}
\hlstd{row_scores} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{li} \hlstd{= ranks_pca}\hlopt{$}\hlstd{li,}
\hlkwc{SampleID} \hlstd{=} \hlkwd{rownames}\hlstd{(abund_ranks))}
\hlstd{col_scores} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{co} \hlstd{= ranks_pca}\hlopt{$}\hlstd{co,}
\hlkwc{seq} \hlstd{=} \hlkwd{colnames}\hlstd{(abund_ranks))}
\hlstd{tax} \hlkwb{<-} \hlkwd{tax_table}\hlstd{(ps)}\hlopt{@}\hlkwc{.Data} \hlopt{%>%}
\hlkwd{data.frame}\hlstd{(}\hlkwc{stringsAsFactors} \hlstd{=} \hlnum{FALSE}\hlstd{)}
\hlstd{tax}\hlopt{$}\hlstd{seq} \hlkwb{<-} \hlkwd{rownames}\hlstd{(tax)}
\hlstd{main_orders} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"Clostridiales"}\hlstd{,} \hlstr{"Bacteroidales"}\hlstd{,} \hlstr{"Lactobacillales"}\hlstd{,}
\hlstr{"Coriobacteriales"}\hlstd{)}
\hlstd{tax}\hlopt{$}\hlstd{Order[}\hlopt{!}\hlstd{(tax}\hlopt{$}\hlstd{Order} \hlopt{%in%} \hlstd{main_orders)]} \hlkwb{<-} \hlstr{"Other"}
\hlstd{tax}\hlopt{$}\hlstd{Order} \hlkwb{<-} \hlkwd{factor}\hlstd{(tax}\hlopt{$}\hlstd{Order,} \hlkwc{levels} \hlstd{=} \hlkwd{c}\hlstd{(main_orders,} \hlstr{"Other"}\hlstd{))}
\hlstd{tax}\hlopt{$}\hlstd{otu_id} \hlkwb{<-} \hlkwd{seq_len}\hlstd{(}\hlkwd{ncol}\hlstd{(}\hlkwd{otu_table}\hlstd{(ps)))}
\hlstd{row_scores} \hlkwb{<-} \hlstd{row_scores} \hlopt{%>%}
\hlkwd{left_join}\hlstd{(}\hlkwd{sample_data}\hlstd{(pslog))}
\hlstd{col_scores} \hlkwb{<-} \hlstd{col_scores} \hlopt{%>%}
\hlkwd{left_join}\hlstd{(tax)}
\end{alltt}
\end{kframe}
\end{knitrout}
The results are similar to the
PCoA analyses computed without applying a truncated-ranking
transformation, reinforcing our confidence in the analysis on the
original data.\\
\begin{figure}[h]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{evals_prop} \hlkwb{<-} \hlnum{100} \hlopt{*} \hlstd{(ranks_pca}\hlopt{$}\hlstd{eig} \hlopt{/} \hlkwd{sum}\hlstd{(ranks_pca}\hlopt{$}\hlstd{eig))}
\hlkwd{ggplot}\hlstd{()} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwc{data} \hlstd{= row_scores,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= li.Axis1,} \hlkwc{y} \hlstd{= li.Axis2),} \hlkwc{shape} \hlstd{=} \hlnum{2}\hlstd{)} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwc{data} \hlstd{= col_scores,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{=} \hlnum{25} \hlopt{*} \hlstd{co.Comp1,} \hlkwc{y} \hlstd{=} \hlnum{25} \hlopt{*} \hlstd{co.Comp2,} \hlkwc{col} \hlstd{= Order),}
\hlkwc{size} \hlstd{=} \hlnum{.3}\hlstd{,} \hlkwc{alpha} \hlstd{=} \hlnum{0.6}\hlstd{)} \hlopt{+}
\hlkwd{scale_color_brewer}\hlstd{(}\hlkwc{palette} \hlstd{=} \hlstr{"Set2"}\hlstd{)} \hlopt{+}
\hlkwd{facet_grid}\hlstd{(}\hlopt{~} \hlstd{age_binned)} \hlopt{+}
\hlkwd{guides}\hlstd{(}\hlkwc{col} \hlstd{=} \hlkwd{guide_legend}\hlstd{(}\hlkwc{override.aes} \hlstd{=} \hlkwd{list}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)))} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{x} \hlstd{=} \hlkwd{sprintf}\hlstd{(}\hlstr{"Axis1 [%s%% variance]"}\hlstd{,} \hlkwd{round}\hlstd{(evals_prop[}\hlnum{1}\hlstd{],} \hlnum{2}\hlstd{)),}
\hlkwc{y} \hlstd{=} \hlkwd{sprintf}\hlstd{(}\hlstr{"Axis2 [%s%% variance]"}\hlstd{,} \hlkwd{round}\hlstd{(evals_prop[}\hlnum{2}\hlstd{],} \hlnum{2}\hlstd{)))} \hlopt{+}
\hlkwd{coord_fixed}\hlstd{(}\hlkwd{sqrt}\hlstd{(ranks_pca}\hlopt{$}\hlstd{eig[}\hlnum{2}\hlstd{]} \hlopt{/} \hlstd{ranks_pca}\hlopt{$}\hlstd{eig[}\hlnum{1}\hlstd{]))} \hlopt{+}
\hlkwd{theme}\hlstd{(}\hlkwc{panel.border} \hlstd{=} \hlkwd{element_rect}\hlstd{(}\hlkwc{color} \hlstd{=} \hlstr{"#787878"}\hlstd{,} \hlkwc{fill} \hlstd{=} \hlkwd{alpha}\hlstd{(}\hlstr{"white"}\hlstd{,} \hlnum{0}\hlstd{)))}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/pca-rank-pca-plot-1}
}
\end{knitrout}
\vskip-0.5cm
\caption{The biplot resulting from the PCA after the truncated-ranking
transformation.}
\label{fig:pca-rank-pca-plot}
\end{figure}
\subsubsection*{Canonical correspondence}
Canonical Correspondence Analysis (CCpnA) is an approach to ordination
of a species by sample table that incorporates supplemental
information about the samples. As before, the purpose of creating
biplots is to determine which types of bacterial communities are most
prominent in different mouse sample types. It can be easier to
interpret these biplots when the ordering between samples reflects
sample characteristics -- variations in age or litter status in the
mouse data, for example -- and this central to the design of CCpnA.
The function allows us to create biplots where the positions of samples are determined by
similarity in both species signatures and environmental
characteristics; in contrast, principal components analysis or
correspondence analysis only look at species signatures. More
formally, it ensures that the resulting CCpnA directions lie in the
span of the environmental variables; thorough treatments are available
in \cite{terBraak:1985, greenacre2007correspondence}.
Like PCoA and DPCoA, this method can be run using
\Rfunction{ordinate} in \BioCpkg{phyloseq}. In order to use
supplemental sample data, it is necessary to provide an extra
argument, specifying which of the features to consider -- otherwise,
\BioCpkg{phyloseq} defaults to using all \Robject{sample\_data}
measurements when producing the ordination.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{ps_ccpna} \hlkwb{<-} \hlkwd{ordinate}\hlstd{(pslog,} \hlstr{"CCA"}\hlstd{,} \hlkwc{formula} \hlstd{= pslog} \hlopt{~} \hlstd{age_binned} \hlopt{+} \hlstd{family_relationship)}
\end{alltt}
\end{kframe}
\end{knitrout}
To access the positions for the biplot, we can use the
\Rfunction{scores} function in the \CRANpkg{vegan}. Further, to
facilitate figure annotation, we also join the site scores with the
environmental data in the \Robject{sample\_data} slot. Of the 23 total
taxonomic orders, we only explicitly annotate the four most abundant
-- this makes the biplot easier to read.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{ps_scores} \hlkwb{<-} \hlstd{vegan}\hlopt{::}\hlkwd{scores}\hlstd{(ps_ccpna)}
\hlstd{sites} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(ps_scores}\hlopt{$}\hlstd{sites)}
\hlstd{sites}\hlopt{$}\hlstd{SampleID} \hlkwb{<-} \hlkwd{rownames}\hlstd{(sites)}
\hlstd{sites} \hlkwb{<-} \hlstd{sites} \hlopt{%>%}
\hlkwd{left_join}\hlstd{(}\hlkwd{sample_data}\hlstd{(ps))}
\hlstd{species} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(ps_scores}\hlopt{$}\hlstd{species)}
\hlstd{species}\hlopt{$}\hlstd{otu_id} \hlkwb{<-} \hlkwd{seq_along}\hlstd{(}\hlkwd{colnames}\hlstd{(}\hlkwd{otu_table}\hlstd{(ps)))}
\hlstd{species} \hlkwb{<-} \hlstd{species} \hlopt{%>%}
\hlkwd{left_join}\hlstd{(tax)}
\end{alltt}
\end{kframe}
\end{knitrout}
Figures \ref{fig:ccpna-plot-age} and \ref{fig:ccpna-plot-litter} plot these
annotated scores, splitting sites by their age bin and litter
membership, respectively. We have labeled individual microbes that are
outliers along the second CCpnA direction.
Evidently, the first CCpnA direction distinguishes between mice
in the two main age bins. Circles on the left and right of the biplot
represent microbes that are characteristic of younger and older mice,
respectively. The second CCpnA direction splits off the few mice
in the oldest age group; it also partially distinguishes between the two
litters. These samples low in the second CCpnA direction have more of
the outlier microbes than the others.
This CCpnA analysis supports our conclusions from the earlier
ordinations -- the main difference between the microbiome communities
of the different mice lies along the age axis. However, in situations
where the influence of environmental variables is not so strong, CCA
can have more power in detecting such associations. In general, it
can be applied whenever it is desirable to incorporate
supplemental data, but in a way that (1) is less aggressive than
supervised methods, and (2) can use several environmental variables at
once.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{evals_prop} \hlkwb{<-} \hlnum{100} \hlopt{*} \hlstd{ps_ccpna}\hlopt{$}\hlstd{CCA}\hlopt{$}\hlstd{eig[}\hlnum{1}\hlopt{:}\hlnum{2}\hlstd{]} \hlopt{/} \hlkwd{sum}\hlstd{(ps_ccpna}\hlopt{$}\hlstd{CA}\hlopt{$}\hlstd{eig)}
\hlkwd{ggplot}\hlstd{()} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwc{data} \hlstd{= sites,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= CCA1,} \hlkwc{y} \hlstd{= CCA2),} \hlkwc{shape} \hlstd{=} \hlnum{2}\hlstd{,} \hlkwc{alpha} \hlstd{=} \hlnum{0.5}\hlstd{)} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwc{data} \hlstd{= species,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= CCA1,} \hlkwc{y} \hlstd{= CCA2,} \hlkwc{col} \hlstd{= Order),} \hlkwc{size} \hlstd{=} \hlnum{0.5}\hlstd{)} \hlopt{+}
\hlkwd{geom_text_repel}\hlstd{(}\hlkwc{data} \hlstd{= species} \hlopt{%>%} \hlkwd{filter}\hlstd{(CCA2} \hlopt{< -}\hlnum{2}\hlstd{),}
\hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= CCA1,} \hlkwc{y} \hlstd{= CCA2,} \hlkwc{label} \hlstd{= otu_id),}
\hlkwc{size} \hlstd{=} \hlnum{1.5}\hlstd{,} \hlkwc{segment.size} \hlstd{=} \hlnum{0.1}\hlstd{)} \hlopt{+}
\hlkwd{facet_grid}\hlstd{(.} \hlopt{~} \hlstd{age_binned)} \hlopt{+}
\hlkwd{guides}\hlstd{(}\hlkwc{col} \hlstd{=} \hlkwd{guide_legend}\hlstd{(}\hlkwc{override.aes} \hlstd{=} \hlkwd{list}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)))} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{x} \hlstd{=} \hlkwd{sprintf}\hlstd{(}\hlstr{"Axis1 [%s%% variance]"}\hlstd{,} \hlkwd{round}\hlstd{(evals_prop[}\hlnum{1}\hlstd{],} \hlnum{2}\hlstd{)),}
\hlkwc{y} \hlstd{=} \hlkwd{sprintf}\hlstd{(}\hlstr{"Axis2 [%s%% variance]"}\hlstd{,} \hlkwd{round}\hlstd{(evals_prop[}\hlnum{2}\hlstd{],} \hlnum{2}\hlstd{)))} \hlopt{+}
\hlkwd{scale_color_brewer}\hlstd{(}\hlkwc{palette} \hlstd{=} \hlstr{"Set2"}\hlstd{)} \hlopt{+}
\hlkwd{coord_fixed}\hlstd{(}\hlkwd{sqrt}\hlstd{(ps_ccpna}\hlopt{$}\hlstd{CCA}\hlopt{$}\hlstd{eig[}\hlnum{2}\hlstd{]} \hlopt{/} \hlstd{ps_ccpna}\hlopt{$}\hlstd{CCA}\hlopt{$}\hlstd{eig[}\hlnum{1}\hlstd{])}\hlopt{*}\hlnum{0.33}\hlstd{)} \hlopt{+}
\hlkwd{theme}\hlstd{(}\hlkwc{panel.border} \hlstd{=} \hlkwd{element_rect}\hlstd{(}\hlkwc{color} \hlstd{=} \hlstr{"#787878"}\hlstd{,} \hlkwc{fill} \hlstd{=} \hlkwd{alpha}\hlstd{(}\hlstr{"white"}\hlstd{,} \hlnum{0}\hlstd{)))}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\includegraphics[width=1.1\linewidth]{analysisfigure/ccpna-plot-age-1}
\vskip-0.5cm
\caption{The mouse and bacteria scores generated by CCpnA. The sites and
species are triangles and circles, respectively. The separate panels
indicate different age groups.}
\label{fig:ccpna-plot-age}
\end{figure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggplot}\hlstd{()} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwc{data} \hlstd{= sites,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= CCA1,} \hlkwc{y} \hlstd{= CCA2),} \hlkwc{shape} \hlstd{=} \hlnum{2}\hlstd{,} \hlkwc{alpha} \hlstd{=} \hlnum{0.5}\hlstd{)} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwc{data} \hlstd{= species,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= CCA1,} \hlkwc{y} \hlstd{= CCA2,} \hlkwc{col} \hlstd{= Order),} \hlkwc{size} \hlstd{=} \hlnum{0.5}\hlstd{)} \hlopt{+}
\hlkwd{geom_text_repel}\hlstd{(}\hlkwc{data} \hlstd{= species} \hlopt{%>%} \hlkwd{filter}\hlstd{(CCA2} \hlopt{< -}\hlnum{2}\hlstd{),}
\hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= CCA1,} \hlkwc{y} \hlstd{= CCA2,} \hlkwc{label} \hlstd{= otu_id),}
\hlkwc{size} \hlstd{=} \hlnum{1.5}\hlstd{,} \hlkwc{segment.size} \hlstd{=} \hlnum{0.1}\hlstd{)} \hlopt{+}
\hlkwd{facet_grid}\hlstd{(.} \hlopt{~} \hlstd{family_relationship)} \hlopt{+}
\hlkwd{guides}\hlstd{(}\hlkwc{col} \hlstd{=} \hlkwd{guide_legend}\hlstd{(}\hlkwc{override.aes} \hlstd{=} \hlkwd{list}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)))} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{x} \hlstd{=} \hlkwd{sprintf}\hlstd{(}\hlstr{"Axis1 [%s%% variance]"}\hlstd{,} \hlkwd{round}\hlstd{(evals_prop[}\hlnum{1}\hlstd{],} \hlnum{2}\hlstd{)),}
\hlkwc{y} \hlstd{=} \hlkwd{sprintf}\hlstd{(}\hlstr{"Axis2 [%s%% variance]"}\hlstd{,} \hlkwd{round}\hlstd{(evals_prop[}\hlnum{2}\hlstd{],} \hlnum{2}\hlstd{)))} \hlopt{+}
\hlkwd{scale_color_brewer}\hlstd{(}\hlkwc{palette} \hlstd{=} \hlstr{"Set2"}\hlstd{)} \hlopt{+}
\hlkwd{coord_fixed}\hlstd{(}\hlkwd{sqrt}\hlstd{(ps_ccpna}\hlopt{$}\hlstd{CCA}\hlopt{$}\hlstd{eig[}\hlnum{2}\hlstd{]} \hlopt{/} \hlstd{ps_ccpna}\hlopt{$}\hlstd{CCA}\hlopt{$}\hlstd{eig[}\hlnum{1}\hlstd{])}\hlopt{*}\hlnum{0.45} \hlstd{)} \hlopt{+}
\hlkwd{theme}\hlstd{(}\hlkwc{panel.border} \hlstd{=} \hlkwd{element_rect}\hlstd{(}\hlkwc{color} \hlstd{=} \hlstr{"#787878"}\hlstd{,} \hlkwc{fill} \hlstd{=} \hlkwd{alpha}\hlstd{(}\hlstr{"white"}\hlstd{,} \hlnum{0}\hlstd{)))}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\includegraphics[width=1.1\linewidth]{analysisfigure/ccpna-plot-litter-1}
\caption{The analogue to Figure \ref{fig:ccpna-plot-age}, faceting by
litter membership rather than age bin.}
\label{fig:ccpna-plot-litter}
\end{figure}
\subsubsection{Supervised learning}
\label{sec:supervised-learning}
Here we illustrate some supervised learning methods that can be easily
run in R. The \CRANpkg{caret} package wraps many prediction algorithms
available in R and performs parameter tuning automatically. Since we
saw that microbiome signatures change with age, we'll apply supervised
techniques to try to predict age from microbiome composition.
We'll first look at Partial Least Squares (PLS)\cite{PLSwold}.
The first step is to
divide the data into training and test sets, with assignments done by mouse,
rather than by sample, to ensure that the test set realistically
simulates the collection of new data. Once we split the data, we can
use the \Rfunction{train} function to fit the PLS model.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{setup_example}\hlstd{(}\hlkwd{c}\hlstd{(}\hlstr{"phyloseq"}\hlstd{,} \hlstr{"ggplot2"}\hlstd{,} \hlstr{"caret"}\hlstd{,} \hlstr{"plyr"}\hlstd{,} \hlstr{"dplyr"}\hlstd{))}
\hlkwd{sample_data}\hlstd{(pslog)}\hlopt{$}\hlstd{age2} \hlkwb{<-} \hlkwd{cut}\hlstd{(}\hlkwd{sample_data}\hlstd{(pslog)}\hlopt{$}\hlstd{age,} \hlkwd{c}\hlstd{(}\hlnum{0}\hlstd{,} \hlnum{100}\hlstd{,} \hlnum{400}\hlstd{))}
\hlstd{dataMatrix} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{age} \hlstd{=} \hlkwd{sample_data}\hlstd{(pslog)}\hlopt{$}\hlstd{age2,} \hlkwd{otu_table}\hlstd{(pslog))}
\hlcom{# take 8 mice at random to be the training set, and the remaining 4 the test set}
\hlstd{trainingMice} \hlkwb{<-} \hlkwd{sample}\hlstd{(}\hlkwd{unique}\hlstd{(}\hlkwd{sample_data}\hlstd{(pslog)}\hlopt{$}\hlstd{host_subject_id),} \hlkwc{size} \hlstd{=} \hlnum{8}\hlstd{)}
\hlstd{inTrain} \hlkwb{<-} \hlkwd{which}\hlstd{(}\hlkwd{sample_data}\hlstd{(pslog)}\hlopt{$}\hlstd{host_subject_id} \hlopt{%in%} \hlstd{trainingMice)}
\hlstd{training} \hlkwb{<-} \hlstd{dataMatrix[inTrain,]}
\hlstd{testing} \hlkwb{<-} \hlstd{dataMatrix[}\hlopt{-}\hlstd{inTrain,]}
\hlstd{plsFit} \hlkwb{<-} \hlkwd{train}\hlstd{(age} \hlopt{~} \hlstd{.,} \hlkwc{data} \hlstd{= training,}
\hlkwc{method} \hlstd{=} \hlstr{"pls"}\hlstd{,} \hlkwc{preProc} \hlstd{=} \hlstr{"center"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
Next we can predict class labels on the test set using the
\Rfunction{predict} function and compare to the truth. We see that the
method does an excellent job of predicting age.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plsClasses} \hlkwb{<-} \hlkwd{predict}\hlstd{(plsFit,} \hlkwc{newdata} \hlstd{= testing)}
\hlkwd{table}\hlstd{(plsClasses, testing}\hlopt{$}\hlstd{age)}
\end{alltt}
\begin{verbatim}
##
## plsClasses (0,100] (100,400]
## (0,100] 64 0
## (100,400] 2 46
\end{verbatim}
\end{kframe}
\end{knitrout}
As another example, we can try out random forests. This is run in
exactly the same way as PLS, by switching the \Robject{method}
argument from \Robject{pls} to \Robject{rf}. Random forests also
perform well at the prediction task on this test set, though there
are more old mice misclassified as young.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{rfFit} \hlkwb{<-} \hlkwd{train}\hlstd{(age} \hlopt{~} \hlstd{.,} \hlkwc{data} \hlstd{= training,} \hlkwc{method} \hlstd{=} \hlstr{"rf"}\hlstd{,}
\hlkwc{preProc} \hlstd{=} \hlstr{"center"}\hlstd{,} \hlkwc{proximity} \hlstd{=} \hlnum{TRUE}\hlstd{)}
\hlstd{rfClasses} \hlkwb{<-} \hlkwd{predict}\hlstd{(rfFit,} \hlkwc{newdata} \hlstd{= testing)}
\hlkwd{table}\hlstd{(rfClasses, testing}\hlopt{$}\hlstd{age)}
\end{alltt}
\begin{verbatim}
##
## rfClasses (0,100] (100,400]
## (0,100] 65 7
## (100,400] 1 39
\end{verbatim}
\end{kframe}
\end{knitrout}
To interpret these PLS and random forest results, it is standard to
produce biplots and proximity plots, respectively. The code below
extracts coordinates and supplies annotation for points to include on
the PLS biplot.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{pls_biplot} \hlkwb{<-} \hlkwd{list}\hlstd{(}\hlstr{"loadings"} \hlstd{=} \hlkwd{loadings}\hlstd{(plsFit}\hlopt{$}\hlstd{finalModel),}
\hlstr{"scores"} \hlstd{=} \hlkwd{scores}\hlstd{(plsFit}\hlopt{$}\hlstd{finalModel))}
\hlkwd{class}\hlstd{(pls_biplot}\hlopt{$}\hlstd{scores)} \hlkwb{<-} \hlstr{"matrix"}
\hlstd{pls_biplot}\hlopt{$}\hlstd{scores} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwd{sample_data}\hlstd{(pslog)[inTrain, ],}
\hlstd{pls_biplot}\hlopt{$}\hlstd{scores)}
\hlstd{tax} \hlkwb{<-} \hlkwd{tax_table}\hlstd{(ps)}\hlopt{@}\hlkwc{.Data} \hlopt{%>%}
\hlkwd{data.frame}\hlstd{(}\hlkwc{stringsAsFactors} \hlstd{=} \hlnum{FALSE}\hlstd{)}
\hlstd{main_orders} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"Clostridiales"}\hlstd{,} \hlstr{"Bacteroidales"}\hlstd{,} \hlstr{"Lactobacillales"}\hlstd{,}
\hlstr{"Coriobacteriales"}\hlstd{)}
\hlstd{tax}\hlopt{$}\hlstd{Order[}\hlopt{!}\hlstd{(tax}\hlopt{$}\hlstd{Order} \hlopt{%in%} \hlstd{main_orders)]} \hlkwb{<-} \hlstr{"Other"}
\hlstd{tax}\hlopt{$}\hlstd{Order} \hlkwb{<-} \hlkwd{factor}\hlstd{(tax}\hlopt{$}\hlstd{Order,} \hlkwc{levels} \hlstd{=} \hlkwd{c}\hlstd{(main_orders,} \hlstr{"Other"}\hlstd{))}
\hlkwd{class}\hlstd{(pls_biplot}\hlopt{$}\hlstd{loadings)} \hlkwb{<-} \hlstr{"matrix"}
\hlstd{pls_biplot}\hlopt{$}\hlstd{loadings} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(tax, pls_biplot}\hlopt{$}\hlstd{loadings)}
\end{alltt}
\end{kframe}
\end{knitrout}
The resulting biplot is displayed in Figure \ref{fig:caret-pls-scores-plot};
it can be interpreted similarly to earlier ordination diagrams, with
the exception that the projection is chosen with an explicit reference
to the binned age variable. Specifically, PLS identifies a subspace to
maximize discrimination between classes, and the biplot displays
sample projections and RSV coefficients with respect to this
subspace.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggplot}\hlstd{()} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwc{data} \hlstd{= pls_biplot}\hlopt{$}\hlstd{scores,}
\hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= Comp.1,} \hlkwc{y} \hlstd{= Comp.2),} \hlkwc{shape} \hlstd{=} \hlnum{2}\hlstd{)} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwc{data} \hlstd{= pls_biplot}\hlopt{$}\hlstd{loadings,}
\hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{=} \hlnum{25} \hlopt{*} \hlstd{Comp.1,} \hlkwc{y} \hlstd{=} \hlnum{25} \hlopt{*} \hlstd{Comp.2,} \hlkwc{col} \hlstd{= Order),}
\hlkwc{size} \hlstd{=} \hlnum{0.3}\hlstd{,} \hlkwc{alpha} \hlstd{=} \hlnum{0.6}\hlstd{)} \hlopt{+}
\hlkwd{scale_color_brewer}\hlstd{(}\hlkwc{palette} \hlstd{=} \hlstr{"Set2"}\hlstd{)} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{x} \hlstd{=} \hlstr{"Axis1"}\hlstd{,} \hlkwc{y} \hlstd{=} \hlstr{"Axis2"}\hlstd{,} \hlkwc{col} \hlstd{=} \hlstr{"Binned Age"}\hlstd{)} \hlopt{+}
\hlkwd{guides}\hlstd{(}\hlkwc{col} \hlstd{=} \hlkwd{guide_legend}\hlstd{(}\hlkwc{override.aes} \hlstd{=} \hlkwd{list}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)))} \hlopt{+}
\hlkwd{facet_grid}\hlstd{(} \hlopt{~} \hlstd{age2)} \hlopt{+}
\hlkwd{theme}\hlstd{(}\hlkwc{panel.border} \hlstd{=} \hlkwd{element_rect}\hlstd{(}\hlkwc{color} \hlstd{=} \hlstr{"#787878"}\hlstd{,} \hlkwc{fill} \hlstd{=} \hlkwd{alpha}\hlstd{(}\hlstr{"white"}\hlstd{,} \hlnum{0}\hlstd{)))}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\includegraphics[width=\linewidth]{analysisfigure/caret-pls-scores-plot-1}
\caption{PLS produces a biplot representation designed to separate
samples by a response varaible.}
\label{fig:caret-pls-scores-plot}
\end{figure}
A random forest proximity plot is displayed in Figure
\ref{fig:caret-proximity}. To generate this representation, a distance
is calculated between samples based on how frequently sample occur in
the same tree partition in the random forest's bootstrapping
procedure. If a pair of samples frequently occur in the same
partition, the pair is assigned a low distance. The resulting
distances are then input to PCoA, giving a glimpse into the random
forests' otherwise complex classification mechanism. The separation
between classes is clear, and manually inspecting points would reveal
what types of samples are easier or harder to classify.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{rf_prox} \hlkwb{<-} \hlkwd{cmdscale}\hlstd{(}\hlnum{1} \hlopt{-} \hlstd{rfFit}\hlopt{$}\hlstd{finalModel}\hlopt{$}\hlstd{proximity)} \hlopt{%>%}
\hlkwd{data.frame}\hlstd{(}\hlkwd{sample_data}\hlstd{(pslog)[inTrain, ])}
\hlkwd{ggplot}\hlstd{(rf_prox)} \hlopt{+}
\hlkwd{geom_point}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= X1,} \hlkwc{y} \hlstd{= X2,} \hlkwc{col} \hlstd{= age_binned),}
\hlkwc{size} \hlstd{=} \hlnum{.4}\hlstd{,} \hlkwc{alpha} \hlstd{=} \hlnum{0.6}\hlstd{)} \hlopt{+}
\hlkwd{scale_color_manual}\hlstd{(}\hlkwc{values} \hlstd{=} \hlkwd{c}\hlstd{(}\hlstr{"#A66EB8"}\hlstd{,} \hlstr{"#238DB5"}\hlstd{,} \hlstr{"#748B4F"}\hlstd{))} \hlopt{+}
\hlkwd{guides}\hlstd{(}\hlkwc{col} \hlstd{=} \hlkwd{guide_legend}\hlstd{(}\hlkwc{override.aes} \hlstd{=} \hlkwd{list}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)))} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{col} \hlstd{=} \hlstr{"Binned Age"}\hlstd{,} \hlkwc{x} \hlstd{=} \hlstr{"Axis1"}\hlstd{,} \hlkwc{y} \hlstd{=} \hlstr{"Axis2"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\includegraphics[width=\linewidth]{analysisfigure/caret-proximity-1}
\caption{The random forest model determines a distance between
samples, which can be input into PCoA to produce a proximity plot.}
\label{fig:caret-proximity}
\end{figure}
To further understand the fitted random forest model, we identify the
microbe with the most influence in the random forest prediction. This
turns out to be a microbe in family \emph{Lachnospiraceae} and genus
\emph{Roseburia}. Figure \ref{fig:caret-rf-importance} plots its abundance
across samples; we see that it is uniformly very low from age 0 to 100
days and much higher from age 100 to 400 days.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{as.vector}\hlstd{(}\hlkwd{tax_table}\hlstd{(ps)[}\hlkwd{which.max}\hlstd{(}\hlkwd{importance}\hlstd{(rfFit}\hlopt{$}\hlstd{finalModel)),} \hlkwd{c}\hlstd{(}\hlstr{"Family"}\hlstd{,} \hlstr{"Genus"}\hlstd{)])}
\end{alltt}
\begin{verbatim}
## [1] "Lachnospiraceae" NA
\end{verbatim}
\begin{alltt}
\hlstd{impOtu} \hlkwb{<-} \hlkwd{as.vector}\hlstd{(}\hlkwd{otu_table}\hlstd{(pslog)[,}\hlkwd{which.max}\hlstd{(}\hlkwd{importance}\hlstd{(rfFit}\hlopt{$}\hlstd{finalModel))])}
\hlstd{maxImpDF} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwd{sample_data}\hlstd{(pslog),} \hlkwc{abund} \hlstd{= impOtu)}
\hlkwd{ggplot}\hlstd{(maxImpDF)} \hlopt{+} \hlkwd{geom_histogram}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= abund))} \hlopt{+}
\hlkwd{facet_grid}\hlstd{(age2} \hlopt{~} \hlstd{.)} \hlopt{+}
\hlkwd{labs}\hlstd{(}\hlkwc{x} \hlstd{=} \hlstr{"Abundance of discriminative bacteria"}\hlstd{,} \hlkwc{y} \hlstd{=} \hlstr{"Number of samples"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\includegraphics[width=\linewidth]{analysisfigure/caret-rf-importance-1}
\caption{A bacteria in genus \emph{Roseburia} becomes much more abundant in
the 100 to 400 day bin.}
\label{fig:caret-rf-importance}
\end{figure}
\section*{Graph-based visualization and testing}
\subsection*{Creating and plotting graphs}
Phyloseq has functionality for creating graphs based on thresholding a
distance matrix, and the resulting networks can be plotting using the
\CRANpkg{ggnetwork}. This package overloads the ggplot syntax, so you
can use the function ggplot on an igraph object and add
\Robject{geom\_edges} and \Robject{geom\_nodes} geoms to plot the
network. To be able to color the nodes or edges a certain way, we need
to add these attributes to the igraph object. Below we create a
network by thresholding the Jaccard dissimilarity (the default
distance for the function \Robject{make\_network}) at .35, and then we
add an attribute to the vertices indicating which mouse the sample
came from and which litter the mouse was in. Then we can plot the
network with the coloring by mouse and shape by litter. We see the
resulting network in Figure \ref{fig:ggnetwork}, and we can see that there is
grouping of the samples by both mouse and litter.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{setup_example}\hlstd{(}\hlkwd{c}\hlstd{(}\hlstr{"igraph"}\hlstd{,} \hlstr{"phyloseq"}\hlstd{,} \hlstr{"phyloseqGraphTest"}\hlstd{,} \hlstr{"ggnetwork"}\hlstd{,} \hlstr{"intergraph"}\hlstd{,} \hlstr{"gridExtra"}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{net} \hlkwb{<-} \hlkwd{make_network}\hlstd{(ps,} \hlkwc{max.dist}\hlstd{=}\hlnum{0.35}\hlstd{)}
\hlstd{sampledata} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwd{sample_data}\hlstd{(ps))}
\hlkwd{V}\hlstd{(net)}\hlopt{$}\hlstd{id} \hlkwb{<-} \hlstd{sampledata[}\hlkwd{names}\hlstd{(}\hlkwd{V}\hlstd{(net)),} \hlstr{"host_subject_id"}\hlstd{]}
\hlkwd{V}\hlstd{(net)}\hlopt{$}\hlstd{litter} \hlkwb{<-} \hlstd{sampledata[}\hlkwd{names}\hlstd{(}\hlkwd{V}\hlstd{(net)),} \hlstr{"family_relationship"}\hlstd{]}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggplot}\hlstd{(net,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= x,} \hlkwc{y} \hlstd{= y,} \hlkwc{xend} \hlstd{= xend,} \hlkwc{yend} \hlstd{= yend),} \hlkwc{layout} \hlstd{=} \hlstr{"fruchtermanreingold"}\hlstd{)} \hlopt{+}
\hlkwd{geom_edges}\hlstd{(}\hlkwc{color} \hlstd{=} \hlstr{"darkgray"}\hlstd{)} \hlopt{+}
\hlkwd{geom_nodes}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{color} \hlstd{= id,} \hlkwc{shape} \hlstd{= litter))} \hlopt{+}
\hlkwd{theme}\hlstd{(}\hlkwc{axis.text} \hlstd{=} \hlkwd{element_blank}\hlstd{(),} \hlkwc{axis.title} \hlstd{=} \hlkwd{element_blank}\hlstd{(),}
\hlkwc{legend.key.height} \hlstd{=} \hlkwd{unit}\hlstd{(}\hlnum{0.5}\hlstd{,}\hlstr{"line"}\hlstd{))} \hlopt{+}
\hlkwd{guides}\hlstd{(}\hlkwc{col} \hlstd{=} \hlkwd{guide_legend}\hlstd{(}\hlkwc{override.aes} \hlstd{=} \hlkwd{list}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{.25}\hlstd{)))}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/ggnetwork-plot-1}
}
\end{knitrout}
\caption{A network created by thresholding the Jaccard dissimilarity
matrix. The colors represent which mouse the sample came from and
the shape represents which litter the mouse was in.}
\label{fig:ggnetwork}
\end{figure}
\subsection*{Graph-based two-sample tests}
Graph-based two-sample tests were introduced by Friedman and Rafsky
\cite{friedman1979multivariate} as a generalization of the
Wald-Wolfowitz runs test. They proposed the use of a minimum spanning tree (MST)
based on the distances between the samples, and then counting the number
of edges on the tree that were between samples in different
groups. It is not necessary to use a minimum spanning tree (MST), the
graph made by linking nearest neighbors
\cite{schilling1986multivariate} or distance thresholding can also
be used as the input graph. No matter what graph we build between the samples, we can
approximate a null distribution by permuting the labels of the nodes of the graph.
\subsubsection*{Minimum Spanning Tree (MST)}
We first perform a test using an MST with Jaccard dissimilarity. We
want to know whether the two litters
(\Robject{family\_relationship}) come from the same
distribution. Since there is a grouping in the data by individual
(\Robject{host\_subject\_id}), we can't simply permute all the
labels, we need to maintain this nested structure -- this is what the
\Robject{grouping} argument does. Here we permute the
\Robject{family\_relationship} labels but keep the
\Robject{host\_subject\_id} structure intact.
This test has a small $p$-value, and we reject the null hypothesis
that the two samples come from the same distribution. From the plot of
the minimum spanning tree in Figure \ref{fig:mst-plot}, we see by eye that
the samples group by litter more than we would expect by chance.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{gt} \hlkwb{<-} \hlkwd{graph_perm_test}\hlstd{(ps,} \hlstr{"family_relationship"}\hlstd{,} \hlkwc{grouping} \hlstd{=} \hlstr{"host_subject_id"}\hlstd{,}
\hlkwc{distance} \hlstd{=} \hlstr{"jaccard"}\hlstd{,} \hlkwc{type} \hlstd{=} \hlstr{"mst"}\hlstd{)}
\hlstd{gt}\hlopt{$}\hlstd{pval}
\end{alltt}
\begin{verbatim}
## [1] 0.01
\end{verbatim}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plotNet1}\hlkwb{=}\hlkwd{plot_test_network}\hlstd{(gt)} \hlopt{+} \hlkwd{theme}\hlstd{(}\hlkwc{legend.text} \hlstd{=} \hlkwd{element_text}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{8}\hlstd{),}
\hlkwc{legend.title} \hlstd{=} \hlkwd{element_text}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{9}\hlstd{))}
\hlstd{plotPerm1}\hlkwb{=}\hlkwd{plot_permutations}\hlstd{(gt)}
\hlkwd{grid.arrange}\hlstd{(}\hlkwc{ncol} \hlstd{=} \hlnum{2}\hlstd{, plotNet1, plotPerm1)}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/mst-plot-1}
}
\end{knitrout}
\caption{The graph and permutation histogram obtained from the minimal
spanning tree with Jaccard similarity.}
\label{fig:mst-plot}
\end{figure}
\subsubsection*{Nearest neighbors}
The $k$-nearest neighbors graph is obtained by putting an edge between
two samples whenever one of them is in the set of $k$-nearest
neighbors of the other. We see from Figure \ref{fig:knn-1-plot} that
if a pair of samples has an edge between them in the nearest neighbor
graph, they are overwhelmingly likely to be in the same litter.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{gt} \hlkwb{<-} \hlkwd{graph_perm_test}\hlstd{(ps,} \hlstr{"family_relationship"}\hlstd{,} \hlkwc{grouping} \hlstd{=} \hlstr{"host_subject_id"}\hlstd{,}
\hlkwc{distance} \hlstd{=} \hlstr{"jaccard"}\hlstd{,} \hlkwc{type} \hlstd{=} \hlstr{"knn"}\hlstd{,} \hlkwc{knn} \hlstd{=} \hlnum{1}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{figure}[H]
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plotNet2}\hlkwb{=}\hlkwd{plot_test_network}\hlstd{(gt)} \hlopt{+} \hlkwd{theme}\hlstd{(}\hlkwc{legend.text} \hlstd{=} \hlkwd{element_text}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{8}\hlstd{),}
\hlkwc{legend.title} \hlstd{=} \hlkwd{element_text}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{9}\hlstd{))}
\hlstd{plotPerm2}\hlkwb{=}\hlkwd{plot_permutations}\hlstd{(gt)}
\hlkwd{grid.arrange}\hlstd{(}\hlkwc{ncol} \hlstd{=} \hlnum{2}\hlstd{, plotNet2, plotPerm2)}
\end{alltt}
\end{kframe}
{\centering \includegraphics[width=\maxwidth]{analysisfigure/knn-1-plot-1}
}
\end{knitrout}
\caption{The graph and permutation histogram obtained from a
nearest-neighbor graph with Jaccard similarity.}
\label{fig:knn-1-plot}
\end{figure}
We can also compute the analogous test with two-nearest neighbors and the
Bray-Curtis dissimilarity. The results are not shown, but the code is
given below.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{gt} \hlkwb{<-} \hlkwd{graph_perm_test}\hlstd{(ps,} \hlstr{"family_relationship"}\hlstd{,}
\hlkwc{grouping} \hlstd{=} \hlstr{"host_subject_id"}\hlstd{,}
\hlkwc{distance} \hlstd{=} \hlstr{"bray"}\hlstd{,} \hlkwc{type} \hlstd{=} \hlstr{"knn"}\hlstd{,} \hlkwc{knn} \hlstd{=} \hlnum{2}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\subsubsection*{Distance threshold}