Lagged Proportion/Percent Differences

Share:

Description

Calculates proportion/percent differences between subsequent (or lagged) elements of a numeric vector.

Usage

1
pdiffs(x, lag = 1, percent = FALSE)

Arguments

x

Numeric vector.

lag

Controls spacing between proportion/percent differences. For example, lag of 1 means you want to calculate differences between elements 1 and 2, 2 and 3, 3 and 4, and so on; a lag of 2 means you want calculate differences between elements 1 and 3, 2 and 4, 3 and 5, and so on.

percent

TRUE for percent differences, FALSE for proportion differences. Percent differences are just proportion differences multiplied by 100.

Details

Each proportion/percent difference is based on two numbers, say x1 and x2. The proportion difference is defined as (x1 - x2) / mean(x1, x2), and the percent difference is the same quantity multiplied by 100. Notice that this is NOT proportion/percent change, which divides by the initial value x1 rather than the average of x1 and x2. See pchanges for the proportion/percent change operation.

Value

Numeric vector.

Note

This function uses C++ code to achieve a 3-4 times speed increase compared to the base R code: len <- length(x); p1 <- x[2: len]; p2 <- x[1: (len - 1)]; (p1 - p2) / (0.5 * (p1 + p2)).

Author(s)

Dane R. Van Domelen

References

Acknowledgment: This material is based upon work supported by the National Science Foundation Graduate Research Fellowship under Grant No. DGE-0940903.

See Also

diffs, pchanges, ratios

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Randomly generate 10 values from a standard normal distribution
x <- rnorm(10)

# Calculate vector of proportion differences between subsequent values
y <- pdiffs(x)

# Calculate percent differences instead
z <- pdiffs(x, percent = TRUE)

# Compare results
x
y
z

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.