Time Series Summaries
Period Change
Compare With Prior Values
A period change compares the current observation with an earlier point in the same series.
Program
Play the script to change the lookback distance and see the difference.
period_change.R
Replay: real traced execution (multi-file project)
periods_back <- 1
values <- c(100, 108, 111, 120)
current <- values[length(values)]
previous <- values[length(values) - periods_back]
change <- current - previous
label <- paste("change", change, sep = ":")
cat(label, "\n", sep = "")
periods_back <- 2
values <- c(100, 108, 111, 120)
current <- values[length(values)]
previous <- values[length(values) - periods_back]
change <- current - previous
label <- paste("change", change, sep = ":")
cat(label, "\n", sep = "")
periods_back <- 3
values <- c(100, 108, 111, 120)
current <- values[length(values)]
previous <- values[length(values) - periods_back]
change <- current - previous
label <- paste("change", change, sep = ":")
cat(label, "\n", sep = "")
periods_back ← 1
1periods_back <- 12values <- c(100, 108, 111, 120)values this step1periods_backvalues ← 100, 108, 111, 120
1periods_back <- 12values <- c(100, 108, 111, 120)3current <- values[length(values)]values this step100, 108, 111, 120valuescurrent ← 120
2values <- c(100, 108, 111, 120)3current <- values[length(values)]4previous <- values[length(values) - periods_back]values this step120currentlatest is lastvaluesprevious ← 111
3current <- values[length(values)]4previous <- values[length(values) - periods_back]5change <- current - previousvalues this step111previous1periods_backchange ← 9
4previous <- values[length(values) - periods_back]5change <- current - previous6label <- paste("change", change, sep = ":")values this step9change120current111previouslabel ← change:9
5change <- current - previous6label <- paste("change", change, sep = ":")7cat(label, "\n", sep = "")values this stepchange:9label9changecat(label, " ", sep = "")
6label <- paste("change", change, sep = ":")7cat(label, "\n", sep = "")outputchange:9values this stepchange:9label
periods_back ← 2
1periods_back <- 22values <- c(100, 108, 111, 120)values this step2periods_backvalues ← 100, 108, 111, 120
1periods_back <- 22values <- c(100, 108, 111, 120)3current <- values[length(values)]values this step100, 108, 111, 120valuescurrent ← 120
2values <- c(100, 108, 111, 120)3current <- values[length(values)]4previous <- values[length(values) - periods_back]values this step120currentlatest is lastvaluesprevious ← 108
3current <- values[length(values)]4previous <- values[length(values) - periods_back]5change <- current - previousvalues this step108previous2periods_backchange ← 12
4previous <- values[length(values) - periods_back]5change <- current - previous6label <- paste("change", change, sep = ":")values this step12change120current108previouslabel ← change:12
5change <- current - previous6label <- paste("change", change, sep = ":")7cat(label, "\n", sep = "")values this stepchange:12label12changecat(label, " ", sep = "")
6label <- paste("change", change, sep = ":")7cat(label, "\n", sep = "")outputchange:12values this stepchange:12label
periods_back ← 3
1periods_back <- 32values <- c(100, 108, 111, 120)values this step3periods_backvalues ← 100, 108, 111, 120
1periods_back <- 32values <- c(100, 108, 111, 120)3current <- values[length(values)]values this step100, 108, 111, 120valuescurrent ← 120
2values <- c(100, 108, 111, 120)3current <- values[length(values)]4previous <- values[length(values) - periods_back]values this step120currentlatest is lastvaluesprevious ← 100
3current <- values[length(values)]4previous <- values[length(values) - periods_back]5change <- current - previousvalues this step100previous3periods_backchange ← 20
4previous <- values[length(values) - periods_back]5change <- current - previous6label <- paste("change", change, sep = ":")values this step20change120current100previouslabel ← change:20
5change <- current - previous6label <- paste("change", change, sep = ":")7cat(label, "\n", sep = "")values this stepchange:20label20changecat(label, " ", sep = "")
6label <- paste("change", change, sep = ":")7cat(label, "\n", sep = "")outputchange:20values this stepchange:20label
current
The current value is the last observation in the ordered vector.
lookback
`periods_back` selects how far back to compare.
difference
`current - previous` is the absolute period change.