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!!
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")
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()
As for the 2 first moments of your random walk, see this post of Mathematics Stack Exchange.