## Conduct accuracy assessment on 2016 NLCD data using Google Earth verified
## iNaturalist records
# Specify  directory for saved functions
#FunctDirect <- "D:/My Drive/win-library/"
FunctDirect <- "C:/Users/jamesltracy61/Documents/R/win-library/"
setwd(FunctDirect)
# Read in User Defined Functions
source("ClassContingencyTable2_Function.R")
source("Kappa.Calc_Function.R")  # used in ClassContingencyTable_Function.R
source("Round2_Function.R")
##
# Load needed packages
#install.packages(c("maptools", "rgdal", "sp", "raster", "plyr","foreign","dplyr","ggpubr","data.table","electivity","reshape","ggcorrplot","psych")) 
library(plyr)
library(foreign)
library(dplyr)
library(ggpubr)
library(data.table)
library(reshape2)
library(ggcorrplot)
library(psych)
###
InDirect <- "K:/Monarch/"
Units <- "km"
SpatFiltBuff <- 1 # units in km for spatial filter
####################################################################################
### Calculate accuracy assessment contingency table comparing Class Name with Revised Class Name
setwd(InDirect)
#
SpeciesList <- c("A_cap","A_vir","A_oeno","A_lat")
DataList <- list()
Count <- 0
for(Species in SpeciesList) {
  PresenceDatComb.df4a <- data.frame(read.csv(paste0(Species, "_Thinned", SpatFiltBuff, Units,"_presNLCD_", "1kPopDenIndexdat.csv"), stringsAsFactors=FALSE))
  #
  head(PresenceDatComb.df4a)
  Count <- Count + 1
  if(Count==1) {
    PresenceDatComb.df4a <- PresenceDatComb.df4a[,-1] 
    colnames(PresenceDatComb.df4a)[1] <- "X" 
  }
  DataList[[Count]] <- PresenceDatComb.df4a
  #
}
#head(DataList[[1]])
#ncol(DataList[[4]])
#colnames(DataList[[1]])
##
### Find columns in first but not second data frame
#NewCols.df <- as.data.frame(setdiff(colnames(DataList[[2]]), colnames(DataList[[3]])), stringsAsFactors=FALSE)
##
PresenceDatComb.df4 <- do.call(rbind, DataList)
#
unique(PresenceDatComb.df4$Habitat)
# Combine Trail, Utility, and RR ROWs
PresenceDatComb.df4$Habitat[grepl("Trail|Utility", PresenceDatComb.df4$Habitat)] <- "Trail/Utility/RR ROW"
#
RefPnts.mat <- as.matrix(PresenceDatComb.df4$RevClassName)
PredPnts.mat <- as.matrix(PresenceDatComb.df4$ClassName)
## Specify directory where function "ClassContingencyTable_Function.R" is kept
FunctDirectIn <- FunctDirect
Blank <- ""
RunType <- "Overall"
ModelNameOut <- ""
SubsetVariableNumber <- ""
VariableSubsets <- ""
output1 <- ""
Conditions2 <- ""
OutDirectIn3 <- InDirect
TestDataType <- ""
Run <- ""
Species <- "Asclepias"
#
ConfusionMat <- ClassContingencyTable2(RefPnts.mat, PredPnts.mat, Species, ModelNameOut, SubsetVariableNumber, VariableSubsets, output1,
Conditions2, OutDirectIn3, TestDataType, Run, RunType, FunctDirectIn, Blank, VarImp=FALSE, Output=TRUE)
#######################################################################################
# Calculate accuracy assessments by Habitat Type
HabitatList <-  unique(PresenceDatComb.df4$Habitat)
for(HabitatType in HabitatList) {
  #HabitatType <- HabitatList[5]
  PresenceDatComb.df4a <- PresenceDatComb.df4[which(PresenceDatComb.df4$Habitat==HabitatType),]
  head(PresenceDatComb.df4a)
  nrow(PresenceDatComb.df4a)
  RefPnts.mat <- as.matrix(PresenceDatComb.df4a$RevClassName)
  PredPnts.mat <- as.matrix(PresenceDatComb.df4a$ClassName)
  ## Specify directory where function "ClassContingencyTable_Function.R" is kept
  FunctDirectIn <- FunctDirect
  Blank <- ""
  RunType <- gsub("/", "", HabitatType)
  ModelNameOut <- ""
  SubsetVariableNumber <- ""
  VariableSubsets <- ""
  output1 <- ""
  Conditions2 <- ""
  OutDirectIn3 <- InDirect
  TestDataType <- ""
  Run <- ""
  #
  ConfusionMat <- ClassContingencyTable2(RefPnts.mat, PredPnts.mat, Species, ModelNameOut, SubsetVariableNumber, VariableSubsets, output1,
  Conditions2, OutDirectIn3, TestDataType, Run, RunType, FunctDirectIn, Blank, VarImp=FALSE, Output=TRUE)
}

