library(sarima)
## Warning: package 'sarima' was built under R version 4.2.2
## Loading required package: stats4
##
## Attaching package: 'sarima'
## The following object is masked from 'package:stats':
##
## spectrum
library(astsa)
##
## Attaching package: 'astsa'
## The following object is masked from 'package:sarima':
##
## sarima
library(TSA)
##
## Attaching package: 'TSA'
## The following object is masked from 'package:sarima':
##
## periodogram
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
library(randtests)
set.seed(50000)
series=arima.sim(n=36,list(ma=0.5))
model=arima(series,order=c(0,0,1))
plot(rstandard(model),ylab ='Residuals', type='o')
The residual plot looks random.
qqnorm(residuals(model))
qqline(residuals(model))
The plot looks fine but toward the upper tail the plot doen’t look like
it’s following the straight line.
acf(residuals(model))
There doesn’t seem to be an issue with the autocorrelation.
LB.test(model,lag=6)
##
## Box-Ljung test
##
## data: residuals from model
## X-squared = 7.6999, df = 5, p-value = 0.1736
There doesn’t seem to be a porblem with the lag.
data(hare)
model=arima(sqrt(hare),order=c(3,0,0))
acf(rstandard(model))
Residual autocorrelations look good.
LB.test(model,lag=9)
##
## Box-Ljung test
##
## data: residuals from model
## X-squared = 6.2475, df = 6, p-value = 0.396
The Ljung-Box test does not reject independence of the error terms.
runs(rstandard(model))
## $pvalue
## [1] 0.602
##
## $observed.runs
## [1] 18
##
## $expected.runs
## [1] 16.09677
##
## $n1
## [1] 13
##
## $n2
## [1] 18
##
## $k
## [1] 0
The p value is 0.6, therefore we can’t reject independence of error term.
qqnorm(residuals(model))
qqline(residuals(model))
The graph has some outliers at both extremes.
shapiro.test(residuals(model))
##
## Shapiro-Wilk normality test
##
## data: residuals(model)
## W = 0.93509, p-value = 0.06043
library(TSA)
data(robot)
mod1=arima(robot,order=c(1,0,0))
res1=rstandard(mod1)
mod1
##
## Call:
## arima(x = robot, order = c(1, 0, 0))
##
## Coefficients:
## ar1 intercept
## 0.3074 0.0015
## s.e. 0.0528 0.0002
##
## sigma^2 estimated as 6.482e-06: log likelihood = 1475.54, aic = -2947.08
mod2=arima(robot,order=c(1,0,1))
res2=rstandard(mod2)
mod2
##
## Call:
## arima(x = robot, order = c(1, 0, 1))
##
## Coefficients:
## ar1 ma1 intercept
## 0.9472 -0.8062 0.0015
## s.e. 0.0309 0.0609 0.0005
##
## sigma^2 estimated as 5.948e-06: log likelihood = 1489.3, aic = -2972.61
The IMA(1,1)model is better. The estimation parameters are better.