Appendix 6 R script. Analyzing camera trap data

# First we load the required libraries
library(behaviouR)
library(ggpubr)

# We will be taking random subsets of the camera trap photos, and we use
# 'set.seed' to make sure that our results are reproducible.
set.seed(2210)

# We can use the following code to query the database by season to see which
# photos are available. The available seasons are 1-4, 6-8,10,11.  This function
# will return a table with all of the available camera trap photos for a
# particular season.
CameraTrapAnnotations(season = 1)

# Now we will use another function to download the camera trap photos and save
# them locally to our computer.  You can change the season
CombinedAnimalDF <- CameraTrapDataAccess(urlpath = "https://lilablobssc.blob.core.windows.net/snapshotserengeti-unzipped/", 
    season = list(1, 2), AnimalID = "zebra", NumPhotos = 5, create.dir = TRUE)

# Here we isolate only the columns from the dataframe that we need.
CombinedAnimalDF <- CombinedAnimalDF[, c("category_id", "season", "location", "filename")]

# Here is the function to view photos and annotate data.
CombinedAnimalDF_TimeAdded <- CameraTrapDataCollection(inputfile = CombinedAnimalDF, 
    rowstart = 8, dataframe.cont = FALSE, option = "Plot")

CombinedAnimalDF_TimeAdded

# Optional: You can save your spreasheet using the following code:
# write.csv(CombinedAnimalDF_TimeAdded,'CombinedAnimalDF_TimeAdded.csv)

# You can then load it back in later using the following code.  CombinedDFTimes
# <- read.csv('CombinedAnimalDF_TimeAdded.csv')

# Now we load your data sheet with the times added NOTE: we are using sample data
# here; you will want to use your own data
data("CombinedDFTimes")

# CombinedDFTimes <- CombinedAnimalDF_TimeAdded

# Let's check the structure
head(CombinedDFTimes)

# Now we can make a density plot that will show the distribution of camera trap
# photos that were taken over 24-hours. We add the fill = 'category_id' so that
# we show different distributions for each animal.
ggdensity(data = CombinedDFTimes, x = "Time", fill = "category_id") + xlab("Time (24 hrs)") + 
    ylab("Density")

# Question 1. What do you notice about the overlap of the two density curves?
# Does it look like there is temporal niche partitioning?

# Now we can calculate an overlap coefficient which can be used to investigate
# potential competitive and interaction possibilities between species.The value
# ranges from ranges from 0 (no overlap) to 1 (complete overlap).

# First we subset our data to focus on the first animal in our dataset
FirstAnimal <- subset(CombinedDFTimes, category_id == "zebra")

# Then we subset our data to focus on the second animal in our dataset
SecondAnimal <- subset(CombinedDFTimes, category_id == "wildebeest")

# Now we use the overlap function to calculate the overlap coefficient
bayestestR::overlap(FirstAnimal$Time, SecondAnimal$Time)

# Question 2. How do you interpret the overlap coefficient for your data?

data("CombinedPartnerDFTimes")

# Now we will read in our partners data. NOTE that you need to make sure the name
# of the .csv file is exactly as shown below. CombinedPartnerDFTimes <-
# read.csv('CombinedAnimalDF_TimeAddedPartner.csv')

# Let's check the structure
head(CombinedPartnerDFTimes)

# Now we can make a density plot for our partner's data.
ggdensity(data = CombinedPartnerDFTimes, x = "Time", fill = "category_id") + xlab("Time (24 hrs)") + 
    ylab("Density")

# Question 3. What do you notice about the overlap of the two density curves for
# your partner's data? Does it look like there is temporal niche partitioning?

# Now we can calculate an overlap coefficient which can be used to investigate
# potential competitive and interaction possibilities between species.The value
# ranges from ranges from 0 (no overlap) to 1 (complete overlap).

# First we subset our data to focus on the first animal in your partner's data
# set
FirstAnimalPartner <- subset(CombinedPartnerDFTimes, category_id == "cheetah")

# Then we subset our data to focus on the second animal in our dataset
SecondAnimalPartner <- subset(CombinedPartnerDFTimes, category_id == "leopard")

# Now we use the overlap function to calculate the overlap coefficient
bayestestR::overlap(FirstAnimalPartner$Time, SecondAnimalPartner$Time)

# Question 4. What is the overlap coefficient for your partner's data?  How do
# you interpret this?

# Now we will combine your data with your partners
AllDataCombined <- rbind.data.frame(CombinedDFTimes, CombinedPartnerDFTimes)

# And we plot the diel activity patterns for all four species
ggdensity(data = AllDataCombined, x = "Time", fill = "category_id", facet.by = "category_id") + 
    xlab("Time (24 hrs)") + ylab("Density")

# Question 5. Based on the activity patterns and your understanding of the
# Serengeti food web how do you interpret these results? Is there evidence of
# temporal niche partitioning among potential competitors?  What about
# interactions between potential predators and prey?