#########################################
#                                       #
#   Mean trophic level function         #
#                                       #
#        created:  23/12/2016           #
#          SURVEY data (EVHOE)_FR       #
#  All species & no Pelagic species     #
#                                       #
#                                       #
#########################################

#! FANCY AXIS FUNCTION
"fancyAxis" <- function(side, at, skip = 2, ...)
{
  axis(side, at = at, labels = FALSE)
  axis(side, at = at[ ( (1:length(at) %% skip ) == 1 )],
       labels = TRUE, tick = FALSE, ...)
}


TL<-read.table("C:/[...]/TL_landings&EVHOE_FR.csv",sep=";",dec=",",header=T)
biomass2<-read.table("C:/[...]/biomass file for all species in BoB continental shelf.csv",sep=";",dec=",",header=T)
biomass3<-read.table("C:/[...]/biomass file excluding pelagic species in BoB continental shelf.csv",sep=";",dec=",",header=T)
View(TL)
View(biomass2)
View(biomass3)


############################################################################################
###################  MTL_2.0 EVHOE survey   ####################################
############################################################################################
cutoff2<-2.0
rep<-500

biomass2$year<-as.factor(biomass2$year)
rownames(TL)<-TL$species
year<-unique(biomass2$year)
MTL2surv<-matrix(nrow=19,ncol=rep,NA)
MTL2surv<-data.frame(MTL2surv)
row.names(MTL2surv)<-year
for (k in 1:rep)
  for (i in year)
  { print(paste("rep: ",k,"    year: ",i,sep=""))
    selection <- biomass2[which(biomass2$year==i),]
    row.names(selection) <- selection$species
    TLselect <- TL[TL$TL >= cutoff2, ]
    selection <- selection[selection$species %in% TLselect$species, ]
    
    biomass_year<-sum(selection$biomass)
    for (j in 1:dim(selection)[1])
    {
      SP<-selection[j,"species"]
      TL_sel<-TL[paste(SP),"TL"]
      SE_sel<-TL[paste(SP),"SE"]
      TL_sample<-sample(seq(TL_sel-SE_sel,TL_sel+SE_sel,0.0001),1)
      selection[j,"biomass"]<-(selection[j,"biomass"]*TL_sample)
    }
    MTL2surv[i,k]<-(sum(selection[,"biomass"])/biomass_year)
  }


############################################################################################
###################  MTL_3.25  EVHOE survey ####################################
############################################################################################
cutoff3<-3.25
rep<-500

biomass2$year<-as.factor(biomass2$year)
rownames(TL)<-TL$species
year<-unique(biomass2$year)

MTL3surv<-matrix(nrow=19,ncol=rep,NA)
MTL3surv<-data.frame(MTL3surv)
row.names(MTL3surv)<-year
for (k in 1:rep)
  for (i in year)
  { print(paste("rep: ",k,"    year: ",i,sep=""))
    selection <- biomass2[which(biomass2$year==i),]
    row.names(selection) <- selection$species
    TLselect <- TL[TL$TL >= cutoff3, ]
    selection <- selection[selection$species %in% TLselect$species, ]
    
    biomass_year<-sum(selection$biomass)
    for (j in 1:dim(selection)[1])
    {
      SP<-selection[j,"species"]
      TL_sel<-TL[paste(SP),"TL"]
      SE_sel<-TL[paste(SP),"SE"]
      TL_sample<-sample(seq(TL_sel-SE_sel,TL_sel+SE_sel,0.0001),1)
      selection[j,"biomass"]<-(selection[j,"biomass"]*TL_sample)
    }
    MTL3surv[i,k]<-(sum(selection[,"biomass"])/biomass_year)
  }

############################################################################################
###################  MTL_4   EVHOE survey ####################################
############################################################################################
cutoff4<-4.0
rep<-500

biomass2$year<-as.factor(biomass2$year)
rownames(TL)<-TL$species
year<-unique(biomass2$year)

MTL4surv<-matrix(nrow=19,ncol=rep,NA)
MTL4surv<-data.frame(MTL4surv)
row.names(MTL4surv)<-year
for (k in 1:rep)
  for (i in year)
  { print(paste("rep: ",k,"    year: ",i,sep=""))
    selection <- biomass2[which(biomass2$year==i),]
    row.names(selection) <- selection$species
    TLselect <- TL[TL$TL >= cutoff4, ]
    selection <- selection[selection$species %in% TLselect$species, ]
    
    biomass_year<-sum(selection$biomass)
    for (j in 1:dim(selection)[1])
    {
      SP<-selection[j,"species"]
      TL_sel<-TL[paste(SP),"TL"]
      SE_sel<-TL[paste(SP),"SE"]
      TL_sample<-sample(seq(TL_sel-SE_sel,TL_sel+SE_sel,0.0001),1)
      selection[j,"biomass"]<-(selection[j,"biomass"]*TL_sample)
    }
    MTL4surv[i,k]<-(sum(selection[,"biomass"])/biomass_year)
  }

############################################################################################
###################  MTL_2.0 EVHOE survey no Pelagic   ####################################
############################################################################################

biomass3$year<-as.factor(biomass3$year)
rownames(TL)<-TL$species
year<-unique(biomass3$year)

MTL2survnoP<-matrix(nrow=19,ncol=rep,NA)
MTL2survnoP<-data.frame(MTL2survnoP)
row.names(MTL2survnoP)<-year
for (k in 1:rep)
  for (i in year)
  { print(paste("rep: ",k,"    year: ",i,sep=""))
    selection <- biomass3[which(biomass3$year==i),]
    row.names(selection) <- selection$species
    TLselect <- TL[TL$TL >= cutoff2, ]
    selection <- selection[selection$species %in% TLselect$species, ]
    
    biomass_year<-sum(selection$biomass)
    for (j in 1:dim(selection)[1])
    {
      SP<-selection[j,"species"]
      TL_sel<-TL[paste(SP),"TL"]
      SE_sel<-TL[paste(SP),"SE"]
      TL_sample<-sample(seq(TL_sel-SE_sel,TL_sel+SE_sel,0.0001),1)
      selection[j,"biomass"]<-(selection[j,"biomass"]*TL_sample)
    }
    MTL2survnoP[i,k]<-(sum(selection[,"biomass"])/biomass_year)
  }

############################################################################################
###################  MTL_3.25  EVHOE survey no Pelagic ####################################
############################################################################################

biomass3$year<-as.factor(biomass3$year)
rownames(TL)<-TL$species
year<-unique(biomass3$year)

MTL3survnoP<-matrix(nrow=19,ncol=rep,NA)
MTL3survnoP<-data.frame(MTL3survnoP)
row.names(MTL3survnoP)<-year
for (k in 1:rep)
  for (i in year)
  { print(paste("rep: ",k,"    year: ",i,sep=""))
    selection <- biomass3[which(biomass3$year==i),]
    row.names(selection) <- selection$species
    TLselect <- TL[TL$TL >= cutoff3, ]
    selection <- selection[selection$species %in% TLselect$species, ]
    
    biomass_year<-sum(selection$biomass)
    for (j in 1:dim(selection)[1])
    {
      SP<-selection[j,"species"]
      TL_sel<-TL[paste(SP),"TL"]
      SE_sel<-TL[paste(SP),"SE"]
      TL_sample<-sample(seq(TL_sel-SE_sel,TL_sel+SE_sel,0.0001),1)
      selection[j,"biomass"]<-(selection[j,"biomass"]*TL_sample)
    }
    MTL3survnoP[i,k]<-(sum(selection[,"biomass"])/biomass_year)
  }

############################################################################################
###################  MTL_4   EVHOE survey no Pelagic ####################################
############################################################################################

biomass3$year<-as.factor(biomass3$year)
rownames(TL)<-TL$species
year<-unique(biomass3$year)

MTL4survnoP<-matrix(nrow=19,ncol=rep,NA)
MTL4survnoP<-data.frame(MTL4survnoP)
row.names(MTL4survnoP)<-year
for (k in 1:rep)
  for (i in year)
  { print(paste("rep: ",k,"    year: ",i,sep=""))
    selection <- biomass3[which(biomass3$year==i),]
    row.names(selection) <- selection$species
    TLselect <- TL[TL$TL >= cutoff4, ]
    selection <- selection[selection$species %in% TLselect$species, ]
    
    biomass_year<-sum(selection$biomass)
    for (j in 1:dim(selection)[1])
    {
      SP<-selection[j,"species"]
      TL_sel<-TL[paste(SP),"TL"]
      SE_sel<-TL[paste(SP),"SE"]
      TL_sample<-sample(seq(TL_sel-SE_sel,TL_sel+SE_sel,0.0001),1)
      selection[j,"biomass"]<-(selection[j,"biomass"]*TL_sample)
    }
    MTL4survnoP[i,k]<-(sum(selection[,"biomass"])/biomass_year)
  }


###################################################
########### PLOT (after calculations) #############
###################################################


####################
# PLOT MTL_2.0    #
####################

years2surv <- as.numeric(rownames(MTL2surv))
MTL2surv[is.na(MTL2surv)] <- 4

par(mar=c(6,7,2,2), mgp=c(4.5, 1, 0), las=0)

plot(NULL, xlim = c(min(years2surv), max(years2surv)), ylim=c(3.5,4.7),
     ylab="MTL_2.0",xlab="Year",cex.lab = 3,
     axes=FALSE, frame.plot = TRUE, bty = "l")

stdevs2surv <- apply(MTL2surv, 1, sd)
lcl2surv <- rowMeans(MTL2surv) - stdevs2surv
ucl2surv <- rowMeans(MTL2surv) + stdevs2surv
x.polygon2surv <- c(years2surv, sort(years2surv, decreasing = TRUE))
y.polygon2surv <- c(ucl2surv, rev(lcl2surv))

polygon(x.polygon2surv, y.polygon2surv, col = "#F2E399", border = NA)
lines(years2surv, rowMeans(MTL2surv), col = "#006349", lwd = 3)

fancyAxis(1, at = seq(1997, 2015, 2), las = 1, cex.axis = 2.5)
fancyAxis(2, at = seq(3.5, 4.7, 0.2), las = 1, cex.axis = 2.5)


####################
# PLOT MTL_3.25    #
####################

years3surv <- as.numeric(rownames(MTL3surv))
MTL3surv[is.na(MTL3surv)] <- 4

par(mar=c(6,7,2,2), mgp=c(4.5, 1, 0), las=0)

plot(NULL, xlim = c(min(years3surv), max(years3surv)), ylim=c(3.5,4.7),
     ylab="MTL_3.25",xlab="Year",cex.lab = 3,
     axes=FALSE, frame.plot = TRUE, bty = "l")

stdevs3surv <- apply(MTL3surv, 1, sd)
lcl3surv <- rowMeans(MTL3surv) - stdevs3surv
ucl3surv <- rowMeans(MTL3surv) + stdevs3surv
x.polygon3surv <- c(years3surv, sort(years3surv, decreasing = TRUE))
y.polygon3surv <- c(ucl3surv, rev(lcl3surv))

polygon(x.polygon3surv, y.polygon3surv, col = "#F2E399", border = NA)
lines(years3surv, rowMeans(MTL3surv), col = "#006349", lwd = 3)

fancyAxis(1, at = seq(1997, 2015, 2), las = 1, cex.axis = 2.5)
fancyAxis(2, at = seq(3.5, 4.7, 0.2), las = 1, cex.axis = 2.5)


####################
# PLOT MTL_4.0    #
####################

years4surv <- as.numeric(rownames(MTL4surv))
MTL4surv[is.na(MTL4surv)] <- 4.2

par(mar=c(6,7,2,2), mgp=c(4.5, 1, 0), las=0)

plot(NULL, xlim = c(min(years4surv), max(years4surv)), ylim=c(3.5,4.7),
     ylab="MTL_4.0",xlab="Year",cex.lab = 3,
     axes=FALSE, frame.plot = TRUE, bty = "l")

stdevs4surv <- apply(MTL4surv, 1, sd)
lcl4surv <- rowMeans(MTL4surv) - stdevs4surv
ucl4surv <- rowMeans(MTL4surv) + stdevs4surv
x.polygon4surv <- c(years4surv, sort(years4surv, decreasing = TRUE))
y.polygon4surv <- c(ucl4surv, rev(lcl4surv))

polygon(x.polygon4surv, y.polygon4surv, col = "#F2E399", border = NA)
lines(years4surv, rowMeans(MTL4surv), col = "#006349", lwd = 3)

fancyAxis(1, at = seq(1997, 2015, 2), las = 1, cex.axis = 2.5)
fancyAxis(2, at = seq(3.5, 4.7, 0.2), las = 1, cex.axis = 2.5)


####################
# PLOT MTL_2.0 noP    #
####################

years2survnoP <- as.numeric(rownames(MTL2survnoP))
MTL2survnoP[is.na(MTL2survnoP)] <- 4

par(mar=c(6,7,2,2), mgp=c(4.5, 1, 0), las=0)

plot(NULL, xlim = c(min(years2survnoP), max(years2survnoP)), ylim=c(3.5,4.7),
     ylab="MTL_2.0",xlab="Year",cex.lab = 3,
     axes=FALSE, frame.plot = TRUE, bty = "l")

stdevs2survnoP <- apply(MTL2survnoP, 1, sd)
lcl2survnoP <- rowMeans(MTL2survnoP) - stdevs2survnoP
ucl2survnoP <- rowMeans(MTL2survnoP) + stdevs2survnoP
x.polygon2survnoP <- c(years2survnoP, sort(years2survnoP, decreasing = TRUE))
y.polygon2survnoP <- c(ucl2survnoP, rev(lcl2survnoP))

polygon(x.polygon2survnoP, y.polygon2survnoP, col = "#F2E399", border = NA)
lines(years2survnoP, rowMeans(MTL2survnoP), col = "#006349", lwd = 3)

fancyAxis(1, at = seq(1997, 2015, 2), las = 1, cex.axis = 2.5)
fancyAxis(2, at = seq(3.5, 4.7, 0.2), las = 1, cex.axis = 2.5)


####################
# PLOT MTL_3.25 noP   #
####################

years3survnoP <- as.numeric(rownames(MTL3survnoP))
MTL3survnoP[is.na(MTL3survnoP)] <- 4

par(mar=c(6,7,2,2), mgp=c(4.5, 1, 0), las=0) 

plot(NULL, xlim = c(min(years3survnoP), max(years3survnoP)), ylim=c(3.5,4.7),
     ylab="MTL_3.25",xlab="Year",cex.lab = 3,
     axes=FALSE, frame.plot = TRUE, bty = "l")

stdevs3survnoP <- apply(MTL3survnoP, 1, sd)
lcl3survnoP <- rowMeans(MTL3survnoP) - stdevs3survnoP
ucl3survnoP <- rowMeans(MTL3survnoP) + stdevs3survnoP
x.polygon3survnoP <- c(years3survnoP, sort(years3survnoP, decreasing = TRUE))
y.polygon3survnoP <- c(ucl3survnoP, rev(lcl3survnoP))

polygon(x.polygon3survnoP, y.polygon3survnoP, col = "#F2E399", border = NA)
lines(years3survnoP, rowMeans(MTL3survnoP), col = "#006349", lwd = 3)

fancyAxis(1, at = seq(1997, 2015, 2), las = 1, cex.axis = 2.5)
fancyAxis(2, at = seq(3.5, 4.7, 0.2), las = 1, cex.axis = 2.5)


####################
# PLOT MTL_4.0  noP  #
####################

years4survnoP <- as.numeric(rownames(MTL4survnoP))
MTL4survnoP[is.na(MTL4survnoP)] <- 4.2

par(mar=c(6,7,2,2), mgp=c(4.5, 1, 0), las=0) 

plot(NULL, xlim = c(min(years4survnoP), max(years4survnoP)), ylim=c(3.5,4.7),
     ylab="MTL_4.0",xlab="Year",cex.lab = 3,
     axes=FALSE, frame.plot = TRUE, bty = "l")

stdevs4survnoP <- apply(MTL4survnoP, 1, sd)
lcl4survnoP <- rowMeans(MTL4survnoP) - stdevs4survnoP
ucl4survnoP <- rowMeans(MTL4survnoP) + stdevs4survnoP
x.polygon4survnoP <- c(years4survnoP, sort(years4survnoP, decreasing = TRUE))
y.polygon4survnoP <- c(ucl4survnoP, rev(lcl4survnoP))

polygon(x.polygon4survnoP, y.polygon4survnoP, col = "#F2E399", border = NA)
lines(years4survnoP, rowMeans(MTL4survnoP), col = "#006349", lwd = 3)

fancyAxis(1, at = seq(1997, 2015, 2), las = 1, cex.axis = 2.5)
fancyAxis(2, at = seq(3.5, 4.7, 0.2), las = 1, cex.axis = 2.5)


#########################################################################################
############################################################################################
###################  Statistical tests ####################################
############################################################################################
########################################################################################

##########################################################################
################ LM analysis (for parametric values) for EVHOE survey ###########
##########################################################################

########MTL_2.0###########

MTL2surv.t<-t(MTL2surv)
MTL2surv.t.m<-colMeans(MTL2surv.t)
year<-c(1997:2015)
year<-as.numeric(year)
MTL2surv.t.m<-as.data.frame(MTL2surv.t.m)
MTL2surv.t.m$year<-year
MTL_2.0b=as.data.frame(MTL2surv.t.m)
MTL_2.0<-MTL_2.0b[-3,]
View(MTL_2.0)

plot(MTL_2.0$year, MTL_2.0$MTL2surv.t.m, ylab="MTL_2.0", xlab="Years")
lm1<-lm(MTL_2.0$MTL2surv.t.m ~ MTL_2.0$year, data=MTL_2.0)
shapiro.test(lm1$residuals) #normality if pvalue > alpha=0.05
library(lmtest)
hmctest(lm1) #if pvalue > alpha -> verified condition #Harrison-McCabe test statistic is the fraction of the residual sum of squares that relates to the fraction of the data before the breakpoint
dwtest(lm1) #no autocorrelation if p>alpha # checking independance (temporal autocorrelation)
summary(lm1) #significant slope if p<0.05

########MTL_3.25###########
MTL_3.25surv.t<-t(MTL3surv)
MTL_3.25surv.t.m<-colMeans(MTL_3.25surv.t)
year<-c(1997:2015)
year<-as.numeric(year)
MTL_3.25surv.t.m<-as.data.frame(MTL_3.25surv.t.m)
MTL_3.25surv.t.m$year<-year
MTL_3.25b=as.data.frame(MTL_3.25surv.t.m)
MTL_3.25<-MTL_3.25b[-3,]
View(MTL_3.25)

plot(MTL_3.25$year, MTL_3.25$MTL_3.25surv.t.m, ylab="MTL_3.25", xlab="Years")
lm3<-lm(MTL_3.25$MTL_3.25surv.t.m ~ MTL_3.25$year, data=MTL_3.25)
shapiro.test(lm3$residuals)
hmctest(lm3)
dwtest(lm3)
summary(lm3)


########MTL_4.0###########
MTL_4.0surv.t<-t(MTL4surv)
MTL_4.0surv.t.m<-colMeans(MTL_4.0surv.t)
year<-c(1997:2015)
year<-as.numeric(year)
MTL_4.0surv.t.m<-as.data.frame(MTL_4.0surv.t.m)
MTL_4.0surv.t.m$year<-year
MTL_4.0b=as.data.frame(MTL_4.0surv.t.m)
MTL_4.0<-MTL_4.0b[-3,]
View(MTL_4.0)

plot(MTL_4.0$year, MTL_4.0$MTL_4.0surv.t.m, ylab="MTL_4.0", xlab="Years")
lm9<-lm(MTL_4.0$MTL_4.0surv.t.m ~ MTL_4.0$year, data=MTL_4.0)
shapiro.test(lm9$residuals) 
hmctest(lm9)
dwtest(lm9)
summary(lm9)

########MTL_2.0 noP###########

MTL2survnoP.t<-t(MTL2survnoP)
MTL2survnoP.t.m<-colMeans(MTL2survnoP.t)
year<-c(1997:2015)
year<-as.numeric(year)
MTL2survnoP.t.m<-as.data.frame(MTL2survnoP.t.m)
MTL2survnoP.t.m$year<-year
MTL_2.0c=as.data.frame(MTL2survnoP.t.m)
MTL_2.0noP<-MTL_2.0c[-3,]
View(MTL_2.0noP)

plot(MTL_2.0noP$year, MTL_2.0noP$MTL2survnoP.t.m, ylab="MTL_2.0", xlab="Years")
lm2<-lm(MTL_2.0noP$MTL2survnoP.t.m ~ MTL_2.0noP$year, data=MTL_2.0noP)
shapiro.test(lm2$residuals)
library(lmtest)
hmctest(lm2)
dwtest(lm2) 
summary(lm2) 

########MTL_3.25 noP###########
MTL_3.25survnoP.t<-t(MTL3survnoP)
MTL_3.25survnoP.t.m<-colMeans(MTL_3.25survnoP.t)
year<-c(1997:2015)
year<-as.numeric(year)
MTL_3.25survnoP.t.m<-as.data.frame(MTL_3.25survnoP.t.m)
MTL_3.25survnoP.t.m$year<-year
MTL_3.25c=as.data.frame(MTL_3.25survnoP.t.m)
MTL_3.25noP<-MTL_3.25c[-3,]
View(MTL_3.25noP)

plot(MTL_3.25noP$year, MTL_3.25noP$MTL_3.25survnoP.t.m, ylab="MTL_3.25", xlab="Years")
lm4<-lm(MTL_3.25noP$MTL_3.25survnoP.t.m ~ MTL_3.25noP$year, data=MTL_3.25noP)
shapiro.test(lm4$residuals)
hmctest(lm4)
dwtest(lm4)
summary(lm4)


########MTL_4.0 noP###########
MTL_4.0survnoP.t<-t(MTL4survnoP)
MTL_4.0survnoP.t.m<-colMeans(MTL_4.0survnoP.t)
year<-c(1997:2015)
year<-as.numeric(year)
MTL_4.0survnoP.t.m<-as.data.frame(MTL_4.0survnoP.t.m)
MTL_4.0survnoP.t.m$year<-year
MTL_4.0c=as.data.frame(MTL_4.0survnoP.t.m)
MTL_4.0noP<-MTL_4.0c[-3,]
View(MTL_4.0noP)

plot(MTL_4.0noP$year, MTL_4.0noP$MTL_4.0survnoP.t.m, ylab="MTL_4.0", xlab="Years")
lm6<-lm(MTL_4.0noP$MTL_4.0survnoP.t.m ~ MTL_4.0noP$year, data=MTL_4.0noP)
shapiro.test(lm6$residuals) 
hmctest(lm6)
dwtest(lm6)
summary(lm6)

#############################################################################
################ MannKendall analysis for EVHOE survey ###########
##########################################################################

##MTL_2.0#####
acf(MTL_2.0)
pacf(MTL_2.0)
library(Kendall)
res2 <-  MannKendall(MTL_2.0$MTL2surv.t.m)
summary(res2)
#p-value associated with the Mann-Kendall test is statistically significant when p<0.05. 
#Tau  -> Kendall's tau statistic
#p-value   -> two-sided p-value
#Score	-> Kendall Score (S)
#Denominator	-> denominator (D), such that tau=S/D
#Var(Score) 	-> variance of Kendall Score

##MTL_3.25#####
acf(MTL_3.25)
pacf(MTL_3.25)
res3 <-  MannKendall(MTL_3.25$MTL_3.25surv.t.m)
summary(res3)

##MTL_4.0#####
acf(MTL_4.0)
pacf(MTL_4.0)
res4 <-  MannKendall(MTL_4.0$MTL_4.0surv.t.m)
summary(res4)


##MTL_2.0 noP#####
acf(MTL_2.0noP)
pacf(MTL_2.0noP)
res5 <-  MannKendall(MTL_2.0noP$MTL2survnoP.t.m)
summary(res5)

##MTL_3.25 noP#####
acf(MTL_3.25noP)
pacf(MTL_3.25noP)
res6 <-  MannKendall(MTL_3.25noP$MTL_3.25survnoP.t.m)
summary(res6)

##MTL_4.0 noP#####
acf(MTL_4.0noP)
pacf(MTL_4.0noP)
res7 <-  MannKendall(MTL_4.0noP$MTL_4.0survnoP.t.m)
summary(res7)
