I have to admit that I'm completely new to R. Thus, my question may be very simple.
For an assignment, I need to simulate a random walk series. The initial position is a fixed point a on the real line. The first step is then taken with length X1 and the current position of the random walk process is changed to S(1)=a+X1. This process continues until n=1000.
After generating the random variable X~N(1,10^2) 1000 times, I need to report the value of S(n) and show the plot of this random walk series. I also need to report the mean and variance of the series.
This is what I have so far:
set.seed(1234)
x<-rnorm(1000,mean=1,sd=10)
a<--2
How do I generate an S so that I can have an S(n) for every n up to 1000? (I'm sorry if this is a very rudimentary question.)
In order to run simulations with random variables, we will use the R command r + distname , where distname is the name of the distribution, such as unif , geom , pois , norm , exp or binom . The first argument to any of these functions is the number of samples to create.
Simulation of non-uniform random variables are often done by transforming (pseudo- random) uniform random variables. Here we consider the simplest method called inversion. In the simplest case of inversion, we have a continuous random variable X with a strictly increasing distribution function F.
Each element of x
is a step, therefore, the total distance traveled, i.e. the value of S(N) is the sum of the elements. In addition, the distance traveled at each S(p)
equals the sum of the random walk vector x
up to index p
.
Using plot
and cumsum
you can generate the graph that shows the function of S(n) over time:
Not to be rude, but I think these kind of basic questions are typically something your supervisor or teacher is best equipped to help you with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With