简体   繁体   中英

r programming for loop not working for time series with fourier series

I am trying to a time series on split data to determine a good model to pick. In the assignment that I have been given, I am told to use a Fourier series. Since I have to do up to 26 models for testing, I decided to use a for-loop.

When I ran what I thought would work in this loop, I noticed that all my outputs are the same, except for the counter which worked as expected. When I did the iterations by had I got the expected results.

I have shortened the series to five to make it easier to work with. Additionally, the data (gasoline) is in the fpp2 package in r studio.

Gas.train = window(gasoline,end=2005)
Gas.test = window(gasoline,start=2005,end=2006)

for(i in c(1,2,3,4,5)){
  fit <- tslm(Gas.train~trend+fourier(Gas.train, K = i))
  cat("k = ", i, sep = " ")
  print(CV(fit))
}

The issue seems to be connected with the iterator name i . When I use another iterator, the code works just fine. Unfortunately, I do not exactly understand why this is the case though.

for(j in c(1,2,3,4,5)){
  fit <- tslm(Gas.train~trend + fourier(Gas.train, K = j))
  cat("k = ", j, sep = " ")
  print(CV(fit))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM