msmna93 - 6 months ago 76

R Question

Suppose that I have the following "for" loop in R.

`USDlogreturns=diff(log(prices))`

for(i in 0:5){

for(j in 0:5){

fit <- arima(USDlogreturns, order=c(i,0,j), include.mean=TRUE)

}

}

How do you tell R to substitute a NA matrix with the coefficients of all the estimated models?

Answer

You will need a matrix `M`

with dimensions 36 times 13.
Then use

```
M=matrix(NA,36,13)
k=0 # current row being filled in
for(i in 0:5){
for(j in 0:5){
k=k+1
fit <- arima(USDlogreturns, order=c(i,0,j), include.mean=TRUE)
if(i>0) M[k,c(1: i) ]=fit$coef[c( 1 : i )] # AR coefficients in the 2nd-6th columns
if(j>0) M[k,c(8:(7+j))]=fit$coef[c((i+1):(i+j))] # MA coefficients in the 8th-12th columns
M[k, 13 ]=tail(fit$coef,1) # "intercept" (actually, mean) in the 13th column
}
}
```

The columns 2 to 6 will contain AR coefficients.

The columns 8 to 12 will contain MA coefficients.

The 13th column will contain the "intercepts" (actually, the means, as the terminology in the `arima`

function is misleading).