Time-Based Rolling Functions

Provides rolling statistical functions based on date and time windows instead of n-lagged observations.


CRANversion Travis buildstatus AppVeyor buildstatus Coveragestatus

License: GPLv3

The goal of tbrf is to provide time-window based rolling statistical functions. The package differs from other rolling statistic packages because the intended use is for irregular measured data. Althogh tbrf can be used to apply statistical functions to regularly sampled data, zoo, RcppRoll, and other packages provide fast, efficient, and rich implementations of rolling/windowed functions.

An appropriate example case is water quality data that is measured at irregular time intervals. Regulatory compliance is often based on a statistical average measure or exceedance probability applied to all samples collected in the previous 7-years. tbrf can be used to display regulatory status at any sample point.

tbrf identifies the previous n measurements within the specified time window, applies the function, and outputs a varaible with the result of the rolling statistical measure.

Installation

tbrf is available on CRAN:

install.packages("tbrf")

The development version is maintained on github and can be installed as:

devtools::install.github("mps9506/tbrf")

Available Functions

  • tbr_binom: Rolling binomal probability with confidence intervals.

  • tbr_gmean: Rolling geometric mean with confidence intervals.

  • tbr_mean: Rolling mean with confidence intervals.

  • tbr_median: Rolling median with confidence intervals.

  • tbr_misc: Accepts user specified function.

  • tbr_sd: Rolling standard deviation.

  • tbr_sum: Rolling sum.

Usage

See:

https://mps9506.github.io/tbrf/

Example

Plot a rolling 6-hour geometric mean

library(tbrf)
library(dplyr)
library(ggplot2)
library(ggalt)
 
y <- -100*log(runif(50))
time = sample(seq(as.POSIXct(strptime("2017-01-01 00:01:00", "%Y-%m-%d %H:%M:%S")),
                  as.POSIXct(strptime("2017-01-03 23:00:00", "%Y-%m-%d %H:%M:%S")),
                  by = "min"), 50)
 
df <- tibble(time, y)
 
df %>% 
  tbr_gmean(y, time, unit = "hours", n = 6, conf = 0.95, type = "perc") %>%
  ggplot() +
  geom_point(aes(time, y)) +
  geom_step(aes(time, mean)) +
  geom_ribbon(aes(time, ymin = lwr_ci, ymax = upr_ci), alpha = 0.5, stat = "stepribbon")

Contributing

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

tbrf code is released under GPL-3 | LICENSE.md

binom_ci() is an implementation of code licensed under GPL (>=2) by Frank Harrell’s Hmisc package.

Test Results

library(tbrf)
 
date()
## [1] "Fri Feb 01 10:30:23 2019"
 
devtools::test()
## v | OK F W S | Context
## 
/ |  0       | core functions work in piped workflow
- |  1       | core functions work in piped workflow
\ |  2       | core functions work in piped workflow
| |  3       | core functions work in piped workflow
/ |  4       | core functions work in piped workflow
- |  5       | core functions work in piped workflow
\ |  6       | core functions work in piped workflow
v |  6       | core functions work in piped workflow [0.9 s]
## 
/ |  0       | core functions return expected errors and messages
- |  1       | core functions return expected errors and messages
\ |  2       | core functions return expected errors and messages
| |  3       | core functions return expected errors and messages
/ |  4       | core functions return expected errors and messages
- |  5       | core functions return expected errors and messages
\ |  6       | core functions return expected errors and messages
| |  7       | core functions return expected errors and messages
v |  7       | core functions return expected errors and messages
## 
/ |  0       | core functions return expected structures and values
- |  1       | core functions return expected structures and values
\ |  2       | core functions return expected structures and values
| |  3       | core functions return expected structures and values
/ |  4       | core functions return expected structures and values
- |  5       | core functions return expected structures and values
\ |  6       | core functions return expected structures and values
| |  7       | core functions return expected structures and values
/ |  8       | core functions return expected structures and values
v |  8       | core functions return expected structures and values [2.0 s]
## 
/ |  0       | internal statistical functions return expected values
- |  1       | internal statistical functions return expected values
\ |  2       | internal statistical functions return expected values
| |  3       | internal statistical functions return expected values
/ |  4       | internal statistical functions return expected values
- |  5       | internal statistical functions return expected values
\ |  6       | internal statistical functions return expected values
| |  7       | internal statistical functions return expected values
/ |  8       | internal statistical functions return expected values
- |  9       | internal statistical functions return expected values
\ | 10       | internal statistical functions return expected values
| | 11       | internal statistical functions return expected values
v | 11       | internal statistical functions return expected values [0.8 s]
## 
## == Results ==========================================================================================
## Duration: 3.8 s
## 
## OK:       32
## Failed:   0
## Warnings: 0
## Skipped:  0

News

tbrf 0.1.2

Bug fixes

  • dplyr 0.8.0rc introduced breaking changes. Evaluation errors are now properly fixed with the expected realese version.

tbrf 0.1.1

Minor Changes

  • dplyr 0.8.0 no longer includes "Evaluation error" in message, unit tests are updated to reflect this change. (pull request #5)

tbrf 0.1.0

  • Intial release.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("tbrf")

0.1.2 by Michael Schramm, 8 months ago


https://mps9506.github.io/tbrf/


Report a bug at https://github.com/mps9506/tbrf/issues


Browse source code at https://github.com/cran/tbrf


Authors: Michael Schramm [aut, cre] , Frank Harrell [ctb]


Documentation:   PDF Manual  


Task views: Time Series Analysis


GPL-3 | file LICENSE license


Imports boot, dplyr, lubridate, purrr, rlang, tibble, tidyr

Suggests covr, ggalt, ggplot2, testthat, knitr, rmarkdown


See at CRAN