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:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
setwd("~/Downloads/R /SOC202 Homework 5")  # Replace /path/to/your/folder with the actual path you just copied.

#run 
getwd() # this must be where your data is stored, otherwise you will not be able to load your data
## [1] "/Users/alessiamacbook/Downloads/R /SOC202 Homework 5"
# List of packages
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.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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"
#install.packages("ggplot2")
packages <- c("ggplot2")
#install.packages("tidyverse")
packages <- ("tidyverse")

library(ggplot2)
library(tidyverse)
ess <- read_fst("All-ESS-Data.fst")
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]
}

TASK 1

table(ess$stfdem)
## 
##     0     1     2     3     4     5     6     7     8     9    10    77    88 
## 29121 18116 31973 45014 46758 80154 55790 67816 59837 23346 12989   755 18211 
##    99 
##   675
table(ess$rdpol)
## 
##     0     1     2     3     4     5     6     7    66    77    88    99 
## 32696 77746 40234 13091  6583  3196  2268  5418 58678    47  1714   435
table(ess$nwsppol)
## 
##      0      1      2      3      4      5      6      7     66     77     88 
##  24865 102663  32425   6342   2126    745    409    548  70234     45   1216 
##     99 
##    488

Note: numbers 77, 88, and 99 need to be treated as missing in each satisfaction with democracy, radio listening to politics or current affairs on a weekday , and newspaper reading of politics or current affairs on a weekday.

switzerland_data <- ess %>% # Start with the original 'ess' dataset
  filter(cntry == "CH") %>% # Filter the data to only include rows where country (cntry) is France ("FR")
  mutate(
    nwsppol = ifelse(nwsppol %in% c(77, 88, 99), NA, nwsppol),
    stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), # For 'stfdem', set values 77, 88, and 99 to NA.
    rdpol = ifelse(rdpol %in% c(77, 88, 99), NA, rdpol), # For 'trstplt', set values 77, 88, and 99 to NA.
  )

TASK 2

satisfaction_by_year <- switzerland_data %>%
  group_by(year) %>%
  summarize(mean_satisfaction = mean(stfdem, na.rm = TRUE))
satisfaction_by_year
## # A tibble: 10 × 2
##     year mean_satisfaction
##    <dbl>             <dbl>
##  1  2002              6.57
##  2  2004              6.36
##  3  2006              6.87
##  4  2008              6.89
##  5  2010              7.07
##  6  2012              7.39
##  7  2014              7.35
##  8  2016              7.38
##  9  2018              7.54
## 10  2020              7.65
switzerland_data %>%
  ggplot(aes(x = stfdem, y = as.factor(year), fill = as.factor(year))) +
  geom_density_ridges() + # geometry used for visualizing
  labs(title = "Satisfaction with Democracy Across Survey Years For Switzerland", 
       x = "Satisfaction (0-10)", y = "Survey Year",
       fill = "Survey Year") +
  theme_minimal()
## Picking joint bandwidth of 0.327
## Warning: Removed 440 rows containing non-finite values
## (`stat_density_ridges()`).

It can be noted that across years there is not significant changes in the satisfaction with democracy in Switzerland, in fact, each of the 10 years is almost identical. This could indicate consistent left-skewed data.

TASK 3

ess_selected <- ess %>%
  filter(cntry %in% c("CH", "FR", "SE")) %>%
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem))


task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -stfdem, FUN=median), y = stfdem, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Comparison of satisfaction with democracy in Switzerland, France,and Sweden", 
       x = "Country", 
       y = "Scale (0-10)")

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

Note: Both Switzerland and Sweden have significant out-lairs whereas France does not appear to. France also has a lower satisfaction with democracy than Switzerland and Sweden, who have similar results.

TASK 4

stfdem_edulvlb <- datasummary_crosstab(stfdem ~ edulvlb, data = switzerland_data)
stfdem_anctry1 <- datasummary_crosstab(stfdem ~ anctry1, data = switzerland_data)
stfdem_wrhpp <- datasummary_crosstab(stfdem ~ wrhpp, data = switzerland_data)
stfdem_edulvlb
stfdem 0  113  212  213  229  312  313  322  323  421  422  423  510  520  610  620  710  720  800  5555  7777  8888 All
0 N 0 1 0 8 2 1 0 13 1 2 0 0 0 2 1 0 1 3 2 0 0 0 103
% row 0.0 1.0 0.0 7.8 1.9 1.0 0.0 12.6 1.0 1.9 0.0 0.0 0.0 1.9 1.0 0.0 1.0 2.9 1.9 0.0 0.0 0.0 100.0
1 N 0 3 1 4 1 1 0 14 0 2 1 0 0 1 0 0 1 1 0 0 0 0 77
% row 0.0 3.9 1.3 5.2 1.3 1.3 0.0 18.2 0.0 2.6 1.3 0.0 0.0 1.3 0.0 0.0 1.3 1.3 0.0 0.0 0.0 0.0 100.0
2 N 1 2 3 12 5 3 2 27 1 4 0 0 0 12 4 1 2 0 1 0 1 0 223
% row 0.4 0.9 1.3 5.4 2.2 1.3 0.9 12.1 0.4 1.8 0.0 0.0 0.0 5.4 1.8 0.4 0.9 0.0 0.4 0.0 0.4 0.0 100.0
3 N 1 5 2 12 12 4 6 75 1 6 1 1 0 25 5 0 8 12 2 0 1 0 465
% row 0.2 1.1 0.4 2.6 2.6 0.9 1.3 16.1 0.2 1.3 0.2 0.2 0.0 5.4 1.1 0.0 1.7 2.6 0.4 0.0 0.2 0.0 100.0
4 N 5 6 8 25 18 4 11 111 5 5 1 1 2 35 13 3 8 17 6 0 0 1 741
% row 0.7 0.8 1.1 3.4 2.4 0.5 1.5 15.0 0.7 0.7 0.1 0.1 0.3 4.7 1.8 0.4 1.1 2.3 0.8 0.0 0.0 0.1 100.0
5 N 1 19 24 86 48 19 24 308 13 10 1 3 5 57 19 9 14 28 9 1 1 0 1791
% row 0.1 1.1 1.3 4.8 2.7 1.1 1.3 17.2 0.7 0.6 0.1 0.2 0.3 3.2 1.1 0.5 0.8 1.6 0.5 0.1 0.1 0.0 100.0
6 N 1 30 19 95 32 22 31 280 13 23 3 3 9 112 30 20 26 55 19 0 0 0 1764
% row 0.1 1.7 1.1 5.4 1.8 1.2 1.8 15.9 0.7 1.3 0.2 0.2 0.5 6.3 1.7 1.1 1.5 3.1 1.1 0.0 0.0 0.0 100.0
7 N 4 43 37 168 95 59 71 677 23 32 7 6 4 228 68 29 66 126 30 1 0 1 3335
% row 0.1 1.3 1.1 5.0 2.8 1.8 2.1 20.3 0.7 1.0 0.2 0.2 0.1 6.8 2.0 0.9 2.0 3.8 0.9 0.0 0.0 0.0 100.0
8 N 6 56 50 280 121 89 109 894 38 71 14 11 16 349 132 50 115 183 86 2 1 4 4552
% row 0.1 1.2 1.1 6.2 2.7 2.0 2.4 19.6 0.8 1.6 0.3 0.2 0.4 7.7 2.9 1.1 2.5 4.0 1.9 0.0 0.0 0.1 100.0
9 N 6 35 25 151 54 38 75 415 24 39 12 5 11 184 79 37 67 161 43 1 0 0 2188
% row 0.3 1.6 1.1 6.9 2.5 1.7 3.4 19.0 1.1 1.8 0.5 0.2 0.5 8.4 3.6 1.7 3.1 7.4 2.0 0.0 0.0 0.0 100.0
10 N 3 39 26 99 39 35 30 234 13 17 3 1 8 103 47 18 47 71 17 0 0 4 1246
% row 0.2 3.1 2.1 7.9 3.1 2.8 2.4 18.8 1.0 1.4 0.2 0.1 0.6 8.3 3.8 1.4 3.8 5.7 1.4 0.0 0.0 0.3 100.0
All N 35 267 207 1000 438 281 369 3104 132 213 43 31 55 1116 402 173 356 664 215 5 5 10 16925
% row 0.2 1.6 1.2 5.9 2.6 1.7 2.2 18.3 0.8 1.3 0.3 0.2 0.3 6.6 2.4 1.0 2.1 3.9 1.3 0.0 0.0 0.1 100.0
stfdem_wrhpp
stfdem 1  2  3  4  8  9 All
0 N 4 4 14 4 0 0 103
% row 3.9 3.9 13.6 3.9 0.0 0.0 100.0
1 N 1 5 8 9 0 0 77
% row 1.3 6.5 10.4 11.7 0.0 0.0 100.0
2 N 1 16 10 12 0 0 223
% row 0.4 7.2 4.5 5.4 0.0 0.0 100.0
3 N 3 19 48 43 0 0 465
% row 0.6 4.1 10.3 9.2 0.0 0.0 100.0
4 N 4 41 80 66 0 0 741
% row 0.5 5.5 10.8 8.9 0.0 0.0 100.0
5 N 11 74 212 151 2 0 1791
% row 0.6 4.1 11.8 8.4 0.1 0.0 100.0
6 N 10 98 238 152 0 0 1764
% row 0.6 5.6 13.5 8.6 0.0 0.0 100.0
7 N 16 164 510 297 2 1 3335
% row 0.5 4.9 15.3 8.9 0.1 0.0 100.0
8 N 16 202 660 476 1 0 4552
% row 0.4 4.4 14.5 10.5 0.0 0.0 100.0
9 N 17 83 368 211 3 0 2188
% row 0.8 3.8 16.8 9.6 0.1 0.0 100.0
10 N 6 44 155 157 0 0 1246
% row 0.5 3.5 12.4 12.6 0.0 0.0 100.0
All N 95 770 2345 1610 8 1 16925
% row 0.6 4.5 13.9 9.5 0.0 0.0 100.0
table(switzerland_data$stfdem, switzerland_data$wrhpp) %>%
  cprop()
##        
##         1     2     3     4     8     9     All  
##   0       4.5   0.5   0.6   0.3   0.0   0.0   0.5
##   1       1.1   0.7   0.3   0.6   0.0   0.0   0.5
##   2       1.1   2.1   0.4   0.8   0.0   0.0   0.8
##   3       3.4   2.5   2.1   2.7   0.0   0.0   2.4
##   4       4.5   5.5   3.5   4.2   0.0   0.0   4.0
##   5      12.4   9.9   9.2   9.6  25.0   0.0   9.5
##   6      11.2  13.1  10.3   9.6   0.0   0.0  10.5
##   7      18.0  21.9  22.1  18.8  25.0 100.0  20.9
##   8      18.0  26.9  28.7  30.2  12.5   0.0  28.7
##   9      19.1  11.1  16.0  13.4  37.5   0.0  14.4
##   10      6.7   5.9   6.7   9.9   0.0   0.0   7.7
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0

Note: This way of presenting data while organized is not easy to read to task 5 will help a lot with that.

Task 5

# double check clean
switzerland_clean <- switzerland_data %>%
  filter(!is.na(stfdem) & !is.na(wrhpp))

# calculate conditional probabilities
switzerland_probs <- switzerland_clean %>%
  count(stfdem, wrhpp) %>%
  group_by() %>%
  mutate(prob = n / sum(stfdem))
# plot
ggplot(switzerland_probs, aes(x = as.factor(stfdem), y = prob, color = wrhpp)) +
  geom_point() +
  geom_line(aes(group = wrhpp)) +
  labs(title = "Conditional Probabilities of Satisfaction with democracy in Switzerland",
       subtitle = "by happiness level",
       x = "Satisfaction scale", 
       y = "Probability") +
  theme_minimal()

Note: that this is not a very nice looking graph, so better choices of variables may have been chosen. It is clear that the satisfaction with democracy in Switzerland is high with a staggered right skewed graph.

# double check clean
switzerland_clean <- switzerland_data %>%
  filter(!is.na(wrhpp) & !is.na(stfdem))

# calculate conditional probabilities
switzerland_probs <- switzerland_clean %>%
  count(wrhpp, stfdem) %>%
  group_by() %>%
  mutate(prob = n / sum(wrhpp))
# plot
ggplot(switzerland_probs, aes(x = as.factor(wrhpp), y = prob, color = stfdem)) +
  geom_point() +
  geom_line(aes(group = stfdem)) +
  labs(title = "Conditional Probabilities of happiness in Switzerland",
       subtitle = "by satisfaction with democracy",
       x = "Happiness scale", 
       y = "Probability") +
  theme_minimal()

Note: there may have been better variables to test. There is clear conditional probabilities between happiness correlated to the satisfaction with democracy in Switzerland.