gorilla
gorilla

Reputation: 35

How to inversely transform time series predictions to add seasonality back in?

I have transformed some data to make it stationary before fitting an ARIMA(1,0,1) model. I specifically want to manually transform the data to better understand the process.

I can successfully fit a model and make n predictions, my issue is now inversely transforming that data back so my predictions are meaningful. I can't use the inverse of the original function as the training data and number of predictions will likely always be different lengths and this seems to conflict with the functions.

 remove_seasonality <- function(x) {
  t <- seq_along(x)
  sin_term <- sin(2 * pi * t / 52)
  cos_term <- cos(2 * pi * t / 52)
  lm_model <- lm(x ~ sin_term + cos_term)
  residuals(lm_model)
}
subset_data_two$x_deseasonalized <- remove_seasonality(subset_data_two$Nitrogen_Dioxide)
detrended_data <- subset_data_two

split_data <- function(data, train_or_test, prop) {
  ordered_data <- data[order(data$Date), ]
  row_count <- nrow(ordered_data)
  train_size <- round(prop * row_count)
  
  if (train_or_test == "train") {
    train_data <- ordered_data[1:train_size, ]
    return(train_data)
  } else if (train_or_test == "test") {
    test_data <- ordered_data[(train_size + 1):row_count, ]
    return(test_data)
  } else {
    stop("train_or_test must be either 'train' or 'test'")
  }
}
training_data <- split_data(detrended_data, "train", 0.85)
test_data <- split_data(detrended_data, "test", 0.85)



# 3.0 Fit Model
fix <- training_data[,c("Date", "x_deseasonalized")]
fixed_tseries <- read.zoo(fix)
fix_model <- arima(fix$x_deseasonalized, order=c(1,0,1))


# 4.0 Forecast
forecasted <- forecast(fix_model, h = 3

Upvotes: 0

Views: 48

Answers (1)

user2554330
user2554330

Reputation: 44957

You are removing seasonality by fitting a linear model and working with the residuals. That's a reasonable approach for simple sinusoidal seasonality, though a different shape of seasonality might need additional harmonics, i.e. higher frequency terms.

After you remove the seasonality, you throw away the coefficients of the linear fit. At that point you've lost the possibility of restoring it. You should either save those coefficients, or recompute them based on the original data, and then add in predictions using that model to the predictions of your ARIMA model.

You mention that you are using training and test data separately. That shouldn't matter, as long as you use predictions based on the same covariates. That's not obvious from your code, since you are using data index as covariate rather than week of year, which is what your detrending suggests you're using. It'll still be okay as long as train_size takes away an exact number of years, but otherwise won't work.

So I'd suggest the following: for detrending, compute day of year or week of year from Date, and use that as the covariate. Then you can just do predictions for a different dataset using the same method.

BTW, if you had posted reproducible code I could have posted demo code, but you didn't, so I didn't.

Upvotes: 1

Related Questions