-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
calcPercentile()
refinements
#408
Open
jack-davison
wants to merge
3
commits into
master
Choose a base branch
from
calcPercentile
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−73
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,133 @@ | ||
#' Calculate percentile values from a time series | ||
#' | ||
#' Calculates multiple percentile values from a time series, with flexible time | ||
#' aggregation. | ||
#' aggregation. This function is a wrapper for [timeAverage()], making it easier | ||
#' to calculate several percentiles at once. Like [timeAverage()], it requires a | ||
#' data frame with a `date` field and one other numeric variable. | ||
#' | ||
#' This is a utility function to calculate percentiles and is used in, for | ||
#' example, \code{timePlot}. Given a data frame with a \code{date} field and one | ||
#' other numeric variable, percentiles are calculated. | ||
#' | ||
#' @param mydata A data frame of data with a \code{date} field in the format | ||
#' \code{Date} or \code{POSIXct}. Must have one variable to apply calculations | ||
#' to. | ||
#' @param pollutant Name of variable to process. Mandatory. | ||
#' @param avg.time Averaging period to use. See [timeAverage()] for details. | ||
#' @param percentile A vector of percentile values. For example \code{percentile | ||
#' = 50} for median values, \code{percentile = c(5, 50, 95} for multiple | ||
#' percentile values. | ||
#' @param data.thresh Data threshold to apply when aggregating data. See | ||
#' [timeAverage()] for details. | ||
#' @param start Start date to use - see [timeAverage()] for details. | ||
#' @inheritParams timeAverage | ||
#' @param pollutant Name of column containing variable to summarise, likely a | ||
#' pollutant (e.g., `"o3"`). | ||
#' @param percentile A vector of percentile values; for example, `percentile = | ||
#' 50` will calculate median values. Multiple values may also be provided as a | ||
#' vector, e.g., `percentile = c(5, 50, 95)` or `percentile = seq(0, 100, | ||
#' 10)`. | ||
#' @param prefix Each new column is named by appending a `prefix` to | ||
#' `percentile`. For example, the default `"percentile."` will name the new | ||
#' column as `percentile.95` when `percentile = 95`. | ||
#' @export | ||
#' @return Returns a data frame with new columns for each percentile level. New | ||
#' columns are given names like percentile.95 e.g. when percentile = 95 is | ||
#' chosen. See examples below. | ||
#' | ||
#' @return Returns a `data.frame` with a `date` column plus an additional | ||
#' column for each given `percentile`. | ||
#' @author David Carslaw | ||
#' @seealso \code{\link{timePlot}}, [timeAverage()] | ||
#' @seealso [timePlot()], [timeAverage()] | ||
#' @examples | ||
#' # 95th percentile monthly o3 concentrations | ||
#' percentiles <- calcPercentile(mydata, pollutant ="o3", | ||
#' avg.time = "month", percentile = 95) | ||
#' percentiles <- calcPercentile(mydata, | ||
#' pollutant = "o3", | ||
#' avg.time = "month", percentile = 95 | ||
#' ) | ||
#' | ||
#' head(percentiles) | ||
#' | ||
#' # 5, 50, 95th percentile monthly o3 concentrations | ||
#' \dontrun{ | ||
#' percentiles <- calcPercentile(mydata, pollutant ="o3", | ||
#' avg.time = "month", percentile = c(5, 50, 95)) | ||
#' percentiles <- calcPercentile(mydata, | ||
#' pollutant = "o3", | ||
#' avg.time = "month", percentile = c(5, 50, 95) | ||
#' ) | ||
#' | ||
#' head(percentiles) | ||
#' } | ||
calcPercentile <- function(mydata, pollutant = "o3", avg.time = "month", percentile = 50, | ||
data.thresh = 0, start = NA) { | ||
|
||
make.percentile <- function(mydata, pollutant = "o3", avg.time = "month", percentile = 50, | ||
data.thresh = 0, start = NA) { | ||
mydata <- timeAverage( | ||
mydata, avg.time, | ||
statistic = "percentile", percentile = percentile, | ||
data.thresh = data.thresh, start.date = NA | ||
calcPercentile <- function(mydata, | ||
pollutant = "o3", | ||
avg.time = "month", | ||
percentile = 50, | ||
type = "default", | ||
data.thresh = 0, | ||
start.date = NA, | ||
end.date = NA, | ||
prefix = "percentile.") { | ||
# check pollutant is valid | ||
if (!pollutant %in% names(mydata)) { | ||
cli::cli_abort( | ||
c( | ||
"x" = "{.field pollutant} '{pollutant}' not present in data.", | ||
"i" = "{.emph Columns in {.field mydata}}: {names(mydata)}" | ||
) | ||
) | ||
## change column name | ||
new.name <- paste("percentile.", percentile, sep = "") | ||
names(mydata)[names(mydata) == pollutant] <- new.name | ||
results <- mydata[, new.name, drop = FALSE] | ||
results <- data.frame(date = mydata$date, results) | ||
} | ||
|
||
# iterate over 'percentile's to calculate %tiles | ||
mydata <- purrr::map( | ||
.x = percentile, | ||
.f = function(x) { | ||
make.percentile( | ||
mydata, | ||
pollutant = pollutant, | ||
avg.time = avg.time, | ||
data.thresh = data.thresh, | ||
percentile = x, | ||
type = type, | ||
start.date = start.date, | ||
end.date = end.date, | ||
prefix = prefix | ||
) | ||
} | ||
) | ||
|
||
results | ||
# get joining columns | ||
by <- "date" | ||
if (type != "default") { | ||
by <- append(by, type) | ||
} | ||
|
||
mydata <- lapply(percentile, function(x) | ||
make.percentile( | ||
mydata, | ||
pollutant = pollutant, avg.time = avg.time, | ||
data.thresh = data.thresh, percentile = x | ||
)) | ||
# bind into one | ||
mydata <- purrr::reduce(mydata, function(x, y) { | ||
dplyr::full_join(x, y, by = by) | ||
}) | ||
|
||
mydata <- Reduce(function(x, y, by = "date") merge(x, y, by = "date", all = TRUE), mydata) | ||
# return | ||
return(mydata) | ||
} | ||
|
||
#' Use timeAverage to calculate percentiles, with some custom naming | ||
#' @noRd | ||
make.percentile <- function(mydata, | ||
pollutant, | ||
avg.time, | ||
percentile, | ||
type, | ||
data.thresh, | ||
start.date, | ||
end.date, | ||
prefix) { | ||
# summarise percentiles with timeAverage | ||
mydata <- timeAverage( | ||
mydata, | ||
avg.time, | ||
statistic = "percentile", | ||
percentile = percentile, | ||
type = type, | ||
data.thresh = data.thresh, | ||
start.date = start.date, | ||
end.date = end.date, | ||
progress = FALSE | ||
) | ||
|
||
# change output column name | ||
new.name <- paste0(prefix, percentile) | ||
names(mydata)[names(mydata) == pollutant] <- new.name | ||
|
||
if (type == "default") { | ||
cols <- c("date", new.name) | ||
} else { | ||
cols <- c("date", type, new.name) | ||
} | ||
|
||
# construct new dataframe with just new column & date | ||
results <- mydata[, cols] | ||
|
||
mydata | ||
# return | ||
return(results) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's this 'start' argument, that seemingly never gets used.