#This analysis investigates how life expectancy varies across continents over time using the Gapminder dataset. We focus on the year 2007, analyzing the relationship between GDP per capita and life expectancy, and summarizing trends across continents.
#We preprocess the data by filtering for the year 2007, selecting
relevant columns (country, continent,
lifeExp, gdpPercap, pop), and
adding a new column for GDP in billions.
#knitr::opts_chunk$set(echo = TRUE) # Load necessary libraries library(dplyr) library(ggplot2) library(gapminder) # Step 1: Filter data for the year 2007 data_2007 <- gapminder %>% filter(year == 2007)
pop exists in the datasetif (!“pop” %in% colnames(gapminder)) { stop(“The ‘pop’ column is missing from the dataset.”) }
selected_data <- data_2007 %>% select(country, continent, lifeExp, gdpPercap, pop)
mutated_data <- selected_data %>% mutate(gdp_in_billions = gdpPercap * pop / 1e9)
head(mutated_data)
#Summary Table #The table below summarizes the average and median life expectancy and GDP per capita for each continent.
summary_table <- mutated_data %>% group_by(continent) %>% summarise( avg_life_expectancy = mean(lifeExp, na.rm = TRUE), median_life_expectancy = median(lifeExp, na.rm = TRUE), avg_gdp_per_capita = mean(gdpPercap, na.rm = TRUE), median_gdp_per_capita = median(gdpPercap, na.rm = TRUE) ) %>% arrange(desc(avg_life_expectancy))
summary_table
#Visualization #Below is a scatter plot showing the relationship between GDP per capita and life expectancy in 2007, colored by continent. A trendline is added to illustrate the correlation.
scatter_plot <- ggplot(mutated_data, aes(x = gdpPercap, y = lifeExp, color = continent)) + geom_point(size = 3, alpha = 0.7) + geom_smooth(method = “lm”, se = FALSE, linetype = “dashed”, color = “black”) + scale_x_log10() + labs( title = “Relationship Between GDP Per Capita and Life Expectancy (2007)”, x = “GDP Per Capita (Log Scale)”, y = “Life Expectancy”, color = “Continent” ) + theme_minimal()
scatter_plot
#Summary and Interpretation #The analysis reveals the following insights:
#1.Higher GDP, Higher Life Expectancy: Continents 2.with higher average GDP per capita, such as Europe, tend to have higher average life expectancy. #3.Positive Correlation: There is a positive correlation between GDP per capita and life expectancy, though the relationship is not perfectly linear. #4.Disparities in Africa: Africa has the lowest average life expectancy and GDP per capita, highlighting disparities in development and health outcomes. #5.Median vs. Average: The inclusion of median values shows trends consistent with averages but reduces the impact of outliers.
#Conclusion #This study underscores the strong relationship between economic development and health outcomes. Addressing disparities in GDP per capita, especially in regions like Africa, may lead to improved life expectancy.