diff --git a/R/format.function.R b/R/format.function.R index e352396d18379fc2062b78bb36d84e5177f48fd5..36cdf9e8e7e6008be9e1dc9c4788ff3833ec8a66 100644 --- a/R/format.function.R +++ b/R/format.function.R @@ -8,12 +8,12 @@ ###### ###### ## FUNCTION TO PLOT MAP OF TREE -fun.circles.plot <- function(plot.select,x,y,plot,D,inches){ +fun.circles.plot <- function(plot.select,x,y,plot,D,inches,...){ x.t <- x[plot==plot.select] y.t <- y[plot==plot.select] D.t <- D[plot==plot.select] D.t[is.na(D.t)] <- 0 -symbols(x.t,y.t,circles=D.t ,main=plot.select,inches=inches) +symbols(x.t,y.t,circles=D.t ,main=plot.select,inches=inches,...) } @@ -246,6 +246,35 @@ save(list.temp,file=paste("./data/process/list",name.country,ecoregion,"Rdata",s } +##################################### +##################################### +### FUNCTION TO COMPUTE BA.SP.XY PER PLOT AND MERGE TOGETHER +#### function to be apply per site +fun.compute.BA.SP.XY.per.plot <- function(i,data.tree,Rlim,xy.name=c('x','y'),parallel=FALSE,rpuDist=FALSE){ +data.tree.s <- subset(data.tree,subset=data.tree[["plot"]] ==i) +BA.SP.temp <- BA.SP.FUN.XY(obs.id=data.tree.s[['obs.id']], + xy.table=data.tree.s[,xy.name], + diam=data.tree.s[['D']], + sp=(data.tree.s[['sp']]), + Rlim=15, + parallel=FALSE, + rpuDist=FALSE) + +## replace NA per zero +print('replacing NA per zero') +BA.SP.temp[is.na(BA.SP.temp)] <- 0 +print('done') +### rpud installation very cumbersome not needed ? +### longer in parallel why ? +if(sum(! rownames(BA.SP.temp)==data.tree.s[['obs.id']]) >0) stop('rows not in the good order') +if(sum(!colnames(BA.SP.temp)==as.character((levels(data.tree.s[['sp']]))))>0) stop('colnames does mot match species name') + +### compute sum per row +BATOT <- apply(BA.SP.temp,MARGIN=1,FUN=sum) +data.res <- data.frame(obs.id=data.tree.s[['obs.id']],BA.SP.temp,BATOT=BATOT) +return(data.res) +} + diff --git a/merge.data.PARACOU.R b/merge.data.PARACOU.R index 41163e869094739565bedf6d73daa6497d6ade2c..ea53409455e4e5efc73db5cea21e1ad1b708ffbf 100644 --- a/merge.data.PARACOU.R +++ b/merge.data.PARACOU.R @@ -107,38 +107,12 @@ data.tree <- subset(data.paracou,select=c(vec.basic.var)) #,vec.abio.var.names ### species as factor because number data.tree[['sp']] <- factor(data.tree[['sp']]) +Rlim <- 15 # set size of neighborhood for competition index +system.time(test <- fun.compute.BA.SP.XY.per.plot(1,data.tree=data.tree,Rlim=15,parallel=TRUE,rpuDist=FALSE)) -#### function to be apply per site -fun.compute.BA.SP.XY.per.plot <- function(i,data.tree,Rlim,xy.name=c('x','y'),parallel=FALSE,rpuDist=FALSE){ -data.tree.s <- subset(data.tree,subset=data.tree[["plot"]] ==i) -BA.SP.temp <- BA.SP.FUN.XY(obs.id=data.tree.s[['obs.id']], - xy.table=data.tree.s[,xy.name], - diam=data.tree.s[['D']], - sp=(data.tree.s[['sp']]), - Rlim=15, - parallel=FALSE, - rpuDist=FALSE) - -## replace NA per zero -print('replacing NA per zero') -BA.SP.temp[is.na(BA.SP.temp)] <- 0 -print('done') -### rpud installation very cumbersome not needed ? -### longer in parallel why ? -if(sum(! rownames(BA.SP.temp)==data.tree.s[['obs.id']]) >0) stop('rows not in the good order') -if(sum(!colnames(BA.SP.temp)==as.character((levels(data.tree.s[['sp']]))))>0) stop('colnames does mot match species name') - -### compute sum per row -BATOT <- apply(BA.SP.temp,MARGIN=1,FUN=sum) -data.res <- data.frame(obs.id=data.tree.s[['obs.id']],BA.SP.temp,BATOT=BATOT) -return(data.res) -} - -test <- fun.compute.BA.SP.XY.per.plot(1,data.tree=data.tree,Rlim=15) - -list.BA.SP.data <- mclapply(unique(data.tree[['plot']]),FUN=fun.compute.BA.SP.XY.per.plot,data.tree=data.tree,Rlim=15,mc.cores=4) -data.BA.SP <- rbind.fill(list.BA.SP.data) +list.BA.SP.data <- mclapply(unique(data.tree[['plot']]),FUN=fun.compute.BA.SP.XY.per.plot,data.tree=data.tree,Rlim=Rlim,mc.cores=4) +data.BA.sp <- rbind.fill(list.BA.SP.data) dim(data.BA.SP) ### TEST DATA FORMAT @@ -148,9 +122,18 @@ if(sum(!colnames(BA.SP.temp)==as.character((levels(data.tree.s[['sp']]))))>0) st if(sum(!data.BA.SP[["obs.id"]] == data.tree[["obs.id"]]) >0) stop("competition index not in the same order than data.tree") ## REMOVE TREE IN BUFFER ZONE BUFFER ZONE +not.in.buffer.zone <- (data.tree[['x']]<(250-Rlim) & +data.tree[['x']]>(0+Rlim) & +data.tree[['y']]<(250-Rlim) & +data.tree[['y']]>(0+Rlim)) + +# remove subset +data.tree <- subset(data.tree,subset=not.in.buffer.zone) +data.BA.sp <- subset(data.BA.sp,subset=not.in.buffer.zone) + ## plot each plot pdf("./figs/plots.tree.pdf") -lapply(unique(data.tree[["plot"]]),FUN=fun.circles.plot,data.tree[['x']],data.tree[['y']],data.tree[["plot"]],data.tree[["D"]],inches=0.2) +lapply(unique(data.tree[["plot"]]),FUN=fun.circles.plot,data.tree[['x']],data.tree[['y']],data.tree[["plot"]],data.tree[["D"]],inches=0.2,xlim=c(0,250),ylim=c(0,250)) dev.off() @@ -162,16 +145,30 @@ dev.off() ### read species names species.clean <- read.csv("./data/raw/DataParacou/20130717_paracou_taxonomie.csv",stringsAsFactors=FALSE, header = T, sep = ";") +species.clean$sp <- species.clean[["idTaxon"]] + +## select species in paracou +species.paracou <- data.frame(sp=species.clean[as.character(species.clean[["sp"]]) %in% as.vector(na.exclude(unique(data.tree[["sp"]]))),c("sp")], + Latin_name=apply((species.clean[as.character(species.clean[["sp"]]) %in% as.vector(na.exclude(unique(data.tree[["sp"]]))) + ,c("Genre","Espece")]),MARGIN=1,FUN=paste,collapse=" ")) + +count.sp.paracou <- data.frame(sp=names(table(data.tree[["sp"]])),n.indiv=as.vector(table(data.tree[["sp"]]))) +species.paracou <- merge(species.paracou,count.sp.paracou,by="sp") + +tapply(species.paracou[["n.indiv"]],INDEX=species.paracou[["Latin_name"]],FUN=sum) + +length(grep("Indet",species.paracou[["Latin_name"]]))/nrow(species.paracou) ### need to read the different traits data based and merge ..... +bridge <- read.csv("./data/raw/DataParacou/BridgeDATA.g.csv",stringsAsFactors=FALSE, header = T, sep = ";") +bridge$Latin_name <- paste(bridge[["Genus"]],bridge[["species"]],sep=" ") +dataWD <- read.csv("./data/raw/DataParacou/WD-Species-Paracou-Ervan_GV.csv",stringsAsFactors=FALSE, header = T,sep=" ") +seed.traits <- read.csv("./data/raw/DataParacou/Autour de Paracou - Releves par trait et taxon.txt",stringsAsFactors=FALSE, header = T, sep = "\t") ## SPECIES CODE COME FROM idTaxon in paracou_taxonomie and taxonid in paracou_1984_2012 to match the traits data we need to use the "Genus species" ## we better work not work with vernacular because this doesn't match necesseraly the Genus species taxonomie -species.clean$sp <- species.clean[["idTaxon"]] -## data.paracou <- merge(data.paracou, as.data.frame(species.clean[!duplicated(species.clean[["sp"]]),c("Genre","Espece","sp")]), by = "sp", sort = FALSE) - ## save everything as a list