Energy Consumption Forecasting Project Using R

By Rohit Sharma

Updated on Aug 06, 2025 | 10 min read | 1.54K+ views

Share:

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

What You Must Understand Before Starting the Energy Consumption Forecasting Project Using R

Here are a few things you should know before starting this energy consumption forecasting project:

  • You should be familiar with basic R syntax and functions.
  • A general understanding of time series data (dates, trends, patterns) is helpful.
  • Know how to install and load R libraries like dplyr, ggplot2, forecast, and prophet.
  • You’ll need to work with date-time formatting and data aggregation (e.g., daily averages).
  • The project assumes you can run R code in Google Colab.

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.

R Libraries and Tools Used in This Energy Forecasting Project

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

Project Duration, Difficulty, and Skill Level Required

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

Breakdown of the Energy Consumption Forecasting Project

This section will explain the various steps involved in building this project.

Step 1: Enable R in Google Colab

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:

  1. Go to Google Colab
  2. Open a new notebook
  3. Click on the Runtime menu
  4. Select Change runtime type
  5. Under the Language dropdown, choose R
  6. Click Save to apply the changes

Here’s an R Project: How to Build an Uber Data Analysis Project in R

Step 2: Install Required R Libraries

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)

Step 3: Load the Energy Consumption Dataset

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

Step 4: Clean and Format the Date-Time Column

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

 

Step 5: Visualize Hourly Energy Demand

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

Step 6: Aggregate Hourly Data into Daily Averages

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

Step 7: Visualize Daily Average Energy Demand

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.

background

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree17 Months

Placement Assistance

Certification6 Months

Level Up Your R Skills With This R Project: Spotify Music Data Analysis Project in R

Step 8: Test for Stationarity

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:

  • Dickey-Fuller = -7.4104
    This is the test statistic. The more negative it is, the stronger the evidence that the series is stationary.
  • p-value = 0.01
    This means there's only a 1% chance that the data is non-stationary. Since this is less than 0.05, you can reject the null hypothesis.

The time series is stationary, which means it doesn't require differencing and is ready for ARIMA modeling.

Step 9: Build and Plot the ARIMA Forecast Model

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

Step 10: Forecast Energy Demand Using Prophet

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.

Conclusion

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

Frequently Asked Questions (FAQs)

1. Why is energy demand forecasting important in today’s data-driven world?

2. How do you prepare time series data for forecasting in R?

3. What is the difference between ARIMA and Prophet models in forecasting?

4. What are some challenges faced while forecasting daily energy demand?

5. What are some other beginner-friendly forecasting or time series analysis projects?

Rohit Sharma

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

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

360° Career Support

Executive PG Program

12 Months

Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree

17 Months

upGrad Logo

Certification

3 Months