You are on page 1of 8

Simulating AR, MA, and ARMA Time Series

R : Copyright 2003, The R Development Core Team


Version 1.7.1 (2003-06-16)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type `license()' or `licence()' for distribution details.
R is a collaborative project with many contributors.
Type `contributors()' for more information.
Type `demo()' for some demos, `help()' for on-line help, or
`help.start()' for a HTML browser interface to help.
Type `q()' to quit R.

1. Simulate 100 observations from an AR(2) Process


> ar.sim<-arima.sim(model=list(ar=c(.9,-.2)),n=100)
> ar.sim
Time Series:
Start = 1
End = 100
Frequency = 1
[1] -1.8433144709 -0.8242793538

1.7323369494

0.6918267491

[89] -1.3957581475 -0.7982898404 -0.4325019866 -0.5814940714


[97] -0.9410123993 -1.6146934819 -1.8047969752 0.2062163735

Make a time series plot of the data


> ts.plot(ar.sim)

Calculate the Sample Autocorrelation Function


> ar.acf<-acf(ar.sim,type="correlation",plot=T)
> ar.acf
$acf
, , 1
[,1]
[1,] 1.000000000
[2,] 0.748512403
[3,] 0.472943619
[4,] 0.343917588
[5,] 0.259486147
[6,] 0.144046313
[7,] 0.054406353
[8,] 0.005221371
[9,] -0.099083683
[10,] -0.207233569
[11,] -0.282578172

[12,]
[13,]
[14,]
[15,]
[16,]
[17,]
[18,]
[19,]
[20,]
[21,]

-0.315600329
-0.320707072
-0.341260786
-0.336039006
-0.299153645
-0.215101539
-0.154908350
-0.162841021
-0.153521927
-0.133979280

$type
[1] "correlation"
$n.used
[1] 100
$lag
, , 1
[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
[10,]
[11,]
[12,]
[13,]
[14,]
[15,]
[16,]
[17,]
[18,]
[19,]
[20,]
[21,]

[,1]
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

$series
[1] "ar.sim"
$snames
NULL
attr(,"class")
[1] "acf"

Calculate the Sample Partial Autocorrelation Function


> ar.pacf<-acf(ar.sim,type="partial",plot=T)
> ar.pacf
$acf
, , 1
[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
[10,]
[11,]
[12,]
[13,]
[14,]
[15,]
[16,]
[17,]
[18,]
[19,]
[20,]

[,1]
0.748512403
-0.198593141
0.161605633
-0.045146522
-0.092461467
-0.001441216
-0.026228801
-0.193432432
-0.065314914
-0.115355223
-0.050063371
-0.026196645
-0.119366937
-0.015351077
-0.016780893
0.071314527
-0.047199599
-0.138808338
-0.021273769
-0.097357883

$type
[1] "partial"
$n.used
[1] 100

$lag
, , 1
[,1]
[1,]
1
[2,]
2
[3,]
3
.
:
[19,]
19
[20,]
20
$series
[1] "ar.sim"
$snames
NULL
attr(,"class")
[1] "acf"

2. Simulate 100 observations from an MA(2) Process


> ma.sim<-arima.sim(model=list(ma=c(-.7,.1)),n=100)
> ma.sim
Time Series:
Start = 1
End = 100
Frequency = 1
[1] -0.87447285
[7] -2.97748126
[97] -0.20538390

0.63656103 -0.54056772 -0.57388832 -2.15373596


2.43923508 -0.14334954 1.03403360 -1.37024531
1.06791016 -0.94993364 -0.78650534

Make a time series plot of the data


> ts.plot(ma.sim)

Calculate the Sample Autocorrelation Function


> ma.acf<-acf(ma.sim,type="correlation",plot=T)
> ma.acf
$acf
, , 1
[,1]
[1,] 1.00000000
[2,] -0.46805524
[3,] 0.04228515
[4,] 0.03384565
[5,] -0.03368293
[6,] -0.09589762
[7,] 0.06240342
[8,] 0.14175077
[9,] -0.14860861
[10,] 0.07104588
[11,] 0.06607164
[12,] -0.07641769
[13,] -0.05545542
[14,] 0.05480033
[15,] -0.01883246
[16,] -0.02417799
[17,] 0.05422257
[18,] 0.05044019
[19,] -0.09032521
[20,] 0.02966781
21,] 0.01376021
$type
[1] "correlation"
$n.used
[1] 100

$lag
, , 1
[,1]
[1,]
0
[2,]
1
[3,]
2
[4,]
3
:
:
[20,]
19
[21,]
20
$series
[1] "ma.sim"
$snames
NULL
attr(,"class")
[1] "acf"

Calculate the Sample Partial Autocorrelation Function


> ma.pacf<-acf(ma.sim,type="partial",plot=T)
> ma.pacf
$acf
, , 1

[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
[10,]
[11,]
[12,]
[13,]
[14,]
[15,]
[16,]
[17,]
[18,]
[19,]
[20,]

[,1]
-0.468055236
-0.226386291
-0.064574413
-0.044515987
-0.167726138
-0.101464760
0.160809025
0.025753880
0.016403226
0.113118340
0.070886919
-0.046819468
-0.055640224
-0.038556585
-0.022126555
-0.017238550
0.055662118
0.003943671
0.002336962
0.012238024

$type
[1] "partial"
$n.used
[1] 100

$lag
, , 1
[,1]
[1,]
1
[2,]
2
[3,]
3
[4,]
4
[5,]
5
:
:
[19,]
19
[20,]
20
$series
[1] "ma.sim"
$snames
NULL
attr(,"class")
[1] "acf"

3. Simulate 100 observations from an ARMA(2,2) Process


> arma.sim<-arima.sim(model=list(ar=c(.9,-.2),ma=c(-.7,.1)),n=100)
> arma.sim
Time Series:
Start = 1
End = 100
Frequency = 1
[1] 0.1483409916
[6] -1.8957362452
[96] -1.7813203295

0.0854933511 -0.0434418077 -1.2835971342 0.3333418141 0.9664180374 0.9278551531 1.1258970748

0.0996796875 -0.1425092157

Make a time series plot of the data


> ts.plot(arma.sim)

Calculate the Sample Autocorrelation Function


> arma.acf<-acf(arma.sim,type="correlation",plot=T)
> arma.acf
$acf
, , 1
[,1]
[1,] 1.000000000
[2,] 0.315245096
[3,] -0.059896299
[4,] -0.237902709
[5,] -0.092537118

[6,]
[7,]
[8,]
[9,]
[10,]
[11,]
[12,]
[13,]
[14,]
[15,]
[16,]
[17,]
[18,]
[19,]
[20,]
[21,]

-0.103890952
-0.028736539
-0.002451483
0.014186051
0.037810416
0.015967242
0.060710828
-0.177129949
-0.269788989
-0.160815161
0.089192658
0.178159031
0.092086805
0.026411807
0.032653798
-0.036084767

$type
[1] "correlation"
$n.used
[1] 100
$lag
, , 1

[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
[10,]
[11,]
[12,]
[13,]
[14,]
[15,]
[16,]
[17,]
[18,]
[19,]
[20,]
[21,]

[,1]
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

$series
[1] "arma.sim"
$snames
NULL
attr(,"class")

Calculate the Sample Partial Autocorrelation Function


[1] "acf"
> arma.pacf<-acf(arma.sim,type="partial",plot=T)
> arma.pacf
$acf
, , 1
[,1]
[1,] 0.31524510
[2,] -0.17685114
[3,] -0.18331078
[4,] 0.04658796
[5,] -0.14628277
[6,] -0.00399760
[7,] -0.01257163
[8,] -0.04032162
[9,] 0.04088976
[10,] -0.02888498
[11,] 0.07058412
[12,] -0.25198075
[13,] -0.16572419
[14,] -0.01731102
[15,] 0.02316375
[16,] 0.06311867
[17,] -0.05903080
[18,] 0.01426275
[19,] 0.05546888
[20,] -0.08827248
$type
[1] "partial"
$n.used
[1] 100
$lag
, , 1
[,1]
[1,]
1
[2,]
2
[3,]
3
[4,]
4
[5,]
5
:
:
[19,]
19
[20,]
20
$series
[1] "arma.sim"
$snames
NULL
attr(,"class")

You might also like