Setting Working Direction

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

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.4     ✔ 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: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## 
## 
## 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
## [[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] "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"        
## 
## [[13]]
##  [1] "infer"        "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"        
## 
## [[14]]
##  [1] "questionr"    "infer"        "ggridges"     "rmarkdown"    "kableExtra"  
##  [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"        
## 
## [[15]]
##  [1] "effects"      "carData"      "questionr"    "infer"        "ggridges"    
##  [6] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
## [11] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [16] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [21] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [26] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[16]]
##  [1] "survey"       "survival"     "Matrix"       "grid"         "effects"     
##  [6] "carData"      "questionr"    "infer"        "ggridges"     "rmarkdown"   
## [11] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
## [16] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [21] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [26] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [31] "utils"        "datasets"     "methods"      "base"        
## 
## [[17]]
##  [1] "interactions" "survey"       "survival"     "Matrix"       "grid"        
##  [6] "effects"      "carData"      "questionr"    "infer"        "ggridges"    
## [11] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
## [16] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [21] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [26] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [31] "grDevices"    "utils"        "datasets"     "methods"      "base"
ess <- read_fst("All-ESS-Data.fst")
france_data <- ess %>%
  filter(cntry == "FR") %>%
  filter(cntry == "FR", yrbrn > 1920 & yrbrn < 2005) %>%
  mutate(trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt),
         gndr = ifelse(gndr %in% c(77, 88, 99), NA, gndr),
         happy = ifelse(happy %in% c(77, 88, 99), NA, happy)
  )

unique(france_data$trstplt)
##  [1]  5  3  0  7  2  1  6  9  4  8 10 NA
unique(france_data$gndr)
## [1] 2 1
unique(france_data$happy)
##  [1] 10  3  6  5  8  7  9  4  2  0  1 NA
france_data <- france_data %>% filter(!is.na(trstplt))
france_data <- france_data %>% filter(!is.na(gndr))
france_data <- france_data %>% filter(!is.na(happy))

unique(france_data$trstplt)
##  [1]  5  3  0  7  2  1  6  9  4  8 10
unique(france_data$gndr)
## [1] 2 1
unique(france_data$happy)
##  [1] 10  3  6  5  8  7  9  4  2  0  1
library(modelsummary)

france_data_subset <- france_data %>%
  filter(cntry == "FR", yrbrn > 2000 & yrbrn < 2020) %>%
  mutate(
    trstplt = as.numeric(trstplt),
    gndr = as.numeric(gndr),
    happy = as.numeric(happy)
  ) %>%
  select(trstplt, gndr, happy, yrbrn)
france_data_v2 <- france_data_subset %>%
  rename(
    `Trust in Politicians` = trstplt,
    `Gender` = gndr,
    `Year` = yrbrn
  )

summary_table_v2 <- datasummary_skim(france_data_v2 %>% select(`Year`,`Gender`, `Trust in Politicians`), 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

Year

4

0

2002.4

1.2

2001.0

2002.0

2004.0

Gender

2

0

1.5

0.5

1.0

1.0

2.0

Trust in Politicians

11

0

4.6

1.9

0.0

5.0

10.0

fr_data <- france_data %>%
  filter(cntry == "FR") %>%
  mutate(
    gndr = recode(as.character(gndr), 
                      '1' = "Male", 
                      '2' = "Female",),
    trstplt = recode(as.character(trstplt), 
                      '0' = "No Trust",
                      '1' = "No Trust", 
                      '2' = "No Trust",
                      '3' = "No Trust", 
                      '4' = "No Trust", 
                      '5' = "No Trust",
                      '6' = "Trust",
                      '7' = "Trust",
                      '8' = "Trust",
                      '9' = "Trust",
                      '10' = "Trust"),
  )
fr_data <- fr_data %>% filter(!is.na(gndr))
fr_data <- fr_data %>% filter(!is.na(trstplt))

unique(fr_data$gndr)
## [1] "Female" "Male"
unique(fr_data$trstplt)
## [1] "No Trust" "Trust"
test_stat <- fr_data %>%
  specify(explanatory = gndr,
          response = trstplt,
          success = "Trust") %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "z")
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "Female" - "Male", or divided in the order "Female" / "Male" for
## ratio-based statistics. To specify this order yourself, supply `order =
## c("Female", "Male")` to the calculate() function.
print(test_stat$stat)
## [1] -4.165514
null_distribution <- fr_data %>%
  specify(explanatory = gndr,
          response = trstplt,
          success = "Trust") %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>%
  calculate(stat = "z")
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "Female" - "Male", or divided in the order "Female" / "Male" for
## ratio-based statistics. To specify this order yourself, supply `order =
## c("Female", "Male")` to the calculate() function.
p_val <- null_distribution %>%
  get_pvalue(obs_stat = test_stat, direction = "two-sided")
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
p_val
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
null_distribution %>%
  visualize() +
  shade_p_value(obs_stat = test_stat, direction = "two-sided")

null_distribution
## Response: trstplt (factor)
## Explanatory: gndr (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 1
##      stat
##     <dbl>
##  1 -1.06 
##  2  0.220
##  3  0.882
##  4  1.71 
##  5 -0.897
##  6 -1.15 
##  7 -0.442
##  8  0.882
##  9 -0.484
## 10  0.716
## # ℹ 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: trstplt (factor)
## Explanatory: gndr (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 1
##      stat
##     <dbl>
##  1 -1.06 
##  2  0.220
##  3  0.882
##  4  1.71 
##  5 -0.897
##  6 -1.15 
##  7 -0.442
##  8  0.882
##  9 -0.484
## 10  0.716
## # ℹ 990 more rows
model1 <- glm(gndr ~ trstplt, data = france_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)     trstplt 
## 1.530792052 0.001135897
model2 <- glm(gndr ~ trstplt + happy, data = france_data)
model3 <- glm(gndr ~ trstplt * happy, data = france_data)
modelsummary(
  list(model1, model2, model3),
  title = "Regression Model: Effect of Gender and Happiness on Trust in Politicians",
  fmt = 1,
  estimate = c("{estimate} ({std.error}){stars}"),
  statistic = NULL,
)
Regression Model: Effect of Gender and Happiness on Trust in Politicians
 (1)   (2)   (3)
(Intercept) 1.5 (0.0)*** 1.5 (0.0)*** 1.5 (0.0)***
trstplt 0.0 (0.0) 0.0 (0.0) 0.0 (0.0)
happy 0.0 (0.0) 0.0 (0.0)
trstplt × happy 0.0 (0.0)
Num.Obs. 18727 18727 18727
R2 0.000 0.000 0.000
AIC 27099.6 27101.6 27103.6
BIC 27123.1 27132.9 27142.7
Log.Lik. −13546.785 −13546.783 −13546.777
RMSE 0.50 0.50 0.50
ess$year <- NA
replacements <- c(2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:11){
  ess$year[ess$essround == i] <- replacements[i]
}
france_data <- ess %>%
  filter(cntry == "FR") %>%
  mutate(
    trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt), 
  )
trust_by_year <- france_data %>%
  group_by(year) %>%
  summarize(mean_trust = mean(trstplt, na.rm = TRUE))
trust_by_year
## # A tibble: 10 × 2
##     year mean_trust
##    <dbl>      <dbl>
##  1  2000       3.65
##  2  2002       3.51
##  3  2004       3.29
##  4  2006       3.50
##  5  2008       3.19
##  6  2010       3.21
##  7  2012       2.76
##  8  2014       2.89
##  9  2016       3.54
## 10  2018       3.90
ggplot(trust_by_year, aes(x = year, y = mean_trust)) +
  geom_line(color = "blue", size = 1) +  # Line to show the trend
  geom_point(color = "red", size = 3) +  # Points to highlight each year's value
  labs(title = "Trust in Politicians in France (2002-2020)", 
       x = "Survey Year", 
       y = "Trust (0-10)") +
  ylim(0, 10) +  # Setting the y-axis limits from 0 to 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.

france_data %>%
  ggplot(aes(x = trstplt, y = as.factor(year), fill = as.factor(year))) +
  geom_density_ridges() + 
  labs(title = "Trust in Politicians For France", 
       x = "Trust in Politicians (0-10)", y = "Survey Year",
       fill = "Survey Year") +
  theme_minimal()
## Picking joint bandwidth of 0.427
## Warning: Removed 146 rows containing non-finite values
## (`stat_density_ridges()`).