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.


1 Answer 1


At the moment you will need to provide a dataset containing the future values of no2_load to the forecast() function.

For example:

NO2_stretch_future <- NO2_stretch %>%
  new_data(n = 24) %>%
  mutate(no2_load = NA_real_)
fc_cv <- fit_cv %>% 
  forecast(new_data = NO2_stretch_future)

I haven't been able to test this code since you haven't provided a minimally reproducible example (MRE). Providing a MRE makes is easier for me to quickly and accurately answer your question.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.