Creating Multiple Random Walk Paths In R

Hi there. This is a follow up to my page on Random Walks In R. The previous page was about generating a single random walk path and plotting it. In this page, the focus is on creating on multiple random walk paths.

 


Sections

The Code For Multiple Random Walks In R

Random Walk Paths Plot

Alternate Way By Using The replicate() Function

References

 


The Code For Multiple Random Walks In R

Create one random walk path is not too bad. When it comes to simulating multiple random walk paths, it gets somewhat tricky.

I first initialize some values such as the time and the number of random walk paths I want to simulate. In this example, I choose 8 for num_paths.

I also initialize my matrix full of zeroes with the number of rows equal to the number of paths and the number of columns being the time + 1 (I have to include the time at t = 0).

Using a for loop, I simulate the positions at each time point for each path. The for loop will start with the first path and go through all the time points and then it goes to the second path and so on.

From the check of head(rw_paths), we have a wide formatted data set:

Instead of this wide format, I would like the time points as rows and the paths going down by column. In R, I add a time = 0 column and I transpose this wide format into a long format. (Transpose comes from linear algebra which means to switch rows with columns and vice versa.)

The columns after Time have no titles. This can be fixed easily. I also remove the row names with the X1, X2, etc.

Here is another check.

 

This table looks nice and can be easily read. The downside of it is that it’s not usable for plotting in ggplot2. I want to reformat the data from a wide format to a long format where I have a column stating which path the position value goes with at each time point.

To do this, I use tidyr. (dplyr is used for the %>% pipe operator).

In the gather() function from tidyr, I leave the time column alone. The Path_1, Path_2 up to Path_8 column names are now values under the new Path column. The values under Path_1, Path_2 up to Path_8 are now values in this new column called Position. These values correspond to the Path value and to the time point.

 


Random Walk Paths Plot

After generating our random walk paths and data formatting, the random walk paths can be plotted in R with ggplot2.

 

 

 

For different coloured paths add in colour = Path in the aes() part in ggplot().

 

 


Alternate Way By Using The replicate() Function

Instead of using a for loop for generating multiple random walk paths, the replicate() function can be used.

To show how replicate() works, here is an example. I use the replicate() function to simulate 8 standard uniform random variables 10 times.

Each of the ten replications are in columns.

 

The R code for multiple random walks is pretty similar to the one in the previous section. The for loop section has been modified for the replicate() function.

The neat part with replicate is that there is no need to transpose this time around. Here is the rest of the code before plotting. (This code section is pretty much the same as before.)

 

Here is the code and output of the plots.

 

 

 

 


References

  • http://www.phytools.org/eqg/Exercise_4.1/
  • https://stackoverflow.com/questions/17150183/plot-multiple-lines-in-one-graph
  • Hands-On Programming With R By Garrett Grolemund

Leave a Reply