knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(escapement)
escapement
?escapement
is an R package developed by the USFWS Alaska Inventory and Monitoring Program for estimating salmon escapement from photo and video double-observer count data. It was developed for estimating sockeye salmon minimum escapement for the Alalura River at Kodiak National Wildlife Refuge, Alaska. The functions follow methods published in Deacy et al. 2016.
The escapement
package requires several programs to function:
R (>=4.0)
the tinytex
package. To install it from R:
a. if (!require("tinytex")) install.packages("tinytex")
b. tinytex::install_tinytex()
To install and load the package:
if (!require("devtools")) install.packages("devtools") devtools::install_github("USFWS/escapement", ref = "master", build_vignettes = TRUE) library(escapement)
The package includes functions to:
Import a raw CSV file containing hourly counts of salmon quantified from photos and videos (import_format()
),
Evaluate a candidate list of competing models (linear, segmented, polynomial, and polynomial segmented) to estimate salmon escapement (model_escapement()
),
Generate tables and plots of the results, and
Produce a dynamic parameterized report (PDF) of the results (run_report()
).
To find a help file for any function in escapement
, type ?
and the name of the function. For example, to get help on how to use the import_format()
function, type ?import_format
.
There are two general approaches to using escapement
and selecting which approach to take depends on your needs. The first approach ("step-wise"), consists of using individual functions to work through the steps of importing and formatting input data, performing a statistical analysis, and creating tables and plots. This approach would be favored if the user is interested in examining the details of the analysis, would like to perform additional analyses not included in the package, or would like to export the tables and plots for a customized reporting format. The second approach ("all in one") performs all the above steps and compiles the results into a PDF report with run_report()
function. This approach would be favored for annual reporting of results, specific to Kodiak Refuge's Akalura salmon escapement survey. Below are more details on the two approaches.
import_format()
The first step to using any functions in escapement
is to import and format data. This is done using the import_format()
function, which takes in a CSV of data and returns a formatted R data frame. The function requires a file directory path to a CSV containing properly formatted data. The input CSV should contain the following three columns:
date
Sequential hourly date values (YYYY-MM-DD hh:mm:ss) that range from the beginning of a summer season to the end of a summer season. Multiple years of data within a single CSV are acceptable. photo
Numeric values describing the number of salmon counted from photos. Any missing values should contain NA
.video
Numeric values describing the number of salmon counted from videos. Any missing values should contain NA
.You can find the file path to an example of a properly formatted CSV by entering the following into the console:
system.file("extdata", "salmon_counts.csv", package = "escapement")
To import the example CSV data using the import_format()
function, and store the output as an R data frame dat
in the Global Environment:
dat <- import_format(system.file("extdata", "salmon_counts.csv", package = "escapement"))
You can then view dat
data frame with View(dat)
.
dat <- import_format(system.file("extdata", "salmon_counts.csv", package = "escapement")) head(dat)
Once you have a formatted data frame of hourly photo and video counts (See Step 1 for how to import and format data), you can analyze the data using escapement
. This is done using two functions: model_escapement()
and boot_escapement()
This function takes in a formatted data frame generated from import_format()
, creates a list of candidate models, evaluates these models $\Delta$AICc, and returns a list of objects described in detail below.
Analyzing data in our dat
data frame that we imported above would be done using:
our_models <- model_escapement(dat)
Taking a look inside the resulting our_models
list:
summary(our_models)
From this you can see that the model_escapement()
function requires an R data frame (returned from import_format()
) and returns a list of three objects:
escapement
runs four candidate models: a linear, polynomial, linear segmented, and polynomial segmented model. Continuing with our example above, you can view a summary of a model by typing the name of the list returned by model_escapement
, selecting the models
element, and then selecting the name of the model. For example:summary(our_models$models$linear)
our_models$aic_table
our_models$top_model
The boot_escapement
function takes a data frame (produced from import_format()
and a list of models (produced from model_escapement()
and calculates 95% confidence intervals around the point estimates of salmon escapement estimates from the top model. Running this function returns a list containing two objects: a summary table of annual escapement estimates with 95% CIs (summary
) and the raw bootstrapped results (raw_boots
):
boots <- boot_escapement(dat, our_models) summary(boots)
Here's the summary
table from our example:
boots$summary
You can explore the raw results by digging into raw_boots
. This list contains how the bootstraps were called, the model specified, and a matrix with a row for each bootstrap replicate of the results. For more information, see the help file for the boot
function in the boot
package here.
The third step in the step-wise approach is creating tables and plots of results. See above (aic_table) for a summary table of AICc results. Below is a brief description of the custom plotting functions in escapement
.
This function returns a list containing two ggplot2
plots. The first (photo_counts) is series of line plot of the raw hourly photo counts over time, grouped by year. The second (hourly_counts) is a plot of the average photo counts for each hour of day summarized by year.
p <- explore_plots(dat) p$photo_counts p$hourly_counts
This function uses the model results from model_escapement
and returns a ggplot2
scatter plot of video and photo counts with a fitted line from the top model.
plot_topmodel(our_models)
This function used the bootstrap results from boot_estimates()
and returns a list of ggplot2
plots: density_estimates
and min_escapement
.
The density_estimates
plot is a density plot of the distribution of bootstrapped estimates of annual salmon escapement grouped by year:
p <- plot_boot_escapement(boots) p$density_estimates
The min_escape
plot shows minimum escapement point estimates and 95% CI bars, grouped by year:
p$min_escape
This function uses the salmon count data frame (from import_format()
) and the model results from model_escapement
to generate a list of ggplot2
plots (daily
and cumul_daily
).
The daily
plot shows estimates of daily salmon passage over time, grouped by year:
p <- plot_daily(dat, our_models) p$daily
The cumul_daily
plot shows estimates of cumulative daily salmon passage over time, grouped by year:
p$cumul_daily
This function uses the salmon count data frame (from import_format()
) and the model results from model_escapement
to generate a ggplot2
faceted plot showing average hourly estimates of salmon passage within a day, grouped by year.
plot_hourly(dat, our_models)
This is a function developed to generate a rmarkdown report that summarizes annual salmon escapement results. It relies on many of the above functions for importing, formatting, analyzing, performing model selection, and generating tables and figures. To run the function, simply run:
run_report()
This will open a Shiny interface that looks like this:
knitr::include_graphics("../inst/rmd/images/report_interface.jpg")
If necessary, edit the default parameter values for the report title, subtitle, author, refuge, email, and phone number.
Then, under "Input dataset", browse to the location of the csv file containing your hourly photo and video count data (the csv should be formatted as required above for the import_format
function).
Next, under "Discussion and Recommendations", browse to a txt file containing your discussion and recommendations section of the report. It is recommended to leave this input empty when you first run a report. After you have reviewed the results in initial report, then draft a discussion and recommendations section in MS Word. Save a copy of this Word document as a txt file. NOTE: do this by opening a new txt file, copying the text from your Word document into this txt file, and saving it. You then rerun run_report
with your discussion and recommendations included.
If you have citations in your discussion and recommendations section that are not found elsewhere in the document, you will need to add these references to the bibliography. To do this, you will need to update the default bibtex file located in the escapement
package. You can locate that file with the following command:
system.file("rmd", "bibliography.bibtex", package = "escapement")
This default bibtex file can be edited manually using a text editor or using a bibliographic software package (e.g., EndNote). Once the new references have been added to the default bibtex file, replace the default bibtex file with the updated version.
You cite a reference in the body of the discussion and recommendation section by adding [@reference_id]
to the end of the sentence, where reference_id is the reference ID listed at the top of each bibtex record. For example, to cite Deacy et. al 2016, you would add the record ID for that article (deacy_etal_2016) into the body of the text (e.g.,"We following established methods [@deacy_etal_2016]
."). When the bibtex file is updated, this approach to inline citations will automatically add the citation to the bibliography section.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.