与之tseries::arma
相比,有三个小问题stats::arima
导致ARMA模型中使用tseries::arma
和ARIMA 的差异序列的结果略有不同stats::arima
。
系数stats::arima
的初始值:将初始AR和MA系数设置为零,同时tseries::arma
使用Hannan和Rissanen(1982)中描述的过程获取系数的初始值。
目标函数的标度:目标函数in tseries::arma
返回条件平方和的值RSS;stats::arima
返回0.5*log(RSS/(n-ncond))
。
优化算法:默认情况下,Nelder-Mead用于tseries::arma
,而stats::arima
采用BFGS算法。
可以通过中的参数更改最后一个optim.method
,stats::arima
但是其他的则需要修改代码。下面,我显示源代码的节略版本(此特定模型的最小代码),stats::arima
其中对上述三个问题进行了修改,使其与中的相同tseries::arma
。解决这些问题后,tseries::arma
可获得与中相同的结果。
的最低版本stats::arima
(具有上述更改):
# 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
}
现在,比较这两个过程并检查是否产生相同的结果(需要x
问题主体中OP生成的序列)。
使用在中选择的初始值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
使用在stats::arima
(零)中选择的初始值:
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
只有1 MA和1 AR参数:您是说fit1<-arma(diff(x,1,lag=1),c(3,3),include.intercept=F)
吗?