Search code examples
rforecasting

Error in `attr(x, "tsp") <- c(1, NROW(x), 1)`: ! invalid time series parameters specified


I am trying to answer the question from

The us_gasoline series consists of weekly data for supplies of US finished motor gasoline product, from 2 February 1991 to 20 January 2017. The units are in “million barrels per day”. Consider only the data to the end of 2004.

a. Fit a harmonic regression with trend to the data. Experiment with changing the number Fourier terms. Plot the observed gasoline and fitted values and comment on what you see.

My code is as follows:

autoplot(gas2004, ylab = "Gas Supply (Weekly)")

fourier.gas1 <- tslm(gas2004 ~ trend + fourier(gas2004, K=7))
fourier.gas2 <- tslm(gas2004 ~ trend + fourier(gas2004, K=12))
fourier.gas3 <- tslm(gas2004 ~ trend + fourier(gas2004, K=20))

autoplot(gas2004, ylab = "Gas Supply (Weekly)",main= "Fourier Transformation") +
  autolayer(fitted(fourier.gas1))+
  autolayer(fitted(fourier.gas2))+
  autolayer(fitted(fourier.gas3))


I am getting the following error from the first line of the code
> Error in `attr(x, "tsp") <- c(1, NROW(x), 1)`:
> ! invalid time series parameters specified
> Backtrace:
> 1. stats::window(us_gasoline, end = 2004)
> 2. stats:::window.default(us_gasoline, end = 2004)
> 3. stats::hasTsp(x)

Solution

  • You appear to be attempting Exercise 5 from https://otexts.com/fpp3/regression-exercises.html. So I assume this is homework.

    The book uses the fpp3 package with tsibble objects and models fitted using fable. Yet you are attempting to use functions designed for ts objects.

    Follow the example at https://otexts.com/fpp3/useful-predictors.html#fourier-series to see how to fit these models with tsibble objects.