我试图了解如何在ARIMA建模/ Box Jenkins(BJ)中估算参数。不幸的是,我所遇到的书都没有详细描述估计程序,例如对数似然估计程序。我发现该网站/教学材料非常有帮助。以下是来自上面引用的来源的公式。
我想自己学习ARIMA / BJ估计。因此,我使用编写了用于手工估算ARMA的代码。下面是我在做,[R
- 我模拟了ARMA(1,1)
- 将上面的方程写成函数
- 使用模拟数据和优化函数来估计AR和MA参数。
- 我还在stats软件包中运行ARIMA,并通过手工比较了ARMA参数。 比较如下:
**以下是我的问题:
- 为什么估计变量和计算变量之间存在细微差异?
- ARIMA是否在R反向广播中起作用,或者估算程序与我的代码中以下概述的有所不同?
- 我已将观测值1的e1或错误指定为0,这是正确的吗?
- 还有没有一种方法可以使用优化的粗略估计来估计预测的置信范围?
一如既往的感谢您的帮助。
下面是代码:
## Load Packages
library(stats)
library(forecast)
set.seed(456)
## Simulate Arima
y <- arima.sim(n = 250, list(ar = 0.3, ma = 0.7), mean = 5)
plot(y)
## Optimize Log-Likelihood for ARIMA
n = length(y) ## Count the number of observations
e = rep(1, n) ## Initialize e
logl <- function(mx){
g <- numeric
mx <- matrix(mx, ncol = 4)
mu <- mx[,1] ## Constant Term
sigma <- mx[,2]
rho <- mx[,3] ## AR coeff
theta <- mx[,4] ## MA coeff
e[1] = 0 ## Since e1 = 0
for (t in (2 : n)){
e[t] = y[t] - mu - rho*y[t-1] - theta*e[t-1]
}
## Maximize Log-Likelihood Function
g1 <- (-((n)/2)*log(2*pi) - ((n)/2)*log(sigma^2+0.000000001) - (1/2)*(1/(sigma^2+0.000000001))*e%*%e)
##note: multiplying Log-Likelihood by "-1" in order to maximize in the optimization
## This is done becuase Optim function in R can only minimize, "X"ing by -1 we can maximize
## also "+"ing by 0.000000001 sigma^2 to avoid divisible by 0
g <- -1 * g1
return(g)
}
## Optimize Log-Likelihood
arimopt <- optim(par=c(10,0.6,0.3,0.5), fn=logl, gr = NULL,
method = c("L-BFGS-B"),control = list(), hessian = T)
arimopt
############# Output Results###############
ar1_calculated = arimopt$par[3]
ma1_calculated = arimopt$par[4]
sigmasq_calculated = (arimopt$par[2])^2
logl_calculated = arimopt$val
ar1_calculated
ma1_calculated
sigmasq_calculated
logl_calculated
############# Estimate Using Arima###############
est <- arima(y,order=c(1,0,1))
est
g1
+0.000000001