1 Research question:
“How the death rate in a country can be explained by other variables?”
PCA was used to adress the research question and identify whether there are patterns and relationships among variables in the dataset.
1.1 Unit and sample size:
Unit: one country
Sample size: 204
1.2 Data:
Data: The World Bank, Health Nutrition and Population Statistics
Source: https://databank.worldbank.org/source/health-nutrition-and-population-statistics#
library(readxl)
Homework <- read_excel("Homework.xlsx")
mydata <- read_xlsx("Homework.xlsx")
colnames(mydata) <- c("ID", "Death", "Curexpend", "Domexpend", "Tobacco", "Overweight")
head(mydata) #showing first 6 units
## # A tibble: 6 × 6
## ID Death Curexpend Domexpend Tobacco Overweight
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 8.25 8.57 0.469 29.1 19.3
## 2 2 9.94 6.42 2.80 15.9 25.9
## 3 3 12.5 3.51 0.676 8.78 25.1
## 4 4 7.03 6.24 2.38 28 53.3
## 5 5 4.75 5.12 3.56 21.5 57.4
## 6 7 2.8 7.88 5.88 34 62.5
1.3 Description of dataset:
ID - identificator variable
Death - death rate, crude (per 1,000 people)
Curexpend - current health expenditure (% of GDP)
Domexpend - domestic general government health expenditure (% of GDP)
Tobacco - prevalence of current tobacco use (% of adults)
Overweight - prevalence of overweight (% of adults)
library(psych)
describe(mydata[ , -1]) #showing descriptive statistics
## vars n mean sd median trimmed mad min max range skew
## Death 1 204 8.28 2.93 7.89 8.11 2.64 1.26 18.64 17.38 0.60
## Curexpend 2 204 6.41 2.79 5.62 6.19 2.45 1.85 16.20 14.34 0.85
## Domexpend 3 204 3.39 2.37 2.78 3.15 2.18 0.19 13.19 13.00 0.96
## Tobacco 4 204 24.36 9.97 25.60 23.97 9.93 4.90 55.50 50.60 0.36
## Overweight 5 204 44.36 16.99 50.90 44.38 16.46 14.70 87.40 72.70 -0.09
## kurtosis se
## Death 0.75 0.21
## Curexpend 0.58 0.20
## Domexpend 0.51 0.17
## Tobacco -0.03 0.70
## Overweight -1.12 1.19
Explanation of few estimates of parameters:
The mean value of Death rate is 8.28. The standard deviation (spread or dispersion of values around the mean) of Curexpend is 2.79. The minimum value observed in the dataset for Death is 1,26.
fit <- lm(Death ~ Curexpend + Domexpend + Tobacco + Overweight, data = mydata ) #fitting linnear regression model
library(car)
## Warning: pakiet 'car' został zbudowany w wersji R 4.3.2
## Ładowanie wymaganego pakietu: carData
## Warning: pakiet 'carData' został zbudowany w wersji R 4.3.2
##
## Dołączanie pakietu: 'car'
## Następujący obiekt został zakryty z 'package:psych':
##
## logit
vif (fit) #checking multicollinearity among explanatory variables
## Curexpend Domexpend Tobacco Overweight
## 2.807229 3.877238 1.146491 1.653844
There is correlation between variables, requirement is met (correlation at least 0,3).
mean(vif(fit)) #mean VIF value
## [1] 2.3712
mydata_PCA <- mydata[, c("Curexpend", "Domexpend", "Tobacco", "Overweight")]
library(pastecs)
## Warning: pakiet 'pastecs' został zbudowany w wersji R 4.3.2
round(stat.desc(mydata_PCA, basic = FALSE), 2) #descriptive statistics for the variables selected for PCA
## Curexpend Domexpend Tobacco Overweight
## median 5.62 2.78 25.60 50.90
## mean 6.41 3.39 24.36 44.36
## SE.mean 0.20 0.17 0.70 1.19
## CI.mean.0.95 0.38 0.33 1.38 2.35
## var 7.77 5.62 99.39 288.64
## std.dev 2.79 2.37 9.97 16.99
## coef.var 0.43 0.70 0.41 0.38
Explanation of few estimates of parameters:
The median ‘Curexpend’ is 5.62, meaning that 50% of the observations have ‘Curexpend’ values less than or equal to 5.62. The 95% confidence interval of the mean for Domexpend is 0,33 - it’s a range within which we can be 95% confident that the population mean lies. The coefficient of variation is the highest for ‘Domexpend’ (0.70) - the relative variability is the greatest for this variable.
R <- cor(mydata_PCA) #correlation matrix
round(R, 3)
## Curexpend Domexpend Tobacco Overweight
## Curexpend 1.000 0.798 0.223 0.446
## Domexpend 0.798 1.000 0.346 0.623
## Tobacco 0.223 0.346 1.000 0.232
## Overweight 0.446 0.623 0.232 1.000
All correlations are positive. The strongest correlation is between Curexpend and Domexpend (0,798).
library(psych)
cortest.bartlett(R, n = nrow(mydata_PCA)) #Bartlett's test of sphericity to check if variables are correlated
## $chisq
## [1] 331.7102
##
## $p.value
## [1] 1.299317e-68
##
## $df
## [1] 6
H0: Population correlation matrix is equal to identity matrix.
H1: Population correlation matrix is not equal to identity matrix.
We reject H0 at p<0,001 -> There is correlation between variables.
det(R) #determinant of the correlation matrix
## [1] 0.1917297
det(R) should be close to zero, here it’s a bit too high but still acceptable (closer to 0 than to 1).
library(psych)
KMO(R) #Kaiser-Meyer-Olkin measure to check sampling adequacy
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = R)
## Overall MSA = 0.63
## MSA for each item =
## Curexpend Domexpend Tobacco Overweight
## 0.61 0.58 0.76 0.73
MSA is 0,63 - data is mediocre and acceptable. Individually, data for variable Tobacco is of the highest adequacy (0,76).
#install.packages("FactoMineR")
library(FactoMineR)
## Warning: pakiet 'FactoMineR' został zbudowany w wersji R 4.3.2
components <- PCA(mydata_PCA, scale.unit = TRUE, graph = FALSE) #performing PCA
library(factoextra)
## Warning: pakiet 'factoextra' został zbudowany w wersji R 4.3.2
## Ładowanie wymaganego pakietu: ggplot2
##
## Dołączanie pakietu: 'ggplot2'
## Następujące obiekty zostały zakryte z 'package:psych':
##
## %+%, alpha
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
get_eigenvalue(components) #eigenvalues of principal components
## eigenvalue variance.percent cumulative.variance.percent
## Dim.1 2.4107899 60.269748 60.26975
## Dim.2 0.8525004 21.312511 81.58226
## Dim.3 0.5742558 14.356395 95.93865
## Dim.4 0.1624538 4.061345 100.00000
The first principal component has 2,41 times more information than initial variable.
library("factoextra")
fviz_eig(components, choice = "eigenvalue", main = "Scree plot", ylab = "Eigenvalue", xlab = "Principal component", addlabels = TRUE) #scree plot
The slope changes the most between first and second principal component,
this suggests we should create 1 principal component.
library(psych)
fa.parallel(mydata_PCA, sim = FALSE, fa = "pc") #parallel analysis to determine the number of factors
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect. Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected. Examine the results carefully
## Parallel analysis suggests that the number of factors = NA and the number of components = 1
Parallel analysis suggests that the number of factors = NA and the number of components = 1
library(FactoMineR)
components <- PCA(mydata_PCA, scale.unit = TRUE, graph = FALSE, ncp = 1) #examining PCA results
components
## **Results for the Principal Component Analysis (PCA)**
## The analysis was performed on 204 individuals, described by 4 variables
## *The results are available in the following objects:
##
## name description
## 1 "$eig" "eigenvalues"
## 2 "$var" "results for the variables"
## 3 "$var$coord" "coord. for the variables"
## 4 "$var$cor" "correlations variables - dimensions"
## 5 "$var$cos2" "cos2 for the variables"
## 6 "$var$contrib" "contributions of the variables"
## 7 "$ind" "results for the individuals"
## 8 "$ind$coord" "coord. for the individuals"
## 9 "$ind$cos2" "cos2 for the individuals"
## 10 "$ind$contrib" "contributions of the individuals"
## 11 "$call" "summary statistics"
## 12 "$call$centre" "mean of the variables"
## 13 "$call$ecart.type" "standard error of the variables"
## 14 "$call$row.w" "weights for the individuals"
## 15 "$call$col.w" "weights for the variables"
print(components$var$cor) #correlation coefficients between original variables and principal components
## Dim.1
## Curexpend -0.8470300
## Domexpend -0.9352176
## Tobacco -0.4888318
## Overweight -0.7614077
Each correlation (relationship) is negative and inverse.
print(components$var$contrib) #contribution of each original variable to each principal component
## Dim.1
## Curexpend 29.760361
## Domexpend 36.279891
## Tobacco 9.911961
## Overweight 24.047787
Curexpend contributes 29,76% to the total variance explained by the principal component. Domexpend contributes in 36,28%, Tobacco in 9,91% and Overweight in 24,05%.
head(components$ind$coord) #coordinates of the observations on the principal components
## Dim.1
## 1 0.89573064
## 2 0.95081556
## 3 2.31109247
## 4 -0.08237950
## 5 -0.07487448
## 6 -1.75161652
mydata$PC1 <- components$ind$coord[ , 1] #coordinates of observations on the first principal component
head(mydata, 3)
## # A tibble: 3 × 7
## ID Death Curexpend Domexpend Tobacco Overweight PC1
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 8.25 8.57 0.469 29.1 19.3 0.896
## 2 2 9.94 6.42 2.80 15.9 25.9 0.951
## 3 3 12.5 3.51 0.676 8.78 25.1 2.31
``` Conclusion
Principal component analysis was performed on 4 standardized variables (n = 204). The KMO measure confirms the appropriateness of the variables, KMO = 0.63, data is mediocre. The MSA statistics for the individual variables are above 0.50 for all four variables.Based on the parallel analysis (and other rules as well), it makes most sense to retain only the first principal component.