Ash - 3 months ago 29

R Question

I need to plot an

`AR(1)`

`y[k] = 0.75 * y[k-1] + e[k] for y0 = 1.`

Assume that

`e[k]`

`[-0.5, 0.5]`

I am trying to use

`arima.sim`

`library(tseries)`

y.0 <- arima.sim(model=list(ar=.75), n=100)

plot(y.0)

It does not seem correct. Also, what parameters do I change if

`y[0] = 10`

Answer

We want to use R base function `arima.sim`

for this task, and no extra libraries are required.

By default, `arima.sim`

generates ARIMA with innovations ~ `N(0,1)`

. If we want to change this, we need to control the `rand.gen`

or `innov`

argument. For example, you want innovations from uniform distributions `U[-0.5, 0.5]`

, we can do either of the following:

```
arima.sim(model=list(ar=.75), n=100, rand.gen = runif, min = -0.5, max = 0.5)
arima.sim(model=list(ar=.75), n = 100, innov = runif(100, -0.5, 0.5))
```

**Example**

```
set.seed(0)
y <- arima.sim(model=list(ar=.75), n = 100, innov = runif(100, -0.5, 0.5))
ts.plot(y)
```

In case we want to have explicit control on `y[0]`

, we can just shift the above time series such that it starts from `y[0]`

. Suppose `y0`

is our desired starting value, we can do

```
y <- y - y[1] + y0
```

For example, starting from `y0 = 1`

:

```
y <- y - y[1] + 1
ts.plot(y)
```