|
10 | 10 | #' @param xlim optional numeric vector of length two for x-axis limits |
11 | 11 | #' @param ylim optional numeric vector of length two for y-axis limits |
12 | 12 | #' @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} |
13 | 14 | #' |
14 | 15 | #' @return A \code{\link[ggplot2]{ggplot}} object |
15 | 16 | #' @export |
16 | 17 | #' |
17 | 18 | #' @details |
18 | 19 | #' 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. |
19 | 20 | #' |
| 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 | +#' |
20 | 25 | #' @concept show |
21 | 26 | #' |
22 | 27 | #' @examples |
|
30 | 35 | #' mod <- anlz_gam(tomod, trans = 'log10') |
31 | 36 | #' |
32 | 37 | #' 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){ |
34 | 39 |
|
35 | 40 | # get predictions |
36 | 41 | prds <- anlz_prd(mod) |
37 | 42 |
|
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)) %>% |
40 | 48 | 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) |
45 | 51 | ) |
| 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 | + } |
46 | 103 |
|
47 | 104 | # get transformation |
48 | 105 | trans <- unique(prds$trans) |
|
0 commit comments