pdiffs {stocks} | R Documentation |
Lagged Proportion Differences
Description
Calculates proportion differences between subsequent (or lagged) elements of a vector.
Usage
pdiffs(x, lag = 1L)
Arguments
x |
Numeric vector. |
lag |
Numeric value (e.g. 2 for differences between 1st and 3rd element, 2nd and 4th, ...). |
Value
Numeric vector.
Examples
# Generate 10 values from N(0, 1)
x <- rnorm(10)
# Calculate vector of proportion differences between subsequent values
(y <- pdiffs(x))
# Equivalent base R computation
len <- length(x)
p1 <- x[2: len]
p2 <- x[1: (len - 1)]
y2 <- (p1 - p2) / (0.5 * (p1 + p2))
all.equal(y, y2)
[Package stocks version 1.1.4 Index]