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

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.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"
ess <- read_fst("All-ESS-Data.fst")

Task 1

Do a data summary table of three variables of interest. Discuss briefly what you note (i.e., add text in your markdown after the Task 1 code).

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]
}
iceland_data <- ess %>% 
  filter(cntry == "IS") %>% 
  mutate(
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy), 
    atchctr = ifelse(atchctr %in% c(77, 88, 99), NA, atchctr), 
    aesfdrk = ifelse(aesfdrk %in% c(7, 8, 9), NA, aesfdrk),
)

datasummary_skim(iceland_data %>% select(happy, atchctr, aesfdrk))
Unique (#) Missing (%) Mean SD Min Median Max
happy 12 1 8.2 1.5 0.0 8.0 10.0
atchctr 12 34 8.2 1.8 0.0 9.0 10.0
aesfdrk 5 1 1.5 0.7 1.0 1.0 4.0

“happy” refers to how happy people are. Icelanders are generally quite happy, as evidenced by the mean score of 8.2 and the left-skewed distribution. “aesfdrk” refers to How safe do you/would you feel walking alone in local area after dark? 1 being very safe and 4 very unsafe. People feel safe walking alone in the dark since the distribution is right-skewed and the mean is 1.5. “atchctr” stands for how emotionally attached to [country] you are. where 0 means not at all and 10 means very emotionally attached. People are quite emotionally attached as mean is at 8.2 and it shows an left-skewed distribution.

Task 2

Choose one of the three variables you just summarized in the table. This will be your current main outcome of interest.

Produce a visual that showcases the mean (average) for your outcome of interest by survey year (can be, e.g., point + line plot or ridge plot, depending on your variable). Discuss briefly what you note (i.e., add text in your markdown after the Task 2 code).

safe_by_year <- iceland_data %>%
  group_by(year) %>%
  summarize(mean_aesfdrk = mean(aesfdrk, na.rm = TRUE))

ggplot(safe_by_year, aes(x = year, y = mean_aesfdrk)) +
  geom_line(color = "purple", size = 1) +  
  geom_point(color = "blue", size = 3) +  
  labs(title = "Feeling of safety of walking alone in local area after dark (2002-2020)", 
       x = "Survey Year", 
       y = "Safeness (1-4)") +
  ylim(1, 4) +  
  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.

Icelanders had always felt safe to walk alone in local area after dark for the past years.

Task 3

Provide a comparison visual of your outcome of interest with two other countries. You can choose the geom() you prefer. Discuss briefly what you note (i.e., add text in your markdown after the Task 3 code).

ess_selected <- ess %>%
  filter(cntry %in% c("IS", "GR", "NO")) %>%
  mutate(aesfdrk = ifelse(aesfdrk %in% c(7, 8, 9), NA, aesfdrk))


task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -aesfdrk, FUN=median), y = aesfdrk, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none",
        plot.title = element_text(size = 12, face = "bold")) + 
  labs(title = "Boxplot comparison for feeling of safety of walking alone in local area after dark",
       subtitle = "(Iceland, Greece, Norway)", 
       x = "Country", 
       y = "Scale (1-4)")

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

The data from Iceland and Norway are consistent to one another, with the same distribution, IQR, median, and outliers above upper whiskers. Greece have a higher median compared to the other two, and it’s IQR is from 2-3 (safe-unsafe) meaning more people feel unsafe in Greece.

Task 4

Produce a cross-tab between your outcome of interest and a socio-demographic variable (use datasummary_crosstab). Then, calculate column percentages using cprop(), making sure to pick a second socio-demographic variable. Discuss briefly what you note (i.e., add text in your markdown after the Task 4 code).

iceland_data <- iceland_data %>%
  mutate(geo = recode(as.character(domicil), 
                      '1' = "Urban", 
                      '2' = "Peri-Urban",
                      '3' = "Rural", 
                      '4' = "Rural", 
                      '5' = "Rural",
                      '7' = NA_character_,
                      '8' = NA_character_,
                      '9' = NA_character_))

table(iceland_data$geo)
## 
## Peri-Urban      Rural      Urban 
##        952       2452        555
safe_geo <- datasummary_crosstab(aesfdrk ~ geo, data = iceland_data)

safe_geo
aesfdrk Peri-Urban Rural Urban All
1 N 491 1611 229 2336
% row 21.0 69.0 9.8 100.0
2 N 347 676 228 1254
% row 27.7 53.9 18.2 100.0
3 N 83 121 77 281
% row 29.5 43.1 27.4 100.0
4 N 25 32 19 76
% row 32.9 42.1 25.0 100.0
All N 952 2452 555 3975
% row 23.9 61.7 14.0 100.0

Ones living in rural areas both had the highest amount of people choosing feeling safe and unsafe to walk alone in local area after dark. But there was significantly more people living in Rural areas (61.7%) that took the survey so it might not be informative to look at it this way.

iceland_data <- iceland_data %>%
  mutate(religion = case_when(
    rlgblg == 2 ~ "No",
    rlgblg == 1 ~ "Yes",
    rlgblg %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(rlgblg)
  ))

table(iceland_data$religion)
## 
##   No  Yes 
## 2136 1811
table(iceland_data$aesfdrk, iceland_data$religion) %>%
  cprop()
##        
##         No    Yes   All  
##   1      61.4  56.8  59.3
##   2      30.4  33.4  31.7
##   3       6.4   7.9   7.1
##   4       1.9   1.9   1.9
##   Total 100.0 100.0 100.0

Icelanders no matter if they were belonged to particular religion or denomination or not had similar feeling to the safeness of walking alone in local area after dark.

Task 5

Choose one of the two socio-demographic variables you just worked with. Visualize the conditional probability (or column percentages) of your outcome given your selected socio-dem variable. Discuss briefly what you note (i.e., add text in your markdown after the Task 5 code).

iceland_clean <- iceland_data %>%
  filter(!is.na(religion) & !is.na(aesfdrk))

iceland_probs <- iceland_clean %>%
  count(aesfdrk, religion) %>%
  group_by(religion) %>%
  mutate(prob = n / sum(n))
ggplot(iceland_probs, aes(x = as.factor(aesfdrk), y = prob, color = religion)) +
  geom_point() +
  geom_line(aes(group = religion)) +
  labs(title = "Conditional Probabilities of feeling of safety of walking alone in local area after dark",
       subtitle = "by Belonging to a Religion",
       x = "Safeness Scale", 
       y = "Probability") +
  theme_minimal() +
  theme(plot.title = element_text(size = 12, face = "bold"))

From the graph, we can see that the red line begins higher than the green line, intersects at the scale before 2, and becomes lower than the green line at 2 till 4, where they meet. There are small difference between them at 2 and 3. It shows that people who do not belong to a religion are a little more likely to feel very safe than people who do. There is small difference between feeling safe and unsafe, where people belonging to a religion are slightly more likely to feel safe and unsafe than people who do not. It’s the same amount of people feeling very unsafe.