---
title: "3. Extracting Data from Camera Trapping Images and videos, creating occupancy & secr input"
author: Juergen Niedballa (<camtrapr@gmail.com>)
date: "`r Sys.Date()`"
output:
  html_document:
    toc: true
    toc_float:
      collapsed: false
      smooth_scroll: false
    toc_depth: 2
vignette: >
  %\VignetteIndexEntry{3. Extracting data from camera trap images, creating occupancy & secr input}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, echo=FALSE, message = FALSE, results = "hide"}
exiftool_present <- Sys.which("exiftool") != ""
```

```{r , echo=FALSE, eval = !exiftool_present}
print("WARNING: THIS VIGNETTE WAS CREATED WITHOUT EXIFTOOL. OUTPUT IS INCOMPLETE SINCE ESSENTIAL FUNCTIONS DID NOT RUN!")
```

```{r message = FALSE, results = "hide"}
library(camtrapR)
library(secr)
```

# Tabulating species and individual records: the recordTable functions

There are 2 function to tabulate species records after identification of species from images and videos.

* `recordTable` tabulates records of all images after species-level identification and 
* `recordTableIndividual` tabulates individuals of one species. 

Nevertheless, the underlying idea is the same. For each image, the date and time it was taken are read from the image's Exif metadata using ExifTool. Species or individual ID are read from the directory structure or image metadata (see vignette "Species and Individual Identification"). Video data are extracted analogously. 


## `recordTable`: tabulating species records

`recordTable` is typically run after identifying species from images. It reads species IDs from the directory structure the images are placed in or from image metadata tags. 

First we define the directory containing our renamed, identified images 

```{r}
# find the directory with sample images contained in the package
wd_images_ID <- system.file("pictures/sample_images_species_dir", package = "camtrapR", lib.loc = .libPaths())
```

and see how many JPG images we have (this is not necessary, but informative here).

```{r}
length(list.files(wd_images_ID, pattern = "JPG", recursive = TRUE))
```

Now we can run `recordTable`. Here is a minimal example:

```{r eval = exiftool_present}
rec.db.species0 <- recordTable(inDir  = wd_images_ID,
                               IDfrom = "directory")

head(rec.db.species0)
```

Argument `IDfrom` tells the function to look for species directories within the station directories and to take species IDs from these. `IDfrom` must be set to "metadata" if metadata tagging was used for species identification.

By default, the function returns all records. Nevertheless, we get a data frame containing `r ifelse(exiftool_present, nrow(rec.db.species0), 56)` records, less than the number of images in the image directory (`r length(list.files(wd_images_ID, pattern = "JPG", recursive = TRUE))`). This is because a number of images were taken at the same time (measured with 1 minute precision by these cameras) and the function removes duplicate records.

It may suffice to illustrate this with the Pig-tailed macaque images from stationB:

```{r}
list.files(file.path(wd_images_ID, "StationB", "MNE"))
```

There are, amongst others, 3 images taken at 17:48:00 on the 28th of April 2009. Of these, only 1 will be returned.


Here is what the columns of the record table contain:

| column  | content  |
|-------|-------------------------------------------------------------|
| `Station`   |   the station the image is from |
| `Species`   |   species name |
| `DateTimeOriginal` |  Date and time of record in R-readable format  |
| `Date` |  record date  |
| `Time` |  record time of day |
| `delta.time.secs`  |  time difference between record and last (independent) record of same species at same station / camera* (in seconds) |
| `delta.time.mins`  |  time difference between record and last (independent) record of same species at same station / camera* (in minutes)  |
| `delta.time.hours` |  time difference between record and last (independent) record of same species at same station / camera* (in hours)   |
| `delta.time.days`  |  time difference between record and last (independent) record of same species at same station / camera* (in days)   |
| `Directory` |  directory the image is in |
| `FileName`   |  image file name |

*see below: Independence between cameras within stations

### Temporal independence between records

Imagine a species that loves to hang out in front of your cameras. You will end up with hundreds of shots of the same species, maybe even the same animal. Therefore, images can be filtered using an adjustable criterion for temporal independence between subsequent records of the same species in an attempt to remove non-independent records. This is achieved via argument `minDeltaTime`. It is the minimum time difference (in minutes) between two records of the same species at the same station which are to be considered independent. The default is 0, causing the function to return all records. Setting it to a higher number, e.g. 60 (i.e., 1 hour), is commonly done to thin the number of records. Note that you will not lose records of different species, even if they fall within the specified time interval from a record.

The argument `deltaTimeComparedTo` further controls how independence between records is assessed. Setting it to "lastRecord" returns only records taken `minDeltaTime` minutes after the last record. Setting it to "lastIndependentRecord" returns only records taken `minDeltaTime` minutes after the last *independent* record.

Let's now apply an 1-hour independence criterion and define a time zone.


```{r eval = exiftool_present}
rec.db.species60 <- recordTable(inDir               = wd_images_ID,
                                IDfrom              = "directory",
                                minDeltaTime        = 60,
                                deltaTimeComparedTo = "lastRecord",
                                timeZone            = "Asia/Kuala_Lumpur")

nrow(rec.db.species60)
```

Now `r ifelse(exiftool_present, nrow(rec.db.species60), 40)` records were returned instead of `r ifelse(exiftool_present, nrow(rec.db.species0), 56)`. The missing records were taken less than 1 hour after the prior record of the same species at the same station and therefore omitted.


### Time zones and daylight saving time

Note the warning about the missing time zone in the minimal example above. By default, camtrapR will assume UTC time zone. This should work well in most situations (even though the time zone may not be correct, strictly speaking), because UTC does not use daylight saving time (DST, aka 'summer time'), and camera traps normally don't use time zones (hence, camera traps will normally not respect DST of the area you work in and will not adjust image timestamps accordingly).

Now, if you work in an area that uses DST, and your camera does not know about DST, things can go wrong in various ways and you may end up with a systematic 1-hour offset. If you set your cameras up during DST (in summer) and set the internal clock accordingly, all records taken in winter (non-DST period) will be recorded as 1 hour later than according to actual clock time. If you set your cameras up during winter (not DST) and set the internal clock accordingly, all records taken in summer (DST period) will be recorded as 1 hour earlier than according to actual clock time. 

In addition if you specify a time zone with DST and your cameras don't respect it, records may fall into the non-existing hour when clocks are advanced in spring, leading to an error in interpreting the date/time.

So, the question whether or not your cameras record the time zone becomes very important. Here is some recommendations on how to use the argument `timeZone`.

If your cameras don't save the time zone, and your time zone does not have DST, set argument `timeZone` to your study area time zone
If your cameras don't save the time zone, and your time zone has DST, leave argument `timeZone` at the default UTC.
If your cameras save the time zone and adjust image timestamps accordingly, set argument `timeZone` to your study area time zone (both if your area has DST or not). 


In other words, it is advised to set argument `timeZone` to your study area's time zone (one of `OlsonNames()`), *unless* the time zone of your study area has DST, but your cameras don't record it. 


### Independence between cameras within stations

The issue of temporal independence between records becomes slightly more complex if more than one camera was operated at stations. That information can and should be included in the output of `recordTable` (and `recordTableIndividual`). Users can then decide whether temporal independence is to be assessed within stations or within cameras at each station. In the first case, argument `camerasIndependent` must be set to FALSE. 2 images taken at different 2 cameras at the same station within `minDeltaTime` minutes will be reported as 1 record in the record table (suitable if cameras were places in pairs). In the second case `camerasIndependent` must be set to TRUE and 2 images taken at different 2 cameras at the same station within `minDeltaTime` minutes will be reported as 2 record in the record table (suitable e.g., if cameras were located at some distance to one another and faced different trails).

The `cameraID` argument controls where camtrapR will look for camera IDs: in the file names (after renaming using `imageRename`, e.g. "renamedImages/StationA/StationA__**Camera1**__2015-12-31__23-59-59(1).JPG") or in the directory structure (e.g. renamedImages/StationA/**Camera1**/StationA__Camera1__2015-12-31__23-59-59(1).JPG"). If missing, it will be assumed there was only 1 camera per station.
  
  
### Ignoring species

Argument `exclude` can be used to ignore certain species. This is useful for omitting images in directories like "team" or "unidentified". Here is an example:

```{r eval = exiftool_present}
# see what species  we recorded
table(rec.db.species60$Species)

# remove "UNID" by setting argument exclude = "UNID"
rec.db.species60.exclude <- recordTable(inDir               = wd_images_ID,
                                        IDfrom              = "directory",
                                        minDeltaTime        = 60,
                                        deltaTimeComparedTo = "lastIndependentRecord",
                                        timeZone            = "Asia/Kuala_Lumpur",
                                        exclude             = "UNID")

# note that "UNID" is gone now
table(rec.db.species60.exclude$Species)

```



### Extracting image metadata

`recordTable` and `recordTableIndividual` can both extract additional metadata from images (apart from date and time). For example, some camera models record ambient temperature or moon phase, which may be of interest. Metadata tags are stored in the images at the time they are taken and can be accessed and extracted if their tag names are known. Some tag names are standardised (e.g. "DateTimeOriginal") while others are manufacturer-specific. Therefore, function `exifTagNames` returns all Exif metadata it finds in a sample image. Users can then choose which to extract with `recordTable` and `recordTableIndividual`.  


```{r eval = exiftool_present}
wd_images_ID <- system.file("pictures/sample_images_species_dir", package = "camtrapR")
exifTagNames(inDir = wd_images_ID)
```

The output table contains tag groups, tag descriptions, tag names and tag values. The values in `tag_group` help to unambiguously identify specific metadata tags.

Use the entries in `tag_name` (not `tag_description`) when providing values to the argument `additionalMetadataTags` in the`recordTable` functions.

Of the tags shown here, "DateTimeOriginal" contains the date and time that camtrapR reads out. Apart from that, there is little information of ecological interest in the example data. However, for demonstration purposes, let's extract information about the camera model and make. To be as precise as possible, we provide tags in the format: tag_group:tag_name (e.g. EXIF:Model)

```{r eval = exiftool_present}
rec.db.species.metadata1 <- recordTable(inDir                  = wd_images_ID,
                                        IDfrom                 = "directory",
                                        timeZone               = "Asia/Kuala_Lumpur",
                                        additionalMetadataTags = c("EXIF:Model", "EXIF:Make"))

head(rec.db.species.metadata1)
```

There are 2 additional columns containing the information from the metadata we requested.


## `recordTableIndividual`: tabulating individuals of a species

Individual identification of species is a prerequiste for running (spatial) capture-recapture models. `recordTableIndividual` and the subsequent `spatialDetectionHistory` function prepare data for these models.




```{r eval = exiftool_present}
# find the directory with tagged sample images contained in the package
wd_images_individual_ID <- system.file("pictures/sample_images_indiv_tag/LeopardCat", package = "camtrapR")
 # missing space in species = "LeopardCat" is because of CRAN package policies

 rec.db.pbe <- recordTableIndividual(inDir                  = wd_images_individual_ID,
                                     IDfrom                 = "metadata",
                                     minDeltaTime           = 60,
                                     deltaTimeComparedTo    = "lastIndependentRecord",
                                     hasStationFolders      = FALSE,         # images are not in station directories
                                     metadataIDTag          = "individual",  # the name of the metadata tag containing individual IDs
                                     timeZone               = "Asia/Kuala_Lumpur"
 )

```


### Extracting custom image metadata

In addition to the metadata that were saved when the image was taken (see above), custom metadata tags assigned in image management software can also be extracted. These may contain species ID tags if species were identified using metadata tags (instead of moving images into species directories), but also individual IDs, animal counts, sex of animals recorded, etc..



```{r eval = exiftool_present}
head(rec.db.pbe)
```


# Camera Operation 

The camera operation matrix is a day-by-station matrix that states how many cameras were active at a station on a given day. Rows represent stations and columns days, beginning with the day the first camera was set up and ending the day the last camera was retrieved.

```{r}
 # first load the camera trap station table

data(camtraps)

dateFormat <- "%d/%m/%Y"
# alternatively, use "dmy" (requires package "lubridate")
 
camop_problem <- cameraOperation(CTtable      = camtraps,
                                 stationCol   = "Station",
                                 setupCol     = "Setup_date",
                                 retrievalCol = "Retrieval_date",
                                 writecsv     = FALSE,
                                 hasProblems  = TRUE,
                                 dateFormat   = dateFormat
)

# as a reminder, these are the dates in our station information table
camtraps[,-which(colnames(camtraps) %in% c("utm_y", "utm_x"))]
# now let's have a look at the first few columns of the camera operation matrix
camop_problem[, 1:5]
# and the last few
camop_problem[, (ncol(camop_problem)-6):ncol(camop_problem)]
```

If stations were not set up, values are NA. If they were set up but malfunctioning, it is 0. Operational stations get value 1.


Here is a little function for plotting the camera operation matrix. Imagine there is a typo in one of your date fields and the setup or retrieval year is wrong. You will easily be able to spot it this way.

```{r}
camopPlot <- function(camOp, 
                      palette = "Heat"){
  
  which.tmp <- grep(as.Date(colnames(camOp)), pattern = "01$")
  label.tmp <- format(as.Date(colnames(camOp))[which.tmp], "%Y-%m")
  at.tmp <- which.tmp / ncol(camOp)
  
  values_tmp <- sort(na.omit(unique(c(camOp))))

  # hcl.colors only exists in R >3.6.0, use heat.colors for earlier versions
  if(getRversion() >= "3.6.0") {
    image_colors <- grDevices::hcl.colors(n = length(values_tmp), palette = palette, rev = TRUE)
  } else {
    image_colors <- heat.colors(n = length(values_tmp), rev = TRUE)
  }
  
  image(t(as.matrix(camOp)), xaxt = "n", yaxt = "n", col = image_colors)
  axis(1, at = at.tmp, labels = label.tmp)
  axis(2, at = seq(from = 0, to = 1, length.out = nrow(camOp)), labels = rownames(camOp), las = 1)
  abline(v = at.tmp, col = rgb(0,0,0, 0.2))
  box()
}
```

And this is what we get (in two different color palettes, on R 3.6.0 or higher).

```{r}
par(mfrow = c(1,2))
camopPlot(camOp = camop_problem)
camopPlot(camOp = camop_problem, palette = "Viridis")
```

```{r include = FALSE}
par(mfrow = c(1,2))
```

Note that the colors are stretched between the lowest and highest value. There is no dedicated color for 0, designating non-operational cameras. 

For a list of color palettes, see [here](https://developer.r-project.org/Blog/public/2019/04/01/hcl-based-color-palettes-in-grdevices/index.html).


## Saving and loading camera operation matrices

The camera operation matrix can easily be saved as a csv file (by setting argument `writecsv = TRUE`, `check.names = FALSE` and defining `outdir`). In order to load the csv into R again, it is necessary to tell R to use the station IDs (the first column) as row names:

```{r eval = FALSE}
camOp <- read.csv(file = ..., row.names = 1, check.names = FALSE)
```

`check.names = FALSE` ensures that column names (the dates) are read back into R as they are (e.g. "2015-12-01"). Otherwise one may end up with unreadable column names (at least for camtrapR) such as "X2015.12.01".

# Input for subsequent analyses (single-season)

It is very easy to prepare input for single-season occupancy and spatial capture-recapture (SCR) analyses in camtrapR. All one needs it a record table and the camera operation matrix. Making input for SCR analyses further requires the camera trap station table. 

## Occupancy analyses

Occupancy models use detection/non-detection matrices in which for every station and every occasion "1"" signifies a detection of a given species, "0"" signifies non-detecion, and NA missing data.

Here is how to obtain a detection/non-detection matrix using function `detectionHistory`. Because the function builds on prior functions (`recordTable` and `cameraOperation`) we also show these function here to provide the context.


```{r eval = exiftool_present}

# create camera operation matrix
camop_no_problem <- cameraOperation(CTtable      = camtraps,
                                    stationCol   = "Station",
                                    setupCol     = "Setup_date",
                                    retrievalCol = "Retrieval_date",
                                    hasProblems  = FALSE,
                                    dateFormat   = dateFormat
)

# define image directory
wd_images_ID <- system.file("pictures/sample_images_species_dir", package = "camtrapR")

# make record table
recordTableSample <- recordTable(inDir               = wd_images_ID,
                                 IDfrom              = "directory",
                                 minDeltaTime        = 60,
                                 deltaTimeComparedTo = "lastIndependentRecord",
                                 timeZone            = "Asia/Kuala_Lumpur"
)

# make detection history (without trapping effort)
DetHist1 <- detectionHistory(recordTable         = recordTableSample,
                            camOp                = camop_no_problem,
                            stationCol           = "Station",
                            speciesCol           = "Species",
                            recordDateTimeCol    = "DateTimeOriginal",
                            species              = "VTA",
                            occasionLength       = 7,
                            day1                 = "station",
                            includeEffort        = FALSE
)

DetHist1

```

Note the warning about the missing time zone (as in the functions `recordTable` and `recordTable`). Normally, it should be fine, but to be on the safe side, better set it to your study area's time zone.

If trapping effort is thought to influence detection probability, it can be returned by setting `includeEffort = TRUE`. This way the number of active trapping days per occasion and station is returned. 


```{r eval = exiftool_present}

# make detection history (with trapping effort)
DetHist2 <- detectionHistory(recordTable          = recordTableSample,
                             camOp                = camop_no_problem,
                             stationCol           = "Station",
                             speciesCol           = "Species",
                             recordDateTimeCol    = "DateTimeOriginal",
                             species              = "VTA",
                             timeZone             = "Asia/Kuala_Lumpur",
                             occasionLength       = 7,
                             day1                 = "station",
                             includeEffort        = TRUE,
                             scaleEffort          = FALSE
)

DetHist2[[1]]  # detection history
DetHist2[[2]]  # effort (in days per occasion)
```

To help with convergence of models, the effort matrix can be scaled to mean = 0 and sd = 1 by setting `scaleEffort = TRUE`. If `writecsv = TRUE`, the scaling parameters will also be saved in a separate csv file.

```{r eval = exiftool_present}

DetHist3 <- detectionHistory(recordTable          = recordTableSample,
                             camOp                = camop_no_problem,
                             stationCol           = "Station",
                             speciesCol           = "Species",
                             recordDateTimeCol    = "DateTimeOriginal",
                             species              = "VTA",
                             timeZone             = "Asia/Kuala_Lumpur",
                             occasionLength       = 7,
                             day1                 = "station",
                             includeEffort        = TRUE,
                             scaleEffort          = TRUE
)

DetHist3[[2]]  # effort (scaled)
DetHist3[[3]]  # scaling parameters for back-transformation

# backtransform scaled effort like this if needed
(DetHist3[[2]] * DetHist3[[3]]$effort.scaled.scale) + DetHist3[[3]]$effort.scaled.center
```

### Handling of incomplete occasions

The following table shows the behaviour of the `detectionHistory` function for different combinations of the function arguments `includeEffort` and `minActiveDaysPerOccasion` and different occasion-level camera operation values (table head, column 3-7). Depending on these arguments, incomplete occasions will either NA or have values of 0/1 (depending on whether there was a detection) in the output detection matrix .

| includeEffort  | minActiveDaysPerOccasion  | camera operation all 1 | at least one 1  | all 0 | 0 and NA |  all NA |
|---|---|---|---|---|---|---|---| 
| TRUE    |   not defined |  0/1  | 0/1     |  NA  |  NA |   NA | 
| TRUE    |   defined     |  0/1  | 0/1/NA* |  NA  |  NA |   NA | 
| FALSE   |   not defined |  0/1  | NA      |  NA  |  NA |   NA | 
| FALSE   |   defined     |  0/1  | 0/1/NA* |  NA  |  NA |   NA | 
*: NA if there were less active days in an occasion than `minActiveDaysPerOccasion`

The same applies to generation of input for spatial capture-recapture analyses using `spatialDetectionHistory` as described below.

### Saving and loading detection histories

The detection history and effort matrices can easily be saved as csv files (by setting argument `writecsv = TRUE` and defining `outdir`). In order to load the csv into R again, it is necessary to tell R to use the station IDs as row names:

```{r eval = FALSE}
detHist <- read.csv(file = ..., row.names = 1)
effort  <- read.csv(file = ..., row.names = 1)
```

## Spatial Capture-Recapture analyses

Input for spatial capture-recapture analyses can be generated in the form of `capthist`-objects as defined in the `secr` package with the function `spatialDetectionHistory`. Output can be in the form of counts (number of individual detections per occasion, argument, argument `output = "count"`) or binary (was an individual detected during an occasion, argument `output = "binary"`). note that the detector type will change accordingly: "proximity" if `output = "binary"` and "count" if `output = "count"`.



```{r}

data(recordTableIndividualSample)

# create camera operation matrix (with problems/malfunction)
camop_problem <- cameraOperation(CTtable      = camtraps,
                                 stationCol   = "Station",
                                 setupCol     = "Setup_date",
                                 retrievalCol = "Retrieval_date",
                                 writecsv     = FALSE,
                                 hasProblems  = TRUE,
                                 dateFormat   = dateFormat
)

sdh <- spatialDetectionHistory(recordTableIndividual = recordTableIndividualSample, 
                               species               = "LeopardCat",  
                               output                = "binary",
                               camOp                 = camop_problem, 
                               CTtable               = camtraps,
                               stationCol            = "Station", 
                               speciesCol            = "Species",
                               Xcol                  = "utm_x",
                               Ycol                  = "utm_y",
                               individualCol         = "Individual",
                               recordDateTimeCol     = "DateTimeOriginal",
                               recordDateTimeFormat  = "%Y-%m-%d %H:%M:%S",
                               occasionLength        = 10, 
                               day1                  = "survey",
                               includeEffort         = TRUE,
                               timeZone              = "Asia/Kuala_Lumpur"
  )
  
# missing space in species = "LeopardCat" was introduced by recordTableIndividual 
# (because of CRAN package policies). 
# In your own data you can have spaces in your directory names.

  summary(sdh)
  plot(sdh, tracks = TRUE)

```


# Input for subsequent analyses (multi-season)

Since version 1.2, camtrapR can prepare input for multi-season occupancy and spatial capture-recapture analyses (in unmarked and secr). The process is very similar to the one for single-season models. 

## Multi-season occupancy analyses

camtrapR prepares detection histories that can be used as input for argument `y` in function `unmarkedMultFrame`. `unmarkedMultFrame` creates the input for function `colext` which fits multi-season occupancy models.

For multi-season occupancy, we need a session column in the camera trap table. Records will be assigned to session automatically based on their dates.

First we simulate data for 2 seasons by duplicating the existing sample data and adjusting the dates (add 1 year)
```{r}
# load multi-season data
data(camtrapsMultiSeason)
data(recordTableSampleMultiSeason)

# also, for clarity, lets remove all unnecessary columns from the record table
recordTableSampleMultiSeason <- recordTableSampleMultiSeason[, c("Station", "Species", "DateTimeOriginal")]

# create camera operation matrix
camop_season <- cameraOperation(CTtable         = camtrapsMultiSeason,
                                    stationCol   = "Station",
                                    setupCol     = "Setup_date",
                                    sessionCol   = "session",
                                    retrievalCol = "Retrieval_date",
                                    hasProblems  = TRUE,
                                    dateFormat   = dateFormat
)

# plot camera operation matrix
par(oma = c(0,7,0,0))
camopPlot(camop_season)


# make multi-season detection history
DetHist_multi <- detectionHistory(recordTable   = recordTableSampleMultiSeason,
                            camOp                = camop_season,
                            stationCol           = "Station",
                            speciesCol           = "Species",
                            species              = "VTA",
                            occasionLength       = 10,
                            day1                 = "station",
                            recordDateTimeCol     = "DateTimeOriginal",
                            includeEffort        = TRUE,
                            scaleEffort          = FALSE,
                            timeZone             = "UTC",
                            unmarkedMultFrameInput = TRUE
)

DetHist_multi
```

Note that the function makes sure that all seasons have identical number of occasions, and that all stations are represented, even if they were not sampled in all seasons.

From here, the resulting object can be used in unmarked.

```{r}

year_matrix <- matrix(unique(as.character(camtrapsMultiSeason$session)),
               ncol = length(unique(as.character(camtrapsMultiSeason$session))),
               nrow = length(unique(camtrapsMultiSeason$Station)),
               byrow = TRUE)

# this is a made up example table with station covariates for demonstration
site_covariates <- data.frame(Station = rownames(DetHist_multi$detection_history),
                              elevation = c(100, 200, 500, 300),
                              treecover = c(80, 100, 50, 10))

umf <- unmarked::unmarkedMultFrame(y = DetHist_multi$detection_history,
                                   siteCovs = site_covariates,
                                   yearlySiteCovs = list(year = year_matrix),
                                   obsCovs = list(effort = DetHist_multi$effort),
                                   numPrimary = 2)



colext_example <- unmarked::colext(psiformula = ~ treecover,    # First-year occupancy
                                   gammaformula = ~ 1,          # Colonization
                                   epsilonformula = ~ 1,        # Extinction
                                   pformula = ~ effort,         # Detection
                                   data = umf,
                                   method="BFGS")
summary(colext_example)
```


## Multi-season spatial capture-recapture

For secr, session IDs must be a gapless sequence of numbers beginning with 1. So 2009 becomes season 1 and 2010 season 2

```{r}
camtrapsMultiSeason$session[camtrapsMultiSeason$session == 2009] <- 1
camtrapsMultiSeason$session[camtrapsMultiSeason$session == 2010] <- 2
```

```{r}

# we also want a few records in season 2
recordTableIndividualSample_season2 <- recordTableIndividualSample[1:10,]
recordTableIndividualSample_season2$DateTimeOriginal <- gsub("2009", "2010", recordTableIndividualSample_season2$DateTimeOriginal)
recordTableIndividualSample_season <- rbind(recordTableIndividualSample, recordTableIndividualSample_season2)



# for clarity, lets remove all unnecessary columns
recordTableIndividualSample_season <- recordTableIndividualSample_season[, c("Station", "Species", "Individual", "DateTimeOriginal")]

```


```{r}

# create camera operation matrix (with problems/malfunction), same as above for multi-season occupancy
camop_season <- cameraOperation(CTtable         = camtrapsMultiSeason,
                                    stationCol   = "Station",
                                    setupCol     = "Setup_date",
                                    sessionCol   = "session",
                                    retrievalCol = "Retrieval_date",
                                    hasProblems  = TRUE,
                                    dateFormat   = dateFormat
)


# create capthist object
sdh_multi <- spatialDetectionHistory(recordTableIndividual = recordTableIndividualSample_season,
                               species                 = "LeopardCat",
                               output                  = "binary",
                               camOp                   = camop_season,
                               CTtable                 = camtrapsMultiSeason,
                               stationCol              = "Station",
                               speciesCol              = "Species",
                               sessionCol              = "session",
                               Xcol                    = "utm_x",
                               Ycol                    = "utm_y",
                               individualCol           = "Individual",
                               recordDateTimeCol       = "DateTimeOriginal",
                               recordDateTimeFormat    = "%Y-%m-%d %H:%M:%S",
                               occasionLength          = 10,
                               day1                    = "survey",
                               includeEffort           = TRUE,
                               timeZone                = "Asia/Kuala_Lumpur",
                               stationCovariateCols    = "utm_y",       # made up, a potential site covariate from camtrapsMultiSeason
                               individualCovariateCols = "Individual"   # made up, a potential individual covariate from recordTable
  )


  summary(sdh_multi)
  par(mfrow = c(1,2))
  plot(sdh_multi)

  secr.fit.example <- secr.fit(capthist = sdh_multi,
                               start = c(-1, -2, 10))   # with starting values, since its only very little data
  summary(secr.fit.example)

```