Climate Change and Impacts in Africa
According to the United Nations, Climate change refers to long-term shifts in temperatures and weather patterns. Such shifts can be natural, due to changes in the sun’s activity or large volcanic eruptions. But since the 1800s, human activities have been the main driver of climate change, primarily due to the burning of fossil fuels like coal, oil, and gas.
The consequences of climate change now include, among others, intense droughts, water scarcity, severe fires, rising sea levels, flooding, melting polar ice, catastrophic storms, and declining biodiversity.
You work for a Non-governmental organization tasked with reporting the state of climate change in Africa at the upcoming African Union Summit. The head of analytics has provided you with IEA-EDGAR CO2 dataset which you will clean, combine and analyze to create a report on the state of climate change in Africa. You will also provide insights on the impact of climate change on African regions (with four countries, one from each African region, as case studies).
Dataset
The dataset, IEA-EDGAR CO2, is a component of the EDGAR (Emissions Database for Global Atmospheric Research) Community GHG database version 7.0 (2022) including or based on data from IEA (2021) Greenhouse Gas Emissions from Energy, www.iea.org/statistics, as modified by the Joint Research Centre. The data source was the EDGARv7.0_GHG website provided by Crippa et. al. (2022) and with DOI.
The dataset contains three sheets - IPCC 2006, 1PCC 1996, and TOTALS BY COUNTRY on the amount of CO2 (a greenhouse gas) generated by countries between 1970 and 2021. You can download the dataset from your workspace or inspect the dataset directly here.
TOTALS BY COUNTRY SHEET
This sheet contains the annual CO2 (kt) produced between 1970 - 2021 in each country. The relevant columns in this sheet are:
| Columns | Description |
|---|---|
C_group_IM24_sh | The region of the world |
Country_code_A3 | The country code |
Name | The name of the country |
Y_1970 - Y_2021 | The amount of CO2 (kt) from 1970 - 2021 |
IPCC 2006
These sheets contain the amount of CO2 by country and the industry responsible.
| Columns | Description |
|---|---|
C_group_IM24_sh | The region of the world |
Country_code_A3 | The country code |
Name | The name of the country |
Y_1970 - Y_2021 | The amount of CO2 (kt) from 1970 - 2021 |
ipcc_code_2006_for_standard_report_name | The industry responsible for generating CO2 |
Instructions
The head of analytics in your organization has specifically asked you to do the following:
- Clean and tidy the datasets.
- Create a line plot to show the trend of
CO2levels across the African regions. - Determine the relationship between time (
Year) andCO2levels across the African regions. - Determine if there is a significant difference in the
CO2levels among the African Regions. - Determine the most common (top 5) industries in each African region.
- Determine the industry responsible for the most amount of CO2 (on average) in each African Region.
- Predict the
CO2levels (at each African region) in the year 2025 using linear regression. - Determine if
CO2levels affect annualtemperaturein the selected African countries using linear regression.
IMPORTANT
-
Make a copy of this workspace.
-
Write your code within the cells provided for you. Each of those cells contain the comment "
#Your code here". -
Next, run the cells containing the checks. We've asked you not to modify these cells. To pass a check, make sure you create the variables mentioned in the instruction tasks. They (the variables) will be verified for correctness; if the cell outputs
TRUEyour solution passes else the cell will throw an error. We included messages to help you fix these errors. -
If you're stuck (even after reviewing related DataCamp courses), then check the
solutions.Rfile. Click Files (left) and download the solutions.R file. We advise you to only look at the solution to your current problem. -
Note that workspaces created inside the "I4G 23/24" group are always private to the group and cannot be made public.
-
If after completion you want to showcase your work on your DataCamp portfolio, use "File > Make a copy" to copy over the workspace to your personal account. Then make it public so it shows up on your DataCamp portfolio.
-
We hope you enjoy working on this project as we enjoyed creating it. Cheers!
# Setup
library(dplyr)
library(readxl)
library(readr)
library(tidyr)
library(ggplot2)
library(assertthat)
library(broom)
library(stringr)
install.packages("rebus");
library(rebus)
# we need only the African regions
african_regions <- c('Eastern_Africa', 'Western_Africa', 'Southern_Africa', 'Northern_Africa')
ipcc_2006_africa <- read_xlsx("IEA_EDGAR_CO2_1970-2021.xlsx", sheet = 'IPCC 2006', skip = 10) %>%
filter(C_group_IM24_sh %in% african_regions)
totals_by_country_africa <- read_xlsx("IEA_EDGAR_CO2_1970-2021.xlsx", sheet = 'TOTALS BY COUNTRY', skip = 10) %>%
filter(C_group_IM24_sh %in% african_regions)
# Read the temperatures datasets containing four African countries
# One from each African Region:
# Nigeria: West Africa
# Ethiopa : East Africa
# Tunisia: North Africa
# Mozambique: South Africa
temperatures <- read_csv('temperatures.csv')# DO NOT MODIFY THIS CELL
# load the tests runner
source('tests.R')Instruction 1: Clean and tidy the datasets
Tasks
- Rename
C_group_IM24_shtoRegion,Country_code_A3toCode, andipcc_code_2006_for_standard_report_nametoIndustryin the corresponding African datasets. - Drop
IPCC_annex,ipcc_code_2006_for_standard_report, andSubstancefrom the corresponding datasets. - Gather
Y_1970toY_2021into a two columnsYearandCO2. Drop rows whereCO2is missing. - Convert
Yeartointtype.
Hints
- Use
rename()function to rename columns. select(-col)will drop a column namedcol.- You might find
pivot_longer()useful. - The
as.integer()can be used to convert to integer.
ipcc_2006_africa <- rename(ipcc_2006_africa, Region = C_group_IM24_sh,
Code = Country_code_A3,
Industry = ipcc_code_2006_for_standard_report_name) %>%
select(-IPCC_annex, -ipcc_code_2006_for_standard_report, -Substance) %>%
gather(key = Year, value = CO2, Y_1970:Y_2021) %>%
filter(!is.na(CO2)) %>%
mutate(Year = as.integer(str_remove_all(Year, pattern = char_class("Y,_"))))
totals_by_country_africa <- rename(totals_by_country_africa, Region = C_group_IM24_sh, Code = Country_code_A3) %>%
select(-IPCC_annex, -Substance) %>%
gather(key = Year, value = CO2, Y_1970:Y_2021) %>%
filter(!is.na(CO2)) %>%
mutate(Year = as.integer(str_remove_all(Year, pattern = char_class("Y,_"))))
# DO NOT MODIFY THIS CELL
# Run this cell to determine if you've done the above correctly
# If there are no error messages, you are correct :)
runner$check_task_1(ipcc_2006_africa, totals_by_country_africa)Instruction 2: Show the trend of CO2 levels across the African regions
Tasks
- Group the
totals_by_country_africadataset byRegionandYearand summarise theCO2column using themean()function. - Save the summarised column as
co2_leveland the resulting data frame asco2_level_by_region_per_year. - Create a line plot using
ggplot()to show the trend ofCO2levels byYearacross the African Regions. For testing purposes, save the line plot as thetrend_of_CO2_emission_plotvariable.
Hints
- You should find
group_by()andsummarize()verb useful. - Use
geom_line()to create a line plot.
totals_by_country_africa %>%
group_by(Region, Year) %>%
summarize(co2_level = mean(CO2)) -> co2_level_by_region_per_year
ggplot(co2_level_by_region_per_year, aes(Year, co2_level, color = Region)) + geom_line() -> trend_of_CO2_emission_plot# DO NOT MODIFY THIS CELL
# Run this cell to determine if you've done the above correctly
# If there are no error messages, you are correct :)
# tests
runner$check_task_2(co2_level_by_region_per_year, trend_of_CO2_emission_plot)Instruction 3: Determine the relationship between time and CO2 levels in each African region
Tasks
- Using the
totals_by_country_africadataset, conduct a Spearman's correlation to determine the relationship between time (Year) andCO2within each AfricanRegion. - Save the results in a variable called
relationship_btw_time_CO2.
Hints
- Use
group_by(var_name)function andsummarise()function. - Use the
cor()function and itsmethodparameter to set the correlation type.
totals_by_country_africa %>%
group_by(Region) %>%
summarize(r = cor(Year, CO2, method = "spearman")) -> relationship_btw_time_CO2# DO NOT MODIFY THIS CELL
# Run this cell to determine if you've done the above correctly
# If there are no error messages, you are correct :)
# tests
runner$check_task_3(relationship_btw_time_CO2)Instruction 4: Determine if there is a significant difference in the CO2 levels among the African Regions
CO2 levels among the African RegionsTasks
- Using
totals_by_country_africa, conduct an ANOVA usingaov()function on theCO2byRegion. Save the results asaov_results. - Conduct a posthoc test (with Bonferroni correction) to find the source of the significant difference. Save the results as
pw_ttest_result. - Is it true that the
CO2levels of theSouthern_AfricaandNorthern_Africaregions do not differ significantly? The previous task should provide you with the answer.
Hints
- Type
?aovin an R console to find help running an ANOVA. - Type
?pairwise.t.testin an R console to find help conducting a pairwise t-test.
aov_results <- aov(CO2 ~ Year, totals_by_country_africa)
summary(aov_results)
(pw_ttest_result <- pairwise.t.test(totals_by_country_africa$CO2, totals_by_country_africa$Region, p.adjust.method = "bonferroni"))