rm(list=ls()); gc() 
##          used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 526467 28.2    1169431 62.5         NA   669520 35.8
## Vcells 968248  7.4    8388608 64.0      16384  1851694 14.2

##Setting up the environment

# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "aod", "interactions", "kableExtra", "flextable", "scales") # add any you need here

# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load the packages
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ 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
## Loading required package: carData
## 
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## 
## Loading required package: grid
## 
## Loading required package: Matrix
## 
## 
## Attaching package: 'Matrix'
## 
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## 
## Loading required package: survival
## 
## 
## Attaching package: 'survey'
## 
## 
## The following object is masked from 'package:graphics':
## 
##     dotchart
## 
## 
## 
## Attaching package: 'MASS'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## 
## 
## Attaching package: 'aod'
## 
## 
## The following object is masked from 'package:survival':
## 
##     rats
## 
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## 
## 
## 
## Attaching package: 'flextable'
## 
## 
## The following objects are masked from 'package:kableExtra':
## 
##     as_image, footnote
## 
## 
## The following object is masked from 'package:purrr':
## 
##     compose
## 
## 
## 
## Attaching package: 'scales'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
## [[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"     
## 
## [[4]]
##  [1] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[6]]
##  [1] "survey"       "survival"     "Matrix"       "grid"         "effects"     
##  [6] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[7]]
##  [1] "MASS"         "survey"       "survival"     "Matrix"       "grid"        
##  [6] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[8]]
##  [1] "aod"          "MASS"         "survey"       "survival"     "Matrix"      
##  [6] "grid"         "effects"      "carData"      "modelsummary" "fst"         
## [11] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "interactions" "aod"          "MASS"         "survey"       "survival"    
##  [6] "Matrix"       "grid"         "effects"      "carData"      "modelsummary"
## [11] "fst"          "infer"        "lubridate"    "forcats"      "stringr"     
## [16] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [21] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [26] "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "kableExtra"   "interactions" "aod"          "MASS"         "survey"      
##  [6] "survival"     "Matrix"       "grid"         "effects"      "carData"     
## [11] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
## [16] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [21] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [26] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[11]]
##  [1] "flextable"    "kableExtra"   "interactions" "aod"          "MASS"        
##  [6] "survey"       "survival"     "Matrix"       "grid"         "effects"     
## [11] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [16] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [21] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [26] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [31] "base"        
## 
## [[12]]
##  [1] "scales"       "flextable"    "kableExtra"   "interactions" "aod"         
##  [6] "MASS"         "survey"       "survival"     "Matrix"       "grid"        
## [11] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [16] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [21] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [26] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [31] "methods"      "base"

##Loading the data

spain_data <- read_fst("spain_data.fst")

##Examining data set

# Directly summarize the 'edulvlb' variable
summary(spain_data$edulvlb)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##     0.0   129.0   222.0   382.3   520.0  9999.0    7844
# Directly summarize the 'polintr' variable
summary(spain_data$polintr)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    2.00    3.00    2.87    4.00    9.00
# Check for missing values in 'edulvlb' and 'polintr'
sum(is.na(spain_data$edulvlb))
## [1] 7844
sum(is.na(spain_data$polintr))
## [1] 0

##Recoding

# Recoding 'edulvlb'
spain_data <- spain_data %>%
  mutate(edulvlb_recode = case_when(
    edulvlb == "No formal education" ~ 1,
    edulvlb == "Primary education" ~ 2,
    edulvlb == "Secondary education" ~ 3,
    edulvlb == "Tertiary education" ~ 4,
    TRUE ~ NA_real_  # Handles cases that do not fit the above categories
  ))
spain_data <- spain_data %>%
  filter(!is.na(edulvlb) & !is.na(polintr))
summary_table <- spain_data %>%
  group_by(edulvlb_recode) %>%
  summarise(
    Mean_Political_Interest = mean(polintr, na.rm = TRUE),
    N = n()
  )

##visualizing

ggplot(spain_data, aes(x = factor(edulvlb_recode), y = polintr)) +
  geom_boxplot() +
  labs(x = "Education Level", y = "Political Interest",
       title = "Political Interest by Education Level in Spain") +
  scale_x_discrete(labels = c("1" = "No formal", "2" = "Primary", "3" = "Secondary", "4" = "Tertiary"))

##Examining data set

# Directly summarize the 'edulvlb' variable
summary(spain_data$edulvlb)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0   129.0   222.0   382.3   520.0  9999.0
# Directly summarize the 'polintr' variable
summary(spain_data$polintr)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   2.000   3.000   2.752   3.000   9.000
# Check for missing values in 'edulvlb' and 'polintr'
sum(is.na(spain_data$edulvlb))
## [1] 0
sum(is.na(spain_data$polintr))
## [1] 0

##Recoding

# Recoding 'edulvlb'
spain_data <- spain_data %>%
  mutate(edulvlb_recode = case_when(
    edulvlb == "No formal education" ~ 1,
    edulvlb == "Primary education" ~ 2,
    edulvlb == "Secondary education" ~ 3,
    edulvlb == "Tertiary education" ~ 4,
    TRUE ~ NA_real_  # Handles cases that do not fit the above categories
  ))
spain_data <- spain_data %>%
  filter(!is.na(edulvlb) & !is.na(polintr))
summary_table <- spain_data %>%
  group_by(edulvlb_recode) %>%
  summarise(
    Mean_Political_Interest = mean(polintr, na.rm = TRUE),
    N = n()
  )

##visualizing

# Descriptive statistics for political interest by education level
spain_data %>%
  group_by(edulvlb_recode) %>%
  summarise(
    mean_polintr = mean(polintr, na.rm = TRUE),
    sd_polintr = sd(polintr, na.rm = TRUE),
    min_polintr = min(polintr, na.rm = TRUE),
    max_polintr = max(polintr, na.rm = TRUE),
    .groups = 'drop'
  )
## # A tibble: 1 × 5
##   edulvlb_recode mean_polintr sd_polintr min_polintr max_polintr
##            <dbl>        <dbl>      <dbl>       <dbl>       <dbl>
## 1             NA         2.75      0.966           1           9
# Enhanced Boxplot with Summary Statistics
ggplot(spain_data, aes(x = factor(edulvlb_recode), y = polintr, fill = factor(edulvlb_recode))) +
  geom_boxplot() +
  stat_summary(fun = "mean", geom = "point", shape = 20, size = 3, color = "red") +
  labs(x = "Education Level", y = "Political Interest", title = "Political Interest by Education Level in Spain") +
  scale_fill_brewer(palette = "Pastel1") +
  theme_minimal()

# Generating a detailed summary of political interest by education level
detailed_summary <- spain_data %>%
  group_by(edulvlb_recode) %>%
  summarise(
    Mean_Political_Interest = mean(polintr, na.rm = TRUE),
    Median_Political_Interest = median(polintr, na.rm = TRUE),
    Std_Deviation = sd(polintr, na.rm = TRUE),
    Count = n(),
    .groups = 'drop'
  )
print(detailed_summary)
## # A tibble: 1 × 5
##   edulvlb_recode Mean_Political_Interest Median_Political_Interest Std_Deviation
##            <dbl>                   <dbl>                     <dbl>         <dbl>
## 1             NA                    2.75                         3         0.966
## # ℹ 1 more variable: Count <int>
## Not necessary, but always good to rename when doing operations so you can backtrack to clean dataset
tab_dat <- spain_data

# Create a crosstable and save it into the object mytab
mytab <- table(tab_dat$edulvlb,tab_dat$polintr)

# 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]         [,3]         [,4]         [,5]
##  [1,] 9.734807e-03 0.0225314263 0.0323394822 0.0202973285 5.859929e-05
##  [2,] 1.839454e-02 0.0425745796 0.0611075322 0.0383531080 1.107271e-04
##  [3,] 8.482202e-04 0.0019632246 0.0028178272 0.0017685615 5.105916e-06
##  [4,] 2.449976e-02 0.0567052310 0.0813893351 0.0510826382 1.474778e-04
##  [5,] 5.247130e-03 0.0121445986 0.0174312103 0.0109404040 3.158543e-05
##  [6,] 6.904118e-05 0.0001597973 0.0002293580 0.0001439527 4.155978e-07
##  [7,] 5.444390e-03 0.0126011624 0.0180865189 0.0113516974 3.277285e-05
##  [8,] 9.152888e-03 0.0211845629 0.0304063216 0.0190840130 5.509639e-05
##  [9,] 2.978634e-03 0.0068941142 0.0098951607 0.0062105301 1.793008e-05
## [10,] 6.006583e-03 0.0139023694 0.0199541486 0.0125238835 3.615701e-05
## [11,] 3.777539e-03 0.0087431979 0.0125491608 0.0078762683 2.273914e-05
## [12,] 1.188495e-02 0.0275079724 0.0394823465 0.0247804263 7.154219e-05
## [13,] 1.454796e-02 0.0336715844 0.0483290134 0.0303328870 8.757239e-05
## [14,] 1.203289e-03 0.0027850395 0.0039973828 0.0025088896 7.243276e-06
## [15,] 9.863026e-05 0.0002282819 0.0003276543 0.0002056467 5.937111e-07
## [16,] 5.917816e-05 0.0001369692 0.0001965926 0.0001233880 3.562267e-07
## [17,] 5.917816e-05 0.0001369692 0.0001965926 0.0001233880 3.562267e-07
## [18,] 4.832883e-04 0.0011185814 0.0016055062 0.0010076688 2.909185e-06
##               [,6]         [,7]
##  [1,] 4.394947e-05 2.197473e-05
##  [2,] 8.304534e-05 4.152267e-05
##  [3,] 3.829437e-06 1.914718e-06
##  [4,] 1.106084e-04 5.530419e-05
##  [5,] 2.368907e-05 1.184454e-05
##  [6,] 3.116983e-07 1.558492e-07
##  [7,] 2.457964e-05 1.228982e-05
##  [8,] 4.132229e-05 2.066115e-05
##  [9,] 1.344756e-05 6.723779e-06
## [10,] 2.711776e-05 1.355888e-05
## [11,] 1.705435e-05 8.527176e-06
## [12,] 5.365664e-05 2.682832e-05
## [13,] 6.567929e-05 3.283965e-05
## [14,] 5.432457e-06 2.716228e-06
## [15,] 4.452833e-07 2.226417e-07
## [16,] 2.671700e-07 1.335850e-07
## [17,] 2.671700e-07 1.335850e-07
## [18,] 2.181888e-06 1.090944e-06
# Table of expected frequencies
ftab <- N * ptab
cat("Table of Expected Frequencies:\n")
## Table of Expected Frequencies:

##Summarizing data

# Load necessary libraries
library(tidyverse)

# Generate descriptive statistics for 'polintr'
spain_data %>%
  summarise(
    Mean_Political_Interest = mean(polintr, na.rm = TRUE),
    SD_Political_Interest = sd(polintr, na.rm = TRUE),
    Median_Political_Interest = median(polintr, na.rm = TRUE),
    Min_Political_Interest = min(polintr, na.rm = TRUE),
    Max_Political_Interest = max(polintr, na.rm = TRUE)
  )
##   Mean_Political_Interest SD_Political_Interest Median_Political_Interest
## 1                2.751637             0.9660815                         3
##   Min_Political_Interest Max_Political_Interest
## 1                      1                      9
# Generate a frequency table for 'edulvlb_recode'
spain_data %>%
  count(edulvlb_recode)
##   edulvlb_recode     n
## 1             NA 11608
# Crosstab of education level and political interest categories
spain_data %>%
  group_by(edulvlb_recode) %>%
  count(polintr)
## # A tibble: 7 × 3
## # Groups:   edulvlb_recode [1]
##   edulvlb_recode polintr     n
##            <dbl>   <dbl> <int>
## 1             NA       1  1329
## 2             NA       2  3076
## 3             NA       3  4415
## 4             NA       4  2771
## 5             NA       7     8
## 6             NA       8     6
## 7             NA       9     3
# Comparative summary statistics by education level
spain_data %>%
  group_by(edulvlb_recode) %>%
  summarise(
    Mean_Political_Interest = mean(polintr, na.rm = TRUE),
    SD_Political_Interest = sd(polintr, na.rm = TRUE),
    .groups = 'drop'
  )
## # A tibble: 1 × 3
##   edulvlb_recode Mean_Political_Interest SD_Political_Interest
##            <dbl>                   <dbl>                 <dbl>
## 1             NA                    2.75                 0.966