1

Generating a Random Walk in R is pretty easy. It's done by the following code:

x <- rnorm(100)
y <- cumsum(x)

But how do I generate/simulate a Random Walk with Trend and / or Drift?

Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56
user3750030
  • 327
  • 3
  • 7
  • how about `cumsum(x+d)`, where `d` is the drift per step??? – Ben Bolker Jun 17 '14 at 20:39
  • 1
    [This question](http://stackoverflow.com/questions/19162429/calculate-correlation-of-data-generated-by-function-in-r) is not identical to yours, but happens to answer your question. – Ben Bolker Jun 17 '14 at 20:44
  • Just add an increasing function to that series. I don't see how that is at all difficult. – IRTFM Jun 17 '14 at 20:46
  • You should probably post your answer as such. It seems reasonably compact but would need an explanation to avoid the "no code only answers" censoring police. – IRTFM Jun 17 '14 at 21:15
  • OK, when I get around to it (anyone else is free to do so, or copy from the linked question, in the meantime) – Ben Bolker Jun 17 '14 at 21:22
  • Don't quite understand why q. was closed -- I could appreciate "too simple", "almost a duplicate" ... but unreproducible or typographical error? – Ben Bolker Jun 20 '14 at 12:46

1 Answers1

5

A slightly more compact/efficient version of the code from here:

cumsum(rnorm(n=100, mean=drift, sd=sqrt(variance)))

should give you a realization of a random walk with variance t*variance and mean t*drift, where t is the index (starting from 1; prepend a zero or add a constant to the whole series if you like).

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453