DSCI-445 Final Project

Ronnie, Lucas, Arya

Kaggle: “Enefit - Predict Energy Behavior of Prosumers”

  • Enefit, a Baltic (Estonian) energy company focused on: solving the issue of energy imbalance, when expected energy usage doesn’t match up with actual energy use or production, which is largely skewed by ‘Prosumers’

   

  • Prosumers: People who both consume and generate energy (through renewable sources)

   

  • Our goal is to develop a predictive model that can accurately forecast the energy behavior and patterns of Estonian prosumers utilizing data science/ techniques learnt from this semester.

Motivation

  • Energy imbalance when the energy expected to be used doesn’t line up with the actual energy used or produced
  • Unpredictable energy use causes logistical and financial problems for energy managers / providers

Data

  • Time series of 637 days of energy production and consumption for 69 unique client categories

    • Categories defined by business type, product type, and location
  • Electricity and natural gas prices on the day-ahead markets

    • Natural gas price daily

    • Electricity price hourly

  • Historical and forecasted weather

    • Mapped by latitude and longitude
  • Datetime / Date: Was split into hourly and daily.

    • Day Before (D-1) : Train (hourly), Client (daily), Electricity, Gas

    • Actual (D) : Forecasted + Historical Weather, Train, Client, Electricity, Gas

    • Predicted (D+1) : Forecasted Weather (hourly)

  • is_consumption: Binary for Consumption (1) and Production (0)

  • data_block_id: Unique identifier to link data across multiple data sets (represented per day)

Data Overview

county is_business product_type target is_consumption datetime data_block_id row_id prediction_unit_id datetime_hour
0 0 1 0.713 0 2021-09-01 0 0 0 00:00:00
0 0 1 96.590 1 2021-09-01 0 1 0 00:00:00
0 0 2 0.000 0 2021-09-01 0 2 1 00:00:00
0 0 2 17.314 1 2021-09-01 0 3 1 00:00:00
0 0 3 2.904 0 2021-09-01 0 4 2 00:00:00
0 0 3 656.859 1 2021-09-01 0 5 2 00:00:00
product_type county eic_count installed_capacity is_business date data_block_id
1 0 108 952.89 0 2021-09-01 2
2 0 17 166.40 0 2021-09-01 2
3 0 688 7207.88 0 2021-09-01 2
0 0 5 400.00 1 2021-09-01 2
1 0 43 1411.00 1 2021-09-01 2
3 0 265 13417.79 1 2021-09-01 2
forecast_date euros_per_mwh origin_date data_block_id forecast_hour
2021-09-01 00:00:00 92.51 2021-08-31 00:00:00 1 00:00:00
2021-09-01 01:00:00 88.90 2021-08-31 01:00:00 1 01:00:00
2021-09-01 02:00:00 87.35 2021-08-31 02:00:00 1 02:00:00
2021-09-01 03:00:00 86.88 2021-08-31 03:00:00 1 03:00:00
2021-09-01 04:00:00 88.43 2021-08-31 04:00:00 1 04:00:00
2021-09-01 05:00:00 93.58 2021-08-31 05:00:00 1 05:00:00
forecast_date lowest_price_per_mwh highest_price_per_mwh origin_date data_block_id
2021-09-01 45.23 46.32 2021-08-31 1
2021-09-02 45.62 46.29 2021-09-01 2
2021-09-03 45.85 46.40 2021-09-02 3
2021-09-04 46.30 46.80 2021-09-03 4
2021-09-05 46.30 46.58 2021-09-04 5
2021-09-06 46.17 46.95 2021-09-05 6
latitude longitude origin_datetime hours_ahead temperature dewpoint cloudcover_high cloudcover_low cloudcover_mid cloudcover_total 10_metre_u_wind_component 10_metre_v_wind_component data_block_id forecast_datetime direct_solar_radiation surface_solar_radiation_downwards snowfall total_precipitation
57.6 21.7 2021-09-01 02:00:00 1 15.65579 11.55361 0.9048157 0.0197144 0.0000000 0.9058991 -0.4113283 -9.106137 1 2021-09-01 03:00:00 0 0 0 0
57.6 22.2 2021-09-01 02:00:00 1 13.00393 10.68984 0.8863220 0.0044556 0.0000000 0.8866578 0.2063475 -5.355405 1 2021-09-01 03:00:00 0 0 0 0
57.6 22.7 2021-09-01 02:00:00 1 14.20657 11.67178 0.7290344 0.0056152 0.0000000 0.7304993 1.4515867 -7.417905 1 2021-09-01 03:00:00 0 0 0 0
57.6 23.2 2021-09-01 02:00:00 1 14.84451 12.26492 0.3363037 0.0743408 0.0006256 0.3854676 1.0908689 -9.163999 1 2021-09-01 03:00:00 0 0 0 0
57.6 23.7 2021-09-01 02:00:00 1 15.29385 12.45889 0.1028748 0.0880737 0.0000153 0.1765900 1.2684813 -8.975766 1 2021-09-01 03:00:00 0 0 0 0
57.6 24.2 2021-09-01 02:00:00 1 15.44900 12.87869 0.0000000 0.2326355 0.0535583 0.2371979 1.1141844 -7.702084 1 2021-09-01 03:00:00 0 0 0 0
datetime temperature dewpoint rain snowfall surface_pressure cloudcover_total cloudcover_low cloudcover_mid cloudcover_high windspeed_10m winddirection_10m shortwave_radiation direct_solar_radiation diffuse_radiation latitude longitude data_block_id
2021-09-01 14.4 12.0 0 0 1015.8 4 4 0 0 6.694444 3 0 0 0 57.6 21.7 1
2021-09-01 14.0 12.0 0 0 1010.6 7 8 0 0 4.944444 353 0 0 0 57.6 22.2 1
2021-09-01 14.4 12.8 0 0 1014.9 6 7 0 0 5.833333 348 0 0 0 57.6 22.7 1
2021-09-01 15.4 13.0 0 0 1014.4 4 2 4 0 7.111111 349 0 0 0 57.6 23.2 1
2021-09-01 15.9 12.6 0 0 1013.8 12 7 0 20 8.388889 360 0 0 0 57.6 23.7 1
2021-09-01 13.1 10.6 0 0 1013.4 1 1 0 0 4.500000 3 0 0 0 57.6 24.2 1
county_name longitude latitude county
NA 21.7 57.6 NA
NA 21.7 57.9 NA
NA 21.7 58.2 NA
NA 21.7 58.5 NA
NA 21.7 58.8 NA
NA 21.7 59.1 NA

Data manipulation

#Loading data 
train <- read_csv("train.csv") 
client <- read_csv("client.csv") 
electricity_prices <- read_csv("electricity_prices.csv") 
gas_prices <- read_csv("gas_prices.csv") 
forecast_weather <- read_csv("forecast_weather.csv") 
historical_weather <- read_csv("historical_weather.csv") 
weather_to_county <- read_csv("weather_station_to_county_mapping.csv")

# Merging data
train <- train %>% 
  mutate("datetime_hour" = format(train$datetime, "%H:%M:%S"))

t_client <- left_join(train, client, by=c("county", "product_type", "is_business", "data_block_id"))

electricity_prices <- electricity_prices %>% 
  mutate("forecast_hour" = format(electricity_prices$forecast_date, "%H:%M:%S"))

t_c_electricity <- left_join(t_client, electricity_prices, by=c("data_block_id", "datetime_hour" = "forecast_hour"))

whole_data <- left_join(t_c_electricity, gas_prices, by="data_block_id")

# Merging weather data
forecast_weather$latitude <- round(forecast_weather$latitude, digits = 1)
forecast_weather$longitude <- round(forecast_weather$longitude, digits = 1)

weather_to_county$latitude <- round(weather_to_county$latitude, digits = 1)
weather_to_county$longitude <- round(weather_to_county$longitude, digits = 1)
# need this else get errors with floating point comparison with ==

forecast_with_county <- left_join(forecast_weather, weather_to_county, by=c("longitude", "latitude"))

weather_forecast_from_day_before <- forecast_with_county %>% filter(hours_ahead >= 24)

# data.table more efficient for large datasets
weather_forecast_from_day_before_dt <- as.data.table(weather_forecast_from_day_before)

weather_forecast_averaged_by_county_dt <- weather_forecast_from_day_before_dt[, .(temperature=mean(temperature),
            dewpoint=mean(dewpoint),
            cloudcover_high=mean(cloudcover_high),
            cloudcover_mid=mean(cloudcover_mid),
            cloudcover_low=mean(cloudcover_low),
            cloudcover_total=mean(cloudcover_total),
            `10_metre_u_wind_component`=mean(`10_metre_u_wind_component`),
            `10_metre_v_wind_component`=mean(`10_metre_v_wind_component`),
            direct_solar_radiation=mean(direct_solar_radiation),
            surface_solar_radiation_downwards=mean(surface_solar_radiation_downwards),
            snowfall=mean(snowfall),
            total_precipitation=mean(total_precipitation)), by=.(county, forecast_datetime)]

weather_forecast_averaged_by_county <- as.data.frame(weather_forecast_averaged_by_county_dt)

whole_data_with_weather <- left_join(whole_data, weather_forecast_averaged_by_county, by=c("county", "datetime" = "forecast_datetime"))

# Cleaning data with weather
whole_data_with_weather <- whole_data_with_weather %>% 
  mutate(cons_or_prod=ifelse(is_consumption == 0, "production", "consumption"),
         pred_unit_id=as.factor(prediction_unit_id),
         county=as.factor(county),
         is_business=as.factor(is_business),
         product_type=as.factor(product_type),
         month=format(whole_data$datetime,"%m"), 
         day=data_block_id,
         hour=datetime_hour,
         nat_gas_price_dif=highest_price_per_mwh-lowest_price_per_mwh,
         nat_gas_price_high=highest_price_per_mwh,
         nat_gas_price_low=lowest_price_per_mwh,
         elect_price=euros_per_mwh,
         pv_capacity=installed_capacity,
         consum_pts=eic_count)

clean_data_weather <- whole_data_with_weather %>% 
  select(cons_or_prod, 
         county, 
         is_business, 
         product_type, 
         month, 
         day, 
         hour, 
         consum_pts, 
         pv_capacity, 
         elect_price, 
         nat_gas_price_high, 
         nat_gas_price_low, 
         temperature, 
         dewpoint, 
         cloudcover_high, 
         cloudcover_mid, 
         cloudcover_low, 
         cloudcover_total, 
         `10_metre_u_wind_component`, 
         `10_metre_v_wind_component`, 
         direct_solar_radiation, 
         surface_solar_radiation_downwards, 
         snowfall, 
         total_precipitation, target)

target_NAs <- which(is.na(clean_data_weather$target))
clean_data_weather <- clean_data_weather[-target_NAs,]

Energy by Time

Temperature by Energy

Distribution of Response Variable

Consumption Production
Mean 460.7081 89.00304
Median 108.8965 0.38700

Photovoltaic Capacity by Energy

Solar Radiation by Energy

Getting into our Models

wtest_prop <- 0.2
wtest_sample <- sample(seq(nrow(clean_data_weather_no_NA)),
                      wtest_prop*nrow(clean_data_weather_no_NA))

wtestset <- clean_data_weather_no_NA[wtest_sample,]
wtrainset <- clean_data_weather_no_NA[-wtest_sample,]
  1. Null Model
  2. Linear Model
  3. Linear Model w/ LASSO
  4. General Additive Model w/ NS
  5. Decision Tree w/ 5-fold CV
  6. XGBoost w/ 5-fold CV

Null Model

  • Null MAE of predicting simply the mean of the response variable from the training set
RMSE MAE
Train 917.7 372.5
Test 913.8 371.4

The Mean Absolute Error (MAE) is calculated using the following formula:

\[ \text{MAE} = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i| \]

Where:

  • \(n\): the number of observations.

  • \(y_i\): the actual value for the \(i^{th}\) observation.

  • \(\hat{y}_i\): the predicted value for the \(i^{th}\) observation.

Linear Model

Residuals vs Fitted

RMSE MAE
Train 656.6 293.4
Test 657.0 293.8

Linear Model + Lasso

  • The lasso procedure only eliminates two of the 74 predictors, an indicator for county12 and nat_gas_price_low
RMSE MAE
Train 656.6 293.2
Test 657.0 293.6

GAM

RMSE MAE
Train 656.22 656.64
Test 656.64 292.96

Energy Consumption and Production

Decision Tree

Prediction high low
high 558177 52188
low 44497 550489

XGBoost

RMSE MAE
Train 118.8 51.6
Test 147.4 56.6

Model Evaluations

RMSE MAE
Null 913.8 371.4
Linear 657.0 293.8
Linear + Lasso 657.0 293.6
GAM with natural spline 656.7 292.6
XGBoost 147.4 56.6

Best Model - XGBoost with Weather Data!

Future Improvements

  • More Models

    • More focus within our classification models due to their improved performance

    • Neural Network

  • Feature Engineer the Weather Data Better

    • Combinations of variables, Dimensionality Reduction or a more thorough feature selection process
  • Optimizing and tuning our hyper parameters

    • More utilization of CV or other validation techniques to improve our model’s parameters
  • Computationally limited by our machines, experiment with cloud based servers

Thank you! Any Questions?

References

  • “Predict Energy Behavior of Prosumers.” Kaggle, https://www.kaggle.com/competitions/predict-energy-behavior-of-prosumers/overview. Accessed 7 Dec. 2023.

  • “Reveal.js Presentations.” Quarto, https://quarto.org/docs/presentations/revealjs/. Accessed 7 Dec. 2023.

  • “Discussion on Predict Energy Behavior of Prosumers Competition.” Kaggle, https://www.kaggle.com/competitions/predict-energy-behavior-of-prosumers/discussion/455833. Accessed 7 Dec. 2023.

  • James, Gareth, et al. An Introduction to Statistical Learning: With Applications in R. 2nd ed., Springer, June 2023. https://hastie.su.domains/ISLR2/ISLRv2_corrected_June_2023.pdf.download.html. Accessed 7 Dec. 2023.