Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection of small style fixes #138

Merged
merged 38 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8d4e8ae
reduce to less than 80 cols
PietrH Jul 8, 2024
f33d6ce
pipe should always have spaces around it
PietrH Jul 8, 2024
eb3123a
reduce to 80 cols
PietrH Jul 8, 2024
4533c2f
remove trailing whitespace
PietrH Jul 8, 2024
0f1e358
End on an empty line
PietrH Jul 8, 2024
d34679c
remove trailing whitespace
PietrH Jul 8, 2024
bb31f33
commas should be followed by a space
PietrH Jul 8, 2024
d2e790e
end on newline
PietrH Jul 8, 2024
547ec18
remove trailing whitespace
PietrH Jul 8, 2024
51e3564
space after comma
PietrH Jul 8, 2024
279fc5f
`else` should be on same line as closing curly bracket
PietrH Jul 8, 2024
51237cc
using 1:length(x) is anti pattern because of case where x has length 0
PietrH Jul 8, 2024
eb7c4c8
remove trailing whitespace
PietrH Jul 8, 2024
f19419c
using 1:length(x) is anti pattern because of case where x has length 0
PietrH Jul 8, 2024
4cfbb14
spaces around assignment operators
PietrH Jul 8, 2024
049d43b
fix missing spaces, remove trailing whitespace
PietrH Jul 8, 2024
4d95863
limit to 80 cols
PietrH Jul 8, 2024
7542db5
`base::` is not necessary
PietrH Jul 8, 2024
b6d111c
spaces around infix operators
PietrH Jul 8, 2024
0d70868
space before opening curly bracket, else on same line as closing curl…
PietrH Jul 8, 2024
19b355d
spaces around infix operator, limit to 80 cols
PietrH Jul 8, 2024
ff80618
no spaces before commas
PietrH Jul 8, 2024
36a5282
remove trailing whitespace
PietrH Jul 8, 2024
71481e3
remove references to `base::`
PietrH Jul 8, 2024
adba2cd
no spaces before comma
PietrH Jul 8, 2024
477fc7a
limit width
PietrH Jul 8, 2024
efc55bf
remove trailing whitespace
PietrH Jul 8, 2024
edaf8a5
spaces around infix operator, no spaces before comma, break on comma...
PietrH Jul 8, 2024
4df7663
`else` on same line as closing curly bracket
PietrH Jul 8, 2024
decfd21
spaces around if, end on empty line
PietrH Jul 8, 2024
6970cad
spaces around infix operator
PietrH Jul 8, 2024
b689e11
undo 51237cc9ce5960103c1399ad26f1b716df3bd5aa, capture case where lev…
PietrH Jul 8, 2024
573ca29
add utils namespace designation
PietrH Jul 8, 2024
63c3ad2
add missing namespace declarations, fix typo
PietrH Jul 8, 2024
eb35c7b
whoops, wrong namespace!
PietrH Jul 8, 2024
9f6ad6b
Merge branch 'main' into style-fixes
damianooldoni Jul 17, 2024
9587f8c
Merge branch 'main' into style-fixes
damianooldoni Jul 17, 2024
a655787
Merge branch 'main' into style-fixes
damianooldoni Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions R/apply_decision_rules.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ apply_decision_rules <- function(df,
df %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::summarize(
has_all_years = dplyr::n() == (max(!!rlang::sym(year)) - min(!!rlang::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 Down Expand Up @@ -193,7 +194,9 @@ apply_decision_rules <- function(df,
))
df <-
df %>%
dplyr::filter(!(!!rlang::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
Expand All @@ -217,9 +220,11 @@ apply_decision_rules <- function(df,
dr_2 <-
df %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::mutate(last_occ = ifelse(!!rlang::sym(year) == max(!!rlang::sym(year)),
!!rlang::sym(y_var), -1
)) %>%
dplyr::mutate(
last_occ = ifelse(!!rlang::sym(year) == max(!!rlang::sym(year)),
!!rlang::sym(y_var),
-1)
) %>%
dplyr::summarize(
median_occ = stats::median(!!rlang::sym(y_var)),
last_occ = max(.data$last_occ)
Expand All @@ -241,11 +246,12 @@ apply_decision_rules <- function(df,
df %>%
dplyr::group_by(!!rlang::sym(taxonKey)) %>%
dplyr::summarize(max_occ = max(!!rlang::sym(y_var))) %>%
dplyr::inner_join(df %>%
dplyr::filter(!!rlang::sym(year) == max(!!rlang::sym(year))) %>%
dplyr::ungroup() %>%
dplyr::rename(last_value = !!rlang::sym(y_var)),
by = taxonKey
dplyr::inner_join(
df %>%
dplyr::filter(!!rlang::sym(year) == max(!!rlang::sym(year))) %>%
dplyr::ungroup() %>%
dplyr::rename(last_value = !!rlang::sym(y_var)),
by = taxonKey
) %>%
dplyr::mutate(dr_4 = .data$last_value == .data$max_occ) %>%
dplyr::select(!!rlang::sym(taxonKey), "dr_4")
Expand Down
92 changes: 46 additions & 46 deletions R/apply_gam.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
#' - `upper_ci`: numeric. Upper bound of the
#' confidence interval of the estimated smooth.
#' - value of argument `year`: column with temporal values.
#' - value of argument `baseline_var`: column with the fitted values for the
#' - value of argument `baseline_var`: column with the fitted values for the
#' baseline. If `baseline_var` is `NULL`, this column is not present.
#'
#' \item `second_derivative`: df. Data.frame with details of second
Expand Down Expand Up @@ -265,7 +265,7 @@
#' ),
#' cobs = rep(0, 24)
#' )
#'
#'
#' # if GAM cannot be applied a warning is returned and the plot mention it
#' \dontrun{
#' no_gam_applied <- apply_gam(df_gam,
Expand Down Expand Up @@ -299,7 +299,7 @@ apply_gam <- function(df,
if (is.numeric(taxon_key)) {
taxon_key <- as.character(taxon_key)
}

# Check right type of inputs
assertthat::assert_that(is.data.frame(df),
msg = paste(
Expand Down Expand Up @@ -337,7 +337,7 @@ apply_gam <- function(df,
"Check value of argument eval_years."
)
)

purrr::map2(
list(baseline_var, taxon_key, name, df_title, dir_name),
c("baseline_var", "taxon_key", "name", "df_title", "dir_name"),
Expand All @@ -359,7 +359,7 @@ apply_gam <- function(df,
)
}
)

purrr::map2(
list(width, height),
c("width", "height"),
Expand All @@ -381,7 +381,7 @@ apply_gam <- function(df,
)
}
)

purrr::map2(
list(saveplot, verbose),
c("saveplot", "verbose"),
Expand All @@ -399,7 +399,7 @@ apply_gam <- function(df,
)
}
)

purrr::map2(
list(y_var, year, taxonKey),
c("y_var", "year", "taxonKey"),
Expand All @@ -414,7 +414,7 @@ apply_gam <- function(df,
)
}
)

if (!is.null(baseline_var)) {
# Check baseline_var is present in df
assertthat::assert_that(
Expand All @@ -428,7 +428,7 @@ apply_gam <- function(df,
} else {
method_em <- "basic"
}

if (isFALSE(saveplot)) {
if (!is.null(dir_name)) {
message(paste(
Expand Down Expand Up @@ -464,10 +464,10 @@ apply_gam <- function(df,
message("height not provided. Set to 1200 pixels.")
}
}

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]]),
msg = paste(
Expand All @@ -476,14 +476,14 @@ apply_gam <- function(df,
"Check value of argument eval_years."
)
)

assertthat::assert_that(is.numeric(p_max) && p_max >= 0 && p_max <= 1,
msg = paste(
"p_max is a p-value: it has to be a",
"number between 0 and 1."
)
)

# Check type_indicator is one of the two allowed values
assertthat::assert_that(type_indicator %in% c("observations", "occupancy"),
msg = paste(
Expand All @@ -492,16 +492,16 @@ apply_gam <- function(df,
"observations, occupancy."
)
)

if (verbose == TRUE) {
print(paste0("Analyzing: ", name, "(", taxon_key, ")"))
}

if (nrow(df) > 0) {
# Maximum minimum time series (year)
fyear <- min(df[[year]], na.rm = TRUE) # first year
lyear <- max(df[[year]], na.rm = TRUE) # last year

# Define model to use for GAM
maxk <- max(round((lyear - fyear) / 10, digits = 0), 5) # max number of knots
}
Expand All @@ -525,7 +525,7 @@ apply_gam <- function(df,
)
fm <- stats::formula(fm)
}

# Initialization
output_model <- dplyr::as_tibble(df)
output_model <-
Expand Down Expand Up @@ -559,7 +559,7 @@ apply_gam <- function(df,
ggplot2::geom_point(color = "black") +
ggplot2::ylab(y_label) +
ggplot2::ggtitle(ptitle)

emerging_status_output <-
output_model %>%
dplyr::filter(!!dplyr::sym(year) %in% eval_years) %>%
Expand All @@ -570,7 +570,7 @@ apply_gam <- function(df,
"growth",
"method"
)

if (nrow(df) > 3 & sum(df[[y_var]][2:nrow(df)]) != 0) {
result <- tryCatch(expr = {
model <- mgcv::gam(
Expand All @@ -583,7 +583,7 @@ apply_gam <- function(df,
summary_pv <- mgcv::summary.gam(model)$s.pv
p_ok <- ifelse(any(summary_pv < p_max), TRUE, FALSE)
}, error = function(e) e, warning = function(w) w)

if (class(result)[1] %in% c("simpleWarning", "simpleError")) {
if (verbose) {
warning(paste0(
Expand Down Expand Up @@ -623,13 +623,13 @@ apply_gam <- function(df,
interval = "prediction",
se.fit = TRUE
)

# Calculate confidence intervals & backtransform to real scale
intercept <- unname(model$coefficients[1])
output_model$fit <- model$family$linkinv(temp$fit[, 1] + intercept)
output_model$ucl <- model$family$linkinv(temp$fit[, 1] + intercept + temp$se.fit[, 1] * 1.96)
output_model$lcl <- model$family$linkinv(temp$fit[, 1] + intercept - temp$se.fit[, 1] * 1.96)

# Check that fit ucl and lcl are all above zero
output_model <-
output_model %>%
Expand All @@ -638,7 +638,7 @@ apply_gam <- function(df,
ucl = ifelse(.data$ucl < 0, 0, .data$ucl),
lcl = ifelse(.data$lcl < 0, 0, .data$lcl)
)

# Calculate first and second derivative + conf. interval
deriv1 <- gratia::derivatives(model,
type = "central", order = 1, level = 0.8,
Expand All @@ -648,21 +648,21 @@ apply_gam <- function(df,
year, baseline_var)
deriv1 <- deriv1 %>%
dplyr::select(dplyr::all_of(cols_to_select)) %>%
dplyr::rename_with(~sub("^\\.", "", .),
dplyr::all_of(c(".smooth", ".derivative",
".se", ".crit",
dplyr::rename_with(~sub("^\\.", "", .),
dplyr::all_of(c(".smooth", ".derivative",
".se", ".crit",
".lower_ci", ".upper_ci")))
deriv2 <- gratia::derivatives(model,
type = "central", order = 2, level = 0.8,
n = nrow(output_model), eps = 1e-4)
deriv2 <- deriv2 %>%
# same columns to select as for 1st derivative
dplyr::select(dplyr::all_of(cols_to_select)) %>%
dplyr::rename_with(~sub("^\\.", "", .),
dplyr::all_of(c(".smooth", ".derivative",
".se", ".crit",
dplyr::rename_with(~sub("^\\.", "", .),
dplyr::all_of(c(".smooth", ".derivative",
".se", ".crit",
".lower_ci", ".upper_ci")))

# Emerging status based on first and second derivative
em1 <-
deriv1 %>%
Expand All @@ -674,7 +674,7 @@ apply_gam <- function(df,
)) %>%
dplyr::select(!!dplyr::sym(year), "em1") %>%
dplyr::mutate(!!dplyr::sym(year) := round(!!dplyr::sym(year)))

em2 <- deriv2 %>%
dplyr::filter(!is.na(!!dplyr::sym(year))) %>%
dplyr::mutate(em2 = dplyr::case_when(
Expand All @@ -684,7 +684,7 @@ apply_gam <- function(df,
)) %>%
dplyr::select(!!dplyr::sym(year), "em2") %>%
dplyr::mutate(!!dplyr::sym(year) := round(!!dplyr::sym(year)))

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

# Emerging status
em_levels <-
em_level_gam %>%
Expand All @@ -707,24 +707,24 @@ apply_gam <- function(df,
.data$em < 3 ~ 2, # potentially emerging
.data$em >= 3 ~ 3 # emerging
))

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

# Lower value of first derivative (minimal guaranteed growth) if
# positive
lower_deriv1 <-
deriv1 %>%
dplyr::filter(!is.na(!!dplyr::sym(year))) %>%
dplyr::mutate(!!dplyr::sym(year) := round(!!dplyr::sym(year),
dplyr::mutate(!!dplyr::sym(year) := round(!!dplyr::sym(year),
digits = 0)) %>%
dplyr::mutate(growth = model$family$linkinv(.data$lower_ci)) %>%
dplyr::select(!!dplyr::sym(year), "growth")

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

# Get emerging status summary for output
emerging_status_output <-
output_model %>%
Expand Down Expand Up @@ -782,7 +782,7 @@ apply_gam <- function(df,
df = df,
y_axis = y_var)
}

# save plot if asked
if (saveplot == TRUE) {
if (stringr::str_ends(dir_name, pattern = "/")) {
Expand All @@ -799,7 +799,7 @@ apply_gam <- function(df,
height = height,
units = "px")
}

return(list(
em_summary = emerging_status_output,
model = model,
Expand Down Expand Up @@ -849,7 +849,7 @@ plot_ribbon_em <- function(df_plot,
ggplot2::geom_point(color = "black") +
ggplot2::ylab(y_label) +
ggplot2::ggtitle(ptitle)

if (all(
all(abs(df_plot$lcl < 10^10)),
all(abs(df_plot$ucl < 10^10)),
Expand Down Expand Up @@ -888,10 +888,10 @@ plot_ribbon_em <- function(df_plot,
}

#' Add annotation when status cannot be assessed
#'
#'
#' Internal function to be used when GAM cannot be applied to it doesn't
#' converge.
#'
#'
#' @param plot_obs ggplot2 plot object showing the observations.
#' @param df tibble data.frame with observations.
#' @param y_axis character. The name of the column containing the data to plot
Expand All @@ -916,4 +916,4 @@ add_annotation <- function(
colour = colour
)
return(annotated_plot)
}
}
Loading
Loading