Load packages

library(dplyr)
library(ggplot2)
library(forecast)
require(maps)
require(viridis)

Load Data

Data from : https://ourworldindata.org/explorers/energy?facet=none&country=USA~GBR~CHN~OWID_WRL~IND~BRA~ZAF&Total+or+Breakdown=Select+a+source&Select+a+source=Fossil+fuels&Energy+or+Electricity=Electricity+only&Metric=Annual+generation

energy <- read.csv("energy.csv")
countryContinent <- read.csv("countryContinent.csv")

df<-countryContinent %>%
  select(code_3,continent)
colnames(df)[1] <-"Code"

energy <- merge(energy,df, by="Code")

Part 1 : Research question

We want to show that as regards the production of electricity from fossil fuels, no country in the world has a significant forecasting growing trend from 2021 to 2031, even the countries that pollute the most (China, India, United States, Russia) do not have a growing trend.

Part 2 : Exploratory data analysis

df<-energy %>%
  filter(Year==2021) %>%
  select(Entity,Electricity.from.fossil.fuels..TWh.)
colnames(df)[1] <- "region"

world_map <- map_data("world")
energy_df <- left_join(df, world_map, by = "region")

energy_df %>%
  ggplot(aes(long, lat, group = group))+
  geom_polygon(aes(fill =Electricity.from.fossil.fuels..TWh.  ), color = "white")+
  scale_fill_viridis_c(option = "C", direction = -1)+
  ggtitle("Electricity generation from fossil fuel in Twh in 2021")

energy %>%
  filter(Year==2021, Electricity.from.fossil.fuels..TWh.>100) %>%
  mutate(Entity= reorder(Entity,Electricity.from.fossil.fuels..TWh.)) %>%
  ggplot(aes(Entity,Electricity.from.fossil.fuels..TWh., fill=Entity) )+
  geom_bar(stat = "identity")+
  geom_text(aes(label=Electricity.from.fossil.fuels..TWh.), hjust=0, size=2)+
  coord_flip() +
  guides(fill="none")+
  ggtitle("Electricity.from.fossil.fuels..TWh. in 2021")

Part 3: Forecasting

forecast_country <- function( country) {
  dis<-energy %>%
    filter(Entity==country) %>%
    select(Electricity.from.fossil.fuels..TWh.)
    
  serie <-ts(dis$Electricity.from.fossil.fuels..TWh., start = 1985)
  fore<-forecast(serie)
  
  plot(fore, main=paste("Electricity generation in Thw -",country), ylab="Electricity.from.fossil.fuels", xlab="Year")
  
}

EUROPE:

df <- energy %>%
  filter(continent=="Europe")

for (c in levels(as.factor(df$Entity))) {
    forecast_country(c)
}

## Russia, China, India , United States

forecast_country("Russia")

forecast_country("China")

forecast_country("India")

forecast_country("United States")