Skip to content

Commit

Permalink
fixed annual_aggs again
Browse files Browse the repository at this point in the history
  • Loading branch information
fawda123 committed Nov 1, 2016
1 parent 223b030 commit e797c0f
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 16 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: WRTDStidal
Type: Package
Title: Weighted regression for water quality evaluation in tidal waters
Version: 0.0.49.9000
Date: 2016-10-12
Version: 1.0.1.9000
Date: 2016-11-01
Author: Marcus W. Beck [aut, cre]
Maintainer: Marcus W. Beck <[email protected]>
Description: An adaptation for estuaries (tidal waters) of weighted regression
Expand Down
21 changes: 15 additions & 6 deletions R/annual_agg.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param dat_in input tidal or tidalmean object
#' @param mo_strt numeric indicating month to start aggregation years, defaults to October for USGS water year from October to September
#' @param min_mo numeric value from one to twelve indicating the minimum number of months with observations for averaging by years
#' @param logspace logical indicating if aggregated data are to be shown in log-space or not
#' @param ... arguments passed to or from other methods
#'
#' @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.
Expand All @@ -30,15 +31,20 @@ annual_agg <- function(dat_in, ...) UseMethod('annual_agg')
#' @export
#'
#' @method annual_agg default
annual_agg.default <- function(dat_in, mo_strt = 10, min_mo = 9, ...){
annual_agg.default <- function(dat_in, mo_strt = 10, min_mo = 9, logspace = TRUE, ...){

# put data in long format by response, fits, norms
# split by response variable
# create new year column based on mo_strt
# average by new year column only if minimum number of months are present
dat_agg <- select(dat_in, matches('date|fit|norm|res')) %>%
mutate(res = exp(res)) %>% # this has to be back-transformed before average
tidyr::gather('var', 'val', -date) %>%
dat_agg <- select(dat_in, matches('date|fit|norm|res'))

# this has to be back-transformed before average if values are not shown in logspace
if(!logspace)
dat_agg <- mutate(dat_agg, res = exp(res))

# take annual averages
dat_agg <- tidyr::gather(dat_agg, 'var', 'val', -date) %>%
split(.$var) %>%
lapply(., function(x){

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

# transform back to log if values not shown in logspace
if(!logspace)
dat_agg <- mutate(dat_agg, res = log(res))

return(dat_agg)

}
4 changes: 2 additions & 2 deletions R/fitplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fitplot.tidal <- function(dat_in, tau = NULL, predicted = TRUE, annuals = TRUE,
# annual aggregations if TRUE, otherwise monthly agg
if(annuals){

to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt)
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt, logspace = logspace)

}

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

to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt)
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt, logspace = logspace)

}

Expand Down
4 changes: 2 additions & 2 deletions R/prdnrmplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ prdnrmplot.tidal <- function(dat_in, tau = NULL, annuals = TRUE, logspace = TRUE
# annual aggregations if TRUE
if(annuals){

to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt)
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt, logspace = logspace)

}

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

to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt)
to_plo <- annual_agg(to_plo, min_mo = min_mo, mo_strt = mo_strt, logspace = logspace)

}

Expand Down
2 changes: 1 addition & 1 deletion R/wrtdstrnd.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ wrtdstrnd.default <- function(dat_in, mobrks, yrbrks, molabs, yrlabs, aves = FAL
# aggregate/summarize separately for each category

# annual aggs
yrdat <- annual_agg(dat_in, mo_strt = mo_strt, min_mo = min_mo) %>%
yrdat <- annual_agg(dat_in, mo_strt = mo_strt, min_mo = min_mo, logspace = FALSE) %>%
select(date, norm) %>%
mutate(
date = year(date),
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Tabular summaries of model performance and trends over time can be obtained for
```{r}
wrtdsperf(tidfit)
# setup month, year categories for trend summaries
mobrks <- c(-Inf, 3, 6, 9, Inf)
mobrks <- list(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9), c(10, 11, 12))
yrbrks <- c(-Inf, 1985, 1994, 2003, Inf)
molabs <- c('JFM', 'AMJ', 'JAS', 'OND')
yrlabs <- c('1974-1985', '1986-1994', '1995-2003', '2004-2012')
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ wrtdsperf(tidfit)

```r
# setup month, year categories for trend summaries
mobrks <- c(-Inf, 3, 6, 9, Inf)
mobrks <- list(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9), c(10, 11, 12))
yrbrks <- c(-Inf, 1985, 1994, 2003, Inf)
molabs <- c('JFM', 'AMJ', 'JAS', 'OND')
yrlabs <- c('1974-1985', '1986-1994', '1995-2003', '2004-2012')
Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions README_files/navigation-1.1/FileSaver.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions README_files/navigation-1.1/codefolding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

window.initializeCodeFolding = function(show) {

// handlers for show-all and hide all
$("#rmd-show-all-code").click(function() {
$('div.r-code-collapse').each(function() {
$(this).collapse('show');
});
});
$("#rmd-hide-all-code").click(function() {
$('div.r-code-collapse').each(function() {
$(this).collapse('hide');
});
});

// index for unique code element ids
var currentIndex = 1;

// select all R code blocks
var rCodeBlocks = $('pre.r');
rCodeBlocks.each(function() {

// create a collapsable div to wrap the code in
var div = $('<div class="collapse r-code-collapse"></div>');
if (show)
div.addClass('in');
var id = 'rcode-643E0F36' + currentIndex++;
div.attr('id', id);
$(this).before(div);
$(this).detach().appendTo(div);

// add a show code button right above
var showCodeText = $('<span>' + (show ? 'Hide' : 'Code') + '</span>');
var showCodeButton = $('<button type="button" class="btn btn-default btn-xs code-folding-btn pull-right"></button>');
showCodeButton.append(showCodeText);
showCodeButton
.attr('data-toggle', 'collapse')
.attr('data-target', '#' + id)
.attr('aria-expanded', show)
.attr('aria-controls', id);

var buttonRow = $('<div class="row"></div>');
var buttonCol = $('<div class="col-md-12"></div>');

buttonCol.append(showCodeButton);
buttonRow.append(buttonCol);

div.before(buttonRow);

// update state of button on show/hide
div.on('hidden.bs.collapse', function () {
showCodeText.text('Code');
});
div.on('show.bs.collapse', function () {
showCodeText.text('Hide');
});
});

}
9 changes: 9 additions & 0 deletions README_files/navigation-1.1/sourceembed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


window.initializeSourceEmbed = function(filename) {
$("#rmd-download-source").click(function() {
var src = window.atob($("#rmd-source-code").html());
var blob = new Blob([src], {type: "text/x-r-markdown"});
saveAs(blob, filename);
});
};
86 changes: 86 additions & 0 deletions README_files/navigation-1.1/tabsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@


window.buildTabsets = function(tocID) {

// build a tabset from a section div with the .tabset class
function buildTabset(tabset) {

// check for fade and pills options
var fade = tabset.hasClass("tabset-fade");
var pills = tabset.hasClass("tabset-pills");
var navClass = pills ? "nav-pills" : "nav-tabs";

// determine the heading level of the tabset and tabs
var match = tabset.attr('class').match(/level(\d) /);
if (match === null)
return;
var tabsetLevel = Number(match[1]);
var tabLevel = tabsetLevel + 1;

// find all subheadings immediately below
var tabs = tabset.find("div.section.level" + tabLevel);
if (!tabs.length)
return;

// create tablist and tab-content elements
var tabList = $('<ul class="nav ' + navClass + '" role="tablist"></ul>');
$(tabs[0]).before(tabList);
var tabContent = $('<div class="tab-content"></div>');
$(tabs[0]).before(tabContent);

// build the tabset
tabs.each(function(i) {

// get the tab div
var tab = $(tabs[i]);

// get the id then sanitize it for use with bootstrap tabs
var id = tab.attr('id');

// remove any table of contents entries associated with
// this ID (since we'll be removing the heading element)
$("div#" + tocID + " li a[href='#" + id + "']").parent().remove();

// sanitize the id for use with bootstrap tabs
id = id.replace(/[.\/?&!#<>]/g, '').replace(/\s/g, '_');
tab.attr('id', id);

// get the heading element within it, grab it's text, then remove it
var heading = tab.find('h' + tabLevel + ':first');
var headingText = heading.html();
heading.remove();

// build and append the tab list item
var a = $('<a role="tab" data-toggle="tab">' + headingText + '</a>');
a.attr('href', '#' + id);
a.attr('aria-controls', id);
var li = $('<li role="presentation"></li>');
li.append(a);
if (i === 0)
li.attr('class', 'active');
tabList.append(li);

// set it's attributes
tab.attr('role', 'tabpanel');
tab.addClass('tab-pane');
tab.addClass('tabbed-pane');
if (fade)
tab.addClass('fade');
if (i === 0) {
tab.addClass('active');
if (fade)
tab.addClass('in');
}

// move it into the tab content div
tab.detach().appendTo(tabContent);
});
}

// convert section divs with the .tabset class to tabsets
var tabsets = $("div.section.tabset");
tabsets.each(function(i) {
buildTabset($(tabsets[i]));
});
};

5 changes: 4 additions & 1 deletion man/annual_agg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e797c0f

Please sign in to comment.