-
Notifications
You must be signed in to change notification settings - Fork 10
/
algorithms.R
620 lines (524 loc) · 27.6 KB
/
algorithms.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
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
#' Vector Sample Function
#' @description Takes an input binary vector and a weight vector.
#' Reassigns 1s randomly in proportion to vector weights.
#' @export
#' @details This function takes an input vector of binary presence-absence
#' values and a vector of non-negative probability weights. Both vectors
#' must be of identical length.
#' @param speciesData binary vector representing species presences and
#' absences.
#' @param weights a vector of non-negative read numbers representing
#' probabilistic weights for species occurrencs of the same length
#' as speciesData.
#' @return Returns a re-ordered binary vector in which the occurrences
#' are placed in cells with probabilities proportional to values given
#' in weights.
#' @references Gotelli, N.J., G.R. Graves, and C. Rahbek. 2010. Macroecological
#' signals of species interactions in the Danish avifauna. Proceedings of the
#' National Academy of Sciences, U.S.A. 107: 530-535.
#' @note Several of the randomization algorithms use this function
#' to assign species occurrences with probabilities that reflect
#' species or site differences. It is an effective method for
#' conditioning the marginal probabilities of a null matrix on
#' independent measurements of site or species characteristics.
#' @seealso \code{\link{sim10}} randomization algorithm.
#' @examples
#' myColonizer <- vector_sample(speciesData=rbinom(10,1,0.5),weights=runif(10))
#' @export
vector_sample <- function(speciesData,weights)
{
x <- mat.or.vec(length(speciesData),1)
x[sample(1:length(speciesData),size=sum(speciesData),prob=weights)] <- 1
return(x)
}
#' Sim1 Co-occurrence Randomization Algorithm
#' @description Randomizes a binary matrix speciesData by reshuffling
#' all of its elements equiprobably.
#' @details This algorithm assumes species and sites are equiprobable.
#' It preserves the total matrix fill, but places no other constraints on
#' row or column totals.
#' @param speciesData binary presence-absence matrix
#' (rows = species, columns = sites).
#' @return Returns a binary presence-absence matrix with the same
#' dimensions and percent fill as the input matrix.
#' @references Gotelli, N.J. 2000. Null model analysis of species co-occurrence
#' patterns. Ecology 81: 2606-2621.
#' @note This is the simplest of all randomization algorithms for a presence-
#' absence matrix. However, it assumes that both species and sites are
#' equiprobable, and has poor Type I error frequencies when tested with
#' purely random matrices. If the input matrix is sparse, it will often
#' generate null matrices with empty rows or columns. Not recommended for
#' co-occurrence analysis.
#' @examples
#' randomMatrix <- sim1(speciesData=matrix(rbinom(40,1,0.5),nrow=8))
#' @export
sim1 <- function(speciesData)
{
matrix(sample(speciesData), ncol=ncol(speciesData))
}
#' Sim2 Co-occurrence Randomization Algorithm
#' @description Randomizes a binary matrix speciesData by reshuffling elements
#' within each row equiprobably.
#' @details This algorithm assumes sites are equiprobable, but preserves
#' differences among species (= row sums).
#' @param speciesData binary presence-absence matrix
#' (rows = species, columns = sites).
#' @return Returns a binary presence-absence matrix with the same
#' dimensions and rowsums as the input matrix.
#' @references Gotelli, N.J. 2000. Null model analysis of species co-occurrence
#' patterns. ecology 81: 2606-2621.
#' @note This algorithm preserves differences in the commonness and rarity of
#' species (= rowsums), but assumes that all sites are equiprobable. It would
#' not be appropriate for islands that vary greatly in area, but it would be
#' appropriate for quadrat censuses in a relatively homogeneous environment.
#' sim2 can sometimes generate matrices with empty columns, but this is
#' unlikely unless the matrix is very sparse. sim2 has good Type I error
#' frequenceis when tested against random matrices. However, if sites do vary
#' in their suitability or habitat quality, it will often identify aggregated
#' patterns of species co-occurrence. sim2 and sim9 have the best overall
#' performance for species co-occurrence analyses. However, because they differ
#' in their assumptions about site quality, they often differ in their results,
#' with sim9 often detecting random or segregated patterns for matrices in
#' which sim2 detects aggregated patterns.
#' @seealso \code{\link{sim9}} co-occurrence algorithm.
#' @examples
#' randomMatrix <- sim2(speciesData=matrix(rbinom(40,1,0.5),nrow=8))
#' @export
sim2 <- function(speciesData)
{
t(apply(speciesData,1,sample))
}
#' Sim3 Co-occurrence Randomization Algorithm
#' @description Randomizes a binary matrix speciesData by reshuffling elements
#' within each column equiprobably.
#' @details This algorithm assumes species are equiprobable, but preserves
#' differences among sites (= column sums).
#' @param speciesData binary presence-absence matrix
#' (rows = species, columns = sites).
#' @return Returns a binary presence-absence matrix with the same
#' dimensions and colsums as the input matrix.
#' @references Gotelli, N.J. 2000. Null model analysis of species co-occurrence
#' patterns. Ecology 81: 2606-2621.
#' @note This algorithm preserves differences in species richness among sites
#' (= colsums), but assumes that all species are equiprobable. This assumption
#' is usually unrealistic, and sim3 has a high frequency of Type I errors with
#' random matrices, so it is not recommended for co-occurrence analysis.
#' @examples
#' randomMatrix <- sim3(speciesData=matrix(rbinom(40,1,0.5),nrow=8))
#' @export
sim3 <- function(speciesData)
{
apply(speciesData,2,sample)
}
#' Sim4 Co-occurrence Randomization Algorithm
#' @description Randomizes a binary matrix speciesData by reshuffling elements
#' within each row. Sampling weights for each column are proportional to
#' column sums. Makes a call to the vector_sample function.
#' @details This algorithm preserves differences among species in occurrence
#' frequencies, but assumes differences among sites in suitability are
#' proportional to observed species richness (= column sums).
#' @param speciesData binary presence-absence matrix
#' (rows = species, columns = sites).
#' @return Returns a binary presence-absence matrix with the same
#' dimensions and rowsums as the input matrix.
#' @references Gotelli, N.J. 2000. Null model analysis of species co-occurrence
#' patterns. Ecology 81: 2606-2621.
#' @note This algorithm preserves differences in the commonness and rarity of
#' species (= rowsums), but assumes differences among sites in suitability are
#' proportional to observed species richness (= column sums). sim4 has a
#' somewhat high frequency of Type I errors with random matrices, so it is not
#' recommended for co-occurrence analysis.
#' @examples
#' randomMatrix <- sim4(speciesData = matrix(rbinom(40,1,0.5),nrow=8))
#' @export
sim4 <- function(speciesData)
{
t(apply(speciesData,1,vector_sample,weights=colSums(speciesData)))
}
#' Sim5 Co-occurrence Randomization Algorithm
#' @description Randomizes a binary matrix speciesData by reshuffling elements
#' within each column. Sampling weights for each row are proportional to
#' row sums. Makes a call to the vector_sample function.
#' @details This algorithm preserves differences among sites in species
#' richness, but assumes differences among species in commonness and rarity
#' are proportional to observed species occurrences (= row sums).
#' @param speciesData binary presence-absence matrix
#' (rows = species, columns = sites).
#' @return Returns a binary presence-absence matrix with the same
#' dimensions and colsums as the input matrix.
#' @references Gotelli, N.J. 2000. Null model analysis of species co-occurrence
#' patterns. Ecology 81: 2606-2621.
#' @note This algorithm preserves differences among sites in species richness
#' (= colsums), but assumes differences among species in commonness and rarity
#' are proportional to observed species occurrences (= rowsums). sim5 has a
#' high frequency of Type I errors with random matrices, so it is not
#' recommended for co-occurrence analysis.
#' @examples
#' randomMatrix <- sim5(speciesData = matrix(rbinom(40,1,0.5),nrow=8))
#' @export
sim5 <- function(speciesData)
{
apply(speciesData,2,vector_sample,weights=rowSums(speciesData))
}
#' Sim6 Co-occurrence Randomization Algorithm
#' @description Randomizes a binary matrix speciesData by reshuffling all
#' elements. Rows are equiprobable, and columns are proportional to column sums.
#' Makes a call to the vector_sample function.
#' @details This algorithm assumes that species are equiprobable, but that
#' differences in suitability among sites are proportional to observed species
#' richness (=colsums).
#' @param speciesData binary presence-absence matrix
#' (rows = species, columns = sites).
#' @return Returns a binary presence-absence matrix with the same
#' dimensions and fill as the input matrix.
#' @references Gotelli, N.J. 2000. Null model analysis of species co-occurrence
#' patterns. Ecology 81: 2606-2621.
#' @note This algorithm assumes that species are equiprobable, and that
#' differences among sites are proportional to observed species richness
#' (=colsums). sim6 has a high frequency of Type I errors with random matrices,
#' so it is not recommended for co-occurrence analysis.
#' @examples
#' randomMatrix <- sim6(speciesData = matrix(rbinom(40,1,0.5),nrow=8))
#' @export
sim6 <- function(speciesData)
{
matrixWeights <- outer(rep(1,nrow(speciesData)),colSums(speciesData))
out <-matrix(vector_sample(speciesData, weights=matrixWeights),ncol=ncol(speciesData))
}
#' Sim7 Co-occurrence Randomization Algorithm
#' @description Randomizes a binary matrix speciesData by reshuffling all
#' elements. Columns are equiprobable, and rows are proportional to row sums.
#' Makes a call to the vector_sample function.
#' @details This algorithm assumes that sites are equiprobable, but that
#' differences in frequency of occurrence among species are proportional to
#' observed species richness (=colsums).
#' @param speciesData binary presence-absence matrix
#' (rows = species, columns = sites).
#' @return Returns a binary presence-absence matrix with the same
#' dimensions and fill as the input matrix.
#' @references Gotelli, N.J. 2000. Null model analysis of species co-occurrence
#' patterns. Ecology 81: 2606-2621.
#' @note This algorithm assumes that species are equiprobable, and that
#' differences among sites are proportional to observed species richness
#' (=colsums). sim7 has a high frequency of Type I errors with random matrices,
#' so it is not recommended for co-occurrence analysis.
#' @examples
#' randomMatrix <- sim7(speciesData = matrix(rbinom(40,1,0.5),nrow=8))
#' @export
sim7 <- function(speciesData)
{
matrixWeights <- outer(rowSums(speciesData),rep(1,ncol(speciesData)))
matrix(vector_sample(speciesData, weights=matrixWeights),ncol=ncol(speciesData))
}
#' Sim8 Co-occurrence Randomization Algorithm
#' @description Randomizes a binary matrix speciesData by reshuffling all
#' elements. Columns are proportional to column sums, and rows are proportional
#' to row sums. Makes a call to the vector_sample function.
#' @details This algorithm assumes that the probability that a species occurs
#' in a site is depends on the joint independent probability of randomly
#' selecting the species and randomly selecting the site, with these
#' probabilities set proportional to row and column sums of the matrix.
#' @param speciesData binary presence-absence matrix
#' (rows = species, columns = sites).
#' @return Returns a binary presence-absence matrix with the same
#' dimensions and fill as the input matrix.
#' @references Gotelli, N.J. 2000. Null model analysis of species co-occurrence
#' patterns. Ecology 81: 2606-2621.
#'
#' Ulrich, W. and N.J. Gotelli. 2012. A null model algorithm for presence-
#' absence matrices based on proportional resampling. Ecological Modelling
#' 244:20-27.
#' @note This algorithm is theoretically attractive because it incorporates
#' heterogeneity in species occurrences and species richness per site in a
#' probabilistic way that does not fix row and column frquencies. However,
#' in spite of its appeal, sim8 does not generate average row and column sums
#' that match the original matrix, and it is susceptible to Type I errors when
#' tested with random matrices. It is not recommended for co-occurrence
#' analysis. See Ulrich and Gotelli (2012) for a more complicated algorithm
#' for probabilistic row and column totals that has better statistical behavior.
#' @examples
#' randomMatrix <- sim8(speciesData = matrix(rbinom(40,1,0.5),nrow=8))
#' @export
sim8 <- function(speciesData)
{
matrixWeights <- outer(rowSums(speciesData),colSums(speciesData))
matrix(vector_sample(speciesData,weights=matrixWeights),ncol=ncol(speciesData))
}
#' Sim10 Co-occurrence Randomization Algorithm
#' @description Randomizes a binary matrix speciesData by reshuffling all
#' elements. Rows and column probabilities are proportional to user-supplied
#' row and column weights, which define relative suitability probabilities for
#' species and sites. Makes a call to the vector_sample function.
#' @details This function incorporates vectors of weights for species and/or
#' sites to condition the simulation. These two vectors are used as outer
#' products to set cell probabilities for the entire matrix. Thus:
#' \deqn{p(cell_{ij})=p(row_i)p(col_j)}{p(cell_ij)=p(row_i)p(col_j)}
#' Weights must be positive real numbers. The algorithm will scale them so they
#' sum to 1.0, so they can be used in their natural units (e.g. island area,
#' species abudance), and will be scaled properly. If all species (or sites)
#' are assumed to be equally likely, the weight vector should be set to the
#' same constant for all elements.
#' @param speciesData binary presence-absence matrix
#' (rows = species, columns = sites).
#' @param rowWeights vector of positive values representing species weights.
#' @param colWeights vector of positive values representing site weights.
#' @return Returns a binary presence-absence matrix with the same
#' dimensions and fill as the input matrix.
#' @references Jenkins, D.G. 2006. In search of quorum effects in metacommunity
#' structure: species co-occurrence analyses. Ecology 87:1523-1531
#'
#' Gotelli, N.J., G.R. Graves, and C. Rahbek. 2010. Macroecological
#' signals of species interactions in the Danish avifauna. Proceedings of the
#' National Academy of Sciences, U.S.A. 107: 530-535.
#' @note sim10 allows users to incorporate independent data on species
#' occurrence probabilities and site suitabilities. This represents an
#' important conceptual advance over standard co-occurrence analyses, which
#' must infer these probabilities from the matrix itself. sim10 may generate
#' empty rows or columns, especially if weights are very small for some species
#' or sites. Also, the results may be sensitive to algebraic transformations
#' of the weights (x, x^2, log(x), etc.), and these transformations may be hard
#' to justify biologically. Nevertheless, sim10 is worth exploring for rich
#' data sets with site and species attributes.
#' @seealso \code{\link{vector_sample}} for weighted vector sampling.
#' @examples
#' randomMatrix <- sim10(speciesData=matrix(rbinom(40,1,0.5),nrow=8))
#' @export
sim10 <- function(speciesData,rowWeights = runif(dim(speciesData)[1]),colWeights = runif(dim(speciesData)[2]))
{
matrixWeights <- outer(rowWeights,colWeights)
matrix(vector_sample(speciesData, weights =matrixWeights),ncol=ncol(speciesData))
}
#' RA1 Niche Overlap Randomization Algorithm
#' @description Randomizes a numeric utilization matrix speciesData by
#' replacing all elements with a random uniform [0,1] value.
#' @details The resource utilization matrix (rows = species, columns = discrete
#' resource categories) may include zeroes, but no negative numbers or missing
#' values. Relative resource within a species is first calculated, so the rows
#' need not sum to 1.0.
#' @param speciesData a resource utilization matrix (rows = species, columns = discrete
#' resource states) filled with non-negative real numbers.
#' @return Returns a random utilization matrix with the same dimensions as the
#' input matrix.
#' @references Kobayashi, S. 1991. Interspecific relations in forest floor
#' coleopteran assemblages: niche overlap and guild structure. Researches
#' in Population Ecology 33: 345-360.
#' @note Because all matrix elements, including zeroes, are replaced with a
#' random uniform distribution, the null expectation is based on an assemblage
#' of generalist species with maximum niche breadth. This algorithm retains
#' neither the niche breadth of the individuals species nor the placement of 0
#' values (= unutilized resource states) in the matrix. These assumptions are
#' unrealistic, and a random matrix with zeroes will generate significantly low
#' niche overlap values with this metric. It is not recommended for niche
#' overlap analysis.
#' @examples
#' ranUtil <- ra1(speciesData=matrix(rpois(40,0.5),nrow=8))
#' @export
ra1 <- function(speciesData=matrix(rpois(80,1),nrow=10)){
matrix(runif(prod(dim(speciesData))),ncol=ncol(speciesData))
}
#' RA2 Niche Overlap Randomization Algorithm
#' @description Randomizes a numeric utilization matrix speciesData by
#' replacing all non-zero elements with a random uniform [0,1] value.
#' @description Randomizes a numeric utilization matrix speciesData by
#' replacing all elements with a random uniform [0,1].
#' @details The resource utilization matrix (rows = species, columns = discrete
#' resource categories) may include zeroes, but no negative numbers or missing
#' values. Relative resource within a species is first calculated, so the rows
#' need not sum to 1.0.
#' @param speciesData a resource utilization matrix (rows = species, columns = discrete
#' resource states) filled with non-negative real numbers.
#' @return Returns a random utilization matrix with the same dimensions as the
#' input matrix.
#' @references Kobayashi, S. 1991. Interspecific relations in forest floor
#' coleopteran assemblages: niche overlap and guild structure. Researches
#' in Population Ecology 33: 345-360.
#'
#' Winemiller, K.O. and E.R. Pianka. 1990. Organization in natural
#' assemblages of desert lizards and tropical fishes. Ecological Monographs
#' 60: 27-55.
#' @note This algorithm retains the number and position of zero states in the
#' original matrix. However, all non-zero values are again replaced by a random
#' [0,1] value, which tends to inflate niche breadths of the simulated
#' assemblage. Although the results are not as severe as for RA1, this
#' algorithm is still prone to Type I errors, and is not recommended for niche
#' overlap analysis.
#' @examples
#' ranUtil <- ra2(speciesData=matrix(rpois(40,0.5),nrow=8))
#' @export
ra2 <- function(speciesData=matrix(rpois(80,1),nrow=10))
{
z <- which(speciesData > 0)
RM <- speciesData
RM[z] <- runif(length(z))
return(RM)
}
#' RA3 Niche Overlap Randomization Algorithm
#' @description Randomizes a numeric utilization matrix speciesData by
#' reshuffling the elements within each row.
#' @details The resource utilization matrix (rows = species, columns = discrete
#' resource categories) may include zeroes, but no negative numbers or missing
#' values. Relative resource within a species is first calculated, so the rows
#' need not sum to 1.0.
#' @param speciesData a resource utilization matrix (rows = species, columns = discrete
#' resource states) filled with non-negative real numbers.
#' @return Returns a random utilization matrix with the same dimensions as the
#' input matrix.
#' @references Winemiller, K.O. and E.R. Pianka. 1990. Organization in natural
#' assemblages of desert lizards and tropical fishes. Ecological Monographs
#' 60: 27-55.
#' @note This algorithm retains the niche breadth and zero states for each
#' species, but randomizes the assignment of each utilization value to a
#' different niche category. It performs effectively in simulation studies and
#' is recommended for analysis of niche overlap patterns.
#' @examples
#' ranUtil <- ra3(speciesData=matrix(rpois(40,0.5),nrow=8))
#' @export
ra3 <- function(speciesData=matrix(rpois(80,1),nrow=10))
{
RM <- apply(speciesData,1,sample)
RM <- t(as.matrix(RM,nrow=nrow(speciesData),ncol=ncol(speciesData)))
return(RM)
}
#' RA4 Niche Overlap Randomization Algorithm
#' @description Randomizes a numeric utilization matrix speciesData by
#' reshuffling the non-zero elements within each row.
#' @details The resource utilization matrix (rows = species, columns = discrete
#' resource categories) may include zeroes, but no negative numbers or missing
#' values. Relative resource within a species is first calculated, so the rows
#' need not sum to 1.0.
#' @param speciesData a resource utilization matrix (rows = species, columns = discrete
#' resource states) filled with non-negative real numbers.
#' @return Returns a random utilization matrix with the same dimensions as the
#' input matrix.
#' @references Winemiller, K.O. and E.R. Pianka. 1990. Organization in natural
#' assemblages of desert lizards and tropical fishes. Ecological Monographs
#' 60: 27-55.
#' @note This algorithm is similar to RA3, but adds the additional constraint of
#' retaining the positions of all of the zero elements of the matrix, and
#' reshuffling only the non-zero elements of the matrix within each row. It is
#' more conservative than RA3, but has a low Type I error rate, and, along with
#' RA3, is recommended for null model analysis of niche overlap.
#' @examples
#' ranUtil <- ra4(speciesData=matrix(rpois(40,0.5),nrow=8))
#' @export
ra4 <- function(speciesData=matrix(rpois(80,1),nrow=10))
{
#.....................................
## Nonzero row shuffle function for RA4
NonZeroRowShuffle <- function(vec=runif(10)) {
nonzero <- which(vec > 0)
shuffledvec <- vec
shuffledvec[nonzero] <- vec[sample(nonzero)]
return(shuffledvec)
}
#....................................
RM <- t(apply(speciesData,1,NonZeroRowShuffle))
return(RM)
}
#' SizeUniform Size Overlap Randomization Algorithm
#' @description Function to randomize body sizes within a uniform distribution
#' with boundaries set by the largest and smallest species in the assemblage.
#' @details If the assemblage contains n species,
#' only the body sizes of the inner n - 2 species are randomized.
#' @param speciesData a vector of positive real values representing the body
#' sizes or trait values for each species.
#' @return Returns a vector of body sizes that have been randomly assigned. The
#' largest and smallest body sizes in the randomized assemblage match those in
#' the empirical data.
#' @references Simberloff, D. and W. Boecklen. 1981. Santa Rosalia
#' reconsidered: size ratios and competition. Evolution 35: 1206-1228.
#'
#' Tonkyn, D.W. and B.J. Cole. 1986. The statistical analysis of size ratios.
#' American Naturalist 128: 66-81.
#' @note Although the distribution of body sizes may not be truly uniform,
#' it may be approximately uniform within the range of observed values,
#' particularly for small assemblages.
#' @seealso \code{\link{size_gamma}} size distribution function.
#' @examples
#' nullSizes <-size_uniform(speciesData=runif(20))
#'@export
size_uniform <- function(speciesData=runif(20)) {
endpoints <- c(min(speciesData),max(speciesData)) # save max and min boundaries
sim <- runif(n=(length(speciesData)-2),min=endpoints[1],max=endpoints[2])
randomVec <- c(endpoints,sim)
return(randomVec)
}
#' SizeUser Size Overlap Randomization Algorithm
#' @description Observed body sizes are randomized with a uniform distribution
#' for which the user has defined the minimum and maximum possible body size.
#' @details Within the user-defined limits, body sizes of all n species are
#' randomized, whereas uniform_size randomizes only n - 2 of the body
#' sizes and uses the extreme values to set the endpoints.
#' @param speciesData a vector of observed body sizes.
#' @param userLow a user-defined lower limit.
#' @param userHigh a user-defined upper limit.
#' @return Returns a vector of randomized body sizes.
#' @note As the difference between the lower and upper boundaries is increased
#' the test will yield results that are random or aggregated, even though the
#' same data might yield a segregated pattern when the uniform_size algorithm
#' is used. For this reason, this algorithm is not recommended for size ratio
#' analyses.
#' @seealso \code{\link{size_uniform}} size distribution algorithm.
#' @examples
#' nullSizes <- size_uniform_user(speciesData=runif(20,min=10,max=20),userLow=8,userHigh=24)
#' @export
size_uniform_user <- function(speciesData=runif(n=20),userLow=0.9*min(speciesData),
userHigh=1.1*max(speciesData)){
# if(!is.null(Param.List$Special)){User.low <- Param.List$Special[1]
# User.high <- Param.List$Special[2]}
randomVec <- runif(n=length(speciesData),min=userLow,max=userHigh)
return(randomVec)
}
#' SizeSourcePoolDraw Size Overlap Randomization Algorithm
#' @description Function to randomize body sizes by drawing species from a
#' user-defined source pool. Species are drawn without replacement,
#' and there is a specified probability vector for the source pool species
#' @param speciesData a vector of observed body sizes.
#' @param sourcePool a vector of body sizes of species in the user-defined
#' pool of potential colonists.
#' @param speciesProbs a vector of relative colonization weights of
#' length 'sourcePool'.
#' @return Returns a vector of body sizes of an assemblage randomly drawn
#' from a user-defined source pool.
#' @references Strong, D.R. Jr., L.A. Szyska, and D. Simberloff. 1979. Tests of
#' community-wide character displacement against null hypotheses. Evolution 33:
#' 897-913.
#' Schluter, D. and P.R. Grant. 1984. Determinants of morphological patterns in
#' communities of Darwin's finches. American Naturalist 123: 175-196.
#' @note Although delineating a source pool of species and estimating their
#' relative colonization probabilities is difficult, this is the most realistic
#' approach to constructing a null distribution.
#' @examples
#' obsOverlap <- size_source_pool(dataRodents$Sonoran)
#' @export
size_source_pool <- function(speciesData=21:30,sourcePool=
runif(n=2*length(speciesData),min=10,max=50),
speciesProbs=rep(1,length(sourcePool))) {
speciesDraw <- sample(sourcePool,size=length(speciesData),replace=FALSE,
prob=speciesProbs)
return(speciesDraw)
}
#' SizeGamma Size Overlap Randomization Algorithm
#' @description Function to generate a random distribution of body sizes by
#' drawing from a gamma distribution. Shape and rate parameters of the gamma
#' are estimated from the empirical data.
#' @param speciesData a vector of body sizes or other trait measurements of
#' species. All values must be positive real numbers.
#' @return Returns a vector of simulated body sizes as the same length as speciesData.
#' @note The shape and rate parameters are estimated from the real data using
#' the maximum likelihood estimators generated from the fitdr function in
#' the MASS library. The flexible gamma distribution can be fit to a variety of
#' normal, log-normal, and exponential distributions that are typical for trait
#' data measured on a continuous non-negative scale.
#' @seealso \code{\link{fitdistr}} in the MASS library.
#' @examples
#' obsOverlap <- size_gamma(speciesData=rnorm(50,mean=100,sd=1))
#' @import MASS
#' @export
size_gamma <- function (speciesData=rnorm(50,mean=100,sd=1)) {
mleFit <- fitdistr(speciesData, 'gamma')
a <- mleFit$estimate["shape"]
b <- mleFit$estimate["rate"]
gammaDraw <- rgamma(length(speciesData),shape=a,rate=b)
return(gammaDraw)
}