R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable") # 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.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
## 载入需要的程辑包:viridisLite
## 
## 
## 载入程辑包:'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"
# read data
ess <- read_fst("All-ESS-Data.fst")
#read france data
france_data <- read_fst("france_data.fst")
clean_data <- france_data %>%
  mutate(stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov),
         stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco),
         trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl),
         netusoft = ifelse(netusoft %in% c(7, 8, 9), NA, netusoft),
         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)
    ))
clean_data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  clean_data$year[clean_data$essround == i] <- replacements[i]
}
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]
}
sum(is.na(clean_data$educ_level))
## [1] 0
sum(is.na(clean_data$stfeco))
## [1] 271
sum(is.na(clean_data$stfgov))
## [1] 280
sum(is.na(clean_data$trstprl))
## [1] 364
sum(is.na(clean_data$netusoft))
## [1] 12982
trstprl_by_year <- clean_data %>%
  group_by(year) %>%
  summarize(mean_trstprl = mean(trstprl, na.rm = TRUE))

ggplot(trstprl_by_year, aes(x = year, y = mean_trstprl)) +
  geom_line(color = "blue", size = 1) +  
  geom_point(color = "red", size = 3) +  
  labs(title = "Trust in National Parliament in France (2002-2020)", 
       x = "Year", 
       y = "Mean Trust (0-10 scale)") +
  ylim(3, 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.

stfgov_by_year <- clean_data %>%
  group_by(year) %>%
  summarize(mean_stfgov = mean(stfgov, na.rm = TRUE))

ggplot(stfgov_by_year, aes(x = year, y = mean_stfgov)) +
  geom_line(color = "blue", size = 1) +  
  geom_point(color = "red", size = 3) +  
  labs(title = "satisfaction with the government in France (2002-2020)", 
       x = "Year", 
       y = "Mean Satifaction (0-10 scale)") +
  ylim(2, 5) +  
  theme_minimal()

ess$trstprl[ess$trstprl %in% c(77, 88, 99)] <- NA

avg_trust_france <- clean_data %>%
  group_by(year) %>%
  summarize(mean_trust_france = mean(trstprl, na.rm = TRUE))

avg_trust_all_countries <- ess %>%
  group_by(year) %>%
  summarize(mean_trust_all = mean(trstprl, na.rm = TRUE))

combined_data <- left_join(avg_trust_france, avg_trust_all_countries, by = "year")

long_data <- combined_data %>%


  pivot_longer(
    cols = starts_with("mean_trust"),  
    
    names_to = "Trust_Type",  
    
    values_to = "Mean_Trust"  
  )
ggplot(long_data) +
  geom_line(aes(x = year, y = Mean_Trust, color = Trust_Type), size = 1) +
  geom_point(aes(x = year, y = Mean_Trust, color = Trust_Type), size = 3) +
  labs(title = "Trust in National Parliament (2002-2020)", 
       x = "Year", 
       y = "Mean Trust (0-10 scale)") +
  ylim(3, 5) +
  scale_color_manual(values = c("blue", "red"), 
                     name = "Dataset", 
                     labels = c("France", "All ESS"), 
                     breaks = c("mean_trust_france", "mean_trust_all")) + 
  theme_minimal()

ess$stfeco[ess$stfeco %in% c(77, 88, 99)] <- NA


avg_trust_france1 <- clean_data %>%
  group_by(year) %>%
  summarize(mean_trust_france = mean(stfeco, na.rm = TRUE))


avg_trust_all_countries1 <- ess %>%
  group_by(year) %>%
  summarize(mean_trust_all = mean(stfeco, na.rm = TRUE))

combined_data1 <- left_join(avg_trust_france1, avg_trust_all_countries1, by = "year")


long_data1 <- combined_data1 %>%

  pivot_longer(
    cols = starts_with("mean_trust"),  
    
    names_to = "Trust_Type1",  
    
    values_to = "Mean_Trust1"  
  )

ggplot(long_data1) +
  geom_line(aes(x = year, y = Mean_Trust1, color = Trust_Type1), size = 1) +
  geom_point(aes(x = year, y = Mean_Trust1, color = Trust_Type1), size = 3) +
  labs(title = "Satifaction in Eco (2002-2020)", 
       x = "Year", 
       y = "Mean Satification (0-10 scale)") +
  ylim(2, 6) +
  scale_color_manual(values = c("blue", "red"), 
                     name = "Dataset", 
                     labels = c("France", "All ESS"), 
                     breaks = c("mean_trust_france", "mean_trust_all")) + 
  theme_minimal()

datasummary_skim(clean_data %>% select(trstprl, stfgov, educ_level, stfeco, netusoft))
Unique (#) Missing (%) Mean SD Min Median Max
trstprl 12 2 4.2 2.3 0.0 5.0 10.0
stfgov 12 1 3.7 2.3 0.0 4.0 10.0
stfeco 12 1 3.5 2.1 0.0 3.0 10.0
netusoft 6 68 4.0 1.5 1.0 5.0 5.0
table(clean_data$educ_level)
## 
##    BA No BA 
##  4235 14803
table(clean_data$trstprl, clean_data$educ_level) %>%
  cprop()
##        
##         BA    No BA All  
##   0       3.8  11.5   9.8
##   1       2.5   4.6   4.1
##   2       6.8  10.3   9.5
##   3      10.5  13.3  12.7
##   4      11.2  12.7  12.4
##   5      21.3  24.7  23.9
##   6      15.0   9.4  10.6
##   7      16.0   7.2   9.2
##   8       9.6   4.5   5.6
##   9       2.0   0.8   1.1
##   10      1.4   1.1   1.2
##   Total 100.0 100.0 100.0
table(clean_data$trstprl, clean_data$stfeco) %>%
  cprop()
##        
##         0     1     2     3     4     5     6     7     8     9     10    All  
##   0      40.0  12.4  10.7   6.0   3.3   5.3   2.4   2.3   2.5   5.3  15.0   9.7
##   1       8.4  11.9   5.6   3.1   2.4   2.6   0.9   1.6   1.4   1.8   1.7   4.1
##   2      13.1  19.1  15.7  10.4   6.9   5.6   4.0   2.3   2.2   5.3   0.0   9.5
##   3       9.8  17.0  17.7  17.3  13.3   9.1   6.2   7.2   6.7   8.8   5.0  12.7
##   4       6.7  11.8  13.2  15.5  17.0  11.6   9.2   8.2   7.2   5.3   3.3  12.4
##   5      14.1  16.0  20.4  23.8  27.2  33.1  25.1  21.1  25.1  15.8  16.7  23.9
##   6       2.5   4.4   6.7   9.6  13.5  13.5  21.1  15.9  11.4   7.0   1.7  10.7
##   7       2.6   3.9   5.5   8.7  10.2  10.5  16.6  19.8  15.9  21.1   8.3   9.2
##   8       1.4   2.5   3.2   4.1   4.8   6.3  11.4  16.5  18.4  12.3  10.0   5.6
##   9       0.5   0.7   0.7   1.0   0.8   0.9   1.9   2.4   3.9   8.8   6.7   1.1
##   10      1.0   0.3   0.5   0.6   0.7   1.6   1.2   2.8   5.3   8.8  31.7   1.2
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
table(clean_data$trstprl, clean_data$netusoft) %>%
  cprop()
##        
##         1     2     3     4     5     All  
##   0      15.9  13.5  12.2   8.0   9.4  10.6
##   1       4.4   2.7   5.8   3.9   3.8   4.0
##   2      11.7  11.4  10.9   8.6   9.7  10.1
##   3      12.8  14.1  11.2  15.7  11.4  12.1
##   4      10.2  15.6  11.2  11.6  12.1  11.9
##   5      24.1  22.2  24.5  23.9  21.0  22.0
##   6       9.0   7.8   8.2  12.0  12.8  11.7
##   7       5.7   7.5   7.5   8.2  10.9   9.5
##   8       4.4   3.9   5.8   6.0   6.3   5.9
##   9       0.8   0.6   2.0   1.5   1.4   1.3
##   10      1.0   0.6   0.7   0.6   1.1   1.0
##   Total 100.0 100.0 100.0 100.0 100.0 100.0
table(clean_data$stfgov, clean_data$educ_level) %>%
  cprop()
##        
##         BA    No BA All  
##   0       7.3  12.5  11.4
##   1       6.0   6.8   6.6
##   2      11.7  13.0  12.7
##   3      15.7  14.9  15.0
##   4      15.9  13.6  14.1
##   5      17.8  19.2  18.9
##   6      12.1   9.2   9.8
##   7       8.9   6.2   6.8
##   8       3.5   3.4   3.5
##   9       0.7   0.7   0.7
##   10      0.3   0.6   0.5
##   Total 100.0 100.0 100.0
table(clean_data$stfgov, clean_data$stfeco) %>%
  cprop()
##        
##         0     1     2     3     4     5     6     7     8     9     10    All  
##   0      56.7  18.2  12.4   5.9   2.7   3.1   1.2   0.8   1.4   5.3   7.5  11.4
##   1       8.6  33.2  12.1   5.7   2.2   1.9   0.7   0.7   1.6   1.8   1.5   6.6
##   2      11.0  18.2  31.4  15.7   9.0   5.6   3.1   2.9   2.5   0.0   0.0  12.8
##   3       7.4  12.1  16.6  30.4  17.9  10.3   5.3   3.8   2.7   7.0   1.5  15.1
##   4       3.6   7.1  10.4  17.3  28.8  14.2  11.8   6.4   1.6   5.3   3.0  14.1
##   5       8.1   7.0  10.0  15.2  22.0  37.2  23.4  17.5  12.6   7.0  13.4  18.8
##   6       1.6   1.2   3.2   5.1  10.3  14.6  29.3  23.5  14.8   7.0   3.0   9.8
##   7       1.1   1.2   2.7   2.9   4.8   8.4  17.6  28.1  23.0  19.3   7.5   6.8
##   8       0.9   1.0   0.9   1.7   1.8   3.5   6.2  12.9  31.8  15.8  17.9   3.4
##   9       0.3   0.3   0.3   0.2   0.4   0.6   1.2   2.4   6.0  21.1   3.0   0.7
##   10      0.6   0.3   0.1   0.1   0.1   0.6   0.2   1.1   1.9  10.5  41.8   0.5
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
table(clean_data$stfgov, clean_data$netusoft) %>%
  cprop()
##        
##         1     2     3     4     5     All  
##   0      16.7  14.3   8.6   9.9  11.1  11.9
##   1       8.4   7.6   7.9   5.3   6.3   6.7
##   2      13.1  11.2  17.1  13.6  11.7  12.3
##   3      12.7  16.7  15.8  14.9  14.3  14.3
##   4      10.1  10.9  13.7  14.5  14.2  13.4
##   5      19.4  17.6  15.8  17.3  18.4  18.3
##   6       7.9   9.4   7.5   9.6  11.1  10.2
##   7       5.9   6.1   7.9   8.8   7.6   7.4
##   8       3.4   3.0   4.5   4.4   3.8   3.8
##   9       1.1   1.8   0.7   0.6   1.0   1.0
##   10      1.4   1.2   0.7   0.9   0.5   0.7
##   Total 100.0 100.0 100.0 100.0 100.0 100.0
clean_data %>%

  group_by(educ_level) %>%
  summarise(
    mean_ = mean(trstprl, na.rm = TRUE),
    std_ = sd(trstprl, na.rm = TRUE)
  )
## # A tibble: 2 × 3
##   educ_level mean_  std_
##   <chr>      <dbl> <dbl>
## 1 BA          5.09  2.19
## 2 No BA       4.00  2.32
clean_data %>%

  group_by(stfeco) %>%
  summarise(
    mean_ = mean(trstprl, na.rm = TRUE),
    std_ = sd(trstprl, na.rm = TRUE)
  )
## # A tibble: 12 × 3
##    stfeco mean_  std_
##     <dbl> <dbl> <dbl>
##  1      0  2.20  2.40
##  2      1  3.12  2.15
##  3      2  3.61  2.17
##  4      3  4.23  2.07
##  5      4  4.64  1.92
##  6      5  4.82  2.08
##  7      6  5.53  1.92
##  8      7  5.81  2.07
##  9      8  5.95  2.20
## 10      9  5.93  2.71
## 11     10  6.38  3.58
## 12     NA  4.08  2.70
clean_data %>%

  group_by(netusoft) %>%
  summarise(
    mean_ = mean(trstprl, na.rm = TRUE),
    std_ = sd(trstprl, na.rm = TRUE)
  )
## # A tibble: 6 × 3
##   netusoft mean_  std_
##      <dbl> <dbl> <dbl>
## 1        1  3.74  2.42
## 2        2  3.83  2.28
## 3        3  4.01  2.44
## 4        4  4.30  2.25
## 5        5  4.39  2.38
## 6       NA  4.25  2.32
clean_data %>%

  group_by(educ_level) %>%
  summarise(
    mean_ = mean(stfgov, na.rm = TRUE),
    std_ = sd(stfgov, na.rm = TRUE)
  )
## # A tibble: 2 × 3
##   educ_level mean_  std_
##   <chr>      <dbl> <dbl>
## 1 BA          4.02  2.16
## 2 No BA       3.66  2.29
clean_data %>%

  group_by(stfeco) %>%
  summarise(
    mean_ = mean(stfgov, na.rm = TRUE),
    std_ = sd(stfgov, na.rm = TRUE)
  )
## # A tibble: 12 × 3
##    stfeco mean_  std_
##     <dbl> <dbl> <dbl>
##  1      0  1.42  2.09
##  2      1  1.99  1.82
##  3      2  2.65  1.83
##  4      3  3.40  1.73
##  5      4  4.14  1.63
##  6      5  4.73  1.77
##  7      6  5.48  1.61
##  8      7  6.04  1.70
##  9      8  6.62  1.93
## 10      9  6.77  2.68
## 11     10  7.43  3.05
## 12     NA  4.66  2.42
clean_data %>%

  group_by(netusoft) %>%
  summarise(
    mean_ = mean(stfgov, na.rm = TRUE),
    std_ = sd(stfgov, na.rm = TRUE)
  )
## # A tibble: 6 × 3
##   netusoft mean_  std_
##      <dbl> <dbl> <dbl>
## 1        1  3.49  2.49
## 2        2  3.64  2.44
## 3        3  3.72  2.27
## 4        4  3.91  2.30
## 5        5  3.86  2.30
## 6       NA  3.72  2.23
ggplot(clean_data, aes(x = educ_level, y = trstprl, fill = factor(educ_level))) +
  geom_boxplot() +
  labs(title = "Trust in National Parliament by Education Level",
       x = "Education",
       y = "Trust Scale") +
  scale_fill_viridis_d(name = "Education Level", option = "A") +
  theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).