I have a dataset of hourly day-ahead electricity prices and hourly forecasted day-ahead demand (from governmental agency) in the Norwegian price area NO2. I am trying to use the forecasted day-ahead demand to predict the next 24-hour electricity prices for each day (cross validation using a rolling window) with a linear model. However, I am having trouble implementing the exogenous predictor (day-ahead demand) in this model without getting an error.
I have tried lagging the predictor, which looks like this:
# rolling window
NO2 <- NO2 %>%
filter(datetime > as.POSIXct("2023-01-01 00:00:00"))
NO2_stretch <- NO2 %>%
stretch_tsibble(.init = 48, .step = 24) %>% # initial training set of size 48 (two days), step = 24 as the training set should expand one day for each forecast iteration
filter(.id !=max(.id)) # remove the last one as there will be no more observations to test
NO2_stretch # te "id" column corresponds to which row in the cross validation that we are in
# Estimate the linear models for each window
fit_cv <- NO2_stretch %>%
model(TSLM(no2_price ~ lag(no2_load, 24) + season())) # get a forecast for each day with an increasing training set
# can pipe this rolling window into forecast in the usual way to produce one step ahead forecasts from all models
fc_cv <- fit_cv %>%
forecast(h="1 day")
# Cross-validated accuracy
fc_cv %>% accuracy(NO2_actual)
The forecast function gives me this error:
Error: Problem with `mutate()` input `TSLM(no2_price ~ no2_load + season())`.
x object 'no2_load' not found
Unable to compute required variables from provided `new_data`.
Does your model require extra variables to produce forecasts?
ℹ Input `TSLM(no2_price ~ no2_load + season())` is `(function (object, ...) ...`.
Any help on this would be highly appreciated, let me know if I can clarify this further.