Setting up the Environment

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

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.1
## ✔ 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: 'flextable'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     compose
## [[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] "rmarkdown"    "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] "ggridges"     "rmarkdown"    "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"    "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"        
## 
## [[11]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "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] "flextable"    "questionr"    "ggridges"     "rmarkdown"    "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"        
## 
## [[13]]
##  [1] "infer"        "flextable"    "questionr"    "ggridges"     "rmarkdown"   
##  [6] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
## [11] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"

Loading dataset

ess <- read_fst("//Users/paulcavaco/Desktop/Alyssa school/Poster/All-ESS-Data.fst")

Subsetting to country

france_data <- ess %>%
  filter(cntry == "FR")

Save data

write_fst(france_data, "~/Desktop/Alyssa school/Poster/france_data.fst") 

Cleaning the Environment

rm(list=ls()); gc()
##           used (Mb) gc trigger   (Mb) limit (Mb)   max used    (Mb)
## Ncells 1254401 67.0    2115590  113.0         NA    2115590   113.0
## Vcells 2115635 16.2 1256989534 9590.1     102400 1355991521 10345.4

##Loading Data

df <- read_fst("~/Desktop/Alyssa school/Poster/france_data.fst")
df$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  df$year[df$essround == i] <- replacements[i]
}

france_data <- df
france_data_table_subset <- france_data %>%
  mutate(
    stfjb = ifelse(stfjb == 2, 0, ifelse(stfjb %in% c(66,77, 88, 99), NA, stfjb)), 
    edulvla = ifelse(edulvla %in% c(55, 77, 88, 99), NA, edulvla), 
    gndr = ifelse(gndr %in% c(77, 88, 99), NA, gndr) 
  )
summary_table <- datasummary_skim(france_data_table_subset %>% select(stfjb, edulvla, gndr), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
summary_table

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

stfjb

11

89

7.1

2.1

0.0

8.0

10.0

edulvla

6

61

3.0

1.4

1.0

3.0

5.0

gndr

2

0

1.5

0.5

1.0

2.0

2.0

france_data_v2 <- france_data_table_subset %>%
  rename(
    `How Satisfied with Job` = stfjb,
    `Level of Education` = edulvla,
    `Gender` = gndr
    
  )
summary_table_v2 <- datasummary_skim(france_data_v2 %>% select(`How Satisfied with Job`,`Level of Education`,`Gender`), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
summary_table_v2

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

How Satisfied with Job

11

89

7.1

2.1

0.0

8.0

10.0

Level of Education

6

61

3.0

1.4

1.0

3.0

5.0

Gender

2

0

1.5

0.5

1.0

2.0

2.0

Recoding and Cleaning Variables of Interest

ess <- read_fst("//Users/paulcavaco/Desktop/Alyssa school/Poster/All-ESS-Data.fst")
france_data <- ess %>%
  filter(cntry == "FR") %>%
  
  mutate(
    stfjb = case_when(
      stfjb %in% c(66, 77, 88, 99) ~ NA_real_,  
      TRUE ~ as.numeric(stfjb)  
    ),
    gndr = ifelse(gndr %in% c(77, 88, 99), NA, gndr),
    
    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)
    )
  )
unique(france_data$gndr)
## [1] "Female" "Male"
unique(france_data$stfjb)
##  [1] NA  0  7  9  5  6  8  3  2  4 10  1
france_data <- france_data %>% filter(!is.na(stfjb))
france_data <- france_data %>% filter(!is.nan(stfjb))
france_data <- france_data %>% filter(!is.infinite(stfjb))
france_data <- france_data %>% filter(!is.na(gndr))
france_data <- france_data %>% filter(!is.na(educ_level))

unique(france_data$stfjb)
##  [1]  0  7  9  5  6  8  3  2  4 10  1
table(france_data$stfjb)
## 
##   0   1   2   3   4   5   6   7   8   9  10 
##  22  16  32  59  64 181 200 448 532 253 238

Calculating and Interpreting Critical Value for Independence

tab_dat <- france_data

mytab <- table(tab_dat$stfjb, 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.003098499 0.007659447
##  [2,] 0.002253454 0.005570507
##  [3,] 0.004506908 0.011141014
##  [4,] 0.008309611 0.020541245
##  [5,] 0.009013815 0.022282028
##  [6,] 0.025492196 0.063016362
##  [7,] 0.028168172 0.069631339
##  [8,] 0.063096706 0.155974199
##  [9,] 0.074927338 0.185219361
## [10,] 0.035632738 0.088083644
## [11,] 0.033520125 0.082861293
ftab <- N * ptab
cat("Table of Expected Frequencies:\n")
## Table of Expected Frequencies:
print(ftab)
##             [,1]      [,2]
##  [1,]   6.336430  15.66357
##  [2,]   4.608313  11.39169
##  [3,]   9.216626  22.78337
##  [4,]  16.993154  42.00685
##  [5,]  18.433252  45.56675
##  [6,]  52.131540 128.86846
##  [7,]  57.603912 142.39609
##  [8,] 129.032763 318.96724
##  [9,] 153.226406 378.77359
## [10,]  72.868949 180.13105
## [11,]  68.548655 169.45134
alpha <- 0.05 
c_val <- qchisq(alpha, df = 1, lower.tail = FALSE)
cat("Critical Value:", round(c_val, 3), "\n")
## Critical Value: 3.841

Calculating and Interpreting Perasons Chi-Squared Statistic

test_stat <- sum((mytab - ftab)^2 / ftab)
cat("Pearson's X^2:", round(test_stat, 4), "\n")
## Pearson's X^2: 50.6757

Calculating the 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 = 50.676, df = 10, p-value = 2.004e-07

Hypothesis Testing Using Infer

Calculate the Test Statistic of your Sample

test_stat <- france_data %>%
  specify(explanatory = educ_level, 
          response = stfjb) %>% 
  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 
## -2.316399

Stimulate the Null Distribution

null_distribution <- france_data %>%
  specify(explanatory = educ_level,
          response = stfjb) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>% 
  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.

Calculate the P-Value of your Sample

p_val <- null_distribution %>% 
  get_pvalue(obs_stat = test_stat, direction = "two-sided") 
p_val
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1   0.028

Visualize

null_distribution %>%
  visualize() +
  shade_p_value(obs_stat = test_stat, direction = "two-sided")

null_distribution
## Response: stfjb (numeric)
## Explanatory: educ_level (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
##    replicate    stat
##        <int>   <dbl>
##  1         1  0.0381
##  2         2 -0.417 
##  3         3  0.806 
##  4         4  1.45  
##  5         5 -0.179 
##  6         6  0.252 
##  7         7 -0.484 
##  8         8 -0.819 
##  9         9  1.26  
## 10        10 -2.27  
## # ℹ 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)

null_distribution
## Response: stfjb (numeric)
## Explanatory: educ_level (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
##    replicate    stat
##        <int>   <dbl>
##  1         1  0.0381
##  2         2 -0.417 
##  3         3  0.806 
##  4         4  1.45  
##  5         5 -0.179 
##  6         6  0.252 
##  7         7 -0.484 
##  8         8 -0.819 
##  9         9  1.26  
## 10        10 -2.27  
## # ℹ 990 more rows

Regression Model

france_data$weight <- france_data$dweight * france_data$pweight
model1 <- lm(stfjb ~ educ_level, data = france_data, weights = weight)
model2 <- lm(stfjb ~ educ_level + gndr, data = france_data, weights = weight)
model3 <- lm(stfjb ~ educ_level + gndr*educ_level, data = france_data, weights = weight)
modelsummary(
  list(model1, model2),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept")
 (1)   (2)
educ_levelNo BA 0.3 (0.1)** 0.3 (0.1)**
gndrMale −0.1 (0.1)
Num.Obs. 2045 2045
R2 0.005 0.005
R2 Adj. 0.005 0.004
AIC 8793.1 8794.7
BIC 8810.0 8817.2
Log.Lik. −4393.567 −4393.369
RMSE 2.04 2.04
modelsummary(
  list(model1, model2),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_rename = c ("stfjb" = "Satisfaction", "edulblv" = "Education Level", "gndr" = "Gender"),
  title = 'Regression models predicting satifaction in current jobs based on level of educational attainment and gender')
Regression models predicting satifaction in current jobs based on level of educational attainment and gender
 (1)   (2)
(Intercept) 7.0 (0.1)*** 7.0 (0.1)***
educ_levelNo BA 0.3 (0.1)** 0.3 (0.1)**
gndrMale −0.1 (0.1)
Num.Obs. 2045 2045
R2 0.005 0.005
R2 Adj. 0.005 0.004
AIC 8793.1 8794.7
BIC 8810.0 8817.2
Log.Lik. −4393.567 −4393.369
RMSE 2.04 2.04
df <- france_data %>%
  filter(!is.na(educ_level) & !is.na(stfjb))

df <- df %>%
  mutate(stfjb = case_when(
    stfjb == 1 ~ "Yes",
    stfjb == 10 ~ "No",
    stfjb %in% c(0, 2, 3:9, 66, 77, 88, 99) ~ NA_character_,
    TRUE ~ as.character(stfjb)  
  ))

table(df$stfjb)
## 
##  No Yes 
## 238  16
table(df$stfjb, df$educ_level) %>%
  cprop() %>%
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge") +
  labs(title="Feeling Satisfied With Current Job in France",
       y = "Conditional Percentage",
       x = "Feeling Satisfied",
       fill = "At least BA vs. Not")