Baixe o app para aproveitar ainda mais
Prévia do material em texto
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,] -0.315600329 [13,] -0.320707072 [14,] -0.341260786 [15,] -0.336039006 [16,] -0.299153645 [17,] -0.215101539 [18,] -0.154908350 [19,] -0.162841021 [20,] -0.153521927 [21,] -0.133979280 $type [1] "correlation" $n.used [1] 100 $lag , , 1 [,1] [1,] 0 [2,] 1 [3,] 2 [4,] 3 [5,] 4 [6,] 5 [7,] 6 [8,] 7 [9,] 8 [10,] 9 [11,] 10 [12,] 11 [13,] 12 [14,] 13 [15,] 14 [16,] 15 [17,] 16 [18,] 17 [19,] 18 [20,] 19 [21,] 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] [1,] 0.748512403 [2,] -0.198593141 [3,] 0.161605633 [4,] -0.045146522 [5,] -0.092461467 [6,] -0.001441216 [7,] -0.026228801 [8,] -0.193432432 [9,] -0.065314914 [10,] -0.115355223 [11,] -0.050063371 [12,] -0.026196645 [13,] -0.119366937 [14,] -0.015351077 [15,] -0.016780893 [16,] 0.071314527 [17,] -0.047199599 [18,] -0.138808338 [19,] -0.021273769 [20,] -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 0.63656103 -0.54056772 -0.57388832 -2.15373596 [7] -2.97748126 2.43923508 -0.14334954 1.03403360 -1.37024531 [97] -0.20538390 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] [1,] -0.468055236 [2,] -0.226386291 [3,] -0.064574413 [4,] -0.044515987 [5,] -0.167726138 [6,] -0.101464760 [7,] 0.160809025 [8,] 0.025753880 [9,] 0.016403226 [10,] 0.113118340 [11,] 0.070886919 [12,] -0.046819468 [13,] -0.055640224 [14,] -0.038556585 [15,] -0.022126555 [16,] -0.017238550 [17,] 0.055662118 [18,] 0.003943671 [19,] 0.002336962 [20,] 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 0.0854933511 -0.0434418077 -1.2835971342 - [6] -1.8957362452 0.3333418141 0.9664180374 0.9278551531 - [96] -1.7813203295 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,] -0.103890952 [7,] -0.028736539 [8,] -0.002451483 [9,] 0.014186051 [10,] 0.037810416 [11,] 0.015967242 [12,] 0.060710828 [13,] -0.177129949 [14,] -0.269788989 [15,] -0.160815161 [16,] 0.089192658 [17,] 0.178159031 [18,] 0.092086805 [19,] 0.026411807 [20,] 0.032653798 [21,] -0.036084767 $type [1] "correlation" $n.used [1] 100 $lag , , 1 [,1] [1,] 0 [2,] 1 [3,] 2 [4,] 3 [5,] 4 [6,] 5 [7,] 6 [8,] 7 [9,] 8 [10,] 9 [11,] 10 [12,] 11 [13,] 12 [14,] 13 [15,] 14 [16,] 15 [17,] 16 [18,] 17 [19,] 18 [20,] 19 [21,] 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")
Compartilhar