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