Skip to content

Commit 282cd16

Browse files
committed
better handling for yromit in show_prdseries
1 parent 2d18d17 commit 282cd16

4 files changed

Lines changed: 77 additions & 10 deletions

File tree

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"positron.r.pipe": "%>%"
3+
}

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: wqtrends
22
Title: Assess Water Quality Trends with Generalized Additive Models
3-
Version: 1.5.1.9000
4-
Date: 2025-08-11
3+
Version: 1.5.1.9001
4+
Date: 2025-08-15
55
Authors@R: c(
66
person(given = "Marcus",
77
family = "Beck",

R/show_prdseries.R

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
#' @param xlim optional numeric vector of length two for x-axis limits
1111
#' @param ylim optional numeric vector of length two for y-axis limits
1212
#' @param col optional chr string for line color
13+
#' @param minomit numeric indicating number of observations in the observed data to exclude it in the predicted series for a given year if provided to \code{yromit}
1314
#'
1415
#' @return A \code{\link[ggplot2]{ggplot}} object
1516
#' @export
1617
#'
1718
#' @details
1819
#' The optional \code{yromit} vector can be used to omit years from the plot. This may be preferred if the predicted values from the model deviate substantially from other years likely due to missing data.
1920
#'
21+
#' The \code{yromit} argument behaves differently for this function compared to others because of a mismatch in the timestep for the predicted time series and the observed data. The function will attempt to find "bookends" in the observed data that match with the predicted time series for each year in \code{yromit}. For example, if there is a gap in the observed data that spans multiple years and a single year value is included with \code{yromit} that is within the gap, the predicted time series will not be shown for the entire gap in the observed data even if additional years within the gap were not provided to \code{yromit}.
22+
#'
23+
#' Entire years where observed data are present can also be removed from the predicted time series if they exceed a minimum number of observations defined by \code{minomit}. For example, if a year has at least 8 observations and \code{yromit} includes that year, the predicted time series for that entire year will be removed.
24+
#'
2025
#' @concept show
2126
#'
2227
#' @examples
@@ -30,19 +35,71 @@
3035
#' mod <- anlz_gam(tomod, trans = 'log10')
3136
#'
3237
#' show_prdseries(mod, ylab = 'Chlorophyll-a (ug/L)')
33-
show_prdseries <- function(mod, ylab, yromit = NULL, alpha = 0.7, base_size = 11, xlim = NULL, ylim = NULL, col = 'brown'){
38+
show_prdseries <- function(mod, ylab, yromit = NULL, alpha = 0.7, base_size = 11, xlim = NULL, ylim = NULL, col = 'brown', minomit = 8){
3439

3540
# get predictions
3641
prds <- anlz_prd(mod)
3742

38-
if(!is.null(yromit))
39-
prds <- prds %>%
43+
if(!is.null(yromit)){
44+
45+
# date range from model data that include yromit
46+
moddat <- mod$model %>%
47+
dplyr::filter(!is.na(value)) %>%
4048
dplyr::mutate(
41-
value = dplyr::case_when(
42-
yr %in% !!yromit ~ NA_real_,
43-
TRUE ~ value
44-
)
49+
date = as.Date(lubridate::date_decimal(cont_year)),
50+
yr = lubridate::year(date)
4551
)
52+
53+
for(yr in yromit){
54+
# omit the full year if there is "lots" of data
55+
if(length(moddat[moddat$yr == yr, 'value']) >= minomit){
56+
prds[prds$yr == yr, 'value'] <- NA_real_
57+
58+
# otherwise, find bookends
59+
} else {
60+
61+
# if year completely missing
62+
if(!yr %in% moddat$yr){
63+
64+
if(yr > max(moddat$yr) | yr < min(moddat$yr))
65+
next()
66+
67+
strdt <- moddat %>%
68+
dplyr::filter(yr < !!yr) %>%
69+
dplyr::pull(date) %>%
70+
max()
71+
enddt <- moddat %>%
72+
dplyr::filter(yr > !!yr) %>%
73+
dplyr::pull(date) %>%
74+
min()
75+
76+
# if year is included, find the gap
77+
} else {
78+
79+
yr <- yr + 0.5 # start in the middle
80+
strdt <- moddat %>%
81+
dplyr::filter(cont_year < !!yr) %>%
82+
dplyr::pull(date) %>%
83+
max()
84+
enddt <- moddat %>%
85+
dplyr::filter(cont_year > !!yr) %>%
86+
dplyr::pull(date) %>%
87+
min()
88+
89+
}
90+
91+
prds <- prds %>%
92+
dplyr::mutate(
93+
value = dplyr::case_when(
94+
date >= strdt & date <= enddt ~ NA_real_,
95+
TRUE ~ value
96+
)
97+
)
98+
99+
}
100+
}
101+
102+
}
46103

47104
# get transformation
48105
trans <- unique(prds$trans)

man/show_prdseries.Rd

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)