Setup

library(fst)
setwd("~/SOC202/R Scripts (hw + tutorial)")
ess <- read_fst("All-ESS-Data.fst")

packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")

# 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.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
## Loading required package: viridisLite
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "fstcore"   "fst"      
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "fstcore"      "fst"          "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "fstcore"      "fst"          "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "fstcore"      "fst"          "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"        
## 
## [[5]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "fstcore"      "fst"          "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"        
## 
## [[6]]
##  [1] "viridis"      "viridisLite"  "RColorBrewer" "modelsummary" "lubridate"   
##  [6] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [11] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "fstcore"     
## [16] "fst"          "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[7]]
##  [1] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "fstcore"      "fst"          "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "RColorBrewer"
##  [6] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "fstcore"      "fst"          "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "fstcore"      "fst"          "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[10]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "fstcore"      "fst"         
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[11]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "fstcore"      "fst"         
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[12]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "fstcore"     
## [21] "fst"          "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"

Task 1

table(ess$essround)
## 
##     1     2     3     4     5     6     7     8     9    10 
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  ess$year[ess$essround == i] <- replacements[i]
}

bulgaria_data <- ess %>%
  filter(cntry == "BG") %>%
  mutate(
    chldhm = ifelse(chldhm %in% c(9), NA, chldhm),
    emplrel = ifelse(emplrel %in% c(6, 7, 8, 9), NA, emplrel),
    imptrad = ifelse(imptrad %in% c(7, 8, 9), NA, imptrad))

table(bulgaria_data$chldhm)
## 
##    1    2 
## 3311 5013
table(bulgaria_data$emplrel)
## 
##     1     2     3 
## 11215   684   297
table(bulgaria_data$imptrad)
## 
##    1    2    3    4    5    6 
## 3915 4619 2690  977  427  209
datasummary_skim(bulgaria_data %>% select(chldhm, emplrel, imptrad))
Unique (#) Missing (%) Mean SD Min Median Max
chldhm 3 37 1.6 0.5 1.0 2.0 2.0
emplrel 4 8 1.1 0.4 1.0 1.0 3.0
imptrad 7 3 2.2 1.2 1.0 2.0 6.0

From the data, I have noticed that the median is higher than the mean for “chldhm,” where the mean is 1.6 and the median is 2.0. It suggests that the data is negatively skewed. It indicates that there is a higher concentration of people in Bulgaria who do not live with children in their household than those who live with children. In “emplrel,” the mean and median are very similar, which is approximately the score of 1. It hints that the average employment relation in Bulgaria is employee, rather than self-employed or working for their own family business. For “imptrad,” the mean (2.2 score) is higher than the median (2.0 score), so the data is positively skewed. It illustrates that most people in Bulgaria believe that it is important to follow traditions and customs. The mean is 2.2, which is a low value since the maximum score respondents can choose is 6. It highlights that there is a strong value of conformity to traditions. The visualisation of the distribution confirms this claim because it shows that the number of people decreases as the score increases. Also, this variable has a large standard deviation of 1.2, which indicates that the data is more spread out, and suggesting that there is a diverse range of scores.

Task 2

tradition_by_year <- bulgaria_data %>%
  group_by(year) %>%
  summarize(mean_imptrad = mean(imptrad, na.rm = TRUE))
tradition_by_year
## # A tibble: 6 × 2
##    year mean_imptrad
##   <dbl>        <dbl>
## 1  2006         2.13
## 2  2008         2.24
## 3  2010         2.14
## 4  2012         2.05
## 5  2018         2.26
## 6  2020         2.43
#Line + point
ggplot(tradition_by_year, aes(x = year, y = mean_imptrad)) +
  geom_line(color = "blue", size = 1) +
  geom_point(color = "red", size = 3) +
  labs(title = "Important to follow traditions and customs in Bulgaria (2006-2020)", 
       x = "Survey Year", 
       y = "Importance scale (1-6)") +
  ylim(1, 6) +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

In Bulgaria, the mean scores for the variable “imptrad” across the years 2006 to 2020 is stable, and not fluctuating. Even though there is an overall increase from 2006 to 2020, the increase is small, from 2.13 in 2006 to 2.43 in 2020. There was a minor increase in 2008 to a score of 2.24, then a minor decrease in 2012 to a score of 2.05, and it increased slightly back up again to a score of 2.43 in 2020. Furthermore, the mean scores have a small range, where 2.05 in 2012 is the smallest value, and 2.43 in 2020 is the largest value. It suggests that there is low variability in the distribution of the data, and that the way people think about the importance of following traditions has not changed a lot over the years.

Task 3

ess_selected <- ess %>%
  filter(cntry %in% c("IS", "BG", "IT")) %>%
  mutate(imptrad = ifelse(imptrad %in% c(7, 8, 9), NA, imptrad))

task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, imptrad, FUN=median), y = imptrad, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Important to follow traditions and customs (Iceland, Bulgaria, Italy)", 
       x = "Country", 
       y = "Scale (1-6)")

task3plot
## Warning: Removed 1955 rows containing non-finite values (`stat_boxplot()`).

Firstly, I found it surprising that Bulgaria and Italy have almost the exact same descriptive statistics. For example, they both have a median score of approximately 2. This indicates that the people in both countries place a similar value on conforming to traditions and customs. Also, similar descriptive statistics suggest that they have a similar distribution of data points, where most people chose the lower scores, and few people chose the higher scores. Once again, showcasing the fact that most of them believe in the importance of following traditions. On the other hand, Iceland has the highest median out of all three countries, with a score of approximately 3. Even though it is the highest median, it remains on the lower end, considering that the scale goes up to 6. Iceland does place a greater emphasis on following customs compared to Bulgaria and Italy, however, this difference is not very large. Iceland has a larger interquartile range than Bulgaria and Italy, which proposes that Iceland’s data points are more spread out in the central portion surrounding the median in comparison to the other two countries. Also, Iceland has a higher proportion of people in the upper quartile compared to the lower quartile, implying the data is positively skewed. Moreover, an observation to note is that there are no outliers in this data, so there are no data points that are skewing the descriptive statistics because they are sensitive to outliers.

Task 4

bulgaria_data <- bulgaria_data %>%
  mutate(chldhm_recode = recode(as.character(chldhm), 
                      '1' = "Yes", 
                      '2' = "No",
                      '9' = NA_character_))
table(bulgaria_data$chldhm_recode)
## 
##   No  Yes 
## 5013 3311
table(bulgaria_data$chldhm)
## 
##    1    2 
## 3311 5013
imptrad_chldhm <- datasummary_crosstab(imptrad ~ chldhm_recode, data = bulgaria_data)

table(bulgaria_data$imptrad, bulgaria_data$chldhm_recode) %>%
  cprop()
##        
##         No    Yes   All  
##   1      32.8  34.2  33.4
##   2      34.5  36.2  35.2
##   3      20.4  20.6  20.5
##   4       7.9   6.1   7.2
##   5       3.0   1.9   2.6
##   6       1.5   1.0   1.3
##   Total 100.0 100.0 100.0

Looking at the table for if respondents live with children in their household and the importance of following traditions, there is a high concentration of people who chose to score on the low end, from scores 1 to 3. This suggests that people in Bulgaria like to follow customs. This opinion does not vary a lot regarding if the respondent lives with a child or not because the percentages are very similar. It seems that people who live with children are more likely to believe in following traditions because 34.2% chose 1, compared to 32.8% for those who do not live with children.

bulgaria_data <- bulgaria_data %>%
  mutate(emplrel_recode = recode(as.character(emplrel), 
                      '1' = "Employee", 
                      '2' = "Self-employed",
                      '3' = "Working for own family business"))
table(bulgaria_data$emplrel_recode)
## 
##                        Employee                   Self-employed 
##                           11215                             684 
## Working for own family business 
##                             297
table(bulgaria_data$emplrel)
## 
##     1     2     3 
## 11215   684   297
imptrad_emplrel <- datasummary_crosstab(imptrad ~ emplrel_recode, data = bulgaria_data)

table(bulgaria_data$imptrad, bulgaria_data$emplrel_recode) %>%
  cprop()
##        
##         Employee Self-employed Working for own family business All  
##   1      31.1     32.2          27.1                            31.1
##   2      36.5     34.9          39.6                            36.4
##   3      20.8     18.6          19.6                            20.7
##   4       7.1      8.4           7.5                             7.2
##   5       3.2      3.3           3.9                             3.2
##   6       1.4      2.5           2.1                             1.5
##   Total 100.0    100.0         100.0                           100.0

Looking at the table for employment relation and the importance of following traditions, it seems very similar with the table for children, where regardless of employment relation, most people believe that traditions are important to follow. For example, the highest percentage (36.4%) of people are in the score of 2 (“Like me”), and there is a large concentration of people who scored between 1 and 3. The mode of 2 is considered a low score when keeping in mind that the scale reaches up to a score of 6. This observation seems to be similar for all employment relation categories, where there is the largest proportion of people who chose 2. For instance, 36.5% for employees, 34.9% for self-employed, and 39.6% for working for their own family business. It suggests that Bulgaria is a highly collective country and may emphasize heavily on group conformity. Overall, it seems that people who are self-employed are more likely to follow traditions, where 32.2% chose 1 (“Very much like me”) compared to 31.1% for employees and 27.1% for working for their own family business.

Task 5

df <- bulgaria_data %>%
  filter(!is.na(chldhm_recode) & !is.na(imptrad))

df1 <- df %>%
  mutate(imptrad = case_when(
    imptrad == 1 ~ "1 Very much like me",
    imptrad == 2 ~ "2 Like me",
    imptrad == 3 ~ "3 Somewhat like me",
    imptrad == 4 ~ "4 A little like me",
    imptrad == 5 ~ "5 Not like me",
    imptrad == 6 ~ "6 Not like me at all",
   TRUE ~ as.character(imptrad)
  ))

table(df1$imptrad)
## 
##  1 Very much like me            2 Like me   3 Somewhat like me 
##                 2713                 2860                 1667 
##   4 A little like me        5 Not like me 6 Not like me at all 
##                  582                  208                  104
table(df1$imptrad, df1$chldhm_recode) %>%
  cprop() %>%
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge") +
  labs(title="Conditional Percentage for importance to follow traditions and customs",
       subtitle = "Given a person lives with a child or not, what's the probability of believing in the value of following traditions?",
       y = "Conditional Percentage",
       x = "Important to follow traditions and customs",
       fill = "Does the respondent live with children at household grid?")+
  theme(legend.position = "bottom", 
        plot.title = element_text(size = 13, face = "bold"),
        plot.subtitle = element_text(size = 9)) 

Overall, in Bulgaria no matter if the respondent is living with a child in their household grid or not, it seems that most people believe in the importance of following traditions and customs. For example, the bar chart is positively skewed, where almost 90% of the respondents scored between 1 and 3. Most people chose a score at the lower end, and few people chose the higher end for thinking that following traditions and customs are important. Furthermore, it appears that there are not many differences in data distribution from scores 1 to 6 between respondents who live with a child and respondents who do not live with a child, where both have a mode of the score 2 and the lowest score being the score of 6. However, it is slightly more likely for people who live with children to believe in the importance of following traditions because there is a higher percentage of people who live with children compared to those who do not for scores 1 to 3.