Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Last active June 22, 2025 08:19
Show Gist options
  • Save thinkphp/06f90957b1c5927bf491be909f419417 to your computer and use it in GitHub Desktop.
Save thinkphp/06f90957b1c5927bf491be909f419417 to your computer and use it in GitHub Desktop.
solutii
# Load required libraries
library(readxl)
library(dplyr)
library(ggplot2)
library(gridExtra)
# Set working directory (adjust as needed)
# setwd("path/to/your/excel/files")
cat("=== ENVIRONMENTAL ECONOMICS PRACTICE EXAM SOLUTION ===\n\n")
# =============================================================================
# PART 1: CARBON EMISSIONS ANALYSIS (25 points)
# =============================================================================
cat("PART 1: CARBON EMISSIONS ANALYSIS\n")
cat("==================================\n\n")
# Load emissions data
emissions_data <- read_excel("emissions_data.xlsx")
# Question 1: Calculate emissions per capita for 2023
cat("Question 1: Emissions per capita for 2023\n")
emissions_2023 <- emissions_data %>%
filter(year == 2023) %>%
mutate(emissions_per_capita = co2_emissions / population)
mean_emissions_per_capita <- round(mean(emissions_2023$emissions_per_capita, na.rm = TRUE), 1)
median_emissions_per_capita <- round(median(emissions_2023$emissions_per_capita, na.rm = TRUE), 1)
cat("Mean global emissions per capita for 2023:", mean_emissions_per_capita, "tonnes CO2 per person\n")
cat("Median global emissions per capita for 2023:", median_emissions_per_capita, "tonnes CO2 per person\n\n")
# Question 2: Carbon intensity by region for 2023
cat("Question 2: Carbon intensity of GDP by region for 2023\n")
emissions_2023 <- emissions_2023 %>%
mutate(carbon_intensity = co2_emissions / gdp_usd * 1000) # tonnes CO2 per million USD
regional_intensity <- emissions_2023 %>%
group_by(region) %>%
summarise(mean_carbon_intensity = round(mean(carbon_intensity, na.rm = TRUE), 1)) %>%
arrange(region)
print(regional_intensity)
cat("\n")
# Question 3: CAGR of CO2 emissions 2000-2023
cat("Question 3: Top 5 countries with highest emissions growth rates (2000-2023)\n")
emissions_growth <- emissions_data %>%
filter(year %in% c(2000, 2023)) %>%
group_by(country_code) %>%
summarise(
emissions_2000 = co2_emissions[year == 2000],
emissions_2023 = co2_emissions[year == 2023],
.groups = 'drop'
) %>%
filter(!is.na(emissions_2000) & !is.na(emissions_2023) & emissions_2000 > 0) %>%
mutate(
cagr = ((emissions_2023 / emissions_2000)^(1/23) - 1) * 100
) %>%
arrange(desc(cagr)) %>%
slice_head(n = 5)
top_5_growth <- sort(emissions_growth$country_code)
cat("Top 5 countries with highest emissions growth (alphabetical order):", paste(top_5_growth, collapse = ", "), "\n\n")
# Question 4: Scatter plot GDP per capita vs CO2 per capita for 2023
cat("Question 4: Creating scatter plot for 2023 data\n")
# Calculate GDP per capita for plotting
plot_data_2023 <- emissions_2023 %>%
mutate(gdp_per_capita = gdp_usd * 1000 / population) %>% # Convert to per capita
filter(!is.na(gdp_per_capita) & !is.na(emissions_per_capita))
# Create the scatter plot
p1 <- ggplot(plot_data_2023, aes(x = gdp_per_capita, y = emissions_per_capita, color = region)) +
geom_point(alpha = 0.7, size = 2) +
geom_smooth(method = "lm", se = FALSE, color = "black", linetype = "dashed") +
scale_x_continuous(limits = c(0, 80000), breaks = seq(0, 80000, 10000),
labels = scales::comma_format()) +
scale_y_continuous(limits = c(0, 25), breaks = seq(0, 25, 5)) +
labs(
title = "CO2 Emissions vs GDP per Capita (2023)",
x = "GDP per Capita (USD)",
y = "CO2 Emissions per Capita (tonnes)",
color = "Region"
) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "bottom"
)
print(p1)
cat("Scatter plot created with trend line and regional color coding\n\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment