Skip to content

Commit e797c0f

Browse files
committed
fixed annual_aggs again
1 parent 223b030 commit e797c0f

14 files changed

+184
-16
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: WRTDStidal
22
Type: Package
33
Title: Weighted regression for water quality evaluation in tidal waters
4-
Version: 0.0.49.9000
5-
Date: 2016-10-12
4+
Version: 1.0.1.9000
5+
Date: 2016-11-01
66
Author: Marcus W. Beck [aut, cre]
77
Maintainer: Marcus W. Beck <[email protected]>
88
Description: An adaptation for estuaries (tidal waters) of weighted regression

R/annual_agg.R

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#' @param dat_in input tidal or tidalmean object
66
#' @param mo_strt numeric indicating month to start aggregation years, defaults to October for USGS water year from October to September
77
#' @param min_mo numeric value from one to twelve indicating the minimum number of months with observations for averaging by years
8+
#' @param logspace logical indicating if aggregated data are to be shown in log-space or not
89
#' @param ... arguments passed to or from other methods
910
#'
1011
#' @details WRTDS output is averaged by year for both predictions and flow-normalized predictions. Years are averaged only if one observation is contained in each of the minimum number of months specified by \code{min_mo} averaging, otherwise results are not returned for the given year. Note that setting \code{min_mo} to values smaller than the default can produce inaccurate trends for years with very few results.
@@ -30,15 +31,20 @@ annual_agg <- function(dat_in, ...) UseMethod('annual_agg')
3031
#' @export
3132
#'
3233
#' @method annual_agg default
33-
annual_agg.default <- function(dat_in, mo_strt = 10, min_mo = 9, ...){
34+
annual_agg.default <- function(dat_in, mo_strt = 10, min_mo = 9, logspace = TRUE, ...){
3435

3536
# put data in long format by response, fits, norms
3637
# split by response variable
3738
# create new year column based on mo_strt
3839
# average by new year column only if minimum number of months are present
39-
dat_agg <- select(dat_in, matches('date|fit|norm|res')) %>%
40-
mutate(res = exp(res)) %>% # this has to be back-transformed before average
41-
tidyr::gather('var', 'val', -date) %>%
40+
dat_agg <- select(dat_in, matches('date|fit|norm|res'))
41+
42+
# this has to be back-transformed before average if values are not shown in logspace
43+
if(!logspace)
44+
dat_agg <- mutate(dat_agg, res = exp(res))
45+
46+
# take annual averages
47+
dat_agg <- tidyr::gather(dat_agg, 'var', 'val', -date) %>%
4248
split(.$var) %>%
4349
lapply(., function(x){
4450

@@ -61,10 +67,13 @@ annual_agg.default <- function(dat_in, mo_strt = 10, min_mo = 9, ...){
6167
ungroup %>%
6268
rename(date = yrchg) %>%
6369
mutate(
64-
date = as.Date(paste0(mo_strt, '-01-', date), format = '%m-%d-%Y'),
65-
res = log(res) # transform res back to log)
70+
date = as.Date(paste0(mo_strt, '-01-', date), format = '%m-%d-%Y')
6671
)
6772

73+
# transform back to log if values not shown in logspace
74+
if(!logspace)
75+
dat_agg <- mutate(dat_agg, res = log(res))
76+
6877
return(dat_agg)
6978

7079
}

R/fitplot.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fitplot.tidal <- function(dat_in, tau = NULL, predicted = TRUE, annuals = TRUE,
123123
# annual aggregations if TRUE, otherwise monthly agg
124124
if(annuals){
125125

126-
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt)
126+
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt, logspace = logspace)
127127

128128
}
129129

@@ -248,7 +248,7 @@ fitplot.tidalmean <- function(dat_in, predicted = TRUE, annuals = TRUE, logspace
248248
# annual aggregations if TRUE, otherwise monthly agg
249249
if(annuals){
250250

251-
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt)
251+
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt, logspace = logspace)
252252

253253
}
254254

R/prdnrmplot.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ prdnrmplot.tidal <- function(dat_in, tau = NULL, annuals = TRUE, logspace = TRUE
124124
# annual aggregations if TRUE
125125
if(annuals){
126126

127-
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt)
127+
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt, logspace = logspace)
128128

129129
}
130130

@@ -234,7 +234,7 @@ prdnrmplot.tidalmean <- function(dat_in, annuals = TRUE, logspace = TRUE, dt_rng
234234
# annual aggregations if TRUE
235235
if(annuals){
236236

237-
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt)
237+
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt, logspace = logspace)
238238

239239
}
240240

R/wrtdstrnd.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ wrtdstrnd.default <- function(dat_in, mobrks, yrbrks, molabs, yrlabs, aves = FAL
6161
# aggregate/summarize separately for each category
6262

6363
# annual aggs
64-
yrdat <- annual_agg(dat_in, mo_strt = mo_strt, min_mo = min_mo) %>%
64+
yrdat <- annual_agg(dat_in, mo_strt = mo_strt, min_mo = min_mo, logspace = FALSE) %>%
6565
select(date, norm) %>%
6666
mutate(
6767
date = year(date),

README.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ Tabular summaries of model performance and trends over time can be obtained for
248248
```{r}
249249
wrtdsperf(tidfit)
250250
# setup month, year categories for trend summaries
251-
mobrks <- c(-Inf, 3, 6, 9, Inf)
251+
mobrks <- list(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9), c(10, 11, 12))
252252
yrbrks <- c(-Inf, 1985, 1994, 2003, Inf)
253253
molabs <- c('JFM', 'AMJ', 'JAS', 'OND')
254254
yrlabs <- c('1974-1985', '1986-1994', '1995-2003', '2004-2012')

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ wrtdsperf(tidfit)
390390

391391
```r
392392
# setup month, year categories for trend summaries
393-
mobrks <- c(-Inf, 3, 6, 9, Inf)
393+
mobrks <- list(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9), c(10, 11, 12))
394394
yrbrks <- c(-Inf, 1985, 1994, 2003, Inf)
395395
molabs <- c('JFM', 'AMJ', 'JAS', 'OND')
396396
yrlabs <- c('1974-1985', '1986-1994', '1995-2003', '2004-2012')
Binary file not shown.
Binary file not shown.

README_files/navigation-1.1/FileSaver.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)