Skip to content

Commit

Permalink
vignettes updating
Browse files Browse the repository at this point in the history
  • Loading branch information
Smith authored and Smith committed Oct 1, 2024
1 parent 878b153 commit 3e054b7
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 31 deletions.
6 changes: 4 additions & 2 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,16 @@ check_regions <- function(regions, stratify_by, stratify_type,
msg <- "Stratification does not match desired regions:\n"

if(stratify_by %in% c("bcr", "latlong") &
any(regions %in% c("country", "prov_state"))) {
any(regions %in% c("country", "prov_state"))
& stratify_type != "custom" & is.null(regions_index)) {
stop(msg,
"BCRs and lat-long degree block stratifications cannot be divided ",
"into regions with political boundaries ('country', 'prov_state').",
call. = FALSE)
}

if(stratify_by == "prov_state" & "bcr" %in% regions){
if(stratify_by == "prov_state" & "bcr" %in% regions
& stratify_type != "custom" & is.null(regions_index)){
stop(msg,
"The States and Provinces stratification",
"cannot be divided into BCR regions.",
Expand Down
43 changes: 42 additions & 1 deletion R/generate-trends.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,14 @@ generate_trends <- function(indices,
max_year = NULL,
quantiles = c(0.025, 0.05, 0.25, 0.75, 0.95, 0.975),
slope = FALSE,
gam = FALSE,
prob_decrease = NULL,
prob_increase = NULL,
hpdi = FALSE) {

# Checks
check_data(indices)
check_logical(slope, hpdi)
check_logical(slope, hpdi, gam)
check_numeric(quantiles)
check_numeric(min_year, max_year, quantiles, prob_decrease, prob_increase,
allow_null = TRUE)
Expand Down Expand Up @@ -263,6 +264,26 @@ generate_trends <- function(indices,
.data[[paste0("slope_trend_q_", q1)]])
}


# Optional gam based trends
if(gam) {
trends <- trends %>%
dplyr::mutate(
sl_t = purrr::map(.data$n, calc_gam,
.env$min_year_num, .env$max_year_num),
gam_trend = purrr::map_dbl(.data$sl_t, stats::median),
gam_trend_q = purrr::map_df(
.data$sl_t, ~stats::setNames(
calc_quantiles(.x, quantiles, names = FALSE),
paste0("gam_trend_q_", quantiles)))) %>%
tidyr::unnest("gam_trend_q") %>%
dplyr::mutate(
"width_of_{q}_percent_credible_interval_gam" :=
.data[[paste0("gam_trend_q_", q2)]] -
.data[[paste0("gam_trend_q_", q1)]])
}


# Model conditional probabilities of population change during trends period
if(!is.null(prob_decrease)) {
trends <- trends %>%
Expand Down Expand Up @@ -292,6 +313,7 @@ generate_trends <- function(indices,
dplyr::starts_with("trend"),
dplyr::starts_with("percent_change"),
dplyr::starts_with("slope"),
dplyr::starts_with("gam"),
dplyr::starts_with("width"),
dplyr::starts_with("prob"),
"rel_abundance", "obs_rel_abundance",
Expand Down Expand Up @@ -325,6 +347,25 @@ calc_slope <- function(n, min_year_num, max_year_num) {
as.vector((exp(m) - 1) * 100)
}

gam_sl <- function(i, wy) {
df <- data.frame(i = i,
y = 1:length(i))
sm <- mgcv::gam(data = df,
formula = i~s(y))
smf <- sm$fitted.values[wy]
ny <- wy[2]-wy[1]
smt <- (smf[2]-smf[1])*(1/ny)
}

calc_gam <- function(n, min_year_num, max_year_num) {
wy <- c(min_year_num,max_year_num)

ne <- log(n)
m <- t(apply(ne, 1, FUN = gam_sl, wy))

as.vector((exp(m) - 1) * 100)
}

calc_prob_crease <- function(x, p, type = "decrease") {
if(type == "decrease") f <- function(p) length(x[x < (-1 * p)]) / length(x)
if(type == "increase") f <- function(p) length(x[x > p]) / length(x)
Expand Down
4 changes: 2 additions & 2 deletions R/prepare-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ prepare_data <- function(strata_data,
check_numeric(min_n_routes, min_max_route_years, min_mean_route_years)
check_logical(quiet)
#warning if stratification == latlong and min_n_routes > 1
if(strata_data$meta_data$stratify_by == "latlong" & min_n_routes == 3){
if(strata_data$meta_data$stratify_by == "latlong" & min_n_routes > 1){
warning("Many strata with data may have been excluded ",
"With latlong stratification, most strata have ",
"only 1 route. You may wish to set min_n_routes = 1",
Expand All @@ -71,7 +71,7 @@ prepare_data <- function(strata_data,
# Add in routes
obs <- strata_data$routes_strata %>%
dplyr::select("country_num", "state_num", "state", "rpid", "bcr", "year",
"strata_name", "route", "obs_n") %>%
"strata_name", "route", "obs_n","latitude","longitude") %>%
dplyr::left_join(obs, by = c("route", "rpid", "year")) %>%
dplyr::mutate(count = tidyr::replace_na(.data$count, 0))

Expand Down
8 changes: 8 additions & 0 deletions R/run-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,17 @@ run_model <- function(model_data,
meta_data <- model_data[["meta_data"]]
raw_data <- model_data[["raw_data"]]
meta_strata <- model_data[["meta_strata"]]

init_values <- model_data[["init_values"]]
folds <- model_data[["folds"]]
model_data <- model_data[["model_data"]]

if(model_data$n_strata < 2){
stop("The data have only 1 stratum. bbsBayes2 models require multiple strata.
If there are sufficient routes with data, you can try an alternate stratification
(e.g., latlong) where the routes may be redistributed among > 1 strata.")
}

species <- stringr::str_remove_all(meta_data[["species"]],
"[^[[:alpha:]]]")

Expand Down
2 changes: 1 addition & 1 deletion data-raw/data_exported.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ usethis::use_data(species_forms, overwrite = TRUE)


species_notes <- readr::read_csv("data-raw/species_notes.csv",
col_types = "ncc", locale = readr::locale(encoding = "latin1"),
col_types = "ccnnc", locale = readr::locale(encoding = "latin1"),
progress = FALSE) %>%
as.data.frame()

Expand Down
75 changes: 75 additions & 0 deletions data-raw/example_model_fits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

library(bbsBayes2)
library(tidyverse)

species <- "Scissor-tailed Flycatcher"
species <- "Arctic Warbler"

# extract the unique numerical identifier for this species in the BBS database
species_number <- search_species(species) %>%
select(aou) %>%
unlist()

s <- stratify("bbs_usgs",
species = species) %>%
prepare_data()

for(j in 1:nrow(bbs_models)){

mod <- as.character(bbs_models[j,"model"])
var <- as.character(bbs_models[j,"variant"])

if(var == "spatial"){
p <- prepare_spatial(s, strata_map = load_map("bbs_usgs")) %>%
prepare_model(model = mod, model_variant = var)
}else{
p <- prepare_model(s,model = mod,
model_variant = var)
}

m <- paste0("output/",
species_number,
"_",
mod,
"_",
var)
m2 <- run_model(p,
output_basename = m,
output_dir = "vignettes/articles")

}


# latlong fits ------------------------------------------------------------



s <- stratify("latlong",
species = species) %>%
prepare_data(min_n_routes = 1)

for(j in 3){

mod <- as.character(bbs_models[j,"model"])
var <- as.character(bbs_models[j,"variant"])

if(var == "spatial"){
p <- prepare_spatial(s, strata_map = load_map("latlong")) %>%
prepare_model(model = mod, model_variant = var)
}else{
p <- prepare_model(s,model = mod,
model_variant = var)
}

m <- paste0("output/",
species_number,
"_latlong_",
mod,
"_",
var)
m2 <- run_model(p,
output_basename = m,
output_dir = "vignettes/articles")

}

79 changes: 79 additions & 0 deletions data-raw/flow_diagram.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
library(DiagrammeR)
gri <- grViz("digraph functions {
# a 'graph' statement
graph [overlap = true, fontsize = 10 compound = true, ranksep = 0.5]
# several 'node' statements
node [shape = rectangle, fontname = 'Courier New',
fixedsize = false, style=filled, fillcolor=white]
fetch [label = 'fetch_bbs_data()', fillcolor=lightblue]
remove [label = 'remove_cache()', fillcolor=orange]
loadbbs [label = 'load_bbs_data()', fillcolor=lightblue]
search [label = 'search_species()', fillcolor=orange]
strat [label = 'stratify()', fillcolor=lightpink]
prepdata [label = 'prepare_data()', fillcolor=lightpink]
prepspatial [label = 'prepare_spatial()', fillcolor=lightpink]
prepmodel [label = 'prepare_model()', fillcolor=darkolivegreen3]
loadmap [label = 'load_map()', fillcolor=orange]
assign_ps [label = 'assign_prov_state()', fillcolor=orange]
runmodel [label = 'run_model()', fillcolor=darkolivegreen3]
modelfile [label = 'copy_model_file()', fillcolor=darkolivegreen3]
conv[label = 'save_model_run()\nget_convergence()\nget_model_vars()\nget_summary()', fillcolor=darkolivegreen3]
genind [label = 'generate_indices()', fillcolor=thistle]
gentre [label = 'generate_trends()', fillcolor=thistle]
geo [label = 'plot_geofacet()', fillcolor=thistle]
plotind [label = 'plot_indices()', fillcolor=thistle]
genmap [label = 'plot_map()', fillcolor=thistle]
# several 'edge' statements
edge []
fetch->remove [style=dashed]
fetch->loadbbs [style=dashed]
fetch->strat [style=dashed]
runmodel->remove [style=dashed]
modelfile->prepmodel [style=dashed]
search->strat [style=dashed]
strat->prepdata
prepdata->prepspatial
prepdata->prepmodel [color=grey50]
prepspatial->prepmodel [color=grey50]
prepmodel->runmodel
runmodel->conv
runmodel->genind
genind->gentre
loadmap->assign_ps [style=dashed]
loadmap->genind [style=dashed]
loadmap->prepspatial [style=dashed]
loadmap->strat [style=dashed]
genind->plotind
genind->geo
gentre->geo [color=grey50]
gentre->genmap
# Invisible edges for arranging
prepspatial->conv [style=invis]
}")


export_graph(file_name = "flow_diagram.png",
graph = gri,
file_type = "png",
width = 1200,height = 1400)
print(gri)
dev.off()


14 changes: 7 additions & 7 deletions data-raw/species_notes.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
aou,minimum_year,warning
4661,1978,Alder and Willow Flycatcher were considered a single species until 1973. It is likely that they are not accurately separated by BBS observers until at least some years after that split.
4660,1978,Alder and Willow Flycatcher were considered a single species until 1973. It is likely that they are not accurately separated by BBS observers until at least some years after that split.
10,1990,Clark's and Western Grebe were considered a single species until 1985. It is likely that they are not accurately separated by BBS observers until at least some years after that split.
10,1990,Clark's and Western Grebe were considered a single species until 1985. It is likely that they are not accurately separated by BBS observers until at least some years after that split.
22860,1990,Eurasian Collared Dove was introduced into North America in the 1980s. 1990 is the first year that the species was observed on at least 3 BBS routes.
6121,1985,Cave Swallows were relatively rare in the areas surveyed by BBS before 1980. There are only two observations during BBS before 1980.
english,french,aou,minimum_year,warning
Alder Flycatcher,Moucherolle des aulnes,4661,1978,Alder and Willow Flycatcher were considered a single species until 1973. It is likely that they are not accurately separated by BBS observers until at least some years after that split.
Willow Flycatcher,Moucherolle des saules,4660,1978,Alder and Willow Flycatcher were considered a single species until 1973. It is likely that they are not accurately separated by BBS observers until at least some years after that split.
Clark's Grebe,Gr�be � face blanche,11,1990,Clark's and Western Grebe were considered a single species until 1985. It is likely that they are not accurately separated by BBS observers until at least some years after that split.
Western Grebe,Gr�be �l�gant,10,1990,Clark's and Western Grebe were considered a single species until 1985. It is likely that they are not accurately separated by BBS observers until at least some years after that split.
Eurasian Collared-Dove,Tourterelle turque,22860,1990,Eurasian Collared Dove was introduced into North America in the 1980s. 1990 is the first year that the species was observed on at least 3 BBS routes.
Cave Swallow,Hirondelle � front brun,6121,1985,Cave Swallows were relatively rare in the areas surveyed by BBS before 1980. There are only two observations during BBS before 1980.
Binary file modified data/species_notes.rda
Binary file not shown.
Loading

0 comments on commit 3e054b7

Please sign in to comment.