Skip to content
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

Feature/helpers #31

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(cut_error)
export(chord_chart)
export(crosstable)
export(cut_volumes)
Expand Down
85 changes: 83 additions & 2 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ numerify <- function(x){
#' @return a numeric vector of \code{length(x)} containing the percent deviation
#' from \code{y}
#'
#' @details Given in percentage points (already multiplied by 100),
#' \eqn{ ((x - y) / y) * 100}
#'
#' @examples
#' pct_error(435, 422)
#'
#' @export
#'
pct_error <- function(x, y) {
(x - y) / y * 100
ifelse(
x == 0 & y == 0, 0, # protect against divide by zero on empty links
(x - y) / y * 100)
}

#' Calculate RMSE
Expand All @@ -35,6 +43,13 @@ pct_error <- function(x, y) {
#'
#' @return The root mean squared error between \code{x} and \code{y}
#'
#' @details \eqn{\sqrt{\sum(x - y)^2/(n - 1)}}
#'
#' @examples
#' x <- runif(10)
#' y <- x + rnorm(10)
#' rmse(x, y)
#'
#' @export
#'
rmse <- function(x, y){
Expand All @@ -49,6 +64,15 @@ rmse <- function(x, y){
#'
#' @return The percent root mean squared error between \code{x} and \code{y}
#'
#' @details \eqn{\sqrt{\sum(x - y)^2/(n - 1)} / \bar{y} * 100}
#'
#' @seealso rmse
#'
#' @examples
#' x <- runif(10)
#' y <- x + rnorm(10)
#' pct_rmse(x, y)
#'
#' @export
#'
pct_rmse <- function(x, y){
Expand All @@ -57,14 +81,18 @@ pct_rmse <- function(x, y){




#' Cut volumes into pretty levels
#'
#' @param x Volume levels
#' @param breaks Breakpoints for the volume groups
#' @return A labeled factor variable of \code{length(x)} with the levels of
#' \code{x} cut into bins.
#'
#' @examples
#' cuts <- cut_volumes(links$volume)
#' cuts[1:10]
#' table(cuts)
#'
#' @export
#'
cut_volumes <- function(x, breaks = c(0, 5, 10, 15, 20, 40, 60, Inf)) {
Expand All @@ -79,3 +107,56 @@ cut_volumes <- function(x, breaks = c(0, 5, 10, 15, 20, 40, 60, Inf)) {

}


#' Cut error measurements into ranges
#'
#' @param x a vector of percent error measurements, as from \link{pct_error}
#'
#' @param breaks A vector of error ranges; zero and infinity will be added.
#' @param negative Mirror the breaks on the negative side, default is TRUE.
#'
#' @return a factor variable with each entry in x binned.
#'
#' @details This is a convenience wrapper to \code{\link[base](cut)} with
#' sensible pre-coded options for travel demand output analysis.
#'
#' @examples
#' cuts <- cut_error(rnorm(100, 0, 10))
#' table(cuts)
#'
#' @export
#'
cut_error <- function(x, breaks = c(5, 10, 20, 1), negative = TRUE){

if(negative){
breaks <- c(-Inf, rev(-1 * breaks), 0, breaks, Inf)
} else {
breaks <- c(0, breaks, Inf)
}

cut(x, breaks)

}


#' Cut diverging differences into ranges
#'
#' @param x a vector of diverging error measurements, as in \eqn{x - y}
#' @return a factor variable with each entry in x binned.
#'
cut_diverror <- function(x){
.Deprecated("cut_error")
brks <- c(1, 10, 100, 1000)
cut(x, breaks = c(-Inf, rev(-1 * brks), 0, brks, Inf))
}

#' Cut absolute differences into ranges
#'
#' @param x a vector of absolute error measurements, as in \eqn{x - y}
#' @return a factor variable with each entry in x binned.
#'
cut_abserror <- function(x){
.Deprecated("cut_error")
brks <- c(1, 10, 100, 1000)
cut(x, breaks = c(-Inf, rev(-1 * brks), 0, brks, Inf))
}
18 changes: 18 additions & 0 deletions man/cut_abserror.Rd

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

18 changes: 18 additions & 0 deletions man/cut_diverror.Rd

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

31 changes: 31 additions & 0 deletions man/cut_error.Rd

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

6 changes: 6 additions & 0 deletions man/cut_volumes.Rd

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

9 changes: 9 additions & 0 deletions man/pct_error.Rd

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

13 changes: 13 additions & 0 deletions man/pct_rmse.Rd

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

10 changes: 10 additions & 0 deletions man/rmse.Rd

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