## camtrapR Simpson Desert Data # __________________________________________________________________________________________ # ## AssessTemporalIndependence # 1. load packages library(camtrapR) # 2. load data setwd("E:/JOURNAL_MANUSCRIPTS/PeerJ/Data&Analyses") RT_Simpson_Desert <- read.csv("RT_Simpson_Desert.csv", stringsAsFactors = FALSE) RT_Lycosid <- subset(RT_Simpson_Desert, Species==“Lycosid”) # 3. run this as is to load function assessTemporalIndependence <- function(intable, deltaTimeComparedTo, columnOfInterest, # species/individual column cameraCol, camerasIndependent, stationCol, minDeltaTime, removeNonIndependentRecords = TRUE) { # check if all Exif DateTimeOriginal tags were read correctly if(any(is.na(intable$DateTimeOriginal))){ which.tmp <- which(is.na(intable$DateTimeOriginal)) if(length(which.tmp) == nrow(intable)) stop("Could not read any Exif DateTimeOriginal tag at station: ", paste(unique(intable[which.tmp, stationCol])), " Consider checking for corrupted Exif metadata.") warning(paste("Could not read Exif DateTimeOriginal tag of", length(which.tmp),"image(s) at station", paste(unique(intable[which.tmp, stationCol]), collapse = ", "), ". Will omit them. Consider checking for corrupted Exif metadata. \n", paste(file.path(intable[which.tmp, "Directory"], intable[which.tmp, "FileName"]), collapse = "\n")), call. = FALSE, immediate. = TRUE) intable <- intable[-which.tmp ,] rm(which.tmp) } # prepare to add time difference between observations columns intable <- data.frame(intable, delta.time.secs = NA, delta.time.mins = NA, delta.time.hours = NA, delta.time.days = NA) # introduce column specifying independence of records if(minDeltaTime == 0) { intable$independent <- TRUE # all independent if no temporal filtering } else { intable$independent <- NA } for(xy in 1:nrow(intable)){ # for every record # set independent = TRUE if it is the 1st/only record of a species / individual if(camerasIndependent == TRUE){ if(intable$DateTimeOriginal[xy] == min(intable$DateTimeOriginal[which(intable[, columnOfInterest] == intable[xy, columnOfInterest] & intable[, stationCol] == intable[xy, stationCol] & intable[, cameraCol] == intable[xy, cameraCol]) ])){ # cameras at same station assessed independently intable$independent[xy] <- TRUE intable$delta.time.secs[xy] <- 0 } } else { if(intable$DateTimeOriginal[xy] == min(intable$DateTimeOriginal[which(intable[, columnOfInterest] == intable[xy, columnOfInterest] & intable[, stationCol] == intable[xy, stationCol]) ])){ intable$independent[xy] <- TRUE intable$delta.time.secs[xy] <- 0 } } if(is.na(intable$delta.time.secs[xy])) { # if not the 1st/only record, calculate time difference to previous records of same species at this station if(deltaTimeComparedTo == "lastIndependentRecord"){ if(camerasIndependent == TRUE){ which_time2 <- which(intable[, columnOfInterest] == intable[xy, columnOfInterest] & # same species/individual intable[, stationCol] == intable[xy, stationCol] & # at same station intable[, cameraCol] == intable[xy, cameraCol] & # at same camera intable$independent == TRUE & # independent (first or only record of a species at a station) intable$DateTimeOriginal < intable$DateTimeOriginal[xy]) # earlier than record xy } else { which_time2 <- which(intable[, columnOfInterest] == intable[xy, columnOfInterest] & intable[, stationCol] == intable[xy, stationCol] & intable$independent == TRUE & intable$DateTimeOriginal < intable$DateTimeOriginal[xy]) } } else { if(camerasIndependent == TRUE){ which_time2 <- which(intable[, columnOfInterest] == intable[xy, columnOfInterest] & intable[, stationCol] == intable[xy, stationCol] & intable[, cameraCol] == intable[xy, cameraCol] & intable$DateTimeOriginal < intable$DateTimeOriginal[xy]) } else { which_time2 <- which(intable[, columnOfInterest] == intable[xy, columnOfInterest] & intable[, stationCol] == intable[xy, stationCol] & intable$DateTimeOriginal < intable$DateTimeOriginal[xy]) } } # time difference to last (independent) record diff_tmp <- min(na.omit(difftime(time1 = intable$DateTimeOriginal[xy], # delta time to last independent record time2 = intable$DateTimeOriginal[which_time2], units = "secs"))) # save delta time in seconds intable$delta.time.secs[xy] <- diff_tmp if(intable$delta.time.secs[xy] >= (minDeltaTime * 60) | intable$delta.time.secs[xy] == 0){ # | intable$delta.time.secs[xy] == 0 intable$independent[xy] <- TRUE } else { intable$independent[xy] <- FALSE } } # end if(intable$DateTimeOriginal[xy] == min(...)} else {...} } # end for(xy in 1:nrow(intable)) if(removeNonIndependentRecords){ # keep only independent records outtable <- intable[intable$delta.time.secs >= (minDeltaTime * 60) | intable$delta.time.secs == 0,] } else { outtable <- intable } return(outtable) } # 4. use function records_filter30_min<- assessTemporalIndependence(intable = RT_Lycosid, deltaTimeComparedTo = "lastIndependentRecord", #"lastRecord", # "lastIndependentRecord", columnOfInterest = "Species", stationCol = "Station", cameraCol = "CameraID", camerasIndependent = TRUE, minDeltaTime = 30, # 30min removeNonIndependentRecords = TRUE) # create delta.time.mins # additional "delta.time..." columns, but only "delta.time.secs" has data, the rest is NA still # (divide by 60 / 60*60 / 60 * 60 * 24 to compute mins / hours / days) records_filter30_min$delta.time.mins <- records_filter30_min$delta.time.secs/60 records_filter30_min$delta.time.hours <- records_filter30_min$delta.time.secs/(60*60) records_filter30_min$delta.time.days <- records_filter30_min$delta.time.secs/(60*60*24) # write new record table to csv write.csv(records_filter30_min,"E:/JOURNAL_MANUSCRIPTS/PeerJ/Data&Analyses//RT_Lycosid_delta_time.csv", row.names = FALSE) # _____________________________________________________________________________________________ # ## Species Presence Maps # map of the number of independent detections of the observed species Lycosids <- read.csv("RT_Lycosid_delta_time.csv", stringsAsFactors = FALSE) SD_camtraps <- read.csv("cams2016JulOct.csv", stringsAsFactors = FALSE) Maplycosids <- detectionMaps(CTtable = SD_camtraps, recordTable = Lycosids, Xcol = "Easting", Ycol = "Northing", stationCol = "Station", speciesCol = "Species", speciesToShow = "Lycosid", printLabels = FALSE, richnessPlot = FALSE, speciesPlots = TRUE, addLegend = TRUE, # writePNG = TRUE, # plotDirectory = "E:/JOURNAL_MANUSCRIPTS/PeerJ/Data&Analyses" ) # the number of independent observations depends on the argument 'mindeltaTime' # in the 'recordTable' function. # __________________________________________________________________________________________ ## Visualising Species Activity Data # Radial Plot activityRadial(recordTable = records_filter30_min, allSpecies = TRUE, writePNG = TRUE, plotDirectory = "E:/JOURNAL_MANUSCRIPTS/PeerJ/Data&Analyses", lwd = 5) # adjust line with of the plot # ______________________________________________________________________________________ # Plots for different factors # burned burned <- subset(records_filter30_min, Habitat==“burned” activityRadial(recordTable = burned, allSpecies = TRUE, byNumber = TRUE, writePNG = TRUE, plotDirectory = "E:/JOURNAL_MANUSCRIPTS/PeerJ/Data&Analyses", lwd = 5) # unburned unburned <- subset(records_filter30_min, Habitat==“unburned” activityRadial(recordTable = unburned, allSpecies = TRUE, byNumber = TRUE, writePNG = TRUE, plotDirectory = "E:/JOURNAL_MANUSCRIPTS/PeerJ/Data&Analyses", lwd = 5) # crest crest <- subset(records_filter30_min, Location==“crest” activityRadial(recordTable = crest, allSpecies = TRUE, byNumber = TRUE, writePNG = TRUE, plotDirectory = "E:/JOURNAL_MANUSCRIPTS/PeerJ/Data&Analyses", lwd = 5) # swale swale <- subset(records_filter30_min, Location==“swale” activityRadial(recordTable = swale, allSpecies = TRUE, byNumber = TRUE, writePNG = TRUE, plotDirectory = "E:/JOURNAL_MANUSCRIPTS/PeerJ/Data&Analyses", lwd = 5)