From 88de0719afd8a81a4e974d7cebe9e7fbf0177128 Mon Sep 17 00:00:00 2001 From: biopsichas Date: Wed, 3 Jul 2024 12:32:15 +0300 Subject: [PATCH] added hc-plot --- DESCRIPTION | 2 +- R/prepare.R | 3 +++ _pkgdown.yml | 1 + vignettes/hc-plot.Rmd | 10 ++++++++++ vignettes/hc.Rmd | 37 +++++++------------------------------ 5 files changed, 22 insertions(+), 31 deletions(-) create mode 100644 vignettes/hc-plot.Rmd diff --git a/DESCRIPTION b/DESCRIPTION index bccb866..5b4c3d9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: SWATtunR Type: Package Title: Soft & Hard Calibration, Validation Package for SWAT+ models -Version: 0.0.1.9008 +Version: 0.0.1.9009 Authors@R: c( person("Svajunas", "Plunge", , "svajunas.plunge@gmail.com", role = c("aut", "cre"), comment = c(ORCID = '0000-0001-8897-3349')), diff --git a/R/prepare.R b/R/prepare.R index 609bedf..a4e5e11 100644 --- a/R/prepare.R +++ b/R/prepare.R @@ -160,6 +160,7 @@ calculate_wyr <- function(sim) { #' obj_tbl <- calculate_performance(sim = sim_flow, obs, "flo_day", c("kge", "nse"), "month", "sum") #' } #' @keywords calculate +#' @seealso \code{\link{calculate_performance_2plus}} calculate_performance <- function(sim, obs, par_name = NULL, perf_metrics = NULL, period = NULL, @@ -326,6 +327,8 @@ calculate_performance <- function(sim, obs, par_name = NULL, perf_metrics = NULL #' vector_weights = c(0.5, 0.3, 0.2), #' perf_metrics = c("nse", "kge", "pbias", "r2", "mae") #' } +#' @keywords calculate +#' @seealso \code{\link{calculate_performance}} calculate_performance_2plus <- function(sim, vector_var, list_obs, list_periods = NULL, vector_weights = NULL, diff --git a/_pkgdown.yml b/_pkgdown.yml index f2708d3..b4af5d0 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -12,3 +12,4 @@ articles: navbar: Hard calibration contents: - hc + - hc-plot diff --git a/vignettes/hc-plot.Rmd b/vignettes/hc-plot.Rmd new file mode 100644 index 0000000..8841496 --- /dev/null +++ b/vignettes/hc-plot.Rmd @@ -0,0 +1,10 @@ +--- +title: "Plotting results" +subtitle: "Plotting SWAT+ model results" +output: html_document +bibliography: ref.bib +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` diff --git a/vignettes/hc.Rmd b/vignettes/hc.Rmd index a025935..6463b02 100644 --- a/vignettes/hc.Rmd +++ b/vignettes/hc.Rmd @@ -306,44 +306,21 @@ The performance metrics can also be applied to multiple variables within the sim ```{r calc_perf_mv, message = FALSE, warning = FALSE, eval = FALSE, echo = FALSE} # Example of usage: -var_to_use <- list("flo_day_52", "no3_day_52_conc", "gwd_day") +var_to_use <- c("flo_day_52", "no3_day_52_conc", "gwd_day") var_weights <- c(0.5, 0.3, 0.2) ## The sum of weights should be 1. periods <- list(q_cal_period, wq_cal_period, c('2007-01-01', '2011-12-26')) # Read the another observed data -# In case you want to use water quality data, read it here before applying the following code. +# In case you want to use water quality data, read it here before applying the +# following code. obs2 <- read_csv(wqobs_path) obs3 <- read_csv(gwobs_path) # Calculate the performance metrics for each variable separately. -calculate_performance_mv <- function(sim, vector_var, list_obs, list_periods = NULL, - vector_weights = 1, - perf_metrics = c("nse", "kge", "pbias", "r2", "mae")){ - obj_tbl_list <- pmap(list(vector_var, list_obs, list_periods), function(.x, .y, .z){ - if(!is.NULL(.z)){ - tmp <- fix_dates(sim, .y, trim_start = .z[1], trim_end = .z[2]) - } - tmp <- fix_dates(sim, .y, trim_start = .z[1], trim_end = .z[2]) - calculate_performance(tmp$sim, tmp$obs, par_name = .x, - perf_metrics = perf_metrics)}) %>% - set_names(vector_var) - - # Calculate the mean of the performance rank for all parameter sets. In this case - # the weights for each variable should be defined and result could be used with - # only option 1 in step 8 - obj_tbl <- map2(obj_tbl_list, vector_weights, function(.x, .y){ - select(.x, c(run_id, rank_tot)) %>% - mutate(rank_tot = rank_tot * .y)}) %>% - reduce(., left_join, by = 'run_id')%>% - mutate(sum_rank = rowSums(across(starts_with("rank_tot")))) %>% - mutate(rank_tot = as.integer(rank(sum_rank))) %>% - select(run_id, rank_tot) - - return(list(obj_tbl_list = obj_tbl_list, obj_tbl = obj_tbl)) -} - - +obj_tbl_m <- calculate_performance_2plus(sim_flow, var_to_use, + list(obs, obs2, obs3), periods, + var_weights) +``` -```