-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanosimPosthoc.R
99 lines (92 loc) · 2.77 KB
/
anosimPosthoc.R
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
#documentation start
#=============================================================================
# File data
# creator: Christiane Hassenrück
# acknowledgements:
# primary authority: Christiane Hassenrück
# other authorities:
#=============================================================================
# File contents
# perform pairwise ANOSIM tests
#
# input:
# M - community matrix
# E - grouping factor
# distance - distance measure to be used in anosim
# padj - p value correction method
# if strata is supplied, the type of permutations for Within() and Plots() also has to be supplied
#
# output:
# list with [[1]] anosim R, [[2]] unadjusted p value, [[3]] adjusted p value in triangular matrix (full)
#
# dependencies:
# require(vegan)
#=============================================================================
#documentation end
ANOSIMposthoc <- function(
M,
E,
distance = "bray",
padj = "fdr",
strata = NULL,
type.within = NULL,
type.plots = NULL
) {
E = droplevels(E)
Mlist = list()
for(i in 1:length(levels(E))){
Mlist[[i]] = M[E == levels(E)[i], ]
}
Elist = list()
for(i in 1:length(levels(E))){
Elist[[i]] = as.numeric(E[E == levels(E)[i]])
}
if(!is.null(strata)) {
Slist = list()
for(i in 1:length(levels(E))){
Slist[[i]] = as.numeric(strata[E == levels(E)[i]])
}
}
result = list(
anosimR = matrix(NA, length(levels(E)), length(levels(E))),
anosimP = matrix(NA, length(levels(E)), length(levels(E))),
anosimPadj = matrix(NA, length(levels(E)), length(levels(E))))
colnames(result$anosimR) = colnames(result$anosimP) = levels(E)
rownames(result$anosimR) = rownames(result$anosimP) = levels(E)
for(i in 1:(length(levels(E)) - 1)){
for(j in (i + 1):length(levels(E))){
if(is.null(strata)) {
temp = anosim(
rbind(Mlist[[i]], Mlist[[j]]),
c(Elist[[i]], Elist[[j]]),
distance = distance
)
} else {
temp = anosim(
rbind(Mlist[[i]], Mlist[[j]]),
c(Elist[[i]], Elist[[j]]),
distance = distance,
permutations = how(
within = Within(type = type.within),
plots = Plots(strata = c(Slist[[i]], Slist[[j]]), type = type.plots),
nperm = 999
)
)
}
result$anosimR[j,i] = temp$statistic
result$anosimP[j,i] = temp$signif
}
}
result$anosimPadj = matrix(
p.adjust(
as.vector(result$anosimP),
method = padj,
n = length(which(!is.na(as.vector(result$anosimP))))
),
length(levels(E)),
length(levels(E))
)
colnames(result$anosimPadj) = levels(E)
rownames(result$anosimPadj) = levels(E)
return(result)
}