## Let's simulate a multivariate response matrix of normally distributed data
## (normal data doesn't occur too often in ecology!)
library(mvtnorm)
true.lv <- rbind(rmvnorm(n=15,mean=c(1,2)),rmvnorm(n=15,mean=c(-3,-1)))
## 30 rows (sites) with two latent variables
lv.coefs <- cbind(matrix(runif(30*3),30,3),1)
## 30 columns (species)
X <- matrix(rnorm(30*4),30,4)
## 4 explanatory variables
X.coefs <- matrix(rnorm(30*4),30,4)
sim.y <- create.life(true.lv, lv.coefs, X, X.coefs, family = "normal")
fit.boral <- boral(sim.y, X = X, family = "normal", num.lv = 2,
site.eff = FALSE, save.model = FALSE)
lvsplot(fit.boral)
## Simulate a multivariate response matrix of ordinal data
true.lv <- rbind(rmvnorm(10,mean=c(-2,-2)),rmvnorm(10,mean=c(2,2)))
## 20 rows (sites) with two latent variables
true.lv.coefs <- rmvnorm(30,mean = rep(0,3));
## 30 columns (species)
true.lv.coefs[nrow(true.lv.coefs),1] <- -sum(true.lv.coefs[-nrow(true.lv.coefs),1])
## Impose a sum-to-zero constraint on the column effects
true.ordinal.cutoffs <- seq(-2,10,length=10-1)
## Cutoffs for proportional odds regression (must be in increasing order)
sim.y <- create.life(true.lv = true.lv, lv.coefs = true.lv.coefs,
family = "ordinal", cutoffs = true.ordinal.cutoffs)
fit.boral <- boral(y = sim.y, family = "ordinal", num.lv = 2, site.eff = F,
n.thin = 5, save.model = FALSE, calc.ics = TRUE)
Run the code above in your browser using DataLab