# Example 1; Fit an unequal tolerances model to the hunting spiders data
data(hspider)
hspider[,1:6]=scale(hspider[,1:6]) # Standardize the environmental variables
p1 = cqo(cbind(Alopacce, Alopcune, Alopfabr, Arctlute, Arctperi, Auloalbi,
Pardlugu, Pardmont, Pardnigr, Pardpull, Trocterr, Zoraspin) ~
WaterCon + BareSand + FallTwig + CoveMoss + CoveHerb + ReflLux,
fam=poissonff, data=hspider, Crow1positive=FALSE, ITol=FALSE)
sort(p1@misc$deviance.Bestof) # A history of all the iterations
if(deviance(p1) > 1177) stop("suboptimal fit obtained")
S = ncol(p1@y) # Number of species
clr = (1:(S+1))[-7] # omits yellow
lvplot(p1, y=TRUE, lcol=clr, pch=1:S, pcol=clr, las=1) # ordination diagram
legend("topright", leg=dimnames(p1@y)[[2]], col=clr,
pch=1:S, merge=TRUE, bty="n", lty=1:S, lwd=2)
(cp = Coef(p1))
(a = cp@lv[cp@lvOrder]) # The ordered site scores along the gradient
# Names of the ordered sites along the gradient:
rownames(cp@lv)[cp@lvOrder]
(a = (cp@Optimum)[,cp@OptimumOrder]) # The ordered optima along the gradient
a = a[!is.na(a)] # Delete the species that is not unimodal
names(a) # Names of the ordered optima along the gradient
trplot(p1, whichSpecies=1:3, log="xy", type="b", lty=1, lwd=2,
col=c("blue","red","green"), label=TRUE) -> ii # trajectory plot
legend(0.00005, 0.3, paste(ii$species[,1], ii$species[,2], sep=" and "),
lwd=2, lty=1, col=c("blue","red","green"))
abline(a=0, b=1, lty="dashed")
S = ncol(p1@y) # Number of species
clr = (1:(S+1))[-7] # omits yellow
persp(p1, col=clr, label=TRUE, las=1) # perspective plot
# Example 2: A rank-2 equal tolerances CQO model with Poisson data
# This example is numerically fraught.
set.seed(555)
p2 = cqo(cbind(Alopacce, Alopcune, Alopfabr, Arctlute, Arctperi, Auloalbi,
Pardlugu, Pardmont, Pardnigr, Pardpull, Trocterr, Zoraspin) ~
WaterCon + BareSand + FallTwig + CoveMoss + CoveHerb + ReflLux,
fam=poissonff, data=hspider, Crow1positive=FALSE,
# ITol=FALSE, EqualTol=TRUE,
Rank=2, Bestof=1, isdlv=c(2.1,0.9))
sort(p2@misc$deviance.Bestof) # A history of all the iterations
if(deviance(p2) > 1127) stop("suboptimal fit obtained")
lvplot(p2, ellips=FALSE, label=TRUE, xlim=c(-3,4),
C=TRUE, Ccol="brown", sites=TRUE, scol="grey",
pcol="blue", pch="+", chull=TRUE, ccol="grey")
# Example 3: species packing model with presence/absence data
n = 200; p = 5; S = 5
mydata = rcqo(n, p, S, fam="binomial", hiabundance=4,
EqualTol=TRUE, ESOpt=TRUE, EqualMax=TRUE)
myform = attr(mydata, "formula")
b1 = cqo(myform, fam=binomialff(mv=TRUE, link="cloglog"), data=mydata)
sort(b1@misc$deviance.Bestof) # A history of all the iterations
lvplot(b1, y=TRUE, lcol=1:S, pch=1:S, pcol=1:S, las=1)
Coef(b1)
# Compare the fitted model with the 'truth'
cbind(truth=attr(mydata, "ccoefficients"), fitted=ccoef(b1))
Run the code above in your browser using DataLab