library(psych)
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
library(rstatix)
##
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
##
## filter
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
## The following object is masked from 'package:psych':
##
## logit
library(Rmisc)
## Loading required package: lattice
## Loading required package: plyr
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:rstatix':
##
## desc, mutate
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
library(olsrr)
##
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
##
## rivers
#WIKIPEDIA WORLD STATISTICS 2023
#the sample includes data for 142 countries. For the purpose of this homework, the variables that were used for the analysis are: human development index (HDI) and UN continental region for the first research question and number of internet users(DelezUporabnikov), HDI and population change for the second.
#source of data for both research questions: https://www.kaggle.com/datasets/jiteshkumarsahoo/wikipedia-country-statistics-2023
x<-read.csv2("Country_data.csv", sep = ",", dec = ".", header = T)
####1. research question: Do countries in different continents differ in terms of the HDI indicator?
x$UN.Continental.Region<-as.factor(x$UN.Continental.Region)
describeBy(x$HDI, group = x$UN.Continental.Region)
##
## Descriptive statistics by group
## group: Africa
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 42 0.55 0.1 0.52 0.54 0.09 0.38 0.78 0.4 0.55 -0.55 0.02
## ------------------------------------------------------------
## group: Americas
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 30 0.74 0.08 0.75 0.75 0.07 0.54 0.94 0.4 -0.23 0.61 0.01
## ------------------------------------------------------------
## group: Asia
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 34 0.73 0.13 0.73 0.73 0.13 0.46 0.94 0.48 -0.22 -0.76 0.02
## ------------------------------------------------------------
## group: Europe
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 29 0.88 0.06 0.89 0.88 0.07 0.77 0.96 0.19 -0.25 -1.34 0.01
## ------------------------------------------------------------
## group: Oceania
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 7 0.65 0.06 0.64 0.65 0.02 0.56 0.73 0.17 0.14 -1.36 0.02
x%>%
group_by(UN.Continental.Region)%>%
#DESCRIPTIVE STATISTICS BY GROUP: the highest HDI can be found in Europe, the lowest in Asia. Average HDI for the Americas is 0.74. Countries in Oceania have the lowest representation with only 7 countries in the sample. The highest representation is of African countries (42).The middle value for Europe is 0.89, which is also the highest.
shapiro_test(HDI) #Shapiro Wilk test for checking normality. We accept null hypothesis in all cases - there is normal distribution of HDI within all groups.
## # A tibble: 5 × 4
## UN.Continental.Region variable statistic p
## <fct> <chr> <dbl> <dbl>
## 1 Africa HDI 0.950 0.0646
## 2 Americas HDI 0.978 0.784
## 3 Asia HDI 0.977 0.689
## 4 Europe HDI 0.933 0.0658
## 5 Oceania HDI 0.926 0.519
leveneTest(x$HDI,x$UN.Continental.Region) #Levene test for checking homoskedasticity. The variances are heterogeneous,therefore homogeneity of variances is violated.
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 4 4.4492 0.002065 **
## 137
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
results_groups<-summarySE(x, measurevar="HDI",groupvars="UN.Continental.Region", conf.interval=0.95)
results_groups
## UN.Continental.Region N HDI sd se ci
## 1 Africa 42 0.5463333 0.10311955 0.01591169 0.03213431
## 2 Americas 30 0.7441000 0.07843000 0.01431929 0.02928624
## 3 Asia 34 0.7265882 0.12619276 0.02164188 0.04403074
## 4 Europe 29 0.8795862 0.05941472 0.01103304 0.02260015
## 5 Oceania 7 0.6475714 0.05532587 0.02091121 0.05116790
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(results_groups, aes(x = UN.Continental.Region, y = HDI)) +
geom_point(size = 3, colour = "blue2")+
scale_y_continuous(limits = c(0,1)) +
geom_errorbar(aes(ymin = HDI - ci, ymax = HDI + ci), width = 0.1) +
xlab ("Continental Region") #Making scatterplot. Interpretation: all continents have relatively similar average HDI, the most similar are the Americas and Asia. All HDIs are above 0.5.

#parametric test ANOVA
results_anova <- aov(HDI ~ UN.Continental.Region, data = x)
summary (results_anova) #Interpretation: we reject H0 and accept H1 at p<0.001, at least one pair of continents has statistically significantly different average HDI.
## Df Sum Sq Mean Sq F value Pr(>F)
## UN.Continental.Region 4 2.026 0.5065 55.2 <2e-16 ***
## Residuals 137 1.257 0.0092
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
eta_squared(results_anova) #η² is 0.617, indicating a large effect (greater than 0.26).
## UN.Continental.Region
## 0.617104
#Pairwise t-test with bonferroni correction (comparing all pairs of variables).
pairwise_t_test(HDI ~ UN.Continental.Region,
paired = FALSE,
p.adjust.method ="bonferroni", data = x)
## # A tibble: 10 × 9
## .y. group1 group2 n1 n2 p p.signif p.adj p.adj.signif
## * <chr> <chr> <chr> <int> <int> <dbl> <chr> <dbl> <chr>
## 1 HDI Africa Americas 42 30 1.32e-14 **** 1.32e-13 ****
## 2 HDI Africa Asia 42 34 1.96e-13 **** 1.96e-12 ****
## 3 HDI Americas Asia 30 34 4.67e- 1 ns 1 e+ 0 ns
## 4 HDI Africa Europe 42 29 3.14e-29 **** 3.14e-28 ****
## 5 HDI Americas Europe 30 29 2.48e- 7 **** 2.48e- 6 ****
## 6 HDI Asia Europe 34 29 3.45e- 9 **** 3.45e- 8 ****
## 7 HDI Africa Oceania 42 7 1.07e- 2 * 1.07e- 1 ns
## 8 HDI Americas Oceania 30 7 1.77e- 2 * 1.77e- 1 ns
## 9 HDI Asia Oceania 34 7 4.89e- 2 * 4.89e- 1 ns
## 10 HDI Europe Oceania 29 7 5.52e- 8 **** 5.52e- 7 ****
####Based on the sample data and parametric test, we reject the null hypothesis of equality of the five arithmetic means at p<0.001.
#non-parametric Kruskal-Wallis test
kruskal.test(HDI ~ UN.Continental.Region, data = x) #statistically significant. At least one median is different.
##
## Kruskal-Wallis rank sum test
##
## data: HDI by UN.Continental.Region
## Kruskal-Wallis chi-squared = 88.288, df = 4, p-value < 2.2e-16
kruskal_effsize(HDI ~ UN.Continental.Region, data = x) #large effect
## # A tibble: 1 × 5
## .y. n effsize method magnitude
## * <chr> <int> <dbl> <chr> <ord>
## 1 HDI 142 0.615 eta2[H] large
wilcox_test(HDI ~ UN.Continental.Region,
paired = FALSE,
p.adjust.method ="bonferroni", data = x) #wilcox test with bonferroni correction
## # A tibble: 10 × 9
## .y. group1 group2 n1 n2 statistic p p.adj p.adj.signif
## * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
## 1 HDI Africa Americas 42 30 96 1.08e-11 1.08e-10 ****
## 2 HDI Africa Asia 42 34 204 1.37e- 8 1.37e- 7 ****
## 3 HDI Africa Europe 42 29 2 1.17e-19 1.17e-18 ****
## 4 HDI Africa Oceania 42 7 54 6 e- 3 6.1 e- 2 ns
## 5 HDI Americas Asia 30 34 548 6.16e- 1 1 e+ 0 ns
## 6 HDI Americas Europe 30 29 65 4.59e-10 4.59e- 9 ****
## 7 HDI Americas Oceania 30 7 178 3 e- 3 3.2 e- 2 *
## 8 HDI Asia Europe 34 29 134 1.28e- 7 1.28e- 6 ****
## 9 HDI Asia Oceania 34 7 168 9.3 e- 2 9.34e- 1 ns
## 10 HDI Europe Oceania 29 7 203 2.40e- 7 2.40e- 6 ****
####Based on the sample data and non-parametric test, we reject the null hypothesis of equality of the five medians at p<0.001, and conclude that the average HDI differs between continents. Using the group comparisons, we find that the average HDI of Africa differs from average HDI of Americas, Asia (p<0,001), the average HDI of Europe differs from average HDI of Africa, Americas, Asia, and Oceania (p<0,001), and the average HDI of Americas differ from the average HDI of Oceania (p=0,032).
##I find non-parametric test to be more appropriate due to the violation of homogeneity of variances, although there are no big differences between the tests.
####2. Research question: is there a relationship between the proportion of internet users and the HDI along with population change?
#I expect that both explanatory variables (HDI and population change) affect the dependent variable (internet users).HDI should affect the dependent variable positively: higher the HDI, more internet users. Population change should affect it negatively: higher the population change, less internet users.
x$DelezUporabnikov<-x$Internet.Users/x$Population.2023
x$DelezUporabnikov<-as.numeric(x$DelezUporabnikov)
z<-x[,c(6,19:20)]
describe(z)
## vars n mean sd median trimmed mad min max range
## HDI 1 142 0.70 0.15 0.72 0.71 0.17 0.38 0.96 0.58
## Population..Change 2 142 1.17 1.12 1.06 1.16 1.03 -3.64 4.98 8.62
## DelezUporabnikov 3 142 0.52 0.29 0.53 0.52 0.39 0.02 0.99 0.97
## skew kurtosis se
## HDI -0.21 -0.95 0.01
## Population..Change -0.15 1.84 0.09
## DelezUporabnikov -0.13 -1.39 0.02
#DESCRIPTIVE STATISTICS BY GROUP: group population change has the highest range (8.62). Also, the highest and lowest value belongs to the population change. All 3 distributions are skewed slightly to the left.Average HDI is 0.7.
shapiro_test(z$HDI) #non-normal distribution
## # A tibble: 1 × 3
## variable statistic p.value
## <chr> <dbl> <dbl>
## 1 z$HDI 0.968 0.00217
shapiro_test(z$Population..Change) #non-normal distribution
## # A tibble: 1 × 3
## variable statistic p.value
## <chr> <dbl> <dbl>
## 1 z$Population..Change 0.970 0.00314
shapiro_test(z$DelezUporabnikov) #non-normal distribution
## # A tibble: 1 × 3
## variable statistic p.value
## <chr> <dbl> <dbl>
## 1 z$DelezUporabnikov 0.929 0.00000146
scatterplotMatrix(z, smooth = F) #there are linear relationships between variables.

fit<-lm(DelezUporabnikov ~ HDI + Population..Change, data = z) #performing linear regression
summary(fit)
##
## Call:
## lm(formula = DelezUporabnikov ~ HDI + Population..Change, data = z)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.35190 -0.08515 -0.00545 0.08478 0.29698
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.52727 0.08262 -6.381 2.44e-09 ***
## HDI 1.54380 0.09976 15.475 < 2e-16 ***
## Population..Change -0.03483 0.01354 -2.573 0.0111 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1318 on 139 degrees of freedom
## Multiple R-squared: 0.8027, Adjusted R-squared: 0.7998
## F-statistic: 282.7 on 2 and 139 DF, p-value: < 2.2e-16
vif(fit) #variance inflation factor for checking multicolinearity. Both are relatively close to 1, which is not a problem - there is no multicolinearity.
## HDI Population..Change
## 1.880692 1.880692
z$stdResid <- round(rstandard(fit), 3)
z$CooksD <- round(cooks.distance (fit), 3)
hist(z$stdResid, xlab = "standardized residuals", ylab ="Frequency", main ="Histrogram of standardized residuals") #Histrogram of st. residuals to check distribution of residuals/errors.

shapiro_test(z$stdResid) #normal distribution of residuals from the histogram and the Shapiro test. No unit is greater than 3 or smaller than -3, indicating we do not have to remove any of them.
## # A tibble: 1 × 3
## variable statistic p.value
## <chr> <dbl> <dbl>
## 1 z$stdResid 0.993 0.665
hist(z$CooksD, xlab = "Cook's distances", ylab ="Frequency", main ="Histrogram of Cook's distances") #Cooks distances for identifying high impact units.

z$ID<-1:142
head(z[order(z$stdResid),c(4,6)], 4)
## stdResid ID
## 13 -2.683 13
## 87 -2.632 87
## 11 -2.595 11
## 69 -2.062 69
head(z[order(-z$stdResid),c(4,6)], 4)
## stdResid ID
## 26 2.270 26
## 67 2.076 67
## 138 2.062 138
## 109 1.988 109
head(z[order(-z$CooksD), c(5:6)], 8) #there is no unit above 1, but there is one that is significantly different from others and therefore has higher impact (81), so we remove it
## CooksD ID
## 81 0.092 81
## 109 0.033 109
## 50 0.031 50
## 87 0.027 87
## 26 0.026 26
## 136 0.024 136
## 13 0.023 13
## 65 0.018 65
z <- z %>%
filter(!ID %in% 81)
fit <- lm(DelezUporabnikov ~ HDI + Population..Change, data = z)
z$stdFitted <- scale(fit$fitted.values)
scatterplot(stdResid ~ stdFitted,
ylab = "Standardized residuals",
xlab = "standardized fitted values",
smooth = FALSE,
regLine = FALSE,
boxplots = FALSE,
data = z) #scatterplot to help us check the assumption of homoskedasticity.Looks like there is everything ok, but we can check with the test to be sure.

ols_test_breusch_pagan(fit) #Breusch Pagan test for checking heteroskedasticity. We accept null hypothesis, there is no heteroskedasticity.
##
## Breusch Pagan Test for Heteroskedasticity
## -----------------------------------------
## Ho: the variance is constant
## Ha: the variance is not constant
##
## Data
## --------------------------------------------
## Response : DelezUporabnikov
## Variables: fitted values of DelezUporabnikov
##
## Test Summary
## ------------------------------
## DF = 1
## Chi2 = 0.006765211
## Prob > Chi2 = 0.9344472
s<-summary(fit)
sqrt(s$r.squared)
## [1] 0.8962435
#Interpretation of linear regression: both predictors are statistically significant and affect the dependent variable. With an increase in HDI, the proportion of internet users also increases (when HDI increases by 1, internet users (DelezUporabnikov) increases by 1.544, p < 0.001). Conversely, with an increase in population change, the proportion of internet users decreases (when population change increases by 1, DelezUporabnikov decreases by 0.04,p < 0.01).
#80,27 % of the variability in the dependent variable internet users is explained by the independent variables population change and internet users (DelezUporabnikov) in the model - (coefficient of determination), which indicates strong overall relationship between the variables. Standard error of the prediction is 0.13.
#Nothing has been excluded from the model. Homoscedasticity and normality of the distribution of residuals are acceptable; therefore, we used parametric linear regression.