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.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ 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
## [[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")
switzerland_data <- ess %>%
# Filter for Switzerland
filter(cntry == "CH")
# Re-coding education
switzerland_data <- switzerland_data %>%
mutate(
# Set 'other' to NA for rounds 1-4
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
# Set 'other' to NA for rounds 5 and later
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
# Create educ_level column
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
# View the table of educ_level
table(switzerland_data$educ_level)
##
## BA No BA
## 3856 13069
# recoding brncntr for Switzerland
switzerland_data <- switzerland_data %>%
mutate(born_in_country = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_))
table(switzerland_data$born_in_country)
##
## No Yes
## 3867 13056
table(switzerland_data$brncntr)
##
## 1 2 7 8
## 13056 3867 1 1
tab_dat <- switzerland_data
tab_dat <- switzerland_data
# Create a crosstable and save it into the object mytab
mytab <- table(tab_dat$brncntr, tab_dat$educ_level)
# Calculate row-wise sums of counts in table
rsums <- rowSums(mytab)
# Calculate column-wise sums of counts in table
csums <- colSums(mytab)
# Get the total count of the table (N)
N <- sum(mytab)
# Generate table of expected proportions
ptab <- tcrossprod(rsums/N, csums/N)
cat("Table of Expected Proportions:\n")
## Table of Expected Proportions:
print(ptab)
## [,1] [,2]
## [1,] 1.757478e-01 5.956555e-01
## [2,] 5.205397e-02 1.764246e-01
## [3,] 1.346107e-05 4.562312e-05
## [4,] 1.346107e-05 4.562312e-05
# Table of frequencies
ftab <- N * ptab
cat("Table of Expected Frequencies:\n")
## Table of Expected Frequencies:
print(ftab)
## [,1] [,2]
## [1,] 2974.5309306 1.008147e+04
## [2,] 881.0134121 2.985987e+03
## [3,] 0.2278287 7.721713e-01
## [4,] 0.2278287 7.721713e-01
# Critical value
alpha <- 0.05 # significance level
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: 65.7174
# p-value:
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))
## Warning in chisq.test(mytab, correct = F): Chi-squared approximation may be
## incorrect
##
## Pearson's Chi-squared test
##
## data: mytab
## X-squared = 65.717, df = 3, p-value = 3.523e-14
The obseved X-squared value (65.717) is quite a bit larger 3.841, proving a statistically significant association between the 2 variables of brncntr and education level.The p-value is small. The large chi-squared value and the small p-value indicate that there’s a significant association between the 2 variables.
Task 2
netherlands_data <- ess %>%
# Filter for NL
filter(cntry == "NL") %>%
mutate(geo = recode(as.character(domicil),
'1' = "Urban",
'2' = "Urban",
'3' = "Rural",
'4' = "Rural",
'5' = "Rural",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_)) %>%
mutate(
gincdif = ifelse(gincdif %in% c(7, 8, 9), NA, gincdif),
)
table(netherlands_data$gincdif)
##
## 1 2 3 4 5
## 2896 7894 3142 3681 543
gincdif <- netherlands_data %>%
mutate(gincdif = case_when(
gincdif == 1 ~ "Agree Strongly",
gincdif == 2 ~ "Agree",
gincdif == 3 ~ "Neutral",
gincdif == 4 ~ "Disagree",
gincdif == 5 ~ "Disagree Strongly"
))
table(gincdif$gincdif)
##
## Agree Agree Strongly Disagree Disagree Strongly
## 7894 2896 3681 543
## Neutral
## 3142
#Test stat
#test_stat <- netherlands_data %>%
#specify(explanatory = domicil,
#response = gincdif) %>%
#hypothesize(null = "independence") %>%
#calculate(stat = "t")
#print(test_stat$stat)
class(netherlands_data$gincdif)
## [1] "numeric"
class(netherlands_data$domicil)
## [1] "numeric"
I can’t find out where I went wrong, and why both of my variables are turning out as numeric. 2 numeric values can’t com[ute either a z-score or a t-score. However, if the larger the magnitude of the t-value, the greater the evidence against the null hypothesis.
Task 3
test_stat <- switzerland_data %>%
specify(explanatory = educ_level,
response = trstprl) %>%
hypothesize(null = "independence") %>%
calculate(stat = "t")
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "BA" - "No BA", or divided in the order "BA" / "No BA" for ratio-based
## statistics. To specify this order yourself, supply `order = c("BA", "No BA")`
## to the calculate() function.
print(test_stat$stat)
## t
## -5.435714
For my research question, I wanted to consider the relationshop between education level and trust in parliament in Switzerland. Using the interpret package, I was able to determine that the t-score was -5.438. The t-stat is quite small indicating that there is not a significant difference in trust in parliament between those with a BA and those without a BA. This siggests that the mean trust level in parliament is not that different fot the 2 groups.