0% found this document useful (0 votes)
12 views10 pages

simulation of time series models practs-1

The document provides an overview of simulating various time series models using the 'arima.sim' function in R, including White Noise, Random Walk, Autoregressive (AR), Moving Average (MA), and ARMA models. It includes examples of how to generate these models, visualize them, and fit them using ARIMA specifications. Additionally, it presents exercises for further practice on simulating and estimating these models.

Uploaded by

cynthia20njeri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

simulation of time series models practs-1

The document provides an overview of simulating various time series models using the 'arima.sim' function in R, including White Noise, Random Walk, Autoregressive (AR), Moving Average (MA), and ARMA models. It includes examples of how to generate these models, visualize them, and fit them using ARIMA specifications. Additionally, it presents exercises for further practice on simulating and estimating these models.

Uploaded by

cynthia20njeri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Simulation of Time Series models

Ms. Matabel Odin

10/16/2023

The function ‘arima.sim’ can be used to simulate a number of time series models. Based on the model we
want to apply, we specify the appropriate values for p, d and q to the model ARIMA(p,d,q)
arima.sim(model, n) model is where we specify the AR, differencing order and MA components. n is the
length of output series.
1. Simulate the White Noise
For the White Noise model, all p, d and q in arima model are 0. So, ARIMA(0,0,0) is simply the White
Noise(WN) model.
WN <- arima.sim(model = list(order = c(0, 0, 0)), n = 200)
#plotting the white noise
plot.ts(WN,col=4, main="White Noise Series")

White Noise Series


3
2
1
WN

0
−1
−2

0 50 100 150 200

Time

#change the value of n to check how the series changes

1
While generating the White Noise series, we can also specify the mean and standard deviation. In the
following example, we simulate White Noise model with mean=60 and sd=8
WN_2 <- arima.sim(model = list(order = c(0, 0, 0)),
n = 200, mean=60, sd=8)
plot.ts(WN_2,col=4, main="White Noise Series (mean=60, sd=8)")

White Noise Series (mean=60, sd=8)


80
70
60
WN_2

50
40
30
20

0 50 100 150 200

Time

Estimating the White Noise (Model Fitting)


arima(WN_2, order = c(0, 0, 0))

##
## Call:
## arima(x = WN_2, order = c(0, 0, 0))
##
## Coefficients:
## intercept
## 58.6068
## s.e. 0.5761
##
## sigma^2 estimated as 66.39: log likelihood = -703.34, aic = 1410.68
The intercept 59.896 is the mean and sigmaˆ2 59.19 is the variance, which gives a standard deviation, sigma
of 7.69. Compare this with the mean and standard deviation we calculated using the standard mean() and
sd() functions
2. Simulating a Random Walk Model
Since the differencing of a Random Walk series is a White Noise series, we can say that a Random Walk

2
series is a cumulative sum (i.e., Integration) of a zero mean White Noise series. With this information, we
can define Random Walk series in the form of ARIMA model as follows:
ARIMA(0,1,0) Where: - Augoregressive part, p = 0 - Integration, d = 1 - Moving average part, q = 0
RW <- arima.sim(model= list(order = c(0, 1, 0)), n=200)
plot.ts(RW,main="Random Walk", col=4)

Random Walk
40
30
20
RW

10
0

0 50 100 150 200

Time

As we can clearly observe, this is a non-stationary series and its mean and standard deviation is not constant
over time.
First Difference Series
In order to make the series stationary, we take the first order difference of the series.
RW_diff <- diff(RW)

#When plotted you will notice that the difference series resembles White Noise

Exercise:
i) Simulate a random walk with drift
ii)Estimate a random walk model using the simulated data.
3. Autoregressive Model (AR)
# Simulate AutoRegressive model with 0.5 slope
AR_1 <- arima.sim(model = list(ar = 0.5), n = 200)
# Simulate AutoRegressive model with -0.5 slope

3
AR_2 <- arima.sim(model = list(ar = -0.5), n = 200)
plot.ts(cbind(AR_1 , AR_2 ), main="AR Model Simulated Data")

AR Model Simulated Data


3
1
AR_1

−1
3−3
1
AR_2

−3 −1

0 50 100 150 200

Time

It is important to note that Random Walk is a special case of AutoRegressive models, where the slope
parameter is equal to 1.
Exercise:
Estimate an AR model using the simulated data
4. Moving Average Model
# Simulate MA model with 0.5 slope
MA_1 <- arima.sim(model = list(ma = 0.5), n = 200)
# Simulate MA model with 0.8 slope
MA_2 <- arima.sim(model = list(ma = 0.9), n = 200)
# Simulate MA model with -0.6 slope
MA_3 <- arima.sim(model = list(ma = -0.6), n = 200)
plot.ts(cbind(MA_1 , MA_2, MA_3 ), main="MA Model Simulated Data")

4
MA Model Simulated Data
3
1
MA_1
−1
1 2 3 −3
MA_2
−1
−3
2
MA_3
1
0
−2

0 50 100 150 200

Time

Exercise:
Estimate an MA model using the simulated data
5. ARMA model
#simulate ARMA(1,1)
set.seed(1)
x=arima.sim(n=200, model=list(ar=0.5, ma=0.5))
plot.ts(x)

5
3
2
1
x

0
−1
−2
−3

0 50 100 150 200

Time

par(mfrow=c(1,2))
acf(x)
pacf(x)
#identifying order of ARMA
## use eacf
library(TSA) #load package for eacf

## Warning: package 'TSA' was built under R version 4.0.5


##
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar

6
Series x Series x
1.0

0.6
0.8

0.4
0.6

Partial ACF
ACF

0.2
0.4
0.2

0.0
0.0

0 5 10 15 20 −0.2 5 10 15 20

Lag Lag

mod1=eacf(x,ar.max=7, ma.max=13)

## AR/MA
## 0 1 2 3 4 5 6 7 8 9 10 11 12 13
## 0 x x x o o o o o o o o o o o
## 1 x o o o o o o o o o o o o o
## 2 x o o o o o o o o o o o o o
## 3 o x x o o o o o o o o o o o
## 4 x x o o o o o o o o o o o o
## 5 x o o o o o o o o o o o o o
## 6 x x o o o o o o o o o o o o
## 7 x o o o o o o o o x o o o o
##selects an ARMA(1,1)
#simulate ARMA(2,1)
set.seed(2)
x1=arima.sim(n=200, model=list(ar=c(0.5,-0.6), ma=-0.5))
par(mfrow=c(1,2))
acf(x1)
pacf(x1)

7
Series x1 Series x1

0.1
0.2

−0.1
Partial ACF
0.0
ACF

−0.2

−0.3
−0.4

5 10 15 20 −0.5 5 10 15 20

Lag Lag

mod2=eacf(x1,ar.max=7, ma.max=13)

## AR/MA
## 0 1 2 3 4 5 6 7 8 9 10 11 12 13
## 0 x x x o x o x x o x o o o o
## 1 x x x x x x x x o x o o o o
## 2 x o o o o o o o o o o o o o
## 3 x o o o o o o o o o o o o o
## 4 x o o o o o o o o o o o o o
## 5 x o o o o o o o o o o o o o
## 6 x x o o o o o o o o o o o o
## 7 x x x x o x o o o o o o o o
##selects ARMA(2,1)
#**try other combinations
names(mod2)

## [1] "eacf" "ar.max" "ma.ma" "symbol"


print(mod2$eacf,digits=2)

## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]


## [1,] 0.15 -0.525 -0.4278 0.1188 0.3561 0.1122 -0.2080 -0.203 0.0636
## [2,] 0.25 -0.449 -0.4946 0.1480 0.3157 0.1562 -0.1885 -0.224 0.0856
## [3,] -0.49 0.133 -0.0451 -0.0234 -0.0444 0.0045 0.0428 -0.063 0.0231
## [4,] -0.33 -0.073 -0.0650 0.0083 -0.0517 0.0035 0.0350 -0.052 -0.0227
## [5,] -0.39 0.086 -0.0073 -0.0687 -0.0213 -0.0402 0.0142 -0.040 -0.0041
## [6,] -0.50 0.104 -0.0168 -0.0453 -0.0362 -0.0442 -0.0067 -0.040 0.0100

8
## [7,] -0.46 -0.432 -0.1279 0.0282 -0.1277 -0.0287 0.0221 -0.068 0.0223
## [8,] -0.49 0.219 -0.2174 -0.2869 0.0039 0.2366 -0.0030 -0.073 0.0152
## [,10] [,11] [,12] [,13] [,14]
## [1,] 0.1636 0.0281 -0.1389 -0.1004 0.050
## [2,] 0.1602 0.0320 -0.1242 -0.1255 0.040
## [3,] -0.0644 -0.0023 -0.0346 0.0473 0.022
## [4,] -0.0640 -0.0017 -0.0121 0.0299 0.021
## [5,] -0.0344 -0.0358 -0.0101 0.0103 0.061
## [6,] -0.0078 -0.0089 -0.0285 0.0065 0.034
## [7,] -0.0114 -0.0092 -0.0110 0.0061 0.080
## [8,] -0.0025 -0.0121 -0.0074 0.0045 0.067
#simulate ARMA(4,1)
set.seed(3)
x111=arima.sim(n=200, model=list(ar=c(0.5,-0.6,0.2,0.3), ma=-0.5))
par(mfrow=c(1,2))
acf(x111)
pacf(x111)

Series x111 Series x111

0.2
0.6

0.0
0.2

Partial ACF

−0.2
ACF

−0.2

−0.4
−0.6
−0.6

5 10 15 20 5 10 15 20

Lag Lag

mod22=eacf(x111,ar.max=7, ma.max=13)

## AR/MA
## 0 1 2 3 4 5 6 7 8 9 10 11 12 13
## 0 o x o x x x x x x x x x x x
## 1 o x o x x x x x x x x x x x
## 2 x x o o o o o o o o o x o x
## 3 x x o o o o o o o o o o o x

9
## 4 x o o o o o o o o o o o o o
## 5 x x o o o o o o o o o o o o
## 6 x o x o o o o o o o o o o o
## 7 x o o o o o o o o o o o o o
##selects ARMA(4,1)

ARIMA Modelling
In an ARIMA model, the elements are specified in the order (AR order, differencing, MA order).
i) A model with one AR term would be specified as an ARIMA of order (1,0,0).
ii) A model with two AR terms would be specified as an ARIMA of order (2,0,0) iii)A model with two MA
terms (MA(2)) would be specified as an ARIMA of order (0,0,2). iv)A model with one AR term, a first
difference, and one MA term would be specified as ARIMA of order (1,1,1).

10

You might also like