#ESM2: The R script used to analyse the Stafford dataset. 
#Code lines are referred to throughout Stroud et al.Sieving the weeds from the grains: an R based package for classifying archaeobotanical samples according to crop-processing stages. Vegetation History and Archaeobotany
#####install package from github
install.packages("devtools")
library(devtools)
install_github("CropPro-Package/CropPro")
library(CropPro)
###### Stafford - triplot #######
#load ESM3_stafford_raw.xlsx sheet this can be done using RStudio's "Import dataset" option under the File tab
#remove na, replace with 0
ESM3_stafford_raw[5:48][is.na(ESM3_stafford_raw[5:48])]<-0
#Clean up for triplots####
#removed pulse and flax and spelt rows
stafford_nospelt<-ESM3_stafford_raw[ESM3_stafford_raw$group!="pulse" & ESM3_stafford_raw$group!="flax"& ESM3_stafford_raw$species!="Triticum spelta",]
#calculate totals based on column object (seed, grain, rachis)
GRW<-aggregate(stafford_nospelt[5:48],by=list(stafford_nospelt$object), FUN=sum)
#check totals and that all samples have >30 items
colSums(GRW[2:45])
#remove sample 1174 as it only has 9 items
GRW <- GRW[!names(GRW) %in% c("1174")]
GRW_named<-GRW[2:44]
#transpose the data
GRWnew<-as.data.frame(t(GRW_named[,]))
colnames(GRWnew)<-c("grain","rachis", "weeds")
crop.triplot(grain=GRWnew$grain, rachis=GRWnew$rachis, weeds=GRWnew$weeds)
crop.triplot(grain=GRWnew$grain, rachis=GRWnew$rachis, weeds=GRWnew$weeds, sample = 16, samplelabel = "478")
###Figure 3
dev.new(width = 10.32, height=5.7, unit="in", noRStudioGD = TRUE)
crop.triplot(grain=GRWnew$grain, rachis=GRWnew$rachis, weeds=GRWnew$weeds, sample = 16, samplelabel = "478")
dev.copy2pdf(file="plot1.pdf", encoding="WinAnsi")
##### stafford - discriminant analysis #####
#remove na rows in dataset using column codes - leaving only the classified weed samples
cropPdata<-ESM3_stafford_raw[!is.na(ESM3_stafford_raw$codes),]
#select just the species, seed attribute and sample columns
cropPdata<-cropPdata[c(1,4:48)]
#sum the totals for each sample to check numbers
colSums(cropPdata[3:43])
#remove samples 1174, 476, 1169 - have less than 10 items
cropPdata10<-cropPdata[3:43][, colSums(cropPdata[3:43])>9.99]
cropP<-cbind.data.frame(cropPdata[1:2], cropPdata10)
#transform the data using crop.dataorg."codes" is the column number of the seed attribute 
#codes fall in and samples is the column number which the samples start in. 
datatrans<-crop.dataorg(cropP, codes = 2, samples=3)
#run linear discriminant analysis on the transformed data using LDAcrop.pro
cropproc<-LDAcrop.pro(datatrans)
#plot the results of the LDAcrop.pro in three dimensions Figure 5 rotated around to show different axes. 
crop.plot3D(cropproc, site ="Stafford")
rgl.snapshot("plot 5a", fmt="png")
rgl.snapshot("plot 5b", fmt="png")
rgl.snapshot("plot 5c", fmt="png")
#plot the results of LDAcrop.pro in two dimensions - which functions are plotted 
#can be changed using Func1 and Func2 arguments
crop.plot2D(cropproc, site = "Stafford")
crop.plot2D(cropproc, site = "Stafford", col="red", Func1 = 1, Func2=3)
crop.plot2D(cropproc, site = "Stafford", col="red", Func1 = 2, Func2=3)
#labeled samples
cropproc$lab<-cropproc$Samples
cropproc$lab<-"red"
cropproc$lab[cropproc$Samples=="1163"|cropproc$Samples=="1168"| cropproc$Samples=="1167" |cropproc$Samples=="1165"|cropproc$Samples=="1164"|cropproc$Samples=="1166"| cropproc$Samples=="1170"|cropproc$Samples=="1171"|cropproc$Samples=="1172"|cropproc$Samples=="1173"]<-"green"
crop.plot2D(cropproc, site = "Stafford", label = c( "1165","1173"))
crop.plot2D(cropproc, col = paste(cropproc$lab),  site = c("Bath St and St Mary's", "Tipping St"))
#Figure 6
dev.new(width = 8.35, height=12.2, unit="in", noRStudioGD = TRUE)
par(mfrow=c(3,2),
    oma=c(0,0,1.5,0),
    mar=c(0,0,0,1), cex=0.8)
crop.plot2D(cropproc, site = "Stafford")
text(-4.5,3.5, "a", cex=1.5)
crop.plot2D(cropproc, site = "Stafford", col="red", Func1 = 1, Func2=3)
text(-4.5,3.1, "b", cex=1.5)
crop.plot2D(cropproc, site = "Stafford", col="red", Func1 = 2, Func2=3)
text(-3.7,3.2, "c", cex=1.5)
crop.plot2D(cropproc, site = "Stafford", label = c( "1165","1173"))
text(-4.5,3.5, "d", cex=1.5)
crop.plot2D(cropproc, col = paste(cropproc$lab),  site = c("Bath St and St Mary's", "Tipping Street"))
text(-4.5,3.5, "e", cex=1.5)
dev.copy2pdf(file="plot6.pdf", encoding="WinAnsi")

#label sample 461
cropproc$lab2<-cropproc$Samples
cropproc$lab2<-"black"
cropproc$lab2[cropproc$Samples=="461"]<-"red"
crop.plot2D(cropproc, col= paste(cropproc$lab2), site = c("461","Stafford"), label = "461")

### Hay meadows ###
#removed species - only three - other species not classifiable
cropPhay<-cropP[cropP$species!="Eleocharis palustris/uniglumis"&cropP$species!="Leucanthemum vulgare"&
                  cropP$species!="Silene flos-cuculi",]
#remove samples with less than ten items (all have ten items but should be checked)
colSums(cropPhay[3:43])
# data org
datatrans_hay<-crop.dataorg(cropPhay, codes = 2, samples=3)
#lda
cropproc_hay<-LDAcrop.pro(datatrans_hay)
#compare different classifications (this only works if you have the same number of samples - otherwise use something like merge)
results<-cbind(cropproc_hay[1:6],cropproc[2:6])
#plot
crop.plot3D(cropproc_hay, site ="Stafford")
crop.plot2D(cropproc_hay, site="Stafford")
#label sample 461
cropproc_hay$lab2<-cropproc_hay$Samples#is this needed?
cropproc_hay$lab2<-"black"
cropproc_hay$lab2[cropproc_hay$Samples=="461"]<-"red"
crop.plot2D(cropproc_hay, col= paste(cropproc_hay$lab2), site = c("461","Stafford"), label = c("461"))
#figure 7
dev.new(width = 10.32, height=5.7, unit="in", noRStudioGD = TRUE)
par(mfrow=c(1,2),
    oma=c(2,0,1.5,0),
    mar=c(0,0,0,1))
crop.plot2D(cropproc, col= paste(cropproc$lab2), site = c("461","Stafford"), label = "461")
text(-4.7,3.4, "a", cex=1.2)
crop.plot2D(cropproc_hay, col= paste(cropproc_hay$lab2), site = c("461","Stafford"), label = c("461"))
text(-4.7,3.4, "b", cex=1.2)
dev.copy2pdf(file="plot7.pdf", encoding="WinAnsi")
