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" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
##  [6] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[11]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[12]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"
setwd("~/Desktop/SOC202 H1/Coding Tut 1/SOC202_Project_MarcoHuang")
getwd()
## [1] "/Users/marcohuang/Desktop/SOC202 H1/Coding Tut 1/SOC202_Project_MarcoHuang"
ess <- read_fst("All-ESS-Data.fst")
#Task 1
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]
}
table(ess$stfgov)
## 
##     0     1     2     3     4     5     6     7     8     9    10    77    88 
## 50082 30238 46909 58918 55276 80173 54097 51400 30634  8648  6253  1012 14087 
##    99 
##   782
table(ess$stfhlth)
## 
##     0     1     2     3     4     5     6     7     8     9    10    77    88 
## 25347 20246 34866 47881 48022 66773 56600 71547 69247 27888 16093   267  4627 
##    99 
##  1151
table(ess$imbgeco)
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##  30849  20020  33483  45104  42850 109677  50943  57981  45079  13848  17362 
##     77     88     99 
##    590  21738   1031
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:lubridate':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## The following object is masked from 'package:purrr':
## 
##     transpose
  france_data <- ess[ess$cntry == "FR", ]
  
  france_data_subset <- france_data %>%
  filter(cntry == "FR", yrbrn > 1990 & yrbrn < 2005) %>%
  mutate(
    stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov),
    stfhlth = ifelse(stfhlth %in% c(77, 88, 99), NA, stfhlth),
    imbgeco = ifelse(imbgeco %in% c(77, 88, 99), NA, imbgeco),) %>%
  select(stfgov, stfhlth, imbgeco)
datasummary_skim(france_data_subset)
Unique (#) Missing (%) Mean SD Min Median Max
stfgov 12 3 4.1 2.1 0.0 4.0 10.0
stfhlth 12 1 6.9 1.9 0.0 7.0 10.0
imbgeco 12 1 5.2 2.3 0.0 5.0 10.0

Looking at tables, we can tell that France people are not feeling satisfied to their government; however, the health service in France shows a relatively high mean which mean people are happy with those services.

#Task 2
Immigration_data <- france_data %>%
  group_by(year) %>%
  summarize(mean_immigration = mean(imbgeco, na.rm = TRUE))
Immigration_data
## # A tibble: 10 × 2
##     year mean_immigration
##    <dbl>            <dbl>
##  1  2002             6.22
##  2  2004             5.79
##  3  2006             5.34
##  4  2008             6.28
##  5  2010             5.23
##  6  2012             5.30
##  7  2014             5.81
##  8  2016             5.88
##  9  2018             7.27
## 10  2020             7.51
ggplot(Immigration_data, aes(x = year, y = mean_immigration)) +
  geom_line(color = "orange", size = 1) +  
  geom_point(color = "blue", size = 3) +  
  labs(title = "Immigration in France (2002-2020)", 
       x = "Year", 
       y = "Immigration (0-10)") +
  ylim(0, 10) +  
  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.

By running the the graph above, I have noticed it shows an inconstant mean in recent reports from 2002- 2020. It sometimes maintains at 6, sometimes drops to 5 and eventually it becomes 7+ in recent years. The level of attitude toward immigration against the economy has a unstable bump.

#Task 3
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]
}
ess_clean <- select(ess,imbgeco, clsprty, cntry, yrbrn, trstplc, year)

ess_clean <- ess_clean %>%
  filter(!is.na(imbgeco) & imbgeco >= 0 & imbgeco <= 10)

table(ess_clean$imbgeco)
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##  30849  20020  33483  45104  42850 109677  50943  57981  45079  13848  17362
ess_selected <- ess_clean %>%
  filter(cntry %in% c("FR", "BE", "RS"))

p2_selected <- ggplot(ess_selected, aes(x = reorder(cntry, -imbgeco, FUN=median), y = imbgeco, fill = cntry)) +
  geom_boxplot(outlier.shape = NA) +  # Hide outliers for clarity
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Boxplots of Immigration bad or good for country's economy", 
       x = "Country", 
       y = "Scale (Good or Bad Level)")

p2_selected

I have noticed that France standing out here when most of their people think it is helpful on improving country’s economy as to support the immigration, but a large proportion of people’s response in Serbia are found to be negatively skewed; suggesting that Serbia does not believe that immigration would help out their economy.

#Task 4
france_data <- france_data %>%
  mutate(
    edlvfdfr = case_when(
      essround < 5 & edlvfdfr == 5555 ~ NA_real_,
      TRUE ~ edlvfdfr
    ),
    edlvmdfr = case_when(
      essround >= 5 & edlvmdfr == 5555 ~ NA_real_,
      TRUE ~ edlvmdfr
    ),
    both_educ_level = case_when(
      essround < 5 & edlvfdfr == 10~ "Doctor or above",
      essround >= 5 & edlvmdfr > 10~ "Doctor or above",
      TRUE ~ "Other or NA"
    )
  )
table(france_data$both_educ_level)
## 
## Doctor or above     Other or NA 
##            3221           15817
clsprtyedu <- datasummary_crosstab(clsprty ~ both_educ_level, data = france_data)
clsprtyedu
clsprty Doctor or above Other or NA All
1 N 1422 8085 9507
% row 15.0 85.0 100.0
2 N 1738 7456 9194
% row 18.9 81.1 100.0
7 N 20 81 101
% row 19.8 80.2 100.0
8 N 41 195 236
% row 17.4 82.6 100.0
All N 3221 15817 19038
% row 16.9 83.1 100.0
table(france_data$clsprty, france_data$both_educ_level) %>%
  cprop()
##        
##         Doctor or above Other or NA All  
##   1      44.1            51.1        49.9
##   2      54.0            47.1        48.3
##   7       0.6             0.5         0.5
##   8       1.3             1.2         1.2
##   Total 100.0           100.0       100.0

By putting two variables, edlvfdfr (fathers’ education in france) and edlvmdfr (mothers’education in france), I have noticed only a small portion of people in france have been above the degree of Doctor while most of other are below it or no degree at all.

#Task 5
france_probs <- france_data %>%
  filter(!is.na(edlvfdfr) & !is.na(imbgeco))

france_probs <- france_data %>%
  count(imbgeco, edlvfdfr) %>%
  group_by(imbgeco) %>%
  mutate(prob = n / sum(n))
ggplot(france_probs, aes(x = as.factor(imbgeco), y = prob, color = edlvfdfr)) +
  geom_point() +
  geom_line(aes(group = edlvfdfr)) +
  labs(title = "Father's Education in Frace",
       subtitle = "The possible highest level",
       x = "Degree", 
       y = "Conditional Percentage") +
  theme_minimal()