Energy Consumption Forecasting Project Using R
By Rohit Sharma
Updated on Aug 06, 2025 | 10 min read | 1.54K+ views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Aug 06, 2025 | 10 min read | 1.54K+ views
Share:
Table of Contents
This project on Energy Consumption Forecasting Using R will show how to predict future energy demand using time series data. By processing hourly electricity consumption into daily averages, we will apply two powerful forecasting models, namely ARIMA and Prophet, to generate short-term predictions.
The analysis is performed entirely in R using Google Colab, making it easy to understand for beginners. This blog focuses on the essential steps needed for energy consumption forecasting, from data cleaning to time series modeling, ensuring a clear understanding of energy demand prediction techniques.
Step into the future of Data Science, master Generative AI, learn from industry veterans, and turn data into opportunity. These top-tier online data science courses are your launchpad to success.
Make Your R Basics Stronger With These: Top 25+ R Projects for Beginners to Boost Your Data Science Skills in 2025
Popular Data Science Programs
Here are a few things you should know before starting this energy consumption forecasting project:
Build future-proof skills with expert-led programs in Artificial Intelligence and Generative AI. If you're aiming for a Master’s or a specialised certificate, take the leap into tomorrow’s tech landscape, learn, innovate, and lead with confidence.
To run this project, we will use essential R libraries that will help in data handling, time series analysis, and visualization. All work is done in Google Colab, so there's no need for local installations or setups. The table below highlights the required tools and libraries.
Tool / Library |
Purpose |
Google Colab | Cloud-based environment to run R code |
R | Programming language used for analysis |
readr, dplyr | Load, clean, and manipulate the dataset |
lubridate | Handle date and time formatting |
ggplot2 | Visualize trends in energy consumption |
forecast, tseries | Build and evaluate the ARIMA model |
prophet | Forecast future energy demand with ease |
This project is designed to be beginner-friendly, with a focus on essential steps only. Here’s a quick overview of what to expect:
Aspect |
Details |
Estimated Duration | 1.5 to 2 hours |
Difficulty Level | Easy to Moderate |
Skill Level Needed | Basic understanding of R and time series concepts |
Ideal For | Beginners exploring time series forecasting in R |
Tools Used | Google Colab, R, ARIMA, Prophet |
This section will explain the various steps involved in building this project.
To begin working with R in Google Colab, you need to switch the notebook's language environment from Python to R. This allows you to run R code directly without needing any external setup.
Instructions:
Here’s an R Project: How to Build an Uber Data Analysis Project in R
Before performing any analysis or forecasting, you need to install the essential R libraries. These packages will help you clean data, visualize trends, and build forecasting models.
# Install necessary libraries
install.packages("tidyverse") # For data manipulation and plotting
install.packages("lubridate") # For working with dates
install.packages("forecast") # For forecasting models
install.packages("tseries") # For statistical tests
install.packages("prophet") # For time series forecasting
install.packages("ggplot2") # For visualizations
The above code gives us the output confirmation of the installation of the libraries:
Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) also installing the dependencies ‘xts’, ‘TTR’, ‘quadprog’, ‘quantmod’, ‘colorspace’, ‘fracdiff’, ‘lmtest’, ‘timeDate’, ‘tseries’, ‘urca’, ‘zoo’, ‘RcppArmadillo’ Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) also installing the dependencies ‘numDeriv’, ‘abind’, ‘tensorA’, ‘distributional’, ‘checkmate’, ‘matrixStats’, ‘posterior’, ‘inline’, ‘gridExtra’, ‘loo’, ‘QuickJSR’, ‘dygraphs’, ‘extraDistr’, ‘RcppParallel’, ‘rstan’, ‘rstantools’, ‘StanHeaders’, ‘BH’, ‘RcppEigen’ Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) |
With the necessary libraries installed, the next step is to load your dataset. This step reads the uploaded CSV file into R so it can be cleaned, analyzed, and used for forecasting. Here’s the code:
# Load readr for reading CSV
library(readr)
# Load dataset from uploaded file
energy_data <- read_csv("Energy Consumption.csv")
# Preview the data
head(energy_data)
This step specifies the number of rows and columns in the dataset and also gives us a glimpse of the dataset we’ll be working with.
Rows: 103776 Columns: 8 ── Column specification ──────────────────────────────────────────────────────── Delimiter: "," chr (1): date dbl (7): year, month, day, weekday, hour, demand, temperature ℹ Use `spec()` to retrieve the full column specification for this data. ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message. |
date |
year |
month |
day |
weekday |
hour |
demand |
temperature |
<chr> |
<dbl> |
<dbl> |
<dbl> |
<dbl> |
<dbl> |
<dbl> |
<dbl> |
3/1/2003 |
2003 |
3 |
1 |
7 |
1 |
12863 |
29 |
3/1/2003 |
2003 |
3 |
1 |
7 |
2 |
12389 |
28 |
3/1/2003 |
2003 |
3 |
1 |
7 |
3 |
12155 |
29 |
3/1/2003 |
2003 |
3 |
1 |
7 |
4 |
12072 |
27 |
3/1/2003 |
2003 |
3 |
1 |
7 |
5 |
12160 |
29 |
3/1/2003 |
2003 |
3 |
1 |
7 |
6 |
12568 |
28 |
Build This R Project: Wine Quality Prediction Project in R
To prepare the dataset for time series forecasting, you need a proper datetime column. This step combines the date and hour columns into a single timestamp using the correct date format. Here’s the code:
library(dplyr)
library(lubridate)
# Use correct date format: m/d/Y
energy_data <- energy_data %>%
mutate(datetime = mdy(date) + hours(hour - 1)) %>% # Create a datetime column
arrange(datetime) # Ensure the data is ordered by time
# Check again
glimpse(energy_data)
head(energy_data)
The above code will clean and correct the data for further analysis. The output is given below:
Rows: 103,776 Columns: 9 $ date <chr> "3/1/2003", "3/1/2003", "3/1/2003", "3/1/2003", "3/1/2003"… $ year <dbl> 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003… $ month <dbl> 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3… $ day <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1… $ weekday <dbl> 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7… $ hour <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,… $ demand <dbl> 12863, 12389, 12155, 12072, 12160, 12568, 13236, 14190, 15… $ temperature <dbl> 29, 28, 29, 27, 29, 28, 29, 29, 28, 27, 30, 33, 35, 37, 37… $ datetime <dttm> 2003-03-01 00:00:00, 2003-03-01 01:00:00, 2003-03-01 02:0… |
date |
year |
month |
day |
weekday |
hour |
demand |
temperature |
datetime |
<chr> |
<dbl> |
<dbl> |
<dbl> |
<dbl> |
<dbl> |
<dbl> |
<dbl> |
<dttm> |
3/1/2003 |
2003 |
3 |
1 |
7 |
1 |
12863 |
29 |
2003-03-01 00:00:00 |
3/1/2003 |
2003 |
3 |
1 |
7 |
2 |
12389 |
28 |
2003-03-01 01:00:00 |
3/1/2003 |
2003 |
3 |
1 |
7 |
3 |
12155 |
29 |
2003-03-01 02:00:00 |
3/1/2003 |
2003 |
3 |
1 |
7 |
4 |
12072 |
27 |
2003-03-01 03:00:00 |
3/1/2003 |
2003 |
3 |
1 |
7 |
5 |
12160 |
29 |
2003-03-01 04:00:00 |
3/1/2003 |
2003 |
3 |
1 |
7 |
6 |
12568 |
28 |
2003-03-01 05:00:00 |
Before forecasting, it's helpful to see how energy demand changes over time. This step plots hourly demand to help identify patterns, trends, or seasonality in the data. Here’s the code:
library(ggplot2)
# Line plot of energy demand over time
ggplot(energy_data, aes(x = datetime, y = demand)) +
geom_line(color = "steelblue") +
labs(title = "Hourly Energy Demand Over Time",
x = "DateTime",
y = "Energy Demand (MW)") +
theme_minimal()
The graph for the above code shows the hourly energy demand over time.
Here’s a Fun R Project: Project On Gender Recognition Using Voice In R Language
Forecasting is more effective on smoother time series data. This step transforms the hourly energy demand into daily average values to reduce noise and highlight overall trends. Here’s the code:
# Group by date and calculate average daily demand
daily_data <- energy_data %>%
group_by(date = as.Date(datetime)) %>%
summarise(daily_demand = mean(demand, na.rm = TRUE))
# Preview the result
head(daily_data)
The output for the above code will give us the table:
date |
daily_demand |
<date> |
<dbl> |
2003-03-01 |
14216.88 |
2003-03-02 |
13851.62 |
2003-03-03 |
16348.88 |
2003-03-04 |
16575.50 |
2003-03-05 |
15391.88 |
2003-03-06 |
15962.21 |
This step generates a line plot to display the trend of daily average energy demand over time. It gives a clearer view of consumption patterns without the fluctuations seen in hourly data. The code for this step is:
# Plot daily average demand over time
ggplot(daily_data, aes(x = date, y = daily_demand)) +
geom_line(color = "darkgreen") +
labs(title = "Daily Average Energy Demand",
x = "Date", y = "Avg Demand (MW)") +
theme_minimal()
The graph shows the daily average energy demand.
Level Up Your R Skills With This R Project: Spotify Music Data Analysis Project in R
Before applying forecasting models like ARIMA, it’s important to check if the time series is stationary, meaning its statistical properties do not change over time. The Augmented Dickey-Fuller (ADF) test is commonly used for this. Here’s the code:
library(tseries)
# Check if the time series is stationary
adf.test(daily_data$daily_demand)
The output of this step shows:
The time series is stationary, which means it doesn't require differencing and is ready for ARIMA modeling.
We now convert the daily demand data into a time series object and apply the ARIMA model to forecast the next 30 days. ARIMA is a commonly used model for time series prediction. Here’s the code:
library(forecast)
# Convert to time series object
ts_data <- ts(daily_data$daily_demand, frequency = 365)
# Fit ARIMA model
model_arima <- auto.arima(ts_data)
# Forecast next 30 days
forecast_arima <- forecast(model_arima, h = 30)
# Plot the forecast
autoplot(forecast_arima) +
labs(title = "Energy Demand Forecast - ARIMA", y = "Demand")
The output for the above code gives us a graph of the energy demand forecast in ARIMA.
Here’s a Great Way To Sharpen Your R Skills: Movie Rating Analysis Project in R
Prophet is a powerful forecasting tool developed by Facebook. It works well with daily time series data that has seasonality and trend components. The code for this step is:
library(prophet)
# Prepare data for Prophet
prophet_data <- daily_data %>%
rename(ds = date, y = daily_demand)
# Fit Prophet model
model_prophet <- prophet(prophet_data)
# Make future dataframe for next 30 days
future <- make_future_dataframe(model_prophet, periods = 30)
# Forecast
forecast <- predict(model_prophet, future)
# Plot forecast
plot(model_prophet, forecast) +
ggtitle("Energy Demand Forecast - Prophet")
The output for the above code gives us a graphical representation of the energy demand forecast using Prophet.
In this Energy Demand Forecasting project, we analyzed hourly electricity consumption data, transformed it into a daily average time series, and applied two powerful forecasting models, ARIMA and Prophet. After cleaning and preparing the dataset, we visualized demand trends and tested for stationarity to ensure the data was suitable for time series modeling.
The ARIMA model provided a statistical baseline, while the Prophet model offered more interpretable forecasts with trend and seasonality components. Both models successfully predicted energy demand for the next 30 days, helping us understand potential future usage patterns for better planning and resource management.
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!
Colab Link:
https://colab.research.google.com/drive/18YmgO5O75B9Vw6h5h7ehCL0lIMhSvxwA
827 articles published
Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...
Speak with Data Science Expert
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources