Jess - 2 months ago 21

R Question

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.)

Answer

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.

Source (Stackoverflow)

Comments