Calculate Percent Change
Delt.RdCalculate the k-period percent difference within one series, or between two series. Primarily used to calculate the percent change from one period to another of a given series, or to calculate the percent difference between two series over the full series.
Usage
Delt(x1, x2 = NULL, k = 0, type = c("arithmetic", "log"))Details
When called with only x1, the one period percent change of the
series is returned by default. Internally this happens by copying
x1 to x2. A two period difference would be specified with k=2.
If called with both x1 and x2, the difference between
the two is returned. That is, k=0. A one period difference would be
specified by k=1. k may also be a vector to calculate
more than one period at a time. The results will then be an m x length(k)
Arithmetic differences are used by default: Lag = (x2(t) - x1(t-k))/x1(t-k)
Log differences are calculated: Lag = log(x2(t)/x1(t-k))
Examples
Stock.Open <- c(102.25,102.87,102.25,100.87,103.44,103.87,103.00)
Stock.Close <- c(102.12,102.62,100.12,103.00,103.87,103.12,105.12)
Delt(Stock.Open) #one period pct. price change
#> Delt.1.arithmetic
#> [1,] NA
#> [2,] 0.006063570
#> [3,] -0.006027024
#> [4,] -0.013496333
#> [5,] 0.025478338
#> [6,] 0.004156999
#> [7,] -0.008375854
Delt(Stock.Open,k=1) #same
#> Delt.1.arithmetic
#> [1,] NA
#> [2,] 0.006063570
#> [3,] -0.006027024
#> [4,] -0.013496333
#> [5,] 0.025478338
#> [6,] 0.004156999
#> [7,] -0.008375854
Delt(Stock.Open,type='arithmetic') #using arithmetic differences (default)
#> Delt.1.arithmetic
#> [1,] NA
#> [2,] 0.006063570
#> [3,] -0.006027024
#> [4,] -0.013496333
#> [5,] 0.025478338
#> [6,] 0.004156999
#> [7,] -0.008375854
Delt(Stock.Open,type='log') #using log differences
#> Delt.1.log
#> [1,] NA
#> [2,] 0.006045260
#> [3,] -0.006045260
#> [4,] -0.013588236
#> [5,] 0.025159175
#> [6,] 0.004148383
#> [7,] -0.008411129
Delt(Stock.Open,Stock.Close) #Open to Close pct. change
#> Delt.0.arithmetic
#> [1,] -0.001271394
#> [2,] -0.002430252
#> [3,] -0.020831296
#> [4,] 0.021116288
#> [5,] 0.004156999
#> [6,] -0.007220564
#> [7,] 0.020582524
Delt(Stock.Open,Stock.Close,k=0:2) #...for 0,1, and 2 periods
#> Delt.0.arithmetic Delt.1.arithmetic Delt.2.arithmetic
#> [1,] -0.001271394 NA NA
#> [2,] -0.002430252 0.003618582 NA
#> [3,] -0.020831296 -0.026732770 -0.020831296
#> [4,] 0.021116288 0.007334963 0.001263731
#> [5,] 0.004156999 0.029741251 0.015843521
#> [6,] -0.007220564 -0.003093581 0.022305938
#> [7,] 0.020582524 0.012034274 0.016241299