The data set comes from this link https://www.kaggle.com/datasets/kumarajarshi/life-expectancy-who
my.data <- read.csv("/Users/izakotnikjesih/Downloads/LED.csv")
summary(my.data)
## Country Year Status Life.expectancy
## Length:2938 Min. :2000 Length:2938 Min. :36.30
## Class :character 1st Qu.:2004 Class :character 1st Qu.:63.10
## Mode :character Median :2008 Mode :character Median :72.10
## Mean :2008 Mean :69.22
## 3rd Qu.:2012 3rd Qu.:75.70
## Max. :2015 Max. :89.00
## NA's :10
## Development Adult.Mortality infant.deaths Alcohol
## Min. :0.0000 Min. : 1.0 Min. : 0.0 Min. : 0.0100
## 1st Qu.:0.0000 1st Qu.: 74.0 1st Qu.: 0.0 1st Qu.: 0.8775
## Median :0.0000 Median :144.0 Median : 3.0 Median : 3.7550
## Mean :0.1743 Mean :164.8 Mean : 30.3 Mean : 4.6029
## 3rd Qu.:0.0000 3rd Qu.:228.0 3rd Qu.: 22.0 3rd Qu.: 7.7025
## Max. :1.0000 Max. :723.0 Max. :1800.0 Max. :17.8700
## NA's :10 NA's :194
## percentage.expenditure Hepatitis.B Measles BMI
## Min. : 0.000 Min. : 1.00 Min. : 0.0 Min. : 1.00
## 1st Qu.: 4.685 1st Qu.:77.00 1st Qu.: 0.0 1st Qu.:19.30
## Median : 64.913 Median :92.00 Median : 17.0 Median :43.50
## Mean : 738.251 Mean :80.94 Mean : 2419.6 Mean :38.32
## 3rd Qu.: 441.534 3rd Qu.:97.00 3rd Qu.: 360.2 3rd Qu.:56.20
## Max. :19479.912 Max. :99.00 Max. :212183.0 Max. :87.30
## NA's :553 NA's :34
## under.five.deaths Polio Total.expenditure Diphtheria
## Min. : 0.00 Min. : 3.00 Min. : 0.370 Min. : 2.00
## 1st Qu.: 0.00 1st Qu.:78.00 1st Qu.: 4.260 1st Qu.:78.00
## Median : 4.00 Median :93.00 Median : 5.755 Median :93.00
## Mean : 42.04 Mean :82.55 Mean : 5.938 Mean :82.32
## 3rd Qu.: 28.00 3rd Qu.:97.00 3rd Qu.: 7.492 3rd Qu.:97.00
## Max. :2500.00 Max. :99.00 Max. :17.600 Max. :99.00
## NA's :19 NA's :226 NA's :19
## HIV.AIDS GDP Population thinness..1.19.years
## Min. : 0.100 Min. : 1.68 Min. :3.400e+01 Min. : 0.10
## 1st Qu.: 0.100 1st Qu.: 463.94 1st Qu.:1.958e+05 1st Qu.: 1.60
## Median : 0.100 Median : 1766.95 Median :1.387e+06 Median : 3.30
## Mean : 1.742 Mean : 7483.16 Mean :1.275e+07 Mean : 4.84
## 3rd Qu.: 0.800 3rd Qu.: 5910.81 3rd Qu.:7.420e+06 3rd Qu.: 7.20
## Max. :50.600 Max. :119172.74 Max. :1.294e+09 Max. :27.70
## NA's :448 NA's :652 NA's :34
## thinness.5.9.years Income.composition.of.resources Schooling
## Min. : 0.10 Min. :0.0000 Min. : 0.00
## 1st Qu.: 1.50 1st Qu.:0.4930 1st Qu.:10.10
## Median : 3.30 Median :0.6770 Median :12.30
## Mean : 4.87 Mean :0.6276 Mean :11.99
## 3rd Qu.: 7.20 3rd Qu.:0.7790 3rd Qu.:14.30
## Max. :28.60 Max. :0.9480 Max. :20.70
## NA's :34 NA's :167 NA's :163
Variables: Country: 193 countries (unique value)
Status: developed or developing (categorical)
H0: Life expectancy in developing countries isn’t significantly different from developed countries.
Parametric test of comparison: t-test
##
## Welch Two Sample t-test
##
## data: Life.expectancy by Status
## t = 47.868, df = 1807, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group Developed and group Developing is not equal to 0
## 95 percent confidence interval:
## 11.59118 12.58159
## sample estimates:
## mean in group Developed mean in group Developing
## 79.19785 67.11147
Non-parametric test of comparison: Wilcoxon rank-sum test
non_parametric_result <- wilcox.test(Life.expectancy ~ Status, data = my.data)
non_parametric_result
##
## Wilcoxon rank sum test with continuity correction
##
## data: Life.expectancy by Status
## W = 1131521, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0
Both t-test and Wilcoxon rank sum test yields extremely low p-values (p-value < 2.2e-16), which strongly indicates evidence against the null hypothesis. Therefore, there is a significant difference in life expectancy between developing and developed countries. Both tests lead to the same conclusion and my ultimate choice of test comes down to the large sample size, as the t-test may be robust to violations of normality and has reliable results, T-test is more appropriate.
The results show that there is a significant difference in life expectancy between developing and developed countries.
I use the same data set as the previous part.
Variables: country:
alcohol: Alcohol, recorded per capita consumption in litres of pure alcohol of people aged 15+ (nominal)
BMI: Average Body Mass Index of entire population (nominal)
total expenditure: total expenditure on health as a percentage of Gross Domestic Product per capita (%) (ratio)
GDP: Gross Domestic Product per capita (in USD) (nominal) S
tatus: Developed or Developing status (categorical)
Schooling: Number of years of Schooling (years) (nominal)
fit <- lm(Life.expectancy ~ Alcohol + BMI + Total.expenditure + GDP + Development + Schooling, data = my.data)
summary(fit)
##
## Call:
## lm(formula = Life.expectancy ~ Alcohol + BMI + Total.expenditure +
## GDP + Development + Schooling, data = my.data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -25.9848 -3.2022 0.4901 3.8236 30.6806
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.587e+01 5.687e-01 80.655 < 2e-16 ***
## Alcohol -3.159e-01 4.119e-02 -7.669 2.54e-14 ***
## BMI 1.103e-01 7.598e-03 14.518 < 2e-16 ***
## Total.expenditure -1.145e-01 5.463e-02 -2.096 0.0362 *
## GDP 7.633e-05 1.009e-05 7.563 5.66e-14 ***
## Development 3.595e+00 4.382e-01 8.204 3.81e-16 ***
## Schooling 1.666e+00 5.368e-02 31.041 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.917 on 2301 degrees of freedom
## (630 observations deleted due to missingness)
## Multiple R-squared: 0.6288, Adjusted R-squared: 0.6279
## F-statistic: 649.7 on 6 and 2301 DF, p-value: < 2.2e-16
For all variables p-values are extremely significant.
Alcohol: For each unit of alcohol consumption that increases, life expectancy decreases by 0.316 years.
BMI: For each unit increase in BMI, life expectancy increases by 0.110 years.
Total expenditure: for each unit increase in total expenditure o n health, life expectancy decreases by 0.115 years.
GDP: For each unit increase in GDP, life expectancy in creases by 0.00007633 years ( 40 minutes).
Development: Living in a developed country is associated with an increase in life expectancy for 3.595 years.
Schooling: Each additional year of schooling increases life expectancy for 1.666 years.
R-squared: Approximately 62.88% of the variance in life expectancy is explained by the model. The adjusted R-squared accounts for the number of predictors in the model, providing a more reliable measure when adding or removing predictors.
#Checking for normality of residuals with Shapiro-Wilk Test
regression_model <- fit
regression_model
##
## Call:
## lm(formula = Life.expectancy ~ Alcohol + BMI + Total.expenditure +
## GDP + Development + Schooling, data = my.data)
##
## Coefficients:
## (Intercept) Alcohol BMI Total.expenditure
## 4.587e+01 -3.159e-01 1.103e-01 -1.145e-01
## GDP Development Schooling
## 7.633e-05 3.595e+00 1.666e+00
There are some noticable outliers between 20-30 (residuals), 50 (fitted values)
shapiro.test(regression_model$residuals) #perfoming Shapiro-Wilk Test to check normality of residuals
##
## Shapiro-Wilk normality test
##
## data: regression_model$residuals
## W = 0.96419, p-value < 2.2e-16
P-value is extremely low, residuals aren’t normally distributed, meaning that normality in the context of linear regression is violated.
Testing heteroscedasticity:
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
test_result <- bptest(fit) #Breusch-Pegan test for heteroscedasticity
print(test_result)
##
## studentized Breusch-Pagan test
##
## data: fit
## BP = 258.35, df = 6, p-value < 2.2e-16
P-value is extremely small meaning there is evidence of heteroscedasticity in the regression model.