-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepeat_multipatt.R
234 lines (147 loc) · 7.67 KB
/
repeat_multipatt.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
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
## This is a function that runs multipatt a specified number of times, then outputs two key things:
# 1. A summary table of the species over 100 runs and
# 2. A plot showing the different indicator species made in ggplot (OPTIONAL)
# Reminder to user: check that matrix site order and cluster order are the same
# repeat.multipatt(matrix.name = matrify.shrub.bysite, cluster.name = gc.cluster$gc.group.3, plot.please = FALSE, freq.cutoff = .1)
# repeat.multipatt(matrix.name = matrify.tree.species, cluster.name = tree.abs.cluster$tree.abs, func.name = "r.g")
# repeat.multipatt(matrix.name = matrify.foraging,
# cluster.name = shrub.abs.cluster$shrub.abs,
# xlab.input = "Bird Incidence Indicator Species \nof shrub clusters")
repeat.multipatt <- function(repeats = 100, matrix.name, cluster.name, p.cutoff = .1, func.name = "IndVal.g", phi = FALSE, plot.please = TRUE, plot.colors = c("deeppink2", "cyan3", "yellowgreen", "gray 50", "violet"), freq.cutoff = .5, xlab.input = "Indicator Species \n(freq > 50%)", ylab.input = "Mean Indicator Value over 100 Runs", quiet = FALSE, stat.cutoff = .5, graph.stat.cutoff = .75) {
library(ggplot2)
library(dplyr)
library(indicspecies)
library(stringr)
if (freq.cutoff != .5) {
print("You probably want to change the default Y axis label!")
}
if (length(unique(cluster.name)) > 5) {
stop("Please use fewer than 5 clusters (or edit the function).")
}
if (func.name == "r.g" & phi == TRUE & quiet == FALSE) {
matrix.name <- as.data.frame(ifelse(matrix.name > 0, 1, 0))
print("you are using the r.g function to calculate Pearson's phi coefficient of association.")
}
if (func.name == "r.g" & phi == FALSE & quiet == FALSE) {
print("you are using the r.g function to calculate the point biserial correlation coefficient.")
}
mp.sign.dump <- data.frame()
mp.AB.dump <- data.frame()
i <- 1
for (i in 1:repeats) {
multipatt.out <- multipatt(x = matrix.name,
cluster = cluster.name, func = func.name)
mp.sign <- multipatt.out$sign[complete.cases(multipatt.out$sign),]
mp.sign <- mp.sign[mp.sign$p.value <= p.cutoff & mp.sign$stat >= stat.cutoff, ]
mp.sign$species <- rownames(mp.sign)
if ( length(multipatt.out$A) > 0 ) {
mp.A <- as.data.frame(multipatt.out$A[complete.cases(multipatt.out$A), ] , make.row.names = FALSE)
mp.A <- mp.A[which(rownames(mp.A) %in% mp.sign$species), ]
colnames(mp.A) <- paste("A.", colnames(mp.A), sep = "")
mp.B <- as.data.frame(multipatt.out$B[complete.cases(multipatt.out$B), ], make.row.names = FALSE)
mp.B <- mp.B[which(rownames(mp.B) %in% mp.sign$species), ]
colnames(mp.B) <- paste("B.", colnames(mp.B), sep = "")
mp.AB <- cbind.data.frame(mp.A, mp.B)
mp.AB$species <- rownames(mp.AB)
if (nrow(mp.AB) > 0) {
mp.AB$i <- i
}
}
if (nrow(mp.sign) > 0) {
mp.sign$i <- i
}
mp.sign.dump <- rbind(mp.sign.dump, mp.sign, make.row.names = FALSE)
if (func.name %in% c("IndVal", "IndVal.g")) {
mp.AB.dump <- rbind(mp.AB.dump, mp.AB, make.row.names = FALSE)
}
i <- i + 1
}
ct <- FALSE
if (!(any(colnames(mp.sign.dump) == "s.1"))) {
ct <- TRUE
colname.temp <- tibble(group = colnames(mp.sign.dump))
colname.temp$number <- 1:nrow(colname.temp)
colnames(mp.sign.dump)[1:sum(str_detect(colnames(mp.sign.dump), "s\\."))] <-
paste("s.", seq.int(from = 1, to = sum(str_detect(colnames(mp.sign.dump), "s\\."))), sep = "")
colname.temp <- colname.temp[str_detect(colname.temp$group, "s\\.") , ]
colname.temp$groupname <- str_sub(colname.temp$group, 3, -1)
}
if (!(any(colnames(mp.sign.dump) == "s.3"))) {mp.sign.dump$s.3 = rep(0, times = length(mp.sign.dump$s.1))}
if (!(any(colnames(mp.sign.dump) == "s.4"))) {mp.sign.dump$s.4 = rep(0, times = length(mp.sign.dump$s.1))}
if (!(any(colnames(mp.sign.dump) == "s.5"))) {mp.sign.dump$s.5 = rep(0, times = length(mp.sign.dump$s.1))}
# Make summary tables
mp.summary <- group_by(mp.sign.dump, species) %>%
summarize(
count.sp = n(),
frequency.sp = round(count.sp/repeats, digits = 3),
mean.stat = round(mean(stat), digits = 3),
group = paste(
(if (any(s.1 == 1)) {"1."}),
(if (any(s.2 == 1)) {".2."}),
(if (any(s.3 == 1)) {".3."}),
(if (any(s.4 == 1)) {".4."}),
(if (any(s.5 == 1)) {".5"}),
sep = ""),
min.p.val = min(p.value),
max.p.val = max(p.value),
all.p.vals = paste(p.value, collapse = ", ")
)
mp.summary$group <- ifelse(grepl("[1-9]..[1-9]", mp.summary$group),
gsub(pattern = "\\.\\.", replacement = " & ", mp.summary$group),
mp.summary$group)
if(ct == TRUE & nrow(mp.summary) > 0) {
mp.summary$groupname <- mp.summary$group
mp.summary$groupname <- gsub(pattern = "\\.", replacement = "", mp.summary$group)
g = 1
for (g in 1:max(colname.temp$number)) {
mp.summary$groupname <- gsub(x = mp.summary$groupname,
pattern = as.character(g),
replacement = colname.temp$groupname[g])
}
}
if(ct == TRUE & nrow(mp.summary) == 0) {
warning("There are no species returned")
mp.summary[1,1] <- "No Species Returned"
}
mp.summary$group <- paste("Group", gsub(pattern = "\\.", replacement = "", mp.summary$group), sep = " ")
if (func.name %in% c("IndVal", "IndVal.g")) {
mp.AB.summary <- group_by(mp.AB.dump, species) %>%
summarize_all(.funs = list(mean = mean))
mp.AB.summary$i <- NULL
mp.AB.summary <- cbind(mp.AB.summary,
group_by(mp.AB.dump, species) %>%
summarize(
count.sp = n(),
frequency.sp = round(count.sp/max(i), digits = 3)
)
)
}
## Output graph
if (plot.please == TRUE) {
if (nrow(subset(mp.summary, frequency.sp > freq.cutoff)) > 0) {
mp.plot <- ggplot(subset(mp.summary, frequency.sp > freq.cutoff & mean.stat > graph.stat.cutoff),
aes(x = reorder(species, mean.stat))) +
geom_bar(aes(y = mean.stat, fill = groupname), alpha = .75, stat = "identity") +
geom_text(aes(y = mean.stat, label = mean.stat), hjust = 1.2) +
facet_grid(group ~ ., scales = "free_x") + coord_flip() + guides(fill = "none") +
xlab(xlab.input) + ylab(ylab.input) +
scale_fill_manual(values = c(plot.colors))
print(mp.plot)
}
else print("can't make graph; no indicator species for any clusters!")
}
# Output data tibble
if (quiet == FALSE) {
if (func.name %in% c("IndVal", "IndVal.g")) {
return(list(mp.summary, mp.AB.summary))
}
else return(mp.summary)
}
}
## Testing
# matrix.name = sqrt(sapro.transpose)
# cluster.name = veg.group.soil[[3]]
# func.name = "r.g"
# repeats = 1
# plot.please = FALSE
# quiet = FALSE