data(finch.ind)
oldpar<-par()
####
#The function com.index allow to choose your own function
#(like mean, range, variance...) to calculate customize index.
require(e1071)
funct<-c("mean(x, na.rm=TRUE)", "kurtosis(x, na.rm=TRUE)",
"max(x, na.rm=TRUE) - min(x, na.rm=TRUE)", "CVNND(x)" )
res.finch.sp_mn2<-com.index(traits=traits.finch, index=funct,
sp=sp.finch, nullmodels=c(2,2,2,2), ind.plot=ind.plot.finch,
nperm=9, print=FALSE)
res.finch.sp_mn3<-com.index(traits=traits.finch, index=funct,
sp=sp.finch, nullmodels=c(3,3,3,3), ind.plot=ind.plot.finch,
nperm=9, print=FALSE)
####
#We can represent Standardized Effect Size (ses)
#using the function plot(as.listofindex(list1, list2, list3))
list.ind2<-list(res.finch.sp_mn2, res.finch.sp_mn3)
index.list2<-as.listofindex(list.ind2)
plot(index.list2, type="bytraits")
plot(index.list2)
####
#This allows to calcul index per site
#for example using "tapply(x, sites, mean)".
funct<-c("tapply(x, ind.plot.finch, function(x) mean(x, na.rm=TRUE))",
"tapply(x, ind.plot.finch, function(x) kurtosis(x, na.rm=TRUE))",
"tapply(x, ind.plot.finch, function(x) max(x, na.rm=TRUE) -
min(x, na.rm=TRUE) )", "tapply(x, ind.plot.finch, function(x)
CVNND(x))" )
##Null model 1 is trivial for this function
#because randomisation is within community only
res.finch.ind_mn1<-com.index(traits=traits.finch, index=funct,
sp=sp.finch, nullmodels=c(1,1,1,1), ind.plot=ind.plot.finch,
nperm=9, print=FALSE)
res.finch.ind_mn2<-com.index(traits=traits.finch, index=funct,
sp=sp.finch, nullmodels=c(2,2,2,2), ind.plot=ind.plot.finch,
nperm=9, print=FALSE)
####
#We can calcul metrics with or without intraspecific variance.
#Calculation of trait averages per population
#(name_sp_site is a name of a population)
#like in the function com.index
#and determine the site for each population (sites_bypop)
name_sp_sites=paste(sp.finch, ind.plot.finch, sep="_")
traits.by.pop<-apply(traits.finch, 2 , function (x)
tapply(x, name_sp_sites, mean , na.rm=TRUE))
sites_bypop<-lapply(strsplit(paste(rownames(traits.by.pop), sep="_"),
split="_"), function(x) x[3])
funct.withoutIV<-c("tapply(x, unlist(sites_bypop), function(x)
mean(x, na.rm=TRUE))", "tapply(x, unlist(sites_bypop), function(x)
kurtosis(x, na.rm=TRUE))", "tapply(x, unlist(sites_bypop), function(x)
max(x, na.rm=TRUE) - min(x, na.rm=TRUE) )",
"tapply(x, unlist(sites_bypop), function(x) CVNND(x))" )
funct.withIV<-c("tapply(x, ind.plot.finch, function(x)
mean(x, na.rm=TRUE))", "tapply(x, ind.plot.finch, function(x)
kurtosis(x, na.rm=TRUE))", "tapply(x, ind.plot.finch, function(x)
max(x, na.rm=TRUE) - min(x, na.rm=TRUE) )",
"tapply(x, ind.plot.finch, function(x) CVNND(x))" )
res.finch.withIV<-com.index(traits=traits.finch, index=funct.withIV,
sp=sp.finch, nullmodels=c(2,2,2,2), ind.plot=ind.plot.finch,
nperm=9, print=FALSE)
res.finch.withoutIV<-com.index(traits=traits.finch, index=funct.withoutIV,
sp=sp.finch, nullmodels=c(3,3,3,3), ind.plot=ind.plot.finch,
nperm=9, print=FALSE)
####
#We can also represent T-statistics and custom index thanks to
#the plot.listofindex function.
res.finch<-Tstats(traits.finch, ind_plot=ind.plot.finch, sp=sp.finch,
nperm=9, print=FALSE)
list.ind<-list(res.finch.withIV, res.finch.withoutIV ,res.finch)
index.list1<-as.listofindex(list.ind, namesindex=c("mean", "kurtosis",
"range", "CVNND", "mean.pop", "kurtosis.pop", "range.pop", "CVNND.pop",
"T_IP.IC", "T_IC.IR", "T_PC.PR"))
class(index.list1)
par(mfrow=c(2,3))
plot(index.list1,type="bytraits", bysite=TRUE)
par(mfrow=c(2,2))
plot(index.list1,type="bytraits")
par(mfrow=c(1,1))
plot(index.list1,type="simple")
plot(index.list1,type="simple_range")
plot(index.list1,type="normal")
plot(index.list1,type="barplot")
############################
####Using ind.value=FALSE and community data matrix if there is no data
#available at the individual level.
#create traits data at the species level
traits_by_sp<-apply(traits.finch,2,function(x) tapply(x,sp.finch,
function(x) mean(x, na.rm=T)))
#create traits data at the populational level
names_sp_ind_plot<-as.factor(paste(sp.finch, ind.plot.finch, sep="@"))
traits_by_pop<-apply(traits.finch,2,function(x) tapply(x,names_sp_ind_plot,
function(x) mean(x, na.rm=T) ))
#create community data matrix at the species or populational level
w1<-table(sp.finch,ind.plot.finch)
dim(w1)
dim(traits_by_sp)
w2<-table(names_sp_ind_plot,ind.plot.finch)
dim(w2)
dim(traits_by_pop)
#Choose indices
require(e1071)
funct<-c("mean(x, na.rm=TRUE)", "kurtosis(x, na.rm=TRUE)",
"max(x, na.rm=TRUE) - min(x, na.rm=TRUE)", "CVNND(x)" )
#################
#with species value
res<-ab_to_ind(traits_by_sp, w1)
com.index(traits_by_sp, nullmodels=2, ind.value=FALSE, index=funct,
sp=rownames(traits_by_sp), com=w1, nperm=9)
#################
#with population value
res<-ab_to_ind(traits_by_pop, w2)
sp.sp<-unlist(strsplit(rownames(traits_by_pop),"@"))[seq(1,39*2,2)]
com.index(traits_by_pop, nullmodels=2, ind.value=FALSE, index=funct,
sp=sp.sp, com=w2)
Run the code above in your browser using DataLab