Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating time series random variable in R?

Tags:

r

time-series

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

like image 635
Jess Avatar asked Feb 11 '13 06:02

Jess


People also ask

How do you simulate a random variable in R?

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.

How do you simulate a random variable?

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.


1 Answers

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:

enter image description here

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.

like image 65
Paul Hiemstra Avatar answered Oct 16 '22 06:10

Paul Hiemstra