#Importing the data from Kaggle
library(readr)
mydata <-read.csv ("~/Desktop/IMB/Life Expectancy Data.csv",header= TRUE,sep=",", dec=".")
#Filtering the time-series data to just one year's observations (for 2008)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mydata2 <- mydata %>%
filter(Year == 2008)
head (mydata2)
## Country Year Status Life.expectancy Adult.Mortality infant.deaths Alcohol
## 1 Afghanistan 2008 Developing 58.1 287 80 0.03
## 2 Albania 2008 Developing 75.3 1 1 5.61
## 3 Algeria 2008 Developing 74.1 126 20 0.46
## 4 Angola 2008 Developing 48.7 371 84 7.07
## 5 Antigua and Barbuda 2008 Developing 75.2 142 0 8.27
## 6 Argentina 2008 Developing 75.4 126 10 8.41
## percentage.expenditure Hepatitis.B Measles BMI under.five.deaths Polio Total.expenditure
## 1 25.87393 64 1599 15.7 110 64 8.33
## 2 36.62207 99 0 52.6 1 99 5.87
## 3 43.08717 91 217 51.8 23 92 4.20
## 4 249.91020 69 265 19.3 133 65 3.84
## 5 180.77627 98 0 43.2 0 99 4.69
## 6 1413.73543 9 0 58.6 11 94 6.66
## Diphtheria HIV.AIDS GDP Population thinness..1.19.years thinness.5.9.years
## 1 64 0.1 373.3611 2729431 18.8 18.9
## 2 99 0.1 437.5396 2947314 1.6 1.6
## 3 93 0.1 495.2549 3486715 6.0 5.9
## 4 69 2.6 3868.5789 2175942 9.5 9.4
## 5 99 0.1 1473.3192 NA 3.4 3.3
## 6 93 0.1 8953.3593 4382389 1.0 0.9
## Income.composition.of.resources Schooling
## 1 0.433 8.7
## 2 0.713 12.0
## 3 0.697 12.6
## 4 0.468 8.1
## 5 0.786 14.4
## 6 0.792 16.3
The source of data is Kaggle, Life Expectancy dataset (https://www.kaggle.com/datasets/kumarajarshi/life-expectancy-who).
#Cleaning the data, removing N/A's.
#install.packages("tidyr")
library(tidyr)
mydata3 <- drop_na(mydata2)
#Deleting the irrelevant variables for correlation analysis.
mydata4 <- mydata3 [,c(-9,-10,-11,-12, -13,-14,-15,-16,-17,-18,-19,-20) ]
head (mydata4)
## Country Year Status Life.expectancy Adult.Mortality infant.deaths Alcohol
## 1 Afghanistan 2008 Developing 58.1 287 80 0.03
## 2 Albania 2008 Developing 75.3 1 1 5.61
## 3 Algeria 2008 Developing 74.1 126 20 0.46
## 4 Angola 2008 Developing 48.7 371 84 7.07
## 5 Argentina 2008 Developing 75.4 126 10 8.41
## 6 Armenia 2008 Developing 73.2 14 1 3.96
## percentage.expenditure Income.composition.of.resources Schooling
## 1 25.873925 0.433 8.7
## 2 36.622068 0.713 12.0
## 3 43.087173 0.697 12.6
## 4 249.910196 0.468 8.1
## 5 1413.735430 0.792 16.3
## 6 2.888898 0.721 12.3
library(psych)
describe(mydata4)
## vars n mean sd median trimmed mad min max
## Country* 1 123 62.00 35.65 62.00 62.00 45.96 1.00 123.00
## Year 2 123 2008.00 0.00 2008.00 2008.00 0.00 2008.00 2008.00
## Status* 3 123 1.87 0.34 2.00 1.96 0.00 1.00 2.00
## Life.expectancy 4 123 68.67 9.01 71.10 69.15 7.12 46.20 89.00
## Adult.Mortality 5 123 180.21 123.78 159.00 167.07 99.33 1.00 632.00
## infant.deaths 6 123 36.79 133.14 3.00 12.12 4.45 0.00 1300.00
## Alcohol 7 123 4.83 4.07 4.16 4.44 4.46 0.01 16.99
## percentage.expenditure 8 123 913.45 2452.54 107.80 278.07 139.23 0.66 18961.35
## Income.composition.of.resources 9 123 0.63 0.17 0.67 0.64 0.17 0.00 0.92
## Schooling 10 123 11.93 2.79 12.20 12.02 2.67 5.00 19.10
## range skew kurtosis se
## Country* 122.00 0.00 -1.23 3.21
## Year 0.00 NaN NaN 0.00
## Status* 1.00 -2.17 2.74 0.03
## Life.expectancy 42.80 -0.49 -0.33 0.81
## Adult.Mortality 631.00 1.12 1.55 11.16
## infant.deaths 1300.00 7.55 64.91 12.00
## Alcohol 16.98 0.69 -0.48 0.37
## percentage.expenditure 18960.69 4.48 24.47 221.14
## Income.composition.of.resources 0.92 -0.81 1.01 0.02
## Schooling 14.10 -0.24 -0.26 0.25
#Making factors out of categorical variables for RQ2.
mydata4$StatusF <- factor(mydata4$Status,
levels = c("Developing", "Developed"),
labels=c("Developing", "Developed"))
#Two independent groups are needed for the analysis. Here we transformed the years of schooling into a categorical variable, for RQ2. One group is for the ID's that have above average years of schooling and the other for the ones that have below average years of schooling.
average_years <- mean(mydata4$Schooling)
mydata4$SchoolingF <- ifelse(mydata4$Schooling < average_years, "BelowAverage", "AboveAverage")
mydata4$SchoolingF <- factor(mydata4$SchoolingF,
levels = c("BelowAverage", "AboveAverage"),
labels=c("BelowAverage", "AboveAverage"))
summary(mydata4)
## Country Year Status Life.expectancy Adult.Mortality
## Length:123 Min. :2008 Length:123 Min. :46.20 Min. : 1.0
## Class :character 1st Qu.:2008 Class :character 1st Qu.:62.55 1st Qu.: 97.5
## Mode :character Median :2008 Mode :character Median :71.10 Median :159.0
## Mean :2008 Mean :68.67 Mean :180.2
## 3rd Qu.:2008 3rd Qu.:74.50 3rd Qu.:234.5
## Max. :2008 Max. :89.00 Max. :632.0
## infant.deaths Alcohol percentage.expenditure Income.composition.of.resources
## Min. : 0.00 Min. : 0.010 Min. : 0.662 Min. :0.0000
## 1st Qu.: 1.00 1st Qu.: 1.275 1st Qu.: 39.629 1st Qu.:0.4925
## Median : 3.00 Median : 4.160 Median : 107.799 Median :0.6720
## Mean : 36.79 Mean : 4.828 Mean : 913.449 Mean :0.6287
## 3rd Qu.: 25.00 3rd Qu.: 7.265 3rd Qu.: 474.543 3rd Qu.:0.7480
## Max. :1300.00 Max. :16.990 Max. :18961.349 Max. :0.9210
## Schooling StatusF SchoolingF
## Min. : 5.00 Developing:107 BelowAverage:59
## 1st Qu.:10.25 Developed : 16 AboveAverage:64
## Median :12.20
## Mean :11.93
## 3rd Qu.:13.65
## Max. :19.10
#Checking for linearity.
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(mydata4, aes(x = Alcohol, y = Life.expectancy)) +
geom_point() +
labs(x = "Alcohol Consumption", y = "Life Expectancy",
title = "Scatterplot: Alcohol Consumption vs. Life Expectancy")
#Checking for linearity.
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
## The following object is masked from 'package:dplyr':
##
## recode
scatterplot(mydata4$Alcohol, mydata4$Life.expectancy,
ylim = c(40, 100),
xlab = "Alcohol",
ylab = "Life Expectancy",
boxplots = FALSE,
smooth = FALSE)
#Checking for normality.
hist(mydata4$Life.expectancy,
xlab = "Life expectancy",
ylab = "Frequency",
main = "Histogram of Life expectancy")
shapiro.test(mydata4$Life.expectancy)
##
## Shapiro-Wilk normality test
##
## data: mydata4$Life.expectancy
## W = 0.9638, p-value = 0.002208
We reject H0 at p-value=0.003. Life expectancy is not normally distributed.
hist(mydata4$Alcohol,
xlab = "Alcohol consumption",
ylab = "Frequency",
main = "Histogram of Alcohol consumption")
shapiro.test(mydata4$Alcohol)
##
## Shapiro-Wilk normality test
##
## data: mydata4$Alcohol
## W = 0.91615, p-value = 1.117e-06
We reject H0 at p-value<0.001. Alcohol consumption is not normally distributed.
#Spearman correlation test
cor(mydata4$Life.expectancy, mydata4$Alcohol,
method = "spearman",
use= "complete.obs")
## [1] 0.4378977
There is a semi strong positive linear relationship between life expectancy and alcohol consumption.
#Significance of the correlaTion coefficient
cor.test(mydata4$Life.expectancy, mydata4$Alcohol,
method = "spearman",
exact = FALSE,
use= "complete.obs")
##
## Spearman's rank correlation rho
##
## data: mydata4$Life.expectancy and mydata4$Alcohol
## S = 174321, p-value = 4.082e-07
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.4378977
We reject H0 at p-value<0.001. Based on sample data we can conclude that there us a linear relationship between life expectancy and alcohol consumption.
#Showing also Pearson coefficient, even though normality is not met
cor(mydata4$Life.expectancy, mydata4$Alcohol,
method = "pearson",
use= "complete.obs")
## [1] 0.3999189
There is a semi strong positive linear relationship between life expectancy and alcohol consumption.
#Significance of the correlation coefficient
cor.test(mydata4$Life.expectancy, mydata4$Alcohol,
method = "pearson",
exact = FALSE,
use= "complete.obs")
##
## Pearson's product-moment correlation
##
## data: mydata4$Life.expectancy and mydata4$Alcohol
## t = 4.7996, df = 121, p-value = 4.589e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.2398670 0.5388061
## sample estimates:
## cor
## 0.3999189
We reject H0 at p-value<0.001. Based on sample data we can conclude that there us a linear relationship between life expectancy and alcohol consumption.
Based on sample data and taking Spearman correlation coefficient into account, we can conclude that there is a semi strong positive linear relationship between life expectancy and alcohol consumption.
#Performing chisq, together with Yates correction, due to 2x2 table
results<- chisq.test(mydata4$StatusF, mydata4$SchoolingF,
correct = TRUE)
results
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mydata4$StatusF and mydata4$SchoolingF
## X-squared = 14.818, df = 1, p-value = 0.0001184
Based on sample data we reject H0 at p-value<0.001. There is association between level of development of the country and average years of schooling.
#Observed/Empirical values.
addmargins(results$observed)
## mydata4$SchoolingF
## mydata4$StatusF BelowAverage AboveAverage Sum
## Developing 59 48 107
## Developed 0 16 16
## Sum 59 64 123
#Expected values.
round(results$expected, 2)
## mydata4$SchoolingF
## mydata4$StatusF BelowAverage AboveAverage
## Developing 51.33 55.67
## Developed 7.67 8.33
From the Chi-Square test we can see that observed frequencies are different from expected ones. If the variables would not be correlated we would expect the following: - 51.33 of developing countries to have below average years of schooling - 55.67 of developing countries to have above average years of schooling - 7.67 of developed countries to have below average years of schooling - 8.33 of developed countries to have above average years of schooling
To check the direction of the relationship and if the differences between observed and expected observations are statistically significant, we will check standardized residuals.
round(results$res, 2)
## mydata4$SchoolingF
## mydata4$StatusF BelowAverage AboveAverage
## Developing 1.07 -1.03
## Developed -2.77 2.66
There is less than expected number of developed countries in the group below average. It had an effect, because the value is -2.77 which is more/less than +-1,96 and I can state that with 95% certainty (alpha=5%). There is more than expected number of developed countries in the group below average with 2.66 and I can state that with 95% certainty (alpha=5%). I can’t say that the differences in number of developing countries in either below or above average group of schooling.
addmargins(round(prop.table(results$observed), 3))
## mydata4$SchoolingF
## mydata4$StatusF BelowAverage AboveAverage Sum
## Developing 0.48 0.39 0.87
## Developed 0.00 0.13 0.13
## Sum 0.48 0.52 1.00
Out of 123 observed countries, 39 % are still developing and have above average years of schooling.
addmargins(round(prop.table(results$observed,1),3),2)
## mydata4$SchoolingF
## mydata4$StatusF BelowAverage AboveAverage Sum
## Developing 0.551 0.449 1.000
## Developed 0.000 1.000 1.000
Out of all developing countries, 44.9 % have above average years of schooling.
addmargins(round(prop.table(results$observed,2),3),1)
## mydata4$SchoolingF
## mydata4$StatusF BelowAverage AboveAverage
## Developing 1.00 0.75
## Developed 0.00 0.25
## Sum 1.00 1.00
Out of all with above average years of schooling, 75 % are still developing.
library(effectsize)
##
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
##
## phi
effectsize::cramers_v(mydata4$StatusF,mydata4$SchoolingF)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.36 | [0.20, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.36)
## [1] "large"
## (Rules: funder2019)
The Cramer’s V value was 0.36, which is interpreted as a ‘large’ effect size according to Funder’s 2019 rules. This means there is large association between the level of country’s development and the average years of schooling in this sample.
fisher.test(mydata4$StatusF, mydata4$SchoolingF)
##
## Fisher's Exact Test for Count Data
##
## data: mydata4$StatusF and mydata4$SchoolingF
## p-value = 1.264e-05
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 4.392223 Inf
## sample estimates:
## odds ratio
## Inf
We reject H0 at p-value < 0.001. Approaches have different success rates. The 95% confidence interval for the odds ratio is calculated to be from 4.392223 to infinity (Inf). This means that, with 95% confidence, the true odds ratio is estimated to be greater than 4.392223, and it could theoretically go to infinity.This implies that the odds of an event occurring in one group are infinitely larger than the odds in the other group.
The estimated odds ratio is reported as infinity, suggesting that the odds of the event occurring in one group are practically infinite compared to the other. Therefore, based on this analysis, there appears to be a substantial and statistically significant relationship between the level of country’s development and the average years of schooling in this sample.