**Life in Code**, and kindly contributed to R-bloggers)

I was wondering about the *null distribution* of successive differences of random sequences, and decided to do some numerical experiments. I quickly realized that successive differences equates to taking successively higher-order numerical derivatives, which functions as a high-pass filter. So, the *null distribution* really depends on the spectrum of the original timeseries.

Here, I’ve only played with random sequences, which are equivalent to white noise. Using the wonderful animation package, I created a movie that shows the timeseries resulting from differencing, along with their associated power spectra. You can see that, by the end, almost all of the power is concentrated in the highest frequencies. The code required to reproduce this video is shown below.

Note — For optimum viewing, switch the player quality to high.

require(animation) ## large canvas, write output to this directory, 1 second per frame ani.options(ani.width=1280, ani.height=720, loop=F, title='Successive differences of white noise', outdir='.', interval=1) require(plyr) require(xts) ## How many realizations to plot? N=5 ## random numbers aa = sapply(1:26, function(x) rnorm(1e2)); colnames(aa) = LETTERS; saveVideo( { ## for successive differences, do... for (ii in 1:50) { ## first make the differences and normalize aa1 = apply(aa, 2, function(x) { ret=diff(x, differences=ii);ret=ret/max(ret) }); ## Turn into timeseries object for easy plotting aa1 = xts(aa1, as.Date(1:nrow(aa1))); ## next, compute spectrum aa2 = alply(aa1, 2, function(x) { ## of each column, don't modify original data ret=spectrum(x, taper=0, fast=F, detrend=F, plot=F); ## turn into timeseries object ret= zoo(ret$spec, ret$freq)}); ## combine into timeseries matrix aa2 = do.call(cbind, aa2 ); colnames(aa2) = LETTERS; ## plot of timeseries differences ## manually set limits so plot area is exactly the same between successive figures myplot = xyplot(aa1[,1:N], layout=c(N,1), xlab=sprintf('Difference order = %d', ii), ylab='Normalized Difference', ylim=c(-1.5, 1.5), scales=list(alternating=F, x=list(rot=90), y=list(relation='same'))); ## plot of spectrum myplot1 = xyplot(aa2[,1:N], layout=c(N,1), ylim=c(-0.01, 5), xlim=c(0.1, 0.51), xlab='Frequency', ylab='Spectral Density', type=c('h','l'), scales=list(y=list(relation='same'), alternating=F)); ## write them to canvas plot(myplot, split=c(1,1,1,2), more=T); plot(myplot1, split=c(1,2,1,2), more=F); ## provide feedback of process print(ii)} ## controls for ffmpeg }, other.opts = "-b 5000k -bt 5000k")

**leave a comment**for the author, please follow the link and comment on their blog:

**Life in Code**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...