There are three minor issues in tseries::arma
compared to stats::arima
that lead to a slightly different result in the ARMA model for the differenced series using tseries::arma
and ARIMA in stats::arima
.
Starting values of the coefficients: stats::arima
sets the initial AR and MA coefficients to zero, while tseries::arma
uses the procedure described in Hannan and Rissanen (1982) is employed to get initial values of the coefficients.
Scale of the objective function: the objective function in tseries::arma
returns the value of the conditional sums of squares, RSS; stats::arima
returns 0.5*log(RSS/(n-ncond))
.
Optimization algorithm: By default, Nelder-Mead is used in tseries::arma
, while stats::arima
employs the BFGS algorithm.
The last one can be changed through the argument optim.method
in stats::arima
but the others would require modifying the code. Below, I show an abridged version of the source code (minimal code for this particular model) for stats::arima
where the three issues mentioned above are modified
so that they are the same as in tseries::arma
. After addressing these issues, the same result as in tseries::arma
is obtained.
Minimal version of stats::arima
(with the changes mentioned above):
# objective function, conditional sum of squares
# adapted from "armaCSS" in stats::arima
armaCSS <- function(p, x, arma, ncond)
{
# this does nothing, except returning the vector of coefficients as a list
trarma <- .Call(stats:::C_ARIMA_transPars, p, arma, FALSE)
res <- .Call(stats:::C_ARIMA_CSS, x, arma, trarma[[1L]], trarma[[2L]], as.integer(ncond), FALSE)
# return the conditional sum of squares instead of 0.5*log(res),
# actually CSS is divided by n-ncond but does not relevant in this case
#0.5 * log(res)
res
}
# initial values of coefficients
# adapted from function "arma.init" within tseries::arma
arma.init <- function(dx, max.order, lag.ar=NULL, lag.ma=NULL)
{
n <- length(dx)
k <- round(1.1*log(n))
e <- as.vector(na.omit(drop(ar.ols(dx, order.max = k, aic = FALSE, demean = FALSE, intercept = FALSE)$resid)))
ee <- embed(e, max.order+1)
xx <- embed(dx[-(1:k)], max.order+1)
return(lm(xx[,1]~xx[,lag.ar+1]+ee[,lag.ma+1]-1)$coef)
}
# modified version of stats::arima
modified.arima <- function(x, order, seasonal, init)
{
n <- length(x)
arma <- as.integer(c(order[-2L], seasonal$order[-2L], seasonal$period, order[2L], seasonal$order[2L]))
narma <- sum(arma[1L:4L])
ncond <- order[2L] + seasonal$order[2L] * seasonal$period
ncond1 <- order[1L] + seasonal$period * seasonal$order[1L]
ncond <- as.integer(ncond + ncond1)
optim(init, armaCSS, method = "Nelder-Mead", hessian = TRUE, x=x, arma=arma, ncond=ncond)$par
}
Now, compare both procedures and check that yield the same result (requires the series x
generated by the OP in the body of the question).
Using the initial values chosen in tseries::arima
:
dx <- diff(x)
fit1 <- arma(dx, order=c(3,3), include.intercept=FALSE)
coef(fit1)
# ar1 ar2 ar3 ma1 ma2 ma3
# 0.33139827 0.80013071 -0.45177254 0.67331027 -0.14600320 -0.08931003
init <- arma.init(diff(x), 3, 1:3, 1:3)
fit2.coef <- modified.arima(x, order=c(3,1,3), seasonal=list(order=c(0,0,0), period=1), init=init)
fit2.coef
# xx[, lag.ar + 1]1 xx[, lag.ar + 1]2 xx[, lag.ar + 1]3 ee[, lag.ma + 1]1
# 0.33139827 0.80013071 -0.45177254 0.67331027
# ee[, lag.ma + 1]2 ee[, lag.ma + 1]3
# -0.14600320 -0.08931003
all.equal(coef(fit1), fit2.coef, check.attributes=FALSE)
# [1] TRUE
Using the initial values chosen in stats::arima
(zeros):
fit3 <- arma(dx, order=c(3,3), include.intercept=FALSE, coef=rep(0,6))
coef(fit3)
# ar1 ar2 ar3 ma1 ma2 ma3
# 0.33176424 0.79999112 -0.45215742 0.67304072 -0.14592152 -0.08900624
init <- rep(0, 6)
fit4.coef <- modified.arima(x, order=c(3,1,3), seasonal=list(order=c(0,0,0), period=1), init=init)
fit4.coef
# [1] 0.33176424 0.79999112 -0.45215742 0.67304072 -0.14592152 -0.08900624
all.equal(coef(fit3), fit4.coef, check.attributes=FALSE)
# [1] TRUE
fit1
has only 1 MA & 1 AR parameter: did you meanfit1<-arma(diff(x,1,lag=1),c(3,3),include.intercept=F)
? $\endgroup$arima
mention ann.cond
argument giving the number of observations at the start of the series to ignore when calculating it - perhaps that's it. (What's wrong with using maximum likelihood anyway?) $\endgroup$