VarMeasures: Variability Measures

Description Usage Arguments Value Note Author(s) Examples

Description

Variability Measures

by_id - Internal function to allow a simple statistic (e.g., SD) to be calculated individually by an ID variable and returned either as per ID (i.e., wide form) or for every observation of an ID (i.e., long form).

sd_id - Calculates the standard deviation of observations by ID.

rmssd - Calculates the root mean square of successive differences (RMSSD). Note that missing values are removed.

rmssd_id - Calculates the RMSSD by ID.

rolling_diff - Calculates the average rolling difference of the data. Within each window, the difference between the maximum and minimum value is computed and these are averaged across all windows. The equation is:

\frac{∑_{t = 1}^{N - k} max(x_{t}, …, x_{t + k}) - min(x_{t}, …, x_{t + k})}{N - k}

rolling_diff_id - Calculates the average rolling difference by ID

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
by_id(x, ID, fun, long = TRUE, ...)

sd_id(x, ID, long = TRUE)

rmssd(x)

rmssd_id(x, ID, long = TRUE)

rolling_diff(x, window = 4)

rolling_diff_id(x, ID, long = TRUE, window = 4)

Arguments

x

A data vector to operate on. Should be a numeric or integer vector, or coercible to such (e.g., logical).

ID

an ID variable indicating how to split up the x vector. Should be the same length as x.

fun

The function to calculate by ID

long

A logical indicating whether to return results in “long” form (the default) or wide (if FALSE).

window

An integer indicating the size of the rolling window. Must be at least the length of x.

...

Additional arguments passed on to fun

Value

by_id - A vector the same length as x if long=TRUE, or the length of unique IDs if long=FALSE.

sd_id - A vector of the standard deviations by ID

rmssd - The RMSSD for the data.

rmssd_id - A vector of the RMSSDs by ID

rolling_diff - The average of the rolling differences between maximum and minimum.

rolling_diff_id - A vector of the average rolling differences by ID

Note

These are a set of functions designed to calculate various measures of variability either on a single data vector, or calculate them by an ID.

Author(s)

Joshua F. Wiley <josh@elkhartgroup.com>

Examples

1
2
3
4
5
6
7
8
9
sd_id(mtcars$mpg, mtcars$cyl, long=TRUE)
sd_id(mtcars$mpg, mtcars$cyl, long=FALSE)
rmssd(1:4)
rmssd(c(1, 3, 2, 4))
rmssd_id(mtcars$mpg, mtcars$cyl)
rmssd_id(mtcars$mpg, mtcars$cyl, long=FALSE)
rolling_diff(1:7, window = 4)
rolling_diff(c(1, 4, 3, 4, 5))
rolling_diff_id(mtcars$mpg, mtcars$cyl, window = 3)

Example output

Loading required package: rstan
Loading required package: ggplot2
Loading required package: StanHeaders
rstan (Version 2.17.3, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
 [1] 1.453567 1.453567 4.509828 1.453567 2.560048 1.453567 2.560048 4.509828
 [9] 4.509828 1.453567 1.453567 2.560048 2.560048 2.560048 2.560048 2.560048
[17] 2.560048 4.509828 4.509828 4.509828 4.509828 2.560048 2.560048 2.560048
[25] 2.560048 4.509828 4.509828 4.509828 2.560048 1.453567 2.560048 4.509828
       4        6        8 
4.509828 1.453567 2.560048 
[1] 1
[1] 1.732051
 [1] 1.723852 1.723852 6.348071 1.723852 3.061799 1.723852 3.061799 6.348071
 [9] 6.348071 1.723852 1.723852 3.061799 3.061799 3.061799 3.061799 3.061799
[17] 3.061799 6.348071 6.348071 6.348071 6.348071 3.061799 3.061799 3.061799
[25] 3.061799 6.348071 6.348071 6.348071 3.061799 1.723852 3.061799 6.348071
       4        6        8 
6.348071 1.723852 3.061799 
[1] 3
[1] 2.5
 [1] 2.060000 2.060000 7.588889 2.060000 4.133333 2.060000 4.133333 7.588889
 [9] 7.588889 2.060000 2.060000 4.133333 4.133333 4.133333 4.133333 4.133333
[17] 4.133333 7.588889 7.588889 7.588889 7.588889 4.133333 4.133333 4.133333
[25] 4.133333 7.588889 7.588889 7.588889 4.133333 2.060000 4.133333 7.588889

varian documentation built on May 2, 2019, 6:09 a.m.