rm(list = ls())
require(tidyverse)
## Zorunlu paket yükleniyor: tidyverse
## Warning: package 'tidyverse' was built under R version 4.2.2
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 0.3.5
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## Warning: package 'ggplot2' was built under R version 4.2.2
## Warning: package 'tidyr' was built under R version 4.2.2
## Warning: package 'readr' was built under R version 4.2.2
## Warning: package 'purrr' was built under R version 4.2.2
## Warning: package 'dplyr' was built under R version 4.2.2
## Warning: package 'forcats' was built under R version 4.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
require(tidymodels)
## Zorunlu paket yükleniyor: tidymodels
## Warning: package 'tidymodels' was built under R version 4.2.2
## ── Attaching packages ────────────────────────────────────── tidymodels 1.0.0 ──
## ✔ broom 1.0.1 ✔ rsample 1.1.0
## ✔ dials 1.1.0 ✔ tune 1.0.1
## ✔ infer 1.0.4 ✔ workflows 1.1.2
## ✔ modeldata 1.0.1 ✔ workflowsets 1.0.0
## ✔ parsnip 1.0.3 ✔ yardstick 1.1.0
## ✔ recipes 1.0.3
## Warning: package 'broom' was built under R version 4.2.2
## Warning: package 'dials' was built under R version 4.2.2
## Warning: package 'scales' was built under R version 4.2.2
## Warning: package 'infer' was built under R version 4.2.2
## Warning: package 'modeldata' was built under R version 4.2.2
## Warning: package 'parsnip' was built under R version 4.2.2
## Warning: package 'recipes' was built under R version 4.2.2
## Warning: package 'rsample' was built under R version 4.2.2
## Warning: package 'tune' was built under R version 4.2.2
## Warning: package 'workflows' was built under R version 4.2.2
## Warning: package 'workflowsets' was built under R version 4.2.2
## Warning: package 'yardstick' was built under R version 4.2.2
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ scales::discard() masks purrr::discard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ recipes::fixed() masks stringr::fixed()
## ✖ dplyr::lag() masks stats::lag()
## ✖ yardstick::spec() masks readr::spec()
## ✖ recipes::step() masks stats::step()
## • Search for functions across packages at https://www.tidymodels.org/find/
require(data.table)
## Zorunlu paket yükleniyor: data.table
## Warning: package 'data.table' was built under R version 4.2.2
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
##
## between, first, last
## The following object is masked from 'package:purrr':
##
## transpose
require(tidyposterior)
## Zorunlu paket yükleniyor: tidyposterior
## Warning: package 'tidyposterior' was built under R version 4.2.2
require(tsibble)
## Zorunlu paket yükleniyor: tsibble
## Warning: package 'tsibble' was built under R version 4.2.2
##
## Attaching package: 'tsibble'
## The following object is masked from 'package:data.table':
##
## key
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
require(fable)
## Zorunlu paket yükleniyor: fable
## Warning: package 'fable' was built under R version 4.2.2
## Zorunlu paket yükleniyor: fabletools
## Warning: package 'fabletools' was built under R version 4.2.2
##
## Attaching package: 'fabletools'
## The following object is masked from 'package:yardstick':
##
## accuracy
## The following object is masked from 'package:parsnip':
##
## null_model
## The following objects are masked from 'package:infer':
##
## generate, hypothesize
require(ggfortify)
## Zorunlu paket yükleniyor: ggfortify
## Warning: package 'ggfortify' was built under R version 4.2.2
## Registered S3 method overwritten by 'ggfortify':
## method from
## autoplot.glmnet parsnip
require(forecast)
## Zorunlu paket yükleniyor: forecast
## Warning: package 'forecast' was built under R version 4.2.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## autoplot.Arima ggfortify
## autoplot.acf ggfortify
## autoplot.ar ggfortify
## autoplot.bats ggfortify
## autoplot.decomposed.ts ggfortify
## autoplot.ets ggfortify
## autoplot.forecast ggfortify
## autoplot.stl ggfortify
## autoplot.ts ggfortify
## fitted.ar ggfortify
## fortify.ts ggfortify
## residuals.ar ggfortify
##
## Attaching package: 'forecast'
## The following objects are masked from 'package:fabletools':
##
## accuracy, forecast
## The following object is masked from 'package:yardstick':
##
## accuracy
require(tseries)
## Zorunlu paket yükleniyor: tseries
## Warning: package 'tseries' was built under R version 4.2.2
require(chron)
## Zorunlu paket yükleniyor: chron
## Warning: package 'chron' was built under R version 4.2.2
##
## Attaching package: 'chron'
## The following object is masked from 'package:tseries':
##
## is.weekend
require(lubridate)
## Zorunlu paket yükleniyor: lubridate
## Warning: package 'lubridate' was built under R version 4.2.2
## Zorunlu paket yükleniyor: timechange
## Warning: package 'timechange' was built under R version 4.2.2
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:chron':
##
## days, hours, minutes, seconds, years
## The following object is masked from 'package:tsibble':
##
## interval
## The following objects are masked from 'package:data.table':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
require(directlabels)
## Zorunlu paket yükleniyor: directlabels
## Warning: package 'directlabels' was built under R version 4.2.2
require(zoo)
## Zorunlu paket yükleniyor: zoo
## Warning: package 'zoo' was built under R version 4.2.2
##
## Attaching package: 'zoo'
## The following object is masked from 'package:tsibble':
##
## index
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
require(lmtest)
## Zorunlu paket yükleniyor: lmtest
## Warning: package 'lmtest' was built under R version 4.2.2
require(TTR)
## Zorunlu paket yükleniyor: TTR
## Warning: package 'TTR' was built under R version 4.2.2
##
## Attaching package: 'TTR'
## The following object is masked from 'package:dials':
##
## momentum
theme_set(theme_bw())
par(mar = c(1,1,1,1))
plot(lynx)
autoplot(lynx) +
xlab("") + ylab("") +
ggtitle("Time Series Plot of the `lynx' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))
par(mar = c(0.01,0.01,0.01,0.01))
Using the R tsdisplay method, I generated the ACF and PACF plots
tsdisplay(lynx)
ggtsdisplay, ggplot grafiklerini kullanarak eşdeğer çizimi üretecektir.
ggtsdisplay(lynx,
plot.type = "partial",
main = "ACF & PACF plot for `lynx' Time-Series",
smooth = TRUE)
## `geom_smooth()` using formula = 'y ~ x'
Artırılmış Dickey-Fuller Testi
Tek değişkenli bir x zaman serisinin birim kökünün sıfır hipotezi için Artırılmış Dickey-Fuller testini gerçekleştirir.
adf.test(lynx)
## Warning in adf.test(lynx): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: lynx
## Dickey-Fuller = -6.3068, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
Verilerin log dönüşümünün birinci dereceden farkı, arima modelini oluşturmak için kullanılacaktır. Otomatik olarak p,d,q üreten R auto.arima yöntemi kullanılacaktır
auto.arima(lynx)
## Series: lynx
## ARIMA(2,0,2) with non-zero mean
##
## Coefficients:
## ar1 ar2 ma1 ma2 mean
## 1.3421 -0.6738 -0.2027 -0.2564 1544.4039
## s.e. 0.0984 0.0801 0.1261 0.1097 131.9242
##
## sigma^2 = 761965: log likelihood = -932.08
## AIC=1876.17 AICc=1876.95 BIC=1892.58
auto.arima(lynx, trace = T)
##
## ARIMA(2,0,2) with non-zero mean : 1876.952
## ARIMA(0,0,0) with non-zero mean : 2006.724
## ARIMA(1,0,0) with non-zero mean : 1927.209
## ARIMA(0,0,1) with non-zero mean : 1918.165
## ARIMA(0,0,0) with zero mean : 2080.721
## ARIMA(1,0,2) with non-zero mean : 1888.757
## ARIMA(2,0,1) with non-zero mean : 1880.014
## ARIMA(3,0,2) with non-zero mean : 1878.603
## ARIMA(2,0,3) with non-zero mean : Inf
## ARIMA(1,0,1) with non-zero mean : 1891.442
## ARIMA(1,0,3) with non-zero mean : 1890.03
## ARIMA(3,0,1) with non-zero mean : 1881.962
## ARIMA(3,0,3) with non-zero mean : Inf
## ARIMA(2,0,2) with zero mean : 1905.595
##
## Best model: ARIMA(2,0,2) with non-zero mean
## Series: lynx
## ARIMA(2,0,2) with non-zero mean
##
## Coefficients:
## ar1 ar2 ma1 ma2 mean
## 1.3421 -0.6738 -0.2027 -0.2564 1544.4039
## s.e. 0.0984 0.0801 0.1261 0.1097 131.9242
##
## sigma^2 = 761965: log likelihood = -932.08
## AIC=1876.17 AICc=1876.95 BIC=1892.58
auto.arima(lynx, trace = T,
stepwise = F,
approximation = F)
##
## ARIMA(0,0,0) with zero mean : 2080.721
## ARIMA(0,0,0) with non-zero mean : 2006.724
## ARIMA(0,0,1) with zero mean : 1972.791
## ARIMA(0,0,1) with non-zero mean : 1918.165
## ARIMA(0,0,2) with zero mean : 1925.15
## ARIMA(0,0,2) with non-zero mean : 1890.428
## ARIMA(0,0,3) with zero mean : 1913.118
## ARIMA(0,0,3) with non-zero mean : 1888.326
## ARIMA(0,0,4) with zero mean : 1906.524
## ARIMA(0,0,4) with non-zero mean : 1889.064
## ARIMA(0,0,5) with zero mean : 1908.619
## ARIMA(0,0,5) with non-zero mean : 1886.754
## ARIMA(1,0,0) with zero mean : 1934.647
## ARIMA(1,0,0) with non-zero mean : 1927.209
## ARIMA(1,0,1) with zero mean : 1903.345
## ARIMA(1,0,1) with non-zero mean : 1891.442
## ARIMA(1,0,2) with zero mean : 1903.567
## ARIMA(1,0,2) with non-zero mean : 1888.757
## ARIMA(1,0,3) with zero mean : 1905.59
## ARIMA(1,0,3) with non-zero mean : 1890.03
## ARIMA(1,0,4) with zero mean : 1907.578
## ARIMA(1,0,4) with non-zero mean : Inf
## ARIMA(2,0,0) with zero mean : 1906.685
## ARIMA(2,0,0) with non-zero mean : 1878.399
## ARIMA(2,0,1) with zero mean : 1903.412
## ARIMA(2,0,1) with non-zero mean : 1880.014
## ARIMA(2,0,2) with zero mean : 1905.595
## ARIMA(2,0,2) with non-zero mean : 1876.952
## ARIMA(2,0,3) with zero mean : Inf
## ARIMA(2,0,3) with non-zero mean : Inf
## ARIMA(3,0,0) with zero mean : 1903.728
## ARIMA(3,0,0) with non-zero mean : 1880.512
## ARIMA(3,0,1) with zero mean : 1905.587
## ARIMA(3,0,1) with non-zero mean : 1881.962
## ARIMA(3,0,2) with zero mean : Inf
## ARIMA(3,0,2) with non-zero mean : 1878.603
## ARIMA(4,0,0) with zero mean : 1905.899
## ARIMA(4,0,0) with non-zero mean : 1875.007
## ARIMA(4,0,1) with zero mean : Inf
## ARIMA(4,0,1) with non-zero mean : 1876.407
## ARIMA(5,0,0) with zero mean : 1904.543
## ARIMA(5,0,0) with non-zero mean : 1876.332
##
##
##
## Best model: ARIMA(4,0,0) with non-zero mean
## Series: lynx
## ARIMA(4,0,0) with non-zero mean
##
## Coefficients:
## ar1 ar2 ar3 ar4 mean
## 1.1246 -0.7174 0.2634 -0.2543 1547.3859
## s.e. 0.0903 0.1367 0.1361 0.0897 136.8501
##
## sigma^2 = 748457: log likelihood = -931.11
## AIC=1874.22 AICc=1875.01 BIC=1890.64
myarima = arima(lynx, order = c(2,0,0))
Tail veri setinin son n satırını görüntülemek için kullanıyoruz
tail(lynx)
## Time Series:
## Start = 1929
## End = 1934
## Frequency = 1
## [1] 485 662 1000 1590 2657 3396
İstatistiksel bir modeldeki kalıntılar, verilerin gözlenen ve tahmin edilen değerleri arasındaki farklardır. Bir modelin kalitesini değerlendirirken kullanılan bir teşhis ölçüsüdür. Bunlar aynı zamanda hatalar olarak da bilinir.
residuals(myarima)
## Time Series:
## Start = 1821
## End = 1934
## Frequency = 1
## [1] -711.715800 -247.179068 -321.014839 -306.751202 127.414827
## [6] 951.890591 876.687792 2428.733153 -212.432514 -237.541926
## [11] -164.223204 344.415030 -313.801319 -572.372533 -499.800869
## [16] 1284.008241 -390.614888 999.532714 -1176.312892 -338.411239
## [21] 76.614594 -581.986383 -592.092428 -537.056449 -356.640535
## [26] -164.773680 572.140125 13.626146 -1375.059569 84.838236
## [31] -162.287575 -690.094698 -371.088497 -246.153634 316.113199
## [36] 584.894187 27.600121 -240.002495 -724.567794 85.994521
## [41] -395.876984 -545.490420 -286.601293 437.533551 1080.751334
## [46] 3196.206424 -2171.180547 -862.324669 1319.008240 -106.590562
## [51] -730.821550 -42.121950 210.099599 -381.832131 584.871735
## [56] -850.724879 -229.236651 -412.244306 -387.695328 -521.330158
## [61] -372.233398 -363.825181 779.748247 210.328753 1731.217687
## [66] -1586.424626 -533.760190 433.588275 -510.481277 -650.987938
## [71] -672.853636 -549.330544 -502.352341 273.149380 2075.597251
## [76] -1054.463188 -1704.735336 828.545228 -314.449678 -424.603800
## [81] -293.316062 -29.674453 1720.888636 3099.981788 -329.627515
## [86] 44.035737 569.799862 -185.278565 388.247443 -122.427802
## [91] -9.044981 905.933577 820.433050 -341.167517 1018.287679
## [96] 1519.696544 -2583.562459 881.643131 -307.733379 -634.234854
## [101] -545.962808 -498.009703 112.495341 673.381411 763.327803
## [106] -406.375377 -386.254717 -173.376326 100.795394 -276.260594
## [111] -167.745563 140.575959 733.302579 601.838001
(2657 - 1545.45)*1.147 - (1590 - 1545.45)*0.6 + 601.84
## [1] 1850.058
3396 - 1545.45
## [1] 1850.55
myarima = arima(lynx, order = c(0,0,2))
myarima
##
## Call:
## arima(x = lynx, order = c(0, 0, 2))
##
## Coefficients:
## ma1 ma2 intercept
## 1.1407 0.4697 1545.3670
## s.e. 0.0776 0.0721 224.5215
##
## sigma^2 estimated as 855092: log likelihood = -941.03, aic = 1890.06
residuals(myarima)
## Time Series:
## Start = 1821
## End = 1934
## Frequency = 1
## [1] -803.732851 -316.819775 -339.796973 -153.575542 256.164758
## [6] 1051.017490 1062.665677 2690.592373 -162.936784 -44.605977
## [11] -894.921151 -405.552321 -478.418368 -530.135762 -306.914693
## [16] 1338.739662 -243.365541 1512.454318 -1332.377780 -326.856600
## [21] -395.701695 -895.452231 -270.030612 -603.745610 -183.818830
## [26] -19.103090 691.762826 210.483679 -1153.389638 32.488716
## [31] -663.690549 -578.528068 -213.686644 -298.876138 533.939929
## [36] 710.925757 263.863961 -61.283359 -915.393384 -173.356483
## [41] -681.659497 -441.346353 -169.735507 478.553892 1299.450551
## [46] 3468.524287 -1858.394332 -367.558957 1.794961 -901.775190
## [51] -159.518680 -155.841195 301.331932 -139.911234 723.702215
## [56] -879.208277 -126.335608 -689.293961 -498.722732 -423.698105
## [61] -358.791482 -201.071547 894.524832 339.653953 2078.024947
## [66] -1564.386859 -347.838999 -340.793301 -954.233203 -247.766795
## [71] -755.533899 -379.124919 -381.015812 359.344984 2254.673608
## [76] -791.145850 -1114.876734 203.012693 -1100.303344 1.440094
## [81] -272.206328 71.473327 1965.953529 3169.419791 228.755266
## [86] 499.031993 -386.077507 -994.344061 152.258905 -444.019657
## [91] 277.629380 1059.482295 915.638387 3.497027 1005.575888
## [96] 1095.889333 -2593.803120 979.758850 -1364.729473 -340.749646
## [101] -286.657856 -659.317506 473.383962 656.300763 1057.619544
## [106] -125.095552 -362.420744 -544.182680 -269.369783 -320.487877
## [111] -53.252771 255.911083 844.717221 766.830502
844.72*1.141 + 255.91*0.47 + 766.83
## [1] 1850.933
3396 - 1545.37
## [1] 1850.63
set.seed(123)
asim <- arima.sim(model = list(order = c(1,0,1),
ar = c(0.4),
ma = c(0.3)),
n = 1000) + 10
plot(asim)
autoplot(asim) +
xlab("") + ylab("") +
ggtitle("Time Series Plot of the `asim' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))
plot(rollmean(asim, 50))
plot(rollmean(asim, 25))
autoplot(rollmean(asim, 50)) +
xlab("") + ylab("") +
ggtitle("50 Days MA plot for `asim' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))
autoplot(rollmean(asim, 25)) +
xlab("") + ylab("") +
ggtitle("25 Days MA plot for `asim' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))
Bir zaman serisinin durağan olup olmadığını belirlemek için aşağıdaki sıfır ve alternatif hipotezleri kullanan artırılmış Dickey-Fuller testi, bunu yapmak için bir tekniktir.
adf.test(asim)
## Warning in adf.test(asim): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: asim
## Dickey-Fuller = -9.0113, Lag order = 9, p-value = 0.01
## alternative hypothesis: stationary
ggplot grafiklerini kullanarak eşdeğer çizimi üretecektir.
ggtsdisplay(asim,
plot.type = "partial",
main = "ACF & PACF plot for `asim' Time-Series",
smooth = TRUE)
## `geom_smooth()` using formula = 'y ~ x'
auto.arima(asim,
trace = T,
stepwise = F,
approximation = F)
##
## ARIMA(0,0,0) with zero mean : 7465.459
## ARIMA(0,0,0) with non-zero mean : 3241.528
## ARIMA(0,0,1) with zero mean : 6218.948
## ARIMA(0,0,1) with non-zero mean : 2878.74
## ARIMA(0,0,2) with zero mean : 5341.968
## ARIMA(0,0,2) with non-zero mean : 2836.895
## ARIMA(0,0,3) with zero mean : 4809.724
## ARIMA(0,0,3) with non-zero mean : 2837.534
## ARIMA(0,0,4) with zero mean : 4450.32
## ARIMA(0,0,4) with non-zero mean : 2838.689
## ARIMA(0,0,5) with zero mean : 4219.275
## ARIMA(0,0,5) with non-zero mean : 2840.557
## ARIMA(1,0,0) with zero mean : Inf
## ARIMA(1,0,0) with non-zero mean : 2870.637
## ARIMA(1,0,1) with zero mean : Inf
## ARIMA(1,0,1) with non-zero mean : 2836.047
## ARIMA(1,0,2) with zero mean : Inf
## ARIMA(1,0,2) with non-zero mean : 2837.165
## ARIMA(1,0,3) with zero mean : Inf
## ARIMA(1,0,3) with non-zero mean : 2839.088
## ARIMA(1,0,4) with zero mean : Inf
## ARIMA(1,0,4) with non-zero mean : 2840.615
## ARIMA(2,0,0) with zero mean : Inf
## ARIMA(2,0,0) with non-zero mean : 2836.945
## ARIMA(2,0,1) with zero mean : Inf
## ARIMA(2,0,1) with non-zero mean : 2837.319
## ARIMA(2,0,2) with zero mean : Inf
## ARIMA(2,0,2) with non-zero mean : 2838.849
## ARIMA(2,0,3) with zero mean : Inf
## ARIMA(2,0,3) with non-zero mean : 2840.867
## ARIMA(3,0,0) with zero mean : Inf
## ARIMA(3,0,0) with non-zero mean : 2837.297
## ARIMA(3,0,1) with zero mean : Inf
## ARIMA(3,0,1) with non-zero mean : 2839.296
## ARIMA(3,0,2) with zero mean : Inf
## ARIMA(3,0,2) with non-zero mean : 2840.86
## ARIMA(4,0,0) with zero mean : Inf
## ARIMA(4,0,0) with non-zero mean : 2839.279
## ARIMA(4,0,1) with zero mean : Inf
## ARIMA(4,0,1) with non-zero mean : 2841.309
## ARIMA(5,0,0) with zero mean : Inf
## ARIMA(5,0,0) with non-zero mean : 2841.162
##
##
##
## Best model: ARIMA(1,0,1) with non-zero mean
## Series: asim
## ARIMA(1,0,1) with non-zero mean
##
## Coefficients:
## ar1 ma1 mean
## 0.3494 0.3183 10.0288
## s.e. 0.0478 0.0473 0.0637
##
## sigma^2 = 0.9927: log likelihood = -1414
## AIC=2836.01 AICc=2836.05 BIC=2855.64
adf.test(lynx)
## Warning in adf.test(lynx): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: lynx
## Dickey-Fuller = -6.3068, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
tsdisplay(lynx)
ggtsdisplay(lynx,
plot.type = "partial",
main = "ACF & PACF plot for `lynx' Time-Series",
smooth = TRUE)
## `geom_smooth()` using formula = 'y ~ x'
myarima <- Arima(lynx,
order = c(4,0,0))
checkresiduals: Bir zaman serisi modelinden kalanların beyaz gürültü gibi görünüp görünmediğini kontrol eder.
checkresiduals(myarima)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(4,0,0) with non-zero mean
## Q* = 13.201, df = 6, p-value = 0.03996
##
## Model df: 4. Total lags used: 10
set.seed(123)
myts <- arima.sim(model = list(order = c(0,0,2),
ma = c(0.3, 0.7)), n = 1000) + 10
adf.test(myts)
## Warning in adf.test(myts): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: myts
## Dickey-Fuller = -9.0469, Lag order = 9, p-value = 0.01
## alternative hypothesis: stationary
ggplot grafiklerini kullanarak eşdeğer çizimi üretecektir.
tsdisplay(myts)
Acf ve pacf, gecikmeli dağılım grafiği veya spektrumu ile birlikte bir zaman serisi çizer.
ggtsdisplay(myts,
plot.type = "partial",
main = "ACF & PACF for `myts' Time-series",
smooth = TRUE)
## `geom_smooth()` using formula = 'y ~ x'
myarima <- Arima(myts, order = c(0,0,3))
checkresiduals(myarima)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(0,0,3) with non-zero mean
## Q* = 4.1475, df = 7, p-value = 0.7626
##
## Model df: 3. Total lags used: 10
auto.arima(myts,
trace = T,
stepwise = F,
approximation = F)
##
## ARIMA(0,0,0) with zero mean : 7465.902
## ARIMA(0,0,0) with non-zero mean : 3239.597
## ARIMA(0,0,1) with zero mean : 6414.662
## ARIMA(0,0,1) with non-zero mean : 3199.385
## ARIMA(0,0,2) with zero mean : 5571.943
## ARIMA(0,0,2) with non-zero mean : 2828.282
## ARIMA(0,0,3) with zero mean : 4982.239
## ARIMA(0,0,3) with non-zero mean : 2829.867
## ARIMA(0,0,4) with zero mean : 4556.587
## ARIMA(0,0,4) with non-zero mean : 2831.522
## ARIMA(0,0,5) with zero mean : 4300.593
## ARIMA(0,0,5) with non-zero mean : 2831.318
## ARIMA(1,0,0) with zero mean : 3610.918
## ARIMA(1,0,0) with non-zero mean : 3163.665
## ARIMA(1,0,1) with zero mean : Inf
## ARIMA(1,0,1) with non-zero mean : 3120.607
## ARIMA(1,0,2) with zero mean : Inf
## ARIMA(1,0,2) with non-zero mean : 2829.89
## ARIMA(1,0,3) with zero mean : Inf
## ARIMA(1,0,3) with non-zero mean : 2831.04
## ARIMA(1,0,4) with zero mean : Inf
## ARIMA(1,0,4) with non-zero mean : 2832.859
## ARIMA(2,0,0) with zero mean : Inf
## ARIMA(2,0,0) with non-zero mean : 3017.436
## ARIMA(2,0,1) with zero mean : Inf
## ARIMA(2,0,1) with non-zero mean : 2977.38
## ARIMA(2,0,2) with zero mean : Inf
## ARIMA(2,0,2) with non-zero mean : 2831.603
## ARIMA(2,0,3) with zero mean : Inf
## ARIMA(2,0,3) with non-zero mean : 2832.823
## ARIMA(3,0,0) with zero mean : Inf
## ARIMA(3,0,0) with non-zero mean : 2929.264
## ARIMA(3,0,1) with zero mean : Inf
## ARIMA(3,0,1) with non-zero mean : 2924.325
## ARIMA(3,0,2) with zero mean : Inf
## ARIMA(3,0,2) with non-zero mean : 2831.357
## ARIMA(4,0,0) with zero mean : Inf
## ARIMA(4,0,0) with non-zero mean : 2914.331
## ARIMA(4,0,1) with zero mean : Inf
## ARIMA(4,0,1) with non-zero mean : 2899.065
## ARIMA(5,0,0) with zero mean : Inf
## ARIMA(5,0,0) with non-zero mean : 2873.303
##
##
##
## Best model: ARIMA(0,0,2) with non-zero mean
## Series: myts
## ARIMA(0,0,2) with non-zero mean
##
## Coefficients:
## ma1 ma2 mean
## 0.2878 0.6838 10.0297
## s.e. 0.0230 0.0231 0.0617
##
## sigma^2 = 0.9842: log likelihood = -1410.12
## AIC=2828.24 AICc=2828.28 BIC=2847.87
myarima <- auto.arima(lynx,
stepwise = F,
approximation = F)
arimafore <- forecast(myarima, h = 10)
plot(arimafore)
autoplot(lynx, PI = TRUE,
shadecols = c("#596DD5", "#D5DBFF"), fcol = "#0000AA", flwd = 0.5) +
autolayer(forecast(myarima, h = 10), level = 95, series = "lynx")
## Warning in ggplot2::geom_line(na.rm = TRUE, ...): Ignoring unknown parameters:
## `PI`, `shadecols`, `fcol`, and `flwd`
autoplot(lynx) + geom_forecast(forecast(lynx), series="lynx",h=10)
## Warning in geom_forecast(forecast(lynx), series = "lynx", h = 10): Use autolayer
## instead of geom_forecast to add a forecast layer to your ggplot object.
## Warning in geom_forecast(mapping = mapping, data = data, stat = "identity", :
## Ignoring unknown parameters: `h`
plot(arimafore,
xlim = c(1930, 1944))
myets <- ets(lynx)
etsfore <- forecast(myets, h = 10)
autoplot(lynx) +
forecast::autolayer(etsfore$mean, series = 'ETS model') +
forecast::autolayer(arimafore$mean, series = 'ARIMA model') +
xlab('year') + ylab('Lynx Trappings') +guides(colour = guide_legend(title = 'Forecast Method')) +
theme(legend.position = c(0.8, 0.8))