1

I'm trying to plot multiple simple Random Walks in R, but am having problems doing so.

Please be aware that by simple Random Walk I mean the Sum of Random Variables that can either be {-1} or {1} with each values having the same probability, not some Random Walk absed on white Noise. (see the definition on https://en.wikipedia.org/wiki/Random_walk#One-dimensional_random_walk )

I use the following code to plot the Random Walks:

set.seed(1)

n <- 200
Random_Walk<- cumsum(sample(c(-1, 1), n, TRUE))

n <- 200
Random_Walk_2 <- cumsum(sample(c(-1, 1), n, TRUE))


ts.plot(Random_Walk, gpars=list(xlab="Length of Random Walk", ylab="Distance from origin",lty=c(1:1)))

This code works fine, but once I try to plot both Random Walks in the same Graph it breaks. Can someone explain how i could plot both of them or even multiple Random Walks in one Graph?

Additionally I was wondering whether there is some tools that could give me the variance or the standard deviation of all those Random Walks

Thank you all in advance!!

user82931
  • 13
  • 3

2 Answers2

2

This is a possible solution in R-base

plot(Random_Walk, type = "l", xlim = c(0, 200), ylim = c(-15, 15), 
     col = "blue", xlab = "n", ylab = "Rw")
par(new=T)
plot(Random_Walk_2, type = "l", xlim = c(0, 200), ylim = c(-15, 15), 
     col = "red", xlab = "n", ylab = "Rw")

This is a possible solution with ggplot2:

library(ggplot2)

df_rw <- data.frame(n = 1:200, r1 = Random_Walk, r2 = Random_Walk_2)

ggplot(df_rw) + 
  geom_line(aes(n, r1), col = "blue") + 
  geom_line(aes(n, r2), col = "red") + 
  labs(x = "n", y = "Rw")

This is another possibile solution with ggplot2

library(ggplot2)

df_rw2 <- data.frame(n = c(1:200, 1:200), 
                     rw = c(Random_Walk, Random_Walk_2), 
                     lab = rep(c("Random Walk 1", "Random Walk 2"), each = 200))

ggplot(df_rw2) + 
  geom_line(aes(x = n, y = rw, color = lab)) + 
  scale_color_manual(values = c("red", "blue"))
Leonardo
  • 2,439
  • 33
  • 17
  • 31
1

Here is a simple base R solution with the many times forgotten function matplot.

RW <- cbind(Random_Walk, Random_Walk_2)
matplot(RW, type = "l", lty = "solid")

enter image description here

A ggplot2 solution could be the following. But the data format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format.

library(tidyverse)

as.data.frame(RW) %>%
  mutate(x = row_number()) %>%
  pivot_longer(-x) %>%
  ggplot(aes(x, value, color = name)) +
  geom_line()

enter image description here


As for the 2 first moments of your random walk, see this post of Mathematics Stack Exchange.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Thank you that comment is great! I was wondering how I could add the specific average value or standard deviation of let's say 10 plotted random walks. Is there any way to do that in R? – user82931 Jan 31 '21 at 20:25
  • @mathNerd01 If you want to have the sample mean and sd on the graph, use function `text` in base R and `geom_text` or `geom_label` in ggplot. – Rui Barradas Jan 31 '21 at 20:29