Joehat
Joehat

Reputation: 1129

Error looping through autoregression outputs

I'm trying to create an autoregression model for an already differenced time-series.

I'd like to put the code below in a loop so it's not repetitive.

gas_ar_date <- arima(diff_gas[[1]], order = c(1, 0, 0))
print("AR for Date")
gas_ar_date
print("******************************************")

gas_ar_change <- arima(diff_gas[[2]], order = c(1, 0, 0))
print("AR for Currency Change")
gas_ar_change
print("******************************************")

gas_ar_domestic <- arima(diff_gas[[3]], order = c(1, 0, 0))
print("AR for UK Domestic Production")
gas_ar_domestic
print("******************************************")

gas_ar_import <- arima(diff_gas[[4]], order = c(1, 0, 0))
print("AR for Import")
gas_ar_import
print("******************************************")

However, when I try:

for (i in 1:8) {
  gas_ar[i] <- arima(diff_gas[[i]], order = c(1, 0, 0))
  gas_ar[i]
}

I get the error:

number of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement length

How could a create a loop in this case and get the full output of the ARIMA autoregression model?

Here's a glance of the data: enter image description here

Upvotes: 0

Views: 38

Answers (2)

razimbres
razimbres

Reputation: 5005

I'm not a R specialist but looks like you can do the following:

list<-c('date','change','domestic','import')

for (i in 1:4) {
   paste('gas_ar',list[i],sep="") <- arima(diff_gas[[i]], order = c(1, 0, 0))
}

Upvotes: 0

kangaroo_cliff
kangaroo_cliff

Reputation: 6222

If you str(fit1) it will should it's a "List of 14"

So either

gas_ar[[i]] <- arima(diff_gas[[i]], order = c(1, 0, 0))

or

gas_ar[i] <- list(arima(diff_gas[[i]], order = c(1, 0, 0)))

should work.

Upvotes: 1

Related Questions