Description Usage Arguments Details Value Author(s) Examples
These function generate random community matrices, where species' abundances/occurrences and environmental relationships can be explicitly specified. Abundances are modelled by species specific models.
1 2 3 4 5 |
n |
The number of sites (rows) in the data matrix. |
spp |
The number of species (columns) in the data matrix. |
b |
Coefficients to be used for determining species'
environmental relationships. They can be a vector of length |
x |
A vector (if only intercepts are provided) or a model matrix
with |
type |
Type of model to be used. |
family |
Currently |
sigma |
Standard deviation of the Gaussian linear model ( |
cor |
Correlation matrix for sites. Diagonal should be 1, must be a symmetric square matrix. |
theta |
Scale parameter of the Gamma distribution used when |
attrib |
Logical, if attributes (call and seed) should be attached.
Can be a vector of length |
seed |
Random seed. |
... |
Further arguments. |
The linear model part for all functions is defined as
mu = x %*% b
. The link function from the family
is then used to link this systematic part with the random component of the model.
The GLMM versions use the mean (mu = x %*% b
)
and the variance covariance matrix
(defined by sigma
and cor
) to generate Multivariate
Normal random numbers.
The model matrix should be in the usual format, providing design
variables for factors. The formula interface of the
model.matrix
function can be applied (see Examples).
A community data matrix with n
rows and spp
columns.
Peter Solymos
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 | ## --- random landscape ---
## rnd seed setup
set.seed(1234)
## no. of sites (20x20 grid)
n <- 400
## Random landscape from B Bolker (http://www.zoology.ufl.edu/bolker/landsc.html)
## landsc R package available at http://www.zoology.ufl.edu/bolker
## randomize phase of a 2-D landscape
rland1 <- function(x) {
n <- nrow(x) # linear dimension
l <- length(x) # total number of nodes
y <- fft(x)
z <- matrix(complex(modulus=Mod(y),arg=runif(l,-pi,pi)))
y <- fft(z,inverse=TRUE)/l
return(matrix(Mod(y),nrow=n))
}
## given a correlation-by-(radial)-distance function r
## (which can be either a vector or a function)
## simulate a 2-D landscape with linear size n
## substitute into a grid of radial distances
simland2 <- function(r,n) {
g <- outer(1:n,1:n,function(x,y)sqrt(x^2+y^2))
if (is.function(r)) {
gr <- apply(g,c(1,2),r)
} else if (is.numeric(r)) {
gr <- matrix(r[round(g)],nrow=n) }
rland1(gr) # pass it to a function which FFTs, randomizes phases,
# and inverse-FFTs
}
## creates binary landscape
simbinland <- function(n, r, prob) {
r[r>1] <- 1
g2 <- simland2(r,n)
gg <- as.numeric(g2 >= quantile(g2, prob))
dim(gg) <- dim(g2)
gg
}
## actual landscape generation takes place here
lscape <- simbinland(20, exp(-(1:(2*n))/20)+0.1*rnorm(2*n), 0.5)
hab <- as.factor(array(lscape))
levels(hab) <- c("forest","meadow")
## hab: factor with 2 levels
## temp: temperature, N-S gradient + some rnd noise
## x, y: grid coordinates
x <- data.frame(expand.grid(x=1:20, y=1:20),
hab=hab, temp=rep(seq(-1,1,len=20), each=20) + rnorm(n, 0, 0.1))
## --- random species responses ---
## no. of species
m <- 20
## preferences for meadow level of hab (2 groups)
beta.meadow <- c(rnorm(m/2, -1, 0.5), rnorm(m/2, 1, 0.5))
## hate meadows: 1. and 3. quartiles
## love meadows: 2. and 4. quartiles
beta.meadow <- beta.meadow[c(1:5,11:15,6:10,16:20)]
## warm (1. and 2. quartiles) vs. cold requiring species(3. and 4. quartiles)
## unimodal relationship
bu <- c(rnorm(m/2, -0.5, 0.1), rnorm(m/2, 0.5, 0.2))
bh <- rlnorm(m, 0.8, 0.5)
bt <- rlnorm(m, -0.5, 0.2)
uht <- cbind(bu, bh, bt)
b <- t(cbind(t(apply(uht, 1, uht2beta)), beta.meadow))
## --- community matrix generation ---
phi <- runif(m, 0.5, 1.5)
## exponential spatial correlation: rho^d
Cor <- 0.25^as.matrix(dist(x[,1:2]))
diag(Cor) <- 1
## stratified sampling from 400 sites
id <- c(sample(which(x$hab=="forest"), 50), sample(which(x$hab=="meadow"), 50))
X <- model.matrix(~ temp + I(temp^2) + hab, x[id,])
ynb <- rspecies(length(id), m, b, X, type="glm.nb", theta=rnorm(m, 5))
y11 <- rspecies(length(id), m, b, X, type="glm", family=poisson)
y21 <- rspecies(length(id), m, b, X, type="glmm", family=poisson, sigma=1.5*phi)
y22 <- rspecies(length(id), m, b, X, type="glmm", family=poisson, sigma=1.5*phi, cor=Cor[id, id])
## --- exploratory examples ---
plot(0, type="n", ylim=c(0,10), xlim=c(-1,1))
apply(uht, 1, function(i) lines(seq(-1,1,len=100), respgau(seq(-1,1,len=100), i)))
if (require(vegan)) {
plot(cca(y11~temp+hab,x[id,]))
plot(hclust(vegdist(t(y11))))
adonis(y11~temp+hab,x[id,])
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.