pchanges: Lagged Proportion/Percent Changes

Description Usage Arguments Details Value Note Author(s) References See Also Examples

Description

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

Usage

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

Arguments

x

Numeric vector.

lag

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

percent

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

Details

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

Value

Numeric vector.

Note

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

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, pdiffs, 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 changes between subsequent values
y <- pchanges(x)

# Calculate percent changes instead
z <- pchanges(x, percent = TRUE)

# Compare results
x
y
z

stocks documentation built on May 2, 2019, 5:22 p.m.