---
title: "eBird_Chao1"
author: "Fang-Yu (Betty) Shen"
date: "2022-10-07"
output: html_document
---

Clear up working environment and set working directory
***You will need to request download eBird data here:                                 #https://science.ebird.org/en/use-ebird-data/download-ebird-data-products" ***

```{r}
rm(list = ls()) 
#To help set up working directory in RMD file
knitr::opts_chunk$set(echo = TRUE) 
knitr::opts_knit$set(root.dir = "~/Documents/Research/eBird_Chao1") #need to set your own path. Yours will look different

getwd()
```

Load libraries

```{r}
install.packages("auk") #package to deal with eBird data
install.packages("iNEXT") #package to deal with species richness estimating
install.packages("data.table") #package for transforming data format
library(auk) 
library(iNEXT)
library(data.table)

```
Read eBird raw data

```{r}
# eBird file name (I changed the original eBird file name to a shorter name)
eBird.data.file <- "eBird_BentonOR.txt"  

# set path to the eBird file 
auk::auk_set_ebd_path("~/Documents/Research/eBird_Chao1", overwrite = TRUE)

# Point out the exact files to use 
ebd <- auk_ebd("eBird_BentonOR.txt")  #This is identical to the eBird file name above
                                #You will need to request Cornell Lab to download eBird data
```

=====Set up filtering criteria and filter eBird data==========
***Note the following filter criteria is an example. Your filter criteria will need to meet your research goal*** 

```{r}
#output text file
f_out <- "ebd_filtered_BentonOR.txt"

# Define" filters to select which data we want 
ebd_filters <- ebd %>% 
  auk_country("US")%>% #select country
  auk_state("US-OR") %>% #select state or county
  auk_year(2021) %>% #select year
  auk_date(c("2021-03-01", "2021-8-31")) %>% #select date
  auk_protocol(c("Stationary", "Traveling")) %>% #select survey protocol
  auk_complete() %>% #select complete checklist 
  auk_filter(file = f_out, overwrite = T) %>% #direct to output file
  read_ebd() #read eBird data after filtering


```

=============Transform eBird format to run iNEXT============


```{r}

##Delete the checklists which include X at the observation count====##
eb.X <- ebd_filters[ebd_filters$observation_count=="X",] #Pick up observation count = "X"
ebd_update  <- ebd_filters[!ebd_filters$sampling_event_identifier %in% eb.X$sampling_event_identifier,] #remove those checklists containing X in observation count

##Convert column "observation_count" to numeric
ebd_update[,"observation_count"] <- lapply(ebd_update[,"observation_count"], as.numeric)

##Aggregate abundance data by species x checklist matrix, because we currently only have a species for each row
 #Note: checklist is based on checklist identifier number (Group checklist start with letter "G"; Single person checklist start with letter "S")

eBird.re <- reshape2::dcast(setDT(ebd_update), scientific_name ~ checklist_id, value.var="observation_count", FUN = sum) 

 #Replace "NA" to "0"
eBird.re[is.na(eBird.re)] <- 0

#Make the first species column into rownames
eBird.2 <- eBird.re[,-1]   
rownames(eBird.2) <- eBird.re[,1] 

```


=============Run iNEXT to perfom Chao1 estimator===================
Note: If your iNEXT shows "This site has only one species. Estimation is not robust", you may need to compile few spatially close related eBird checklist to do run iNEXT. In our paper, we compile eBird checklists that fall within each 2x2 km Breeding Bird Survey site.

```{r}
#Running iNEXT
eBird.iNEXT <- iNEXT(eBird.2, q = 0, datatype = "abundance", endpoint = 200) # "endpoint" means the sample effort which you want to standardize in rarefaction curve

rare.eB <- eBird.iNEXT$iNextEst   # You can use "$" sign to see the specific rarefaction result
data.info.eB <- eBird.iNEXT$DataInfo
asymptotic.eB <- eBird.iNEXT$AsyEst

#Extract Chao 1 species richness estimation
spe.est.eB <- asymptotic.eB[asymptotic.eB$Diversity == "Species richness", ] 
```
