summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
The chosen dataset provides information about the life expectancy of people in different countries. It consists of 22 columns and 2938 rows of data, which were collected from the World Health Organization (WHO) and the United Nations Development Program (UNDP) from 2000 to 2015 and covers different aspects of life expectancy like infant mortality rate, adult mortality rate, GDP per capita, education, and healthcare expenditure. The information can also be used to predict the life expectancy of people in different countries based on the factors that influence it. Researchers can use the dataset to conduct studies on the impact of healthcare and education on life expectancy and to suggest policy changes that can improve people’s quality of life.
The dataset is relevant as life expectancy is a crucial indicator of a country’s development and well-being. This can be used to study and explore the trends in life expectancy over the years and identify the countries that need more attention in healthcare and education via visual analysis and data analytics.
Variables: The dataset consists of 22 predicting variables. Country: Consisting of 193 values equivalent to 193 countries around the world. Year: The dataset works with collected data for each variable from 2000 to 2015. Status: Countries are classified into developing or developed status. Life expectancy: The average life expectancy of countries in age. Adult mortality: Adult mortality rates of both sexes (probability of dying between 15 and 60 years per 1000 population). Infant deaths: Number of infant deaths per 1000 population. Alcohol: Alcohol, recorded per capita (15+) consumption (in liters of pure alcohol). Percentage expenditure: Expenditure on health as a percentage of Gross Domestic Product per capita (%). Hepatitis B: Hepatitis B (HepB) immunization coverage among 1-year-olds (%). Measles: Measles - number of reported cases per 1000 population. BMI: The average Body Mass Index of the entire population. Under-five deaths: Number of under-five deaths per 1000 population. Polio: The Polio (Pol3) vaccination rate for 1-year-old children (%). Total expenditure: The percentage a country pays for health services as part of total government expenditure (%). Diphtheria: The Diphtheria and Pertussis vaccination rate for 1-year-old children (%). HIV/AIDS: The rate of deaths per 1000 live births of HIV/AIDS (%). GDP: The Gross Domestic Product per capita in a country (USD). Population: The number of people living in a country (people). Thinness 10-19 years: The percentage of thinness among children and adolescents for 10 - 19 year-olds (%). Thinness 5-9 years: The proportion of thinness among children for 5 - 9 year-old (%). Income composition: Human Development Index in terms of income composition of resources (index ranging from 0 to 1). Schooling: The number of years that people in each country go to school (years).
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.2.0
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: mvtnorm
## Loading required package: survival
## Loading required package: TH.data
## Loading required package: MASS
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
##
##
## Attaching package: 'TH.data'
##
## The following object is masked from 'package:MASS':
##
## geyser
##
## Rows: 2938 Columns: 22── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Country, Status
## dbl (20): Year, Life, Adult, Infant, Alcohol, percentageexpenditure, Hepatit...
## ℹ 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.
## Warning: Removed 10 rows containing non-finite values (`stat_boxplot()`).
## [1] 2.465086e-170
## === Analysis of Variance (ANOVA) ===
## Df Sum Sq Mean Sq F value Pr(>F)
## Status 1 61715 61715 886.2 <2e-16 ***
## Residuals 2926 203776 70
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 10 observations deleted due to missingness
## The overall ANOVA is statistically significant (p < 0.01 ).
## There is evidence of a significant difference in life expectancy across different country statuses.
## The overall ANOVA is statistically significant (p < 0.05).
## There is evidence of a significant difference in life expectancy across different country statuses.
## Warning: Removed 10 rows containing non-finite values (`stat_summary()`).
## Warning: Removed 453 rows containing missing values (`geom_point()`).
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
# Load required libraries
library(tidyverse)
library(cowplot)
##
## Attaching package: 'cowplot'
## The following object is masked from 'package:lubridate':
##
## stamp
# Read the dataset
my_data <- read_csv("C:\\Users\\Vu Cam Thu\\Documents\\life expectancy.csv")
## Rows: 2938 Columns: 22
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Country, Status
## dbl (20): Year, Life, Adult, Infant, Alcohol, percentageexpenditure, Hepatit...
##
## ℹ 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.
# Set the threshold for low-income countries based on GDP
gdp_threshold <- 1000 # Adjust this threshold according to your specific criteria
# Filter for low-income countries based on the threshold
low_income_data <- my_data %>%
filter(GDP < gdp_threshold)
# Subset variables of interest
subset_data <- low_income_data %>%
dplyr::select(GDP, Schooling)
# Calculate the Pearson correlation coefficient
correlation <- cor(subset_data$GDP, subset_data$Schooling, use = "complete.obs") # Specify "use = 'complete.obs'" to handle missing values
# Print the correlation coefficient
print(paste("Pearson correlation coefficient:", correlation))
## [1] "Pearson correlation coefficient: 0.108196033259806"
# Scatter plot with a trend line
scatter_plot <- ggplot(subset_data, aes(x = GDP, y = Schooling)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "darkorange") + # Add a linear regression trend line
xlab("GDP") +
ylab("Schooling") +
ggtitle("For Low-Income Countries")
# Histogram of GDP insight
gdp_histogram <- ggplot(subset_data, aes(x = GDP)) +
geom_histogram(binwidth = 500, fill = "burlywood1", color = "white") +
xlab("GDP") +
ylab("Frequency") +
ggtitle("GDP for Low-Income Countries")
# Box plot of Schooling insight
schooling_boxplot <- ggplot(subset_data, aes(x = "Schooling", y = Schooling)) +
geom_boxplot(fill = "darksalmon", color = "black") +
xlab("") +
ylab("Schooling") +
ggtitle("Schooling for Low-Income Countries")
# Bubble chart
bubble_chart <- ggplot(low_income_data, aes(x = GDP, y = Schooling, size = Life)) +
geom_point(alpha = 0.2, color = "navy") +
scale_size(range = c(2, 3)) +
xlab("GDP") +
ylab("Schooling") +
ggtitle("GDP, Schooling, Life Expectancy
for Low-Income Countries")
# Arrange and display the plots
plot_grid(scatter_plot, gdp_histogram, schooling_boxplot, bubble_chart, ncol = 2)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 3 rows containing missing values (`geom_point()`).
## Warning: Removed 3 rows containing non-finite values (`stat_boxplot()`).
## Warning: Removed 5 rows containing missing values (`geom_point()`).
# Load required libraries
library(tidyverse)
library(survival)
library(knitr)
library(tidyverse)
library(survival)
library(knitr)
library(MASS)
my_data <- read_csv("C:\\Users\\Vu Cam Thu\\Documents\\life expectancy.csv")
## Rows: 2938 Columns: 22
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Country, Status
## dbl (20): Year, Life, Adult, Infant, Alcohol, percentageexpenditure, Hepatit...
##
## ℹ 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.
filtered_data <- my_data %>%
dplyr::select(Country, Year, GDP, Population, bmi, Adult) %>%
filter(!is.na(GDP) & !is.na(Population) & !is.na(bmi) & !is.na(Adult))
filtered_data$GDP_Per_Capita <- filtered_data$GDP / filtered_data$Population
# Group data
mean_GDP_per_capita <- filtered_data %>%
group_by(Country) %>%
summarize(Mean_GDP_Per_Capita = mean(GDP_Per_Capita, na.rm = TRUE)) %>%
arrange(desc(Mean_GDP_Per_Capita)) # Arrange by decreasing GDP per capita
# Get top 5 wealthiest countries
top_5_countries <- mean_GDP_per_capita$Country[1:5]
top_5_data <- filtered_data %>%
filter(Country %in% top_5_countries)
top_5_countries_table <- data.frame(Country = top_5_countries)
kable(top_5_countries_table, caption = "Top 5 Wealthiest Countries")
| Country |
|---|
| Maldives |
| Georgia |
| Israel |
| Croatia |
| Tonga |
# Display data for top 5 wealthiest countries
top_5_countries_table <- top_5_data %>%
dplyr::select(Country, bmi, Adult)
kable(top_5_countries_table, caption = "Data for Top 5 Wealthiest Countries")
| Country | bmi | Adult |
|---|---|---|
| Croatia | 63.7 | 95 |
| Croatia | 63.1 | 97 |
| Croatia | 62.5 | 97 |
| Croatia | 61.9 | 14 |
| Croatia | 61.3 | 14 |
| Croatia | 6.6 | 16 |
| Croatia | 6.0 | 19 |
| Croatia | 59.4 | 116 |
| Croatia | 58.7 | 114 |
| Croatia | 58.1 | 113 |
| Croatia | 57.5 | 116 |
| Croatia | 56.9 | 114 |
| Croatia | 56.3 | 122 |
| Croatia | 55.8 | 124 |
| Croatia | 55.2 | 126 |
| Croatia | 54.7 | 127 |
| Georgia | 56.2 | 129 |
| Georgia | 55.3 | 125 |
| Georgia | 54.4 | 128 |
| Georgia | 53.6 | 13 |
| Georgia | 52.8 | 127 |
| Georgia | 52.0 | 132 |
| Georgia | 51.3 | 133 |
| Georgia | 5.5 | 128 |
| Georgia | 49.9 | 12 |
| Georgia | 49.2 | 126 |
| Georgia | 48.6 | 128 |
| Georgia | 48.1 | 134 |
| Georgia | 47.5 | 132 |
| Georgia | 47.0 | 142 |
| Georgia | 46.5 | 121 |
| Georgia | 46.0 | 129 |
| Israel | 64.9 | 58 |
| Israel | 64.6 | 6 |
| Israel | 64.2 | 61 |
| Israel | 63.8 | 6 |
| Israel | 63.4 | 61 |
| Israel | 63.0 | 61 |
| Israel | 62.6 | 63 |
| Israel | 62.1 | 65 |
| Israel | 61.6 | 68 |
| Israel | 61.1 | 68 |
| Israel | 6.6 | 71 |
| Israel | 6.1 | 69 |
| Israel | 59.6 | 71 |
| Israel | 59.2 | 74 |
| Israel | 58.7 | 74 |
| Israel | 58.3 | 76 |
| Maldives | 27.4 | 61 |
| Maldives | 26.2 | 62 |
| Maldives | 25.1 | 64 |
| Maldives | 24.1 | 65 |
| Maldives | 23.1 | 67 |
| Maldives | 22.1 | 73 |
| Maldives | 21.2 | 75 |
| Maldives | 2.3 | 81 |
| Maldives | 19.5 | 82 |
| Maldives | 18.7 | 88 |
| Maldives | 18.0 | 93 |
| Maldives | 17.3 | 16 |
| Maldives | 16.7 | 112 |
| Maldives | 16.2 | 124 |
| Maldives | 15.6 | 129 |
| Maldives | 15.2 | 139 |
| Tonga | 75.2 | 133 |
| Tonga | 74.8 | 135 |
| Tonga | 74.3 | 137 |
| Tonga | 73.8 | 138 |
| Tonga | 73.3 | 14 |
| Tonga | 72.7 | 142 |
| Tonga | 72.1 | 147 |
| Tonga | 71.5 | 145 |
| Tonga | 7.8 | 146 |
| Tonga | 7.1 | 148 |
| Tonga | 69.4 | 15 |
| Tonga | 68.6 | 151 |
| Tonga | 67.8 | 153 |
| Tonga | 67.0 | 155 |
| Tonga | 66.2 | 157 |
| Tonga | 65.5 | 158 |
# Use linear regression model
linear_model <- lm(Adult ~ bmi, data = top_5_data)
summary(linear_model)
##
## Call:
## lm(formula = Adult ~ bmi, data = top_5_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -93.58 -28.20 15.49 35.13 62.95
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 83.2580 11.8600 7.020 7.21e-10 ***
## bmi 0.2526 0.2270 1.113 0.269
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 44.29 on 78 degrees of freedom
## Multiple R-squared: 0.01563, Adjusted R-squared: 0.003006
## F-statistic: 1.238 on 1 and 78 DF, p-value: 0.2692
# Draw scatter plot
scatter_plot <- ggplot(top_5_data, aes(x = bmi, y = Adult, color = Country)) +
geom_point() +
labs(x = "Body Mass Index", y = "Adult Mortality", title = "Scatter plot of BMI vs Adult Mortality in Top 5 Wealthiest Countries") +
theme_minimal() +
theme(legend.position = "bottom")
print(scatter_plot)
# Draw histogram
histogram <- ggplot(top_5_data, aes(x = Adult, fill = Country)) +
geom_histogram(binwidth = 10) +
labs(x = "Adult Mortality", y = "Frequency", title = "Histogram of Adult Mortality by Country in Top 5 Wealthiest Countries") +
theme_minimal() +
theme(legend.position = "none")
print(histogram)
# Draw line plot
line_plot <- ggplot(top_5_data, aes(x = Year, y = bmi, color = Country, group = Country)) +
geom_line(size = 1) +
labs(x = "Year", y = "Body Mass Index", title = "Average BMI over Time for Top 5 Wealthiest Countries") +
theme_minimal() +
theme(legend.position = "bottom")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
print(line_plot)
# Box plot of BMI by Country
boxplot <- ggplot(top_5_data, aes(x = Country, y = bmi, fill = Country)) +
geom_boxplot() +
labs(x = "Country", y = "Body Mass Index", title = "Box Plot of BMI by Country in Top 5 Wealthiest Countries") +
theme_minimal() +
theme(legend.position = "none")
print(boxplot)
# Load required libraries
library(tidyverse)
# Read the data
my_data <- read_csv("C:\\Users\\Vu Cam Thu\\Documents\\life expectancy.csv")
## Rows: 2938 Columns: 22
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Country, Status
## dbl (20): Year, Life, Adult, Infant, Alcohol, percentageexpenditure, Hepatit...
##
## ℹ 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.
show_col_types = FALSE
# Filter data
data_2000 <- filter(my_data, Year == 2000)$Life
data_2005 <- filter(my_data, Year == 2005)$Life
data_2010 <- filter(my_data, Year == 2010)$Life
data_2015 <- filter(my_data, Year == 2015)$Life
# Calculate mean differences
mean_diff_2000_2005 <- mean(data_2005 - data_2000)
mean_diff_2005_2010 <- mean(data_2010 - data_2005)
mean_diff_2010_2015 <- mean(data_2015 - data_2010)
# Perform paired t-tests to test the hypothesis
t_test_2000_2005 <- t.test(data_2005, data_2000, paired = TRUE, alternative = "greater")
t_test_2005_2010 <- t.test(data_2010, data_2005, paired = TRUE, alternative = "greater")
t_test_2010_2015 <- t.test(data_2015, data_2010, paired = TRUE, alternative = "greater")
# Display the t-test results
print(t_test_2000_2005)
##
## Paired t-test
##
## data: data_2005 and data_2000
## t = 6.9778, df = 182, p-value = 2.695e-11
## alternative hypothesis: true mean difference is greater than 0
## 95 percent confidence interval:
## 1.113328 Inf
## sample estimates:
## mean difference
## 1.459016
print(t_test_2005_2010)
##
## Paired t-test
##
## data: data_2010 and data_2005
## t = 6.9744, df = 182, p-value = 2.747e-11
## alternative hypothesis: true mean difference is greater than 0
## 95 percent confidence interval:
## 1.403333 Inf
## sample estimates:
## mean difference
## 1.839344
print(t_test_2010_2015)
##
## Paired t-test
##
## data: data_2015 and data_2010
## t = 6.5621, df = 182, p-value = 2.673e-10
## alternative hypothesis: true mean difference is greater than 0
## 95 percent confidence interval:
## 1.173185 Inf
## sample estimates:
## mean difference
## 1.568306
# Create box plots
boxplot_data <- data.frame(
"2000-2005" = data_2005 - data_2000,
"2005-2010" = data_2010 - data_2005,
"2010-2015" = data_2015 - data_2010
)
colors <- c("goldenrod1", "goldenrod3", "orange4")
boxplot(boxplot_data,
xlab = "Years",
ylab = "Difference in Life Expectancy",
main = "Life Expectancy between Consecutive Years",
col = colors)
# Create violin plots
violin_data <- data.frame(
Year = rep(c("2000-2005", "2005-2010", "2010-2015"), each = length(data_2000)),
Life_Expectancy = c(data_2005 - data_2000, data_2010 - data_2005, data_2015 - data_2010)
)
colors <- c("lightpink", "lightpink3", "lightpink4")
ggplot(violin_data, aes(x = Year, y = Life_Expectancy, fill = Year)) +
geom_violin() +
scale_fill_manual(values = colors) +
labs(x = "Years", y = "Difference in Life Expectancy", title = "Life Expectancy Difference between Consecutive Years")
# Create line plot
line_data <- data.frame(
Year = c(2000, 2005, 2010, 2015),
Mean_Life_Expectancy = c(mean(data_2000), mean(data_2005), mean(data_2010), mean(data_2015))
)
# Create stacked area plot
stacked_data <- my_data %>%
group_by(Year, Status) %>%
summarise(Total = sum(Life), .groups = 'drop')
ggplot(stacked_data, aes(x = Year, y = Total, fill = Status)) +
geom_area(position = "stack") +
xlab("Year") +
ylab("Total Life Expectancy") +
ggtitle("Total Life Expectancy by Year and Status") +
scale_fill_manual(values = c("darkolivegreen3", "palevioletred")) +
theme(legend.position = "bottom")
## Warning: Removed 1 rows containing non-finite values (`stat_align()`).
# Create histogram
hist_plot <- ggplot(my_data, aes(x = Life, fill = Status)) +
geom_histogram(binwidth = 5, position = "identity", alpha = 0.7) +
xlab("Life Expectancy") +
ylab("Count") +
ggtitle("Distribution of Life Expectancy by Status") +
scale_fill_manual(values = c("blue4", "tomato3")) +
theme(legend.position = "bottom")
print(hist_plot)
## Warning: Removed 10 rows containing non-finite values (`stat_bin()`).