AR和AR的R和Stata之间存在重大差异

时间:2014-03-16 21:39:01

标签: r statistics stata

使用历史Lynx Pelt数据(https://www.dropbox.com/s/v0h9oywa4pdjblu/Lynxpelt.csv),这里是来自R和Stata的AIC值的两个表,用于ARIMA(p,q)模型,0 <= p <= 5且0 <= q <= 5。注意,对于(p,q)=(0,1),(0,2),(0,3),(1,0),(1,1),(1,2),(2,0) ,(2,1),(2,2),(2,3),(3,0),(3,1),(3,2),(4,0)和(4,1)的值与七位有效数字相同。然而,剩下的情况大相径庭 - 只看(4,2)!当AIC不匹配时,系数估计也非常不同。这是ARIMA核心功能中的错误,还是正在发生的事情?

AIC calculations from R for ARIMA(p,q)
          q0        q1       q2       q3       q4
p0 145.25613 100.20123 87.45927 77.57073 85.86376
p1 101.54847  84.91691 82.11806 77.15318 74.26392
p2  63.41165  49.42414 44.14899 40.96787 44.33848
p3  52.26069  49.19660 52.00560 43.50156 45.17175
p4  46.19617  48.19530 49.50422 42.43198 45.71375

R参数估算值:http://pastie.org/8942238

    AIC ( Stata )   FOR   LOG   MODELS  
    q               
p   0   1   2   3   4
0               100.2012    87.45929    77.57074    83.86378
1   101.5485    84.91692    82.11809    86.44413    74.26394
2   63.41167    49.42417    44.14902    40.96633    40.76029
3   52.26072    49.19663    52.00562    40.37268    42.20399
4   46.19619    48.19532    40.39699    43.12795    na

Stata参数估算值:http://pastie.org/8942232

下面是在R中创建AIC表的代码。注意,我强制使用最大似然,没有参数转换,并增加了最大迭代次数。

pelts <- read.csv("Lynxpelt.csv")
pelts$log <- log(pelts$W7)
models <- array(list(),5)
aic <- data.frame(q0=rep(NA,5), q1=rep(NA,5), q2=rep(NA,5), q3=rep(NA,5), q4=rep(NA,5), row.names=c("p0", "p1", "p2", "p3", "p4"))

makeModel <- function(p,q) {
    arima(pelts$log, order=c(p,0,q), transform.pars=FALSE, method="ML", optim.control=list(maxit=1000))
}

options(warn=1)

for (p in 0:4) {
    for (q in 0:4) {
        model <- makeModel(p,q)
        models[[p+1]][[q+1]] <- model
        aic[p+1,q+1] <- model$aic
        print(cat("p=",p,", q=",q))
    }
}

aic

以下是Stata的代码:

insheet using Lynxpelt.csv
save Lynxpelt, replace

tsset year
tsline w7

gen logw7=log(w7)
label var logw7 "logarithm of w7"

mat A=J(5,5,0) /*This matrix is a 5*5 matrix with 0s*/
mat list A /*show the matrix A*/

forvalues i=0/4 {
forvalues j=0/4 {
set more off
quietly arima logw7, arima(`i',0,`j')
estat ic
matrix list r(S)
matrix s=r(S)
scalar alpha=s[1,5]
mat A[`i'+1,`j'+1]=alpha
}
}


* ARMA(4,4) cannot be done since stata cannot choose an initial value - we give one manually *
* I will use the estimates from ARMA(3,4) *
* Let's run ARMA(3,4) again *
quietly arima logw7, ar(1/3) ma(1/4)
matrix list e(b)
mat B=e(b)

*Now, let's run ARMA(4,4) with initial values from ARMA(3,4) *
quietly arima logw7, ar(1/4) ma(1/4) from(B)
estat ic
matrix s=r(S)
scalar alpha=s[1,5]
mat A[5,5]=alpha

编辑:添加了参数估算的链接&amp;在R代码中添加了一行来修复&#34;未找到的模型&#34;错误

编辑2:根据iacobus的建议,手动强制Stata使用BFGS作为优化方法。 (4,3)&amp; (3,3)得到很大改善。其他价值仍然存在很大差异。例如,(3,2)用于匹配,现在非常不同。

STATA results with technique(bfgs):
           c1         c2         c3         c4         c5
r1  145.25614  100.20123   87.45929  77.570744  85.863777
r2  101.54848  84.916921   82.11809  86.444131  74.263937
r3  63.411671  49.424167  44.149023  40.966325  42.760294
r4  52.260723  49.196628  40.442078  43.498413  43.622292
r5  46.196192  48.195322  42.396986  42.289595          0

R results from above for easy comparison:

AIC calculations from R for ARIMA(p,q)
          q0        q1       q2       q3       q4
p0 145.25613 100.20123 87.45927 77.57073 85.86376
p1 101.54847  84.91691 82.11806 77.15318 74.26392
p2  63.41165  49.42414 44.14899 40.96787 44.33848
p3  52.26069  49.19660 52.00560 43.50156 45.17175
p4  46.19617  48.19530 49.50422 42.43198 45.71375

1 个答案:

答案 0 :(得分:10)

我认为您的数据产生了数值不稳定的似然函数,特别是对于高阶模型。事实上,R(至少对我而言)是在某些高阶模型上给我警告而你使用Stata使用无限制MLE来装配它们时表明可能存在一些数值问题。 SAS也向我发出关于左右收敛的警告。

如果存在可能性的数值问题,这可能会影响优化步骤。默认情况下,Stata似乎使用Be​​rndt-Hall-Hall-Hausman算法使用5个步骤,然后使用BFGS使用10个步骤,根据需要重复组合直到收敛。另一方面,R默认使用BFGS。您可以使用optim.method参数更改它,但R不支持使用BHHH或像Stata那样在BHHH和BFGS之间移动。

在R中使用各种不同的优化器来处理数据表明,通过在优化器之间进行更改,结果的AIC会有相当大的变化。我怀疑这是Stata和R估计之间差异的原因。

我建议去Stata并设置最大化选项BFGS(有关如何执行此操作的详细信息,请参阅http://www.stata.com/help.cgi?arima#maximize_options)。如果Stata估计在做出改变之后与R的那些收敛,那我就不会感到惊讶。