rm(list=ls())

#Packages
library(wCorr)
library(divDyn)

################
#Functions
#Spearman's correlation on generalised differences
gen.diff.corr<-function(x,z){
  f1<-cor.test(x[-1],x[-length(x)]) #First order correlation
  new.f1<-x[-1]-f1$estimate*x[-length(x)] #Generalised differences temp
  f2<-cor.test(z[-1],z[-length(z)]) #First order correlation
  new.f2<-z[-1]-f2$estimate*z[-length(z)] #Generalised differences temp
  out<-cor.test(new.f1,new.f2,method="spearman")
  return(out)
}

#Weighted Spearman's correlation on generalised differences
gen.diff.wcorr<-function(x,z,wt){
  f1<-cor.test(x[-1],x[-length(x)]) #First order correlation
  new.f1<-x[-1]-f1$estimate*x[-length(x)] #Generalised differences temp
  f2<-cor.test(z[-1],z[-length(z)]) #First order correlation
  new.f2<-z[-1]-f2$estimate*z[-length(z)] #Generalised differences temp
  out<-weightedCorr(new.f1,new.f2,method="Spearman",weights = wt[-1])
  return(out)
}

#Moving window around three bins
ma.pres <- function(x,n=3){filter(x,rep(1/n,n), sides=2)}   

#Slices occurrences into stage bins
slicer<-function(x){stages$stg.num[x<stages$Age.b  &  x>=stages$Age.t]}

NAmedian<-function(x){
  y<-median(x, na.rm = TRUE)
  return(y)
}

###########################
#Data
setwd("C:/Users/...") #Amend to your working directory

#Oxygen isotope temperature data
isotemp <- read.csv(file="TimeSeriesUsed.csv", header=TRUE,row.names = 1) 
isotemp<-isotemp[,5]

#The main occurrence dataset, downloaded from the Paleobiology Database (https://paleobiodb.org/) 8th February 2017 
pbd <- read.csv(file="pbdb_data.Ord.slim.csv", header=TRUE) 

# load corresponding stages
stages <- read.table(file="stages_2012PBDB.csv", header=TRUE, sep=",")

age <- stages$Age.m[1:94] 
bin.n <- stages$Stage[1:94]
age.b <- stages$Age.b
durats <- -diff(stages$Age.b)

# Exclude non-rotated localities
dat <- subset(pbd, is.na(paleolatdec)==F & paleolatdec!=0)

#Add mean age
dat$mean_ma<-(dat$max_ma+dat$min_ma)/2

#Add time slice number using 'stages'
dat$slc<-lapply(dat$mean_ma,slicer)

# Exclude unassigned bins
dat <- subset(dat, is.na(slc)==F)

dat$clgen <- paste(dat$class, dat$genus) #Makes clgen for next step

# Delete duplicate genus occurrences within collections
t1 <- subset(dat, select=c(collection_no, clgen))
dat <- dat[!duplicated(t1),]

#Separate out groups
cl.n <- c("Bivalvia", "Gastropoda","Rhynchonellata","Anthozoa","Strophomenata","Trilobita","Lingulata","Stromatoporoidea") 
dat <- subset(dat, class %in% cl.n) 

dat <- subset(dat, dat$slc %in% 14:94) #From Ordovician
dat$slc<-as.numeric(dat$slc)

#Read in the cell names and add them to the data
cellColl <- read.csv("Palaeo222cells.csv", header=T, stringsAsFactors=F,sep=";")

#Remaining '.aff' in clgen removed and low resolution records (e.g. Period) removed
dat<-dat[-grep("aff.",dat$clgen),]
temp2<-dat[!(dat$max_ma-dat$min_ma)>19.5,]

#add the cell names to the occurrence data
temp2<-merge(cellColl, temp2, "collection_no")

#############################################################
#Calculate latitudinal deviations

bin.1 <- 13 #Start one bin prior to the Ordovician
n=1200 

#Separating hemispheres
allmovtpl<-matrix(data = NA, nrow = n, ncol = 94) 
stoplatspl<-matrix(data = NA, nrow = n, ncol = 94)
startlatspl<-matrix(data = NA, nrow = n, ncol = 94)
allmovtng<-matrix(data = NA, nrow = n, ncol = 94)
stoplatsng<-matrix(data = NA, nrow = n, ncol = 94)
startlatsng<-matrix(data = NA, nrow = n, ncol = 94)
nspmovt<-numeric()

for (i in bin.1:94)   {  #Code analyses changes in two timers from one stage to next
  movts<-numeric() 
  bin12 <- subset(temp2, slc==i | slc==i-1)
  rfl<-levels(factor(bin12$clgen[bin12$slc==i])) #All (order specific) genera in bin
  rf1d<-levels(factor(bin12$clgen[bin12$slc==(i-1)])) # All genera in previous bin     
  tt <- rfl [rfl %in% rf1d] # Two-timers at bottom   
  bin1 <- subset(bin12, slc==i-1)
  bin2 <- subset(bin12, slc==i)
  
  #################
  #The original erroneous line of code, which leads to nonsensical subsetting of data:
  #gridmatch <- subset(bin12,bin1$cell_name %in% bin2$cell_name) #Both stages agreements by cells
  
  #An update to obtain a correct version of object 'gridmatch', although this is shown to be not useful
  #shared_cells <- intersect(bin1$cell_name, bin2$cell_name) 
  #gridmatch <- subset(bin12, bin12$cell_name %in% shared_cells) 
  ###################
  
  #Replacing the following two lines, which use matched cells...
  #bin1 <- subset(gridmatch, slc==i-1 & clgen %in% tt) #Survivors in previous bin from matched cells
  #bin2 <- subset(gridmatch, slc==i & clgen %in% tt) #Survivors in bin from matched cells
  
  #...with these two, which do not use matched cells
  bin1 <- subset(bin1, slc==i-1 & clgen %in% tt) #Survivors in previous bin 
  bin2 <- subset(bin2, slc==i & clgen %in% tt) #Survivors in bin
  lat2.bg.splus <- tapply(abs(bin2$paleolatdec[bin2$paleolatdec>=0]), factor(bin2$clgen[bin2$paleolatdec>=0]), median) # Genus's median latitude in bin
  lat1.bg.splus <- tapply(abs(bin1$paleolatdec[bin1$paleolatdec>=0]), factor(bin1$clgen[bin1$paleolatdec>=0]), median) # Genus's median latitude in previous bin
  lat2.bg.sneg <- tapply(abs(bin2$paleolatdec[bin2$paleolatdec<=0]), factor(bin2$clgen[bin2$paleolatdec<=0]), median) # Genus's median latitude in bin
  lat1.bg.sneg <- tapply(abs(bin1$paleolatdec[bin1$paleolatdec<=0]), factor(bin1$clgen[bin1$paleolatdec<=0]), median) # Genus's median latitude in previous bin
  
  #North (note species listed here have to be present in the previous time-step, so this is a second forcing of two-timers, additional to clgen %in% tt)
  d<-length(lat2.bg.splus[names(lat2.bg.splus)%in%names(lat1.bg.splus)]-lat1.bg.splus[names(lat1.bg.splus)%in%names(lat2.bg.splus)]) #if loop added to escape zero length error
  if(d>0){
    movtspl<-lat2.bg.splus[names(lat2.bg.splus)%in%names(lat1.bg.splus)]-lat1.bg.splus[names(lat1.bg.splus)%in%names(lat2.bg.splus)] #save individual survivor destination lat
    stoplatpl<-lat2.bg.splus[names(lat2.bg.splus)%in%names(lat1.bg.splus)] #save individual survivor destination lat. Clgen names
    startlatpl<-names(lat2.bg.splus[names(lat2.bg.splus)%in%names(lat1.bg.splus)]) #save individual survivor origin lat
    
    allmovtpl[1:d,i]<-movtspl
    stoplatspl[1:d,i]<-stoplatpl
    startlatspl[1:d,i]<-startlatpl
  }
  
  #South
  d<-length(lat2.bg.sneg[names(lat2.bg.sneg)%in%names(lat1.bg.sneg)]-lat1.bg.sneg[names(lat1.bg.sneg)%in%names(lat2.bg.sneg)]) #if loop added to escape zero length error
  if(d>0){
    movtsng<-lat2.bg.sneg[names(lat2.bg.sneg)%in%names(lat1.bg.sneg)]-lat1.bg.sneg[names(lat1.bg.sneg)%in%names(lat2.bg.sneg)] #save individual survivor destination lat
    stoplatng<-lat2.bg.sneg[names(lat2.bg.sneg)%in%names(lat1.bg.sneg)] #save individual survivor destination lat
    startlatng<-names(lat2.bg.sneg[names(lat2.bg.sneg)%in%names(lat1.bg.sneg)]) #save individual survivor origin lat
    
    allmovtng[1:d,i]<-movtsng
    stoplatsng[1:d,i]<-stoplatng
    startlatsng[1:d,i]<-startlatng
  }
  
  nspmovt[i]<-length(tt)
}

#Northern deviations. gh is the Northern Hemisphere occurrence median
gh<-aggregate(temp2$paleolatdec[temp2$paleolatdec>=0],by=list(as.numeric(temp2$slc[temp2$paleolatdec>=0])), FUN=median)[,2] #Add -1 rows for Ord2
y=apply(stoplatspl[,14:94],2,NAmedian)-gh

#Southern deviations. ghl is the Southern Hemisphere occurrence median
ghl<-aggregate(temp2$paleolatdec[temp2$paleolatdec<=0],by=list(as.numeric(temp2$slc[temp2$paleolatdec<=0])), FUN=median)[,2] #Add -1 rows for Ord2
y4=apply(stoplatsng[,14:94],2,NAmedian)-abs(ghl)

#Hemisphere switching: Following a flexible cutoff for the worst segments in the N hemisphere (add to data exploration)
y3results<-numeric(length=200)
y3ps<-numeric(length=200)

for(i in 1:200){
  cutof<-length(allmovtpl[,1])-apply(is.na(stoplatspl[,14:94]),2,sum)>i
  y3<-numeric(length=81)
  y3[1:81]<-NA
  y3[cutof]<-y[cutof]
  y3[!cutof]<-y4[!cutof]
  y3results[i]<-gen.diff.corr(ma.pres(isotemp),ma.pres(y3))$estimate
  y3ps[i]<-gen.diff.corr(ma.pres(isotemp),ma.pres(y3))$p.value
}

#Figure 3
plot(1:200,y3results,ylim=c(-0.11,0.5),ylab="Correlation with climate, rho",xlab=c("Minimum number of sampled genera,","below which the alternative hemisphere is used"),type="n")
points(1:200,y3results,pch=3,cex=0.5)
points((1:200)[y3ps<0.05],y3results[y3ps<0.05],pch=19)#,bg="grey")

#Using the above code with i set to the ideal cutoff to get y3:
cutof<-length(allmovtpl[,1])-apply(is.na(stoplatspl[,14:94]),2,sum)>max(which(y3results==max(y3results)))
y3<-numeric(length=81)
y3[1:81]<-NA
y3[cutof]<-y[cutof]
y3[!cutof]<-y4[!cutof]

#Diversity dynamics using divDyn package to get extinction rates, used for weighted correlation
x<-divDyn(temp2, tax = "clgen", bin = "slc")

####################
#Final correlations

gen.diff.corr(ma.pres(isotemp),ma.pres(y)) 
gen.diff.corr(ma.pres(isotemp),ma.pres(y4)) 
gen.diff.corr(ma.pres(isotemp),ma.pres(y3)) 
gen.diff.wcorr(ma.pres(isotemp)[4:78],ma.pres(y3)[4:78], (ma.pres(1-x$extPC)[(4:78)+13])) #Gen diff, Weighting by smoothed extinction rate 
