Simulating a random variable of a time series in R?

I have to admit that I am completely new to R. So my question could be very simple.

For the assignment, I need to simulate a series of random walks. The starting position is the 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 a random variable X ~ N (1.10 ^ 2) 1000 times, I need to report the value of S (n) and show a graph of this series of random walks. I also need to report the mean and variance of the batch.

This is what I have so far:

set.seed(1234)
x<-rnorm(1000,mean=1,sd=10)
a<--2

      

How do I create S so that I can have S (n) for every n up to 1000? (I'm sorry if this is a very rudimentary question.)

+3


source to share


2 answers


Each element x

is a step, therefore the total distance traveled, i.e. the S (N) value is the sum of the elements. In addition, the distance traveled at each S(p)

is equal to the sum of the vector of the random walk x

to the index p

.

Using plot

and cumsum

, you can generate a graph that shows the S (n) function over time:



enter image description here

Not to be rude, but I think these basic questions are usually what your supervisor or teacher can best help you with.

+4


source


M=1000; x = rnorm(M); plot(cumsum(x), type='l')



0


source







All Articles