##Task 1
packages <- c("tidyverse", "infer", "fst")
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Warning: package 'infer' was built under R version 4.3.2
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "infer" "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [7] "readr" "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "fst" "infer" "lubridate" "forcats" "stringr" "dplyr"
## [7] "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [13] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
## [19] "base"
ess <- read_fst("All-ESS-Data.fst")
CH_data <- ess %>%
filter(cntry == "CH") %>%
mutate(
clsprty = case_when(
clsprty == 1 ~ "Yes",
clsprty == 2 ~ "No",
clsprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(clsprty)
),
trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt),
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
),
gndr = case_when(
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
gndr == 9 ~ NA_character_,
TRUE ~ as.character(gndr)
),
brncntr = case_when(
brncntr == 1 ~ "Yes",
brncntr == 2 ~ "No",
brncntr == 7 ~ NA_character_,
brncntr == 8 ~ NA_character_,
brncntr == 9 ~ NA_character_,
TRUE ~ as.character(brncntr)
),
)
CH_data <- CH_data %>% filter(!is.na(clsprty))
CH_data <- CH_data %>% filter(!is.na(trstplt))
CH_data <- CH_data %>% filter(!is.na(educ_level))
CH_data <- CH_data %>% filter(!is.na(brncntr))
unique(CH_data$clsprty)
## [1] "Yes" "No"
unique(CH_data$trstplt)
## [1] 5 6 3 8 4 2 7 0 1 9 10
unique(CH_data$educ_level)
## [1] "BA" "No BA"
unique(CH_data$brncntr)
## [1] "No" "Yes"
table(CH_data$educ_level)
##
## BA No BA
## 3712 12418
table(CH_data$brncntr)
##
## No Yes
## 3430 12700
tab_dat <- CH_data
mytab <- table(tab_dat$brncntr, tab_dat$educ_level)
rsums <- rowSums(mytab)
csums <- colSums(mytab)
N <- sum(mytab)
ptab <- tcrossprod(rsums/N, csums/N)
cat("Table of Expected Proportions:\n")
## Table of Expected Proportions:
print(ptab)
## [,1] [,2]
## [1,] 0.04893655 0.1637107
## [2,] 0.18119364 0.6061591
ftab <- N * ptab
cat("Table of Expected Frequencies:\n")
## Table of Expected Frequencies:
print(ftab)
## [,1] [,2]
## [1,] 789.3466 2640.653
## [2,] 2922.6534 9777.347
alpha <- 0.05
c_val <- qchisq(alpha, df = 1, lower.tail = FALSE)
cat("Critical Value:", round(c_val, 3), "\n")
## Critical Value: 3.841
test_stat <- sum((mytab - ftab)^2 / ftab)
cat("Pearson's X^2:", round(test_stat, 4), "\n")
## Pearson's X^2: 68.2084
p_val <- pchisq(test_stat, df = 1, lower.tail = F)
cat("p-value:", round(p_val, 4), "\n")
## p-value: 0
cat("Chi-squared test result:\n")
## Chi-squared test result:
print(chisq.test(mytab, correct = F))
##
## Pearson's Chi-squared test
##
## data: mytab
## X-squared = 68.208, df = 1, p-value < 2.2e-16
The p-value of 2.2e-16 means that we are able to reject the null hypothesis that the two variables of born in country and education level are independent of each other. In other words, there’s strong evidence to suggest that there’s a significant association between the two variables in mytab. The x-squared value further proved this point as the value would tend to be 0 if the two variables are independent. ##Task 2
NL_data <- ess %>%
filter(cntry == "NL") %>%
mutate(
domicil = case_when(
domicil == 1 ~ "Urban",
domicil == 2 ~ "Urban",
domicil == 3 ~ "Rural",
domicil == 4 ~ "Rural",
domicil == 5 ~ "Rural",
domicil %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(domicil)
),
gincdif = case_when(
gincdif == 1 ~ "Agree strongly",
gincdif == 2 ~ "Agree",
gincdif == 3 ~ "Neither agree nor disagree",
gincdif == 4 ~ "Disagree",
gincdif == 5 ~ "Disagree strongly",
gincdif %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(gincdif)
),
)
test_stat <- NL_data %>%
specify(explanatory = domicil,
response = gincdif) %>%
hypothesize(null = "independence") %>%
calculate(stat = "Chisq")
## Warning: Removed 198 rows containing missing values.
print(test_stat$stat)
## X-squared
## 8.176048
null_distribution <- NL_data %>%
specify(explanatory = domicil,
response = gincdif) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "Chisq")
## Warning: Removed 198 rows containing missing values.
p_val <- null_distribution %>%
get_pvalue(obs_stat = test_stat, direction = "two-sided")
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0.18
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided")
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution
## Response: gincdif (factor)
## Explanatory: domicil (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 5.67
## 2 2 4.85
## 3 3 3.23
## 4 4 2.94
## 5 5 2.85
## 6 6 1.69
## 7 7 1.89
## 8 8 6.01
## 9 9 6.72
## 10 10 2.67
## # ℹ 990 more rows
conf_int <- null_distribution %>%
get_confidence_interval(level = 0.95, type = "percentile")
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided") +
shade_confidence_interval(endpoints = conf_int)
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution
## Response: gincdif (factor)
## Explanatory: domicil (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 5.67
## 2 2 4.85
## 3 3 3.23
## 4 4 2.94
## 5 5 2.85
## 6 6 1.69
## 7 7 1.89
## 8 8 6.01
## 9 9 6.72
## 10 10 2.67
## # ℹ 990 more rows
The p-value here is 0.164 which is greater than 0.05, therefore I cannot reject the null hypothesis. In other words it is not evident to prove the gincdif value and domicil value are stongly related. The x squared value of 8.17 could also prove this point. ##Task 3 My research question: Are there relationships between the gender of people and the place people live in the Czech Republic?
CZ_data <- ess %>%
filter(cntry == "CZ") %>%
mutate(
gndr = case_when(
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
gndr == 9 ~ NA_character_,
TRUE ~ as.character(gndr)
),
domicil = case_when(
domicil == 1 ~ "Urban",
domicil == 2 ~ "Urban",
domicil == 3 ~ "Rural",
domicil == 4 ~ "Rural",
domicil == 5 ~ "Rural",
domicil %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(domicil)
),
)
CZ_stat <- CZ_data %>%
specify(explanatory = gndr,
response = domicil,
success = "Urban") %>%
hypothesize(null = "independence") %>%
calculate(stat = "Chisq")
## Warning: Removed 171 rows containing missing values.
print(CZ_stat$stat)
## X-squared
## 0.005506999
null_distribution <- CZ_data %>%
specify(explanatory = gndr,
response = domicil,
success = "Urban") %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "Chisq")
## Warning: Removed 171 rows containing missing values.
p_val <- null_distribution %>%
get_pvalue(obs_stat = CZ_stat, direction = "two-sided")
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0.124
null_distribution %>%
visualize() +
shade_p_value(obs_stat = CZ_stat, direction = "two-sided")
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution
## Response: domicil (factor)
## Explanatory: gndr (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.801
## 2 2 1.66
## 3 3 0.143
## 4 4 0.980
## 5 5 0.0109
## 6 6 2.26
## 7 7 5.12
## 8 8 0.604
## 9 9 0.101
## 10 10 0.980
## # ℹ 990 more rows
conf_int <- null_distribution %>%
get_confidence_interval(level = 0.95, type = "percentile")
null_distribution %>%
visualize() +
shade_p_value(obs_stat = CZ_stat, direction = "two-sided") +
shade_confidence_interval(endpoints = conf_int)
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution
## Response: domicil (factor)
## Explanatory: gndr (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.801
## 2 2 1.66
## 3 3 0.143
## 4 4 0.980
## 5 5 0.0109
## 6 6 2.26
## 7 7 5.12
## 8 8 0.604
## 9 9 0.101
## 10 10 0.980
## # ℹ 990 more rows
My p-value here is 0.118 which is greater than the value of 0.05. This could represent that we cannot reject the null hypothesis. I.e., it is not evident to prove that the gender of people live in the Czech republic are related to where they live. The x^2 value could also prove this point similarly.