We can use the animation package to produce animations in R. This short article shows you how to create a Brownian motion with the brownian.motion() function.

Simply speaking, a Brownian motion shows the trace of the coordinates

where $\epsilon_i$ is i.i.d from a standard Normal distribution. That is fairly easy to program in R – it is nothing but cumsum(rnorm(n)), and that is what brownian.motion() does internally.

library(animation)
ani.options(nmax = 50)  # create 50 image frames
set.seed(20121106)
brownian.motion(n = 20, pch = 21, cex = 4, col = "magenta", bg = "cyan", 
  xlim = c(-10, 10), ylim = c(-15, 15))

Done.

References

This article was reproduced form vistat.



Published

06 November 2012

Tags