[R-SIG-Finance] Loop For - ARMA model estimation and selection
Andrea Bosio
andrea_bosio at rocketmail.com
Sun Oct 16 21:00:09 CEST 2016
Hi,
Suppose that I have the following loop for estimating ARMA models on a
series of log-returns. The loop varies both the p and the q orders
between 0 and 10. The returned objects are the matrices of respectively
the Akaike Information Criterion indicators for model goodness of fit,
the model equation coefficients, the p-values of the Augmented Dickey
Fuller test for stationarity of the residuals, the p-values of Box Ljung
tests for serial uncorrelation and independence of the residuals and the
p-values of the Jarque Bera test for normality of the residuals.
My questions are:
1)How do you tell R not to display the AIC, the model coefficients and
all the tests p-values in the case the estimated ARMA model features
convergence problems? That would probably require to specify an IF
condition in the loop which I am not currently able to do.
2)Take a look at the “fit degrees of freedom” argument of the Box tests
for serial uncorrelation and independence. There I need to tell R to fit
a number of degrees of freedom which equals p+q (the sum of the orders
of the estimated ARMA model). Is it correct to write fitdf=i+j+2 as I did?
3)How could I loop for a third variable which is
“include.mean=TRUE/FALSE”? I tried to loop over a third, string-type
variable z<-c(TRUE,FALSE) but always got the error message: “error no
'dimnames' attribute for array”.
Please note that I am an R beginner. So any help from anybody is deeply
appreciated.
Here follows the code.
USDlogreturns=diff(log(prices))
armaaicmat=matrix(NA, 11, 11)
armacoefmat=matrix(NA,121,23)
armaadftestmat=matrix(NA, 11, 11)
armaboxtestmat=matrix(NA, 11, 11)
armaboxtest2mat=matrix(NA, 11, 11)
armajarqueberamat=matrix(NA, 11, 11)
k=0
for(i in 0:10){
for(j in 0:10){
k=k+1
fit <- arima(USDlogreturns, order=c(i,0,j), include.mean=TRUE)
armaaicmat[i+1,j+1] = fit$aic
if(i>0) armacoefmat[k,c(1:i) ]=fit$coef[c(1 :i )]
if(j>0) armacoefmat[k,c(13:(12+j))]=fit$coef[c((i+1):(i+j))]
armacoefmat[k,23]=tail(fit$coef,1)
armaadftestmat[i+1,j+1]=adf.test(fit$resid,k=trunc((length(fit$resid)-1)^(1/3)))$p.value
armaboxtestmat[i+1,j+1]=Box.test(fit$resid, fitdf=i+j+2,
lag=30,type=c("Ljung-Box"))$p.value
armaboxtest2mat[i+1,j+1]=Box.test(fit$resid^2, fitdf=i+j+2,
lag=30,type=c("Ljung-Box"))$p.value
armajarqueberamat[i+1,j+1]=jarque.bera.test(fit$resid)$p.value
}
}
---
Questa e-mail è stata controllata per individuare virus con Avast antivirus.
https://www.avast.com/antivirus
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list