source:Global Trust Rate. (n.d.). Www.kaggle.com. Retrieved January 1, 2024, from https://www.kaggle.com/datasets/elmoallistair/global-trust-rate
Research question: Is there a significant difference in the level of trust among the general population towards different institutions and professions between countries? The institutions included in the survey were national government, scientists, journalists, doctors and nurses, NGO workers, and traditional healers?”
Opening csv file:
library(readr)
file_name <- "global_trust-rate.csv"
mydata <- read_csv(file_name, col_types = cols())
head(mydata)
## # A tibble: 6 × 8
## Country Neighbourhood Government Scientist Journalist `Doctor and Nurses`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Albania 50.7 43.6 49.2 53.5 69.0
## 2 Algeria 67.3 65.0 84.4 44.3 81.1
## 3 Argentina 75.9 43.5 89.4 52.8 92.5
## 4 Australia 92.2 74.5 93.8 71.3 98.1
## 5 Austria 88.4 73.9 95.7 75.4 97.0
## 6 Bahrain 64.3 NA 71.1 51.5 82.4
## # ℹ 2 more variables: Philantropist <dbl>, `Traditional Healers` <dbl>
#sample size 140 countries
#unit of observation: country
Description:
Country: Country where survey was conducted Neighbourhood: Trust in
Neighbourhood
Government: Trust in National Government in TCountry
Scientist: Trust in Scientists in This Country Journalist:Trust in
Journalists in This Country
Doctor and Nurses: Trust in Doctors and Nurses in This Country
Philanthropist: Trust in People Who Work at Charitable Organizations or
NGOs in This Country
Traditional Healers: Trust in Traditional Healers in This Country
Transforming wide data to long data:
library(rstatix)
##
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
##
## filter
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ purrr 1.0.2
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks rstatix::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
mydata_long <- mydata %>%
pivot_longer(
cols = c("Neighbourhood", "Government", "Scientist", "Journalist", "Doctor and Nurses", "Philantropist", "Traditional Healers"),
names_to = "System",
values_to = "Trust"
) %>%
convert_as_factor(System)
mydata_long <- as.data.frame(mydata_long)
tail(mydata_long, 10)
## Country System Trust
## 782 Zambia Doctor and Nurses 72.71
## 783 Zambia Philantropist 62.07
## 784 Zambia Traditional Healers 14.17
## 785 Zimbabwe Neighbourhood 64.96
## 786 Zimbabwe Government 67.77
## 787 Zimbabwe Scientist 73.22
## 788 Zimbabwe Journalist 65.99
## 789 Zimbabwe Doctor and Nurses 81.51
## 790 Zimbabwe Philantropist 83.90
## 791 Zimbabwe Traditional Healers 31.66
Identifying outliers with graph:
library(ggpubr)
ggboxplot(mydata_long,
x = "System",
y = "Trust",
add = "jitter") +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 12)) # Adjust angle and size as needed
## Warning: Removed 7 rows containing non-finite values (`stat_boxplot()`).
## Warning: Removed 7 rows containing missing values (`geom_point()`).
#here we can see that in Journalist boxplot there is one really small value, and some in Neighbouhood and Philatntropist seem away from others (also low values).
Identifying outliers:
library(tidyverse)
library(ggpubr)
library(rstatix)
mydata_long %>%
group_by(System) %>%
identify_outliers(Trust)
## # A tibble: 9 × 5
## System Country Trust is.outlier is.extreme
## <fct> <chr> <dbl> <lgl> <lgl>
## 1 Doctor and Nurses Cameroon 46.0 TRUE FALSE
## 2 Doctor and Nurses Congo Brazzaville 53.5 TRUE FALSE
## 3 Doctor and Nurses Gabon 53.8 TRUE FALSE
## 4 Doctor and Nurses Ivory Coast 54.3 TRUE FALSE
## 5 Journalist Greece 12.4 TRUE FALSE
## 6 Neighbourhood Benin 23.0 TRUE FALSE
## 7 Neighbourhood Gabon 28.2 TRUE FALSE
## 8 Neighbourhood Ivory Coast 31.2 TRUE FALSE
## 9 Philantropist Greece 29.0 TRUE FALSE
#Removing outliers identified in the table
library(dplyr)
mydata_long <- mydata_long %>%
filter(!(Country %in% c("Cameroon", "Congo Brazzaville", "Gabon", "Greece", "Benin", "Ivory Coast")))
#checking if there are still some outliers to be removed
mydata_long %>%
group_by(System) %>%
identify_outliers(Trust)
## # A tibble: 2 × 5
## System Country Trust is.outlier is.extreme
## <fct> <chr> <dbl> <lgl> <lgl>
## 1 Doctor and Nurses Guinea 56.4 TRUE FALSE
## 2 Journalist Serbia 29.0 TRUE FALSE
#removing Serbia and Guinea
library(dplyr)
mydata_long <- mydata_long %>%
filter(!(Country %in% c("Serbia", "Guinea")))
#Checking again for outliers
mydata_long %>%
group_by(System) %>%
identify_outliers(Trust)
## [1] System Country Trust is.outlier is.extreme
## <0 rows> (or 0-length row.names)
#No more outliers to remove
Normality check:
library(rstatix)
mydata_long %>%
group_by(System) %>%
shapiro_test(Trust)
## # A tibble: 7 × 4
## System variable statistic p
## <fct> <chr> <dbl> <dbl>
## 1 Doctor and Nurses Trust 0.929 0.0000288
## 2 Government Trust 0.977 0.0749
## 3 Journalist Trust 0.991 0.758
## 4 Neighbourhood Trust 0.959 0.00244
## 5 Philantropist Trust 0.978 0.0802
## 6 Scientist Trust 0.906 0.00000159
## 7 Traditional Healers Trust 0.979 0.0871
H0: Variable trust is normally distributed
H1: Variable is not normally distributed
In the case of Government trust, trust in traditional healers and Journalist the variable is normally distributed as the p value is less than 0.05, however in all other cases we have to reject HO and we should use a non parametric test, Friedman Anova
Descriptive statistics:
library(rstatix)
mydata_long %>%
group_by(System) %>%
get_summary_stats(Trust, type = "common")
## # A tibble: 7 × 11
## System variable n min max median iqr mean sd se ci
## <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Doctor and Nu… Trust 105 62.3 99.4 88.4 15.1 86.4 9.74 0.951 1.88
## 2 Government Trust 99 17.8 97.4 55.2 28.0 57.9 19.8 1.99 3.95
## 3 Journalist Trust 104 31.1 90.8 61.5 15.8 61.3 13.1 1.29 2.55
## 4 Neighbourhood Trust 105 42.5 96.1 77.5 19.5 75.8 12.7 1.24 2.47
## 5 Philantropist Trust 105 44.1 94.0 72.1 17.5 71.4 11.8 1.15 2.27
## 6 Scientist Trust 105 47.4 98.2 84.7 21.5 81.4 13.3 1.29 2.57
## 7 Traditional H… Trust 105 8.54 92.5 46.7 29.2 45.8 20.4 1.99 3.95
#From the descriptive statistics we can see that:
#maximum value for Doctors and Nurses is 99.4 and minimum value was
62.27, mean is 86.4 and median 88.43
#maximum value for Government is 97.45 and minimum value was 17.75, mean
is 57.93 and median 55.24. #maximum value for Journalist is 90.83 and
minimum value was 31.07, mean is 61.30 and median 61.51
#maximum value for Neigbourhood is 96.13 and minimum value was 42.47,
mean is 75.84 and median 77.54
#maximum value for Philantropist is 94.02 and minimum value was 44.08,
mean is 71.41 and median 72.12
#maximum value for Scientist is 98.17 and minimum value was 47.41, mean
is 81.43 and median 84.69
#maximum value for Traditional Healers is 92.47 and minimum value was
8.54, mean is 45.77 and median 46.74
Checking for missing values:
# Identify rows with any missing values
rows_with_na <- rowSums(is.na(mydata_long)) > 0
rows_with_missing_values <- mydata_long[rows_with_na, ]
# View the rows with missing values
print(rows_with_missing_values)
## Country System Trust
## 37 Bahrain Government NA
## 114 China Government NA
## 548 Saudi Arabia Government NA
## 550 Saudi Arabia Journalist NA
## 625 Tajikistan Government NA
## 674 United Arab Emirates Government NA
## 716 Vietnam Government NA
# Removing countries with missing values
library(dplyr)
mydata_long <- mydata_long %>%
filter(!(Country %in% c("Bahrain", "China", "Saudi Arabia", "Tajikistan", "United Arab Emirates", "Vietnam")))
#Check again for missing values
rows_with_na <- rowSums(is.na(mydata_long)) > 0
rows_with_missing_values <- mydata_long[rows_with_na, ]
# View the rows with missing values
print(rows_with_missing_values)
## [1] Country System Trust
## <0 rows> (or 0-length row.names)
#No more missing values that have to be removed
Repeated ANOVA - parametric test.
library(rstatix)
ANOVA_results <- anova_test(dv = Trust,
wid = Country,
within = System,
data = mydata_long)
ANOVA_results
## ANOVA Table (type III tests)
##
## $ANOVA
## Effect DFn DFd F p p<.05 ges
## 1 System 6 588 177.655 1.63e-128 * 0.458
##
## $`Mauchly's Test for Sphericity`
## Effect W p p<.05
## 1 System 0.046 1.2e-50 *
##
## $`Sphericity Corrections`
## Effect GGe DF[GG] p[GG] p[GG]<.05 HFe DF[HF] p[HF]
## 1 System 0.532 3.19, 312.56 1.13e-69 * 0.552 3.31, 324.3 3.48e-72
## p[HF]<.05
## 1 *
Sphericity is not met, we reject H0 at p value < 0.001. ,
so we use P value with sphericity corrections where it is 1.13e-69.
H0: all 4 means are equal
H1: at least one mean is different
and we can reject H0 at p value < 0.001.
Get eta squared:
get_anova_table(ANOVA_results, correction = "auto")
## ANOVA Table (type III tests)
##
## Effect DFn DFd F p p<.05 ges
## 1 System 3.19 312.56 177.655 1.13e-69 * 0.458
eta squared = 0.458
Interpret eta squared with effect size Cohen 1992:
library(effectsize)
##
## Attaching package: 'effectsize'
## The following objects are masked from 'package:rstatix':
##
## cohens_d, eta_squared
interpret_eta_squared(0.369, rules = "cohen1992")
## [1] "large"
## (Rules: cohen1992)
Here we interpreted eta squared with Cohen 1992 and found out that the difference between averages is large.
POST HOC test:
library(rstatix)
pwc <- mydata_long %>%
pairwise_t_test(Trust ~ System,
paired = TRUE,
p.adjust.method = "bonferroni")
pwc
## # A tibble: 21 × 10
## .y. group1 group2 n1 n2 statistic df p p.adj
## * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 Trust Doctor and Nurses Govern… 99 99 17.8 98 2.09e-32 4.39e-31
## 2 Trust Doctor and Nurses Journa… 99 99 21.7 98 2.76e-39 5.80e-38
## 3 Trust Doctor and Nurses Neighb… 99 99 11.8 98 1.39e-20 2.92e-19
## 4 Trust Doctor and Nurses Philan… 99 99 18.1 98 6.30e-33 1.32e-31
## 5 Trust Doctor and Nurses Scient… 99 99 6.86 98 6.21e-10 1.3 e- 8
## 6 Trust Doctor and Nurses Tradit… 99 99 22.0 98 9.84e-40 2.07e-38
## 7 Trust Government Journa… 99 99 -2.36 98 2 e- 2 4.3 e- 1
## 8 Trust Government Neighb… 99 99 -10.0 98 1.12e-16 2.35e-15
## 9 Trust Government Philan… 99 99 -8.36 98 4.28e-13 8.99e-12
## 10 Trust Government Scient… 99 99 -12.9 98 8.65e-23 1.82e-21
## # ℹ 11 more rows
## # ℹ 1 more variable: p.adj.signif <chr>
#here we listed all possible pairs to see the differences
Friedman ANOVA - non parametric test:
library(rstatix)
FriedmanANOVA <- friedman_test(Trust ~ System | Country,
data = mydata_long)
FriedmanANOVA
## # A tibble: 1 × 6
## .y. n statistic df p method
## * <chr> <int> <dbl> <dbl> <dbl> <chr>
## 1 Trust 99 394. 6 4.27e-82 Friedman test
Friedman ANOVA - nonparametric test
H0: location distributions for trust in all 7 systems is the same H1: at least one of location distributions of trust from all 7 systems is different
p value < 0.001 and we can rejet H0.
Effect size: Kendall W - Landis, 1977
library(effectsize)
effectsize::kendalls_w(Trust ~ System | Country,
data = mydata_long)
## Kendall's W | 95% CI
## --------------------------
## 0.66 | [0.62, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
I used Landis & Koch (1977) table to interpret effect size of 0.63.
interpret_kendalls_w(0.63)
## [1] "substantial agreement"
## (Rules: landis1977)
#Effect size interpretation is “substantial agreement”
POST HOC test:
Table of all possible pairs:
library(rstatix)
paires_nonpar <- wilcox_test(Trust ~ System,
paired = TRUE,
p.adjust.method = "bonferroni",
data = mydata_long)
paires_nonpar
## # A tibble: 21 × 9
## .y. group1 group2 n1 n2 statistic p p.adj p.adj.signif
## * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
## 1 Trust Doctor and… Gover… 99 99 4937 8.60e-18 1.81e-16 ****
## 2 Trust Doctor and… Journ… 99 99 4950 5.78e-18 1.21e-16 ****
## 3 Trust Doctor and… Neigh… 99 99 4741 2.63e-15 5.52e-14 ****
## 4 Trust Doctor and… Phila… 99 99 4932 1 e-17 2.10e-16 ****
## 5 Trust Doctor and… Scien… 99 99 4236 8.01e-10 1.68e- 8 ****
## 6 Trust Doctor and… Tradi… 99 99 4949 5.96e-18 1.25e-16 ****
## 7 Trust Government Journ… 99 99 1778. 1.5 e- 2 3.17e- 1 ns
## 8 Trust Government Neigh… 99 99 386 3.11e-13 6.53e-12 ****
## 9 Trust Government Phila… 99 99 580 3.78e-11 7.94e-10 ****
## 10 Trust Government Scien… 99 99 150. 5 e-16 1.05e-14 ****
## # ℹ 11 more rows
From the p ajdusted we can see that all p values are less than 0.001. ,except for the differences between the trust in Journalist and Government where p value is 0.317 which means that there is no significant difference between location of distribution between trust in Journalist and trust Government.
Graph to show the differences between the trust in each category:
library(rstatix)
library(ggpubr)
comparisons <- paires_nonpar %>%
add_y_position(fun = "median", step.increase = 0.35)
ggboxplot(mydata_long, x = "System", y = "Trust", add = "point") +
stat_pvalue_manual(comparisons, hide.ns = FALSE) +
stat_summary(
fun = median,
geom = "point",
shape = 16,
size = 4,
aes(group = System),
color = "darkred",
position = position_dodge(width = 0.8)
) +
stat_summary(
fun = median,
geom = "text",
vjust = 0.5,
hjust = -0.8,
aes(label = after_stat(y), group = System),
color = "darkred",
position = position_dodge(width = 0.8)
) +
labs(subtitle = get_test_label(FriedmanANOVA, detailed = TRUE), caption = get_pwc_label(comparisons)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1, size = 10))
#conclusion #Based on the sample data, we reject the null hypothesis about the equality of the locations of the seven distributions (p < 0.001) and accept the conclusion that at least one distribution location differs from the others. We also found substantial agreement effect size, expressed by Landis & Koch (1977), of 0.63. When comparing pairs of variables, we find that all distribution differs (p < 0.001), except for the differences between the trust in Journalist and Government where p value is 0.317 which means that there is no significant difference between the locations of distribution between trust in Journalist and trust Government.