Skip to content

Commit

Permalink
Finalize to apply #86 to apply_*() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
damianooldoni committed Sep 8, 2022
1 parent 8761512 commit c67b150
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 67 deletions.
7 changes: 0 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ importFrom(dplyr,case_when)
importFrom(dplyr,count)
importFrom(dplyr,desc)
importFrom(dplyr,distinct)
importFrom(dplyr,enquo)
importFrom(dplyr,filter)
importFrom(dplyr,filter_at)
importFrom(dplyr,full_join)
importFrom(dplyr,group_by)
importFrom(dplyr,inner_join)
importFrom(dplyr,left_join)
Expand All @@ -49,7 +47,6 @@ importFrom(dplyr,rowwise)
importFrom(dplyr,sample_n)
importFrom(dplyr,select)
importFrom(dplyr,starts_with)
importFrom(dplyr,sym)
importFrom(dplyr,tibble)
importFrom(dplyr,ungroup)
importFrom(dplyr,vars)
Expand All @@ -62,11 +59,9 @@ importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(plotly,ggplotly)
importFrom(plotly,layout)
importFrom(purrr,map2)
importFrom(purrr,map2_chr)
importFrom(purrr,map_chr)
importFrom(purrr,pmap_dfr)
importFrom(purrr,reduce)
importFrom(readr,read_tsv)
importFrom(readr,write_tsv)
importFrom(reshape2,melt)
Expand All @@ -77,12 +72,10 @@ importFrom(rlang,"!!")
importFrom(rlang,":=")
importFrom(rlang,sym)
importFrom(scales,percent_format)
importFrom(stats,median)
importFrom(stringr,str_c)
importFrom(stringr,str_remove)
importFrom(stringr,str_split)
importFrom(svDialogs,dlgInput)
importFrom(tibble,tibble)
importFrom(tidyselect,ends_with)
importFrom(tidyselect,one_of)
importFrom(tidyselect,vars_pull)
79 changes: 38 additions & 41 deletions R/apply_decision_rules.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
#' maximum ever observed up to \code{eval_year}?}}
#' @export
#' @importFrom dplyr .data %>%
#' @importFrom tidyselect vars_pull
#' @importFrom purrr map2 reduce
#' @importFrom rlang sym !! :=
#' @importFrom stats median
#' @details
#' Based on the decision rules output we define the emergency status value,
#' `em`:
Expand Down Expand Up @@ -79,7 +76,7 @@ apply_decision_rules <- function(df,
)
)

map2(
purrr::map2(
list(y_var, year, taxonKey),
c("y_var", "year", "taxonKey"),
function(x, y) {
Expand Down Expand Up @@ -114,7 +111,7 @@ apply_decision_rules <- function(df,
msg = paste0("Multiple values for argument eval_year provided.")
)

map2(
purrr::map2(
list(y_var, year, taxonKey),
c("y_var", "year", "taxonKey"),
function(x, y) {
Expand All @@ -129,8 +126,8 @@ apply_decision_rules <- function(df,
}
)

year <- vars_pull(names(df), !!dplyr::enquo(year))
taxonKey <- vars_pull(names(df), !!dplyr::enquo(taxonKey))
year <- tidyselect::vars_pull(names(df), !!dplyr::enquo(year))
taxonKey <- tidyselect::vars_pull(names(df), !!dplyr::enquo(taxonKey))

# Check eval_year is present in column year
assertthat::assert_that(eval_year %in% df[[year]],
Expand All @@ -142,9 +139,9 @@ apply_decision_rules <- function(df,

# Check timeseries has distinct time values per each taxon (no duplicates)
assertthat::assert_that(all(df %>%
dplyr::group_by(!!sym(taxonKey)) %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::summarize(
has_distinct_years = dplyr::n_distinct(!!sym(year)) == dplyr::n()
has_distinct_years = dplyr::n_distinct(!!rlang::sym(year)) == dplyr::n()
) %>%
distinct(.data$has_distinct_years) %>%
dplyr::pull() == TRUE),
Expand All @@ -158,9 +155,9 @@ apply_decision_rules <- function(df,
# Check timeseries has no holes (consecutive years)
taxa_not_consecutive_ts <-
df %>%
dplyr::group_by(!!sym(taxonKey)) %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::summarize(
has_all_years = dplyr::n() == (max(!!sym(year)) - min(!!sym(year)) + 1)
has_all_years = dplyr::n() == (max(!!rlang::sym(year)) - min(!!rlang::sym(year)) + 1)
) %>%
dplyr::filter(.data$has_all_years == FALSE)

Expand All @@ -176,17 +173,17 @@ apply_decision_rules <- function(df,
# Get all taxa in df
taxon_keys <-
df %>%
distinct(!!sym(taxonKey)) %>%
distinct(!!rlang::sym(taxonKey)) %>%
dplyr::pull()

# Find taxa whose timeseries don't contain eval_year, remove them and throw a
# warning
taxa_eval_out_of_min_max <-
df %>%
dplyr::group_by(!!sym(taxonKey)) %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::summarize(
min_ts = min(!!sym(year)),
max_ts = max(!!sym(year))
min_ts = min(!!rlang::sym(year)),
max_ts = max(!!rlang::sym(year))
) %>%
dplyr::filter(eval_year < .data$min_ts | eval_year > .data$max_ts)

Expand All @@ -199,67 +196,67 @@ apply_decision_rules <- function(df,
))
df <-
df %>%
dplyr::filter(!(!!sym(taxonKey)) %in% taxa_eval_out_of_min_max[[taxonKey]])
dplyr::filter(!(!!rlang::sym(taxonKey)) %in% taxa_eval_out_of_min_max[[taxonKey]])
}

# Cut time series up to eval_year
df <-
df %>%
dplyr::group_by(!!sym(taxonKey)) %>%
dplyr::filter(!!sym(year) <= eval_year) %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::filter(!!rlang::sym(year) <= eval_year) %>%
dplyr::ungroup()

# Rule 1: Time series with only one positive value at evaluation year
# appearing at eval_year
dr_1 <-
df %>%
dplyr::group_by(!!sym(taxonKey)) %>%
dplyr::filter(!!sym(y_var) > 0) %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::filter(!!rlang::sym(y_var) > 0) %>%
dplyr::add_tally(wt = NULL) %>%
dplyr::mutate(dr_1 = n == 1) %>%
distinct(!!sym(taxonKey), dr_1)
distinct(!!rlang::sym(taxonKey), dr_1)

# Rule 2: last value (at eval_year) above median value?
dr_2 <-
df %>%
dplyr::group_by(!!sym(taxonKey)) %>%
dplyr::mutate(last_occ = ifelse(!!sym(year) == max(!!sym(year)),
!!sym(y_var), -1
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::mutate(last_occ = ifelse(!!rlang::sym(year) == max(!!rlang::sym(year)),
!!rlang::sym(y_var), -1
)) %>%
dplyr::summarize(
median_occ = median(!!sym(y_var)),
median_occ = stats::median(!!rlang::sym(y_var)),
last_occ = max(.data$last_occ)
) %>%
dplyr::mutate(dr_2 = .data$last_occ > .data$median_occ) %>%
dplyr::select(!!sym(taxonKey), .data$dr_2)
dplyr::select(!!rlang::sym(taxonKey), .data$dr_2)

# Rule 3: 0 in the last 5 years?
dr_3 <-
df %>%
dplyr::group_by(!!sym(taxonKey)) %>%
dplyr::filter(!!sym(year) > (max(!!sym(year)) - 5)) %>%
dplyr::tally(!!sym(y_var)) %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::filter(!!rlang::sym(year) > (max(!!rlang::sym(year)) - 5)) %>%
dplyr::tally(!!rlang::sym(y_var)) %>%
dplyr::mutate(dr_3 = n == 0) %>%
dplyr::select(!!sym(taxonKey), .data$dr_3)
dplyr::select(!!rlang::sym(taxonKey), .data$dr_3)

# Rule 4: last value (at eval_year) is the maximum ever observed?
dr_4 <-
df %>%
dplyr::group_by(!!sym(taxonKey)) %>%
dplyr::summarize(max_occ = max(!!sym(y_var))) %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::summarize(max_occ = max(!!rlang::sym(y_var))) %>%
dplyr::inner_join(df %>%
dplyr::filter(!!sym(year) == max(!!sym(year))) %>%
dplyr::filter(!!rlang::sym(year) == max(!!rlang::sym(year))) %>%
dplyr::ungroup() %>%
dplyr::rename(last_value = !!sym(y_var)),
dplyr::rename(last_value = !!rlang::sym(y_var)),
by = taxonKey
) %>%
dplyr::mutate(dr_4 = .data$last_value == .data$max_occ) %>%
dplyr::select(!!sym(taxonKey), .data$dr_4)
dplyr::select(!!rlang::sym(taxonKey), .data$dr_4)

# Join all decision rules together
dr_all <-
list(dr_1, dr_2, dr_3, dr_4) %>%
reduce(dplyr::inner_join, by = taxonKey)
purrr::reduce(dplyr::inner_join, by = taxonKey)

# convert to em status codes:
# 0 = not emerging
Expand All @@ -281,10 +278,10 @@ apply_decision_rules <- function(df,
(.data$dr_1 == TRUE & .data$dr_3 == FALSE) |
(.data$dr_1 == FALSE & .data$dr_2 == FALSE & .data$dr_3 == FALSE) ~ 1 # unclear
)) %>%
dplyr::mutate(!!sym(year) := eval_year) %>%
dplyr::mutate(!!rlang::sym(year) := eval_year) %>%
dplyr::select(
!!sym(taxonKey),
!!sym(year),
!!rlang::sym(taxonKey),
!!rlang::sym(year),
.data$em_status,
.data$dr_1,
.data$dr_2,
Expand All @@ -297,8 +294,8 @@ apply_decision_rules <- function(df,
taxon_keys_to_add <- taxon_keys[!taxon_keys %in% em_dr[[taxonKey]]]

taxa_without_em <- dplyr::tibble(
!!sym(taxonKey) := taxon_keys_to_add,
!!sym(year) := rep(eval_year, length(taxon_keys_to_add))
!!rlang::sym(taxonKey) := taxon_keys_to_add,
!!rlang::sym(year) := rep(eval_year, length(taxon_keys_to_add))
)

em_dr <-
Expand Down
36 changes: 17 additions & 19 deletions R/apply_gam.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@
#' a ggplot2 object. Plot of GAM output and emerging. \code{NULL} if GAM
#' cannot be applied.}}
#' @export
#' @importFrom dplyr filter select case_when rename left_join full_join
#' %>% .data sym enquo
#' @importFrom tidyselect vars_pull
#' @importFrom dplyr %>% .data
#' @importFrom rlang !! :=
#'
#' @details
Expand Down Expand Up @@ -350,8 +348,8 @@ apply_gam <- function(df,
}
}

year <- vars_pull(names(df), !!enquo(year))
taxonKey <- vars_pull(names(df), !!enquo(taxonKey))
year <- tidyselect::vars_pull(names(df), !!dplyr::enquo(year))
taxonKey <- tidyselect::vars_pull(names(df), !!dplyr::enquo(taxonKey))

# Check eval_year is present in column year
assertthat::assert_that(all(eval_years %in% df[[year]]),
Expand Down Expand Up @@ -429,9 +427,9 @@ apply_gam <- function(df,
model <- deriv1 <- deriv2 <- plot_gam <- summary_pv <- p_ok <- NULL
emerging_status_output <-
output_model %>%
dplyr::filter(!!sym(year) %in% eval_years) %>%
dplyr::filter(!!dplyr::sym(year) %in% eval_years) %>%
dplyr::select(
!!sym(taxonKey),
!!dplyr::sym(taxonKey),
.data$year,
.data$em_status,
.data$growth,
Expand Down Expand Up @@ -518,8 +516,8 @@ apply_gam <- function(df,
.data$lower < 0 & .data$upper > 0 ~ 0,
.data$lower >= 0 & .data$upper > 0 ~ 1
)) %>%
dplyr::select(!!sym(year) := .data$data, .data$em1) %>%
dplyr::mutate(!!sym(year) := round(!!sym(year)))
dplyr::select(!!dplyr::sym(year) := .data$data, .data$em1) %>%
dplyr::mutate(!!dplyr::sym(year) := round(!!dplyr::sym(year)))

em2 <- deriv2 %>%
dplyr::as_tibble() %>%
Expand All @@ -529,10 +527,10 @@ apply_gam <- function(df,
.data$lower < 0 & .data$upper > 0 ~ 0,
.data$lower >= 0 & .data$upper > 0 ~ 1
)) %>%
dplyr::select(!!sym(year) := .data$data, .data$em2) %>%
dplyr::mutate(!!sym(year) := round(!!sym(year)))
dplyr::select(!!dplyr::sym(year) := .data$data, .data$em2) %>%
dplyr::mutate(!!dplyr::sym(year) := round(!!dplyr::sym(year)))

em_level_gam <- full_join(em1, em2, by = year) %>%
em_level_gam <- dplyr::full_join(em1, em2, by = year) %>%
dplyr::mutate(em = dplyr::case_when(
.data$em1 == 1 & .data$em2 == 1 ~ 4,
.data$em1 == 1 & .data$em2 == 0 ~ 3,
Expand All @@ -555,26 +553,26 @@ apply_gam <- function(df,
.data$em >= 3 ~ 3 # emerging
))

output_model <- left_join(output_model, em_levels, by = year)
output_model <- dplyr::left_join(output_model, em_levels, by = year)

# Lower value of first dedrivative (minimal guaranted growth) if positive
lower_deriv1 <-
deriv1 %>%
dplyr::filter(.data$var == year) %>%
rename(!!sym(year) := .data$data) %>%
dplyr::mutate(!!sym(year) := round(!!sym(year), digits = 0)) %>%
dplyr::rename(!!dplyr::sym(year) := .data$data) %>%
dplyr::mutate(!!dplyr::sym(year) := round(!!dplyr::sym(year), digits = 0)) %>%
dplyr::mutate(growth = model$family$linkinv(.data$lower)) %>%
dplyr::select(!!sym(year), .data$growth)
dplyr::select(!!dplyr::sym(year), .data$growth)

# Add lower value of first derivative
output_model <- left_join(output_model, lower_deriv1, by = "year")
output_model <- dplyr::left_join(output_model, lower_deriv1, by = "year")

# Get emergin status summary for output
emerging_status_output <-
output_model %>%
dplyr::filter(!!sym(year) %in% eval_years) %>%
dplyr::filter(!!dplyr::sym(year) %in% eval_years) %>%
dplyr::select(
!!sym(taxonKey),
!!dplyr::sym(taxonKey),
.data$year,
.data$em_status,
.data$growth,
Expand Down

0 comments on commit c67b150

Please sign in to comment.