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

CST-455 Lake Erie results #2

Merged
merged 14 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
^LICENSE\.md$
^\.vscode$
^inst/extdata/ProtectedAreas$
^pres$
^NOTES\.md$
^\.github$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
output_data
.vscode
pres
.~lock*
NOTES.md
18 changes: 10 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
Package: watershedPrioritization
Type: Package
Title: Priortize Watersheds Within Canada
Date: 2025-02-03
Version: 0.0.0.9002
Title: Prioritize Watersheds Within Canada
Date: 2025-02-17
Version: 0.1.0
Authors@R: c(
person("Cindy", "Chu", email = "[email protected]", role = c("aut", "cre")),
person("inSileco Inc", comment = "https://insileco.io/", role = c("aut"))
)
Maintainer: Cindy Chu <[email protected]>
Description: Priortize watersheds within Canada for four freshwater fish conservation objectives.
Description: Reproduce the anlyses in 'Prioritizing Canadian watersheds for conservation of freshwater fish and fish habitat'.
Encoding: UTF-8
License: MIT + file LICENSE
Roxygen: list(markdown = TRUE)
Expand All @@ -21,20 +20,23 @@ Imports:
ggbeeswarm (>= 0.7.2),
ggplot2 (>= 3.5.1),
ggpubr (>= 0.6.0),
purrr (>= 1.0.2),
patchwork (>= 1.3.0),
readr (>= 2.1.5),
readxl (>= 1.4.3),
sf (>= 1.0-18),
tidyr (>= 1.3.1),
vegan (>= 2.6-10),
viridis (>= 0.6.5)
Suggests:
bslib (>= 0.9.0),
htmltools (>= 0.5.8.1),
leaflet (>= 2.2.2),
leafem (>= 0.2.3),
rsconnect (>= 1.3.2),
shiny (>= 1.9.1),
shinyWidgets (>= 0.8.7),
shinybusy (>= 0.3.3),
shinythemes (>= 1.2.0),
shinyjs (>= 2.1.0),
terra (>= 1.8-15),
testthat (>= 3.0.0)
RoxygenNote: 7.3.2
LazyLoad: yes
Expand Down
19 changes: 14 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# Generated by roxygen2: do not edit by hand

export(apply_weight)
export(compute_fish_priority_index)
export(feow_scaling)
export(generate_priorization_data)
export(get_results)
export(apply_weights)
export(compute_minns_Q_I)
export(generate_canada_dataset)
export(generate_lake_erie_dataset)
export(plot_comparison)
export(plot_comparison_pca)
export(plot_input_variables)
export(plot_protected_areas)
export(plot_scale_dependency)
export(plot_scores)
export(plot_scores_feow)
export(plot_weightings)
export(run_pipeline)
export(spatialize_results)
import(ggplot2)
import(patchwork)
93 changes: 0 additions & 93 deletions R/apply_weight.R

This file was deleted.

114 changes: 114 additions & 0 deletions R/apply_weights.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#' Applying expert weight to watershed prioritization data
#'
#' @param ws_data `[data.frame]`\cr A data frame with the variable of interest
#' (see [generate_canada_dataset()] and [generate_lake_erie_dataset()]).
#'
#' @details
#' Use the median values of expert weights used to generate weighted-scores for
#' four management strategies (weighting schema):
#' * Area-based protection
#' * Restoration
#' * SAR management
#' * Invasive species management
#'
#' @return
#' A data frame with the same data as `ws_data` plus four columns for the
#' scores computed and four additional four columns including the scaled
#' score within FEOW if `ws_data` includes `FEOW_ID`.
#'
#' @export

apply_weights <- function(ws_data) {
### read weightings data
weights <- path_input_data("Co_author_weightings.csv") |>
readr::read_csv(show_col_types = FALSE)

med_weights <- weights |>
dplyr::select(-Co_author) |>
dplyr::summarise_all(stats::median) |>
tidyr::pivot_longer(
cols = dplyr::everything(),
names_to = "Variable",
values_to = "Weight"
)

ws_data_w <- ws_data |>
dplyr::mutate(
#---------------- Priorities for Protected areas
protection_score =
WSI_n * med_weights$Weight[1] +
FBCI_n * med_weights$Weight[2] +
CCI_n * med_weights$Weight[3] +
SARI_n * med_weights$Weight[4] +
Fish_richness_n * med_weights$Weight[5] +
Priority_n * med_weights$Weight[6],
#---------------- Priorities for restoration
restoration_score = WSI_n * med_weights$Weight[7] +
FBCI_n * med_weights$Weight[8] +
CCI_n * med_weights$Weight[9] +
SARI_n * med_weights$Weight[10] +
Fish_richness_n * med_weights$Weight[11] +
Priority_n * med_weights$Weight[12],
#---------------- Priorities for SAR
SAR_score = WSI_n * med_weights$Weight[13] +
FBCI_n * med_weights$Weight[14] +
CCI_n * med_weights$Weight[15] +
SARI_n * med_weights$Weight[16] +
Fish_richness_n * med_weights$Weight[17] +
Priority_n * med_weights$Weight[18],

#---------------- Priorities for AIS
AIS_score = WSI_n * med_weights$Weight[19] +
FBCI_n * med_weights$Weight[20] +
CCI_n * med_weights$Weight[21] +
SARI_n * med_weights$Weight[22] +
Fish_richness_n * med_weights$Weight[23] +
Priority_n * med_weights$Weight[24]
)

if ("FEOW_ID" %in% names(ws_data_w)) {
# Scale ranks within FEOW to number of FEOW
ws_data_w <- ws_data_w |>
# protection_score ranked within FEOW
dplyr::ungroup() |>
dplyr::group_by(FEOW_ID) |>
dplyr::arrange(FEOW_ID, -protection_score) |>
dplyr::mutate(Prot_rank_feow = dplyr::row_number()) |>
# restoration_score ranked within FEOW
dplyr::ungroup() |>
dplyr::group_by(FEOW_ID) |>
dplyr::arrange(FEOW_ID, -restoration_score) |>
dplyr::mutate(Rest_rank_feow = dplyr::row_number()) |>
# SAR_score ranked within FEOW
dplyr::ungroup() |>
dplyr::group_by(FEOW_ID) |>
dplyr::ungroup() |>
dplyr::arrange(FEOW_ID, -SAR_score) |>
dplyr::mutate(SAR_rank_feow = dplyr::row_number()) |>
# AIS_score ranked within FEOW
dplyr::ungroup() |>
dplyr::group_by(FEOW_ID) |>
dplyr::arrange(FEOW_ID, -AIS_score) |>
dplyr::mutate(AIS_rank_feow = dplyr::row_number()) |>
# Scale within FEOW
dplyr::ungroup() |>
dplyr::group_by(FEOW_ID) |>
dplyr::mutate(
Prot_rank_feow_scaled = scale_rank(Prot_rank_feow),
Rest_rank_feow_scaled = scale_rank(Rest_rank_feow),
SAR_rank_feow_scaled = scale_rank(SAR_rank_feow),
AIS_rank_feow_scaled = scale_rank(AIS_rank_feow)
)
} else {
# scale all value
ws_data_w <- ws_data_w |>
dplyr::mutate(
Prot_rank_scaled = scale_rank(protection_score),
Rest_rank_scaled = scale_rank(restoration_score),
SAR_rank_scaled = scale_rank(SAR_score),
AIS_rank_scaled = scale_rank(AIS_score)
)
}

ws_data_w
}
Loading