Setting up the Environment

packages <- c ("tidyverse", "modelsummary", "forcats", "RColorBrewer", "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "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] "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"

Loading Class Dataset

ess <- read_fst("ALL-ESS-Data.fst")
print("hell yeah this works!")
## [1] "hell yeah this works!"

Participation 3

Mission 1 - Creating tables between ess and variables of interest (eduyrs and stflife)

ess_cleaned <- ess %>%
  mutate(
    eduyrs = ifelse(eduyrs %in% c(77, 88, 99), NA, eduyrs),    
    stflife = ifelse(stflife %in% c(77, 88), NA, stflife),
  )

table(ess_cleaned$eduyrs)
## 
##     0     1     2     3     4     5     6     7     8     9    10    11  11.5 
##  3074   822  1376  3072  9433  5913 10136 10161 29136 28228 32029 49334     4 
##    12    13    14  14.5    15    16    17    18  18.5    19    20    21    22 
## 80272 45535 33862     1 34024 32604 24410 19866     1  9734  9369  3192  2625 
##    23    24    25    26    27    28    29    30    31    32    33    34    35 
##  1510  1022  1004   308   191   142    80   269    28    32    23    13    35 
##    36    37    38    39    40    41    42    43    44    45    47    48    50 
##    18     7    10     9    67     2     6     4     4    11     2     6    14 
##    51    54    55    56    60    65    76 
##     4     2     2     2     3     1     3
table(ess_cleaned$stflife)
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##   9190   6123  11739  20254  21903  56264  43866  81908 118078  67371  50950 
##     99 
##    443

Mission 2 - create a table for an additional socio-demographic varaible

# Checking variable type 
  class(ess$marsts)
## [1] "numeric"
# subset data to select the socio-demographic variable of choice = marsts - "legal marital status"
df <- ess %>%
  select(cntry, marsts)%>%
  mutate(
    marsts = recode(marsts, `66` = NA_real_, `77` = NA_real_, `88` = NA_real_, `99` = NA_real_),
  )
    
# Create table for cleaned variable
table(df$marsts)
## 
##     1     2     3     4     5     6 
##  5935   851  2416 25271 26290 80048

Mission 3 - (1) Subsetting for country of interes [Germany], (2) Cleaning & Selecting Variables of Interest

Germany_data <- ess %>%
  filter(cntry == "DE") %>%

#Cleaning Missing Data
  mutate(
    eduyrs = ifelse(eduyrs %in% c(77, 88, 99), NA, eduyrs),    
    stflife = ifelse(stflife %in% c(77, 88), NA, stflife),
    marsts = ifelse(marsts %in% c(66, 77, 88, 99), NA, marsts)    
  )

Mission 4 - datasummary for varaibles of interest

  datasummary_skim(Germany_data %>% select(eduyrs, stflife, marsts))
Unique (#) Missing (%) Mean SD Min Median Max
eduyrs 42 2 13.7 3.5 0.0 13.0 76.0
stflife 13 0 7.3 5.0 0.0 8.0 99.0
marsts 7 81 5.3 1.2 1.0 6.0 6.0

Mission 7 - Creating a Cross-tabulaltion of variables of interest

eduyrs_stflife <- datasummary_crosstab(eduyrs ~ stflife, data = Germany_data)

eduyrs_stflife
eduyrs 0 1  2  3  4  5  6  7  8  9  10  99 All
0 N 3 0 1 0 1 7 2 2 2 0 2 0 20
% row 15.0 0.0 5.0 0.0 5.0 35.0 10.0 10.0 10.0 0.0 10.0 0.0 100.0
1 N 0 0 0 0 0 2 1 1 5 1 1 0 11
% row 0.0 0.0 0.0 0.0 0.0 18.2 9.1 9.1 45.5 9.1 9.1 0.0 100.0
2 N 2 0 0 3 1 3 2 4 2 4 5 0 26
% row 7.7 0.0 0.0 11.5 3.8 11.5 7.7 15.4 7.7 15.4 19.2 0.0 100.0
3 N 4 1 4 3 4 10 4 11 11 7 16 1 76
% row 5.3 1.3 5.3 3.9 5.3 13.2 5.3 14.5 14.5 9.2 21.1 1.3 100.0
4 N 1 1 1 3 3 6 4 6 16 4 7 0 52
% row 1.9 1.9 1.9 5.8 5.8 11.5 7.7 11.5 30.8 7.7 13.5 0.0 100.0
5 N 3 1 2 5 2 8 6 15 18 8 15 0 84
% row 3.6 1.2 2.4 6.0 2.4 9.5 7.1 17.9 21.4 9.5 17.9 0.0 100.0
6 N 5 1 2 7 7 18 9 9 16 13 15 0 102
% row 4.9 1.0 2.0 6.9 6.9 17.6 8.8 8.8 15.7 12.7 14.7 0.0 100.0
7 N 4 0 10 5 16 13 24 14 26 12 15 1 140
% row 2.9 0.0 7.1 3.6 11.4 9.3 17.1 10.0 18.6 8.6 10.7 0.7 100.0
8 N 28 17 35 48 54 175 97 171 248 99 161 1 1138
% row 2.5 1.5 3.1 4.2 4.7 15.4 8.5 15.0 21.8 8.7 14.1 0.1 100.0
9 N 23 7 25 52 45 110 70 141 201 111 130 1 920
% row 2.5 0.8 2.7 5.7 4.9 12.0 7.6 15.3 21.8 12.1 14.1 0.1 100.0
10 N 48 28 56 114 108 294 173 360 576 336 340 4 2441
% row 2.0 1.1 2.3 4.7 4.4 12.0 7.1 14.7 23.6 13.8 13.9 0.2 100.0
11 N 59 38 59 105 140 359 246 440 787 398 492 11 3145
% row 1.9 1.2 1.9 3.3 4.5 11.4 7.8 14.0 25.0 12.7 15.6 0.3 100.0
12 N 90 52 129 228 208 595 388 815 1245 648 590 11 5004
% row 1.8 1.0 2.6 4.6 4.2 11.9 7.8 16.3 24.9 12.9 11.8 0.2 100.0
13 N 63 40 132 197 206 483 421 916 1451 770 527 12 5219
% row 1.2 0.8 2.5 3.8 3.9 9.3 8.1 17.6 27.8 14.8 10.1 0.2 100.0
14 N 33 26 66 106 139 282 213 513 809 476 325 4 2993
% row 1.1 0.9 2.2 3.5 4.6 9.4 7.1 17.1 27.0 15.9 10.9 0.1 100.0
15 N 24 34 64 125 120 260 213 534 875 450 272 5 2977
% row 0.8 1.1 2.1 4.2 4.0 8.7 7.2 17.9 29.4 15.1 9.1 0.2 100.0
16 N 17 13 44 81 73 188 178 447 742 446 244 1 2475
% row 0.7 0.5 1.8 3.3 2.9 7.6 7.2 18.1 30.0 18.0 9.9 0.0 100.0
17 N 9 13 27 67 54 140 144 341 551 347 164 1 1858
% row 0.5 0.7 1.5 3.6 2.9 7.5 7.8 18.4 29.7 18.7 8.8 0.1 100.0
18 N 8 4 20 40 41 92 102 278 545 364 180 0 1676
% row 0.5 0.2 1.2 2.4 2.4 5.5 6.1 16.6 32.5 21.7 10.7 0.0 100.0
19 N 9 7 17 25 23 64 55 181 347 264 119 1 1113
% row 0.8 0.6 1.5 2.2 2.1 5.8 4.9 16.3 31.2 23.7 10.7 0.1 100.0
20 N 4 7 24 31 28 55 61 171 342 212 94 0 1031
% row 0.4 0.7 2.3 3.0 2.7 5.3 5.9 16.6 33.2 20.6 9.1 0.0 100.0
21 N 0 3 12 6 12 24 24 63 107 68 35 0 354
% row 0.0 0.8 3.4 1.7 3.4 6.8 6.8 17.8 30.2 19.2 9.9 0.0 100.0
22 N 4 1 3 12 16 22 22 53 83 48 33 0 297
% row 1.3 0.3 1.0 4.0 5.4 7.4 7.4 17.8 27.9 16.2 11.1 0.0 100.0
23 N 2 3 2 3 12 10 9 30 60 38 14 0 183
% row 1.1 1.6 1.1 1.6 6.6 5.5 4.9 16.4 32.8 20.8 7.7 0.0 100.0
24 N 0 1 2 0 4 6 4 13 24 23 9 1 87
% row 0.0 1.1 2.3 0.0 4.6 6.9 4.6 14.9 27.6 26.4 10.3 1.1 100.0
25 N 0 0 2 1 3 4 5 24 21 15 7 0 82
% row 0.0 0.0 2.4 1.2 3.7 4.9 6.1 29.3 25.6 18.3 8.5 0.0 100.0
26 N 0 0 0 1 1 2 0 5 9 6 2 0 26
% row 0.0 0.0 0.0 3.8 3.8 7.7 0.0 19.2 34.6 23.1 7.7 0.0 100.0
27 N 0 0 1 0 0 2 3 2 2 5 1 0 16
% row 0.0 0.0 6.2 0.0 0.0 12.5 18.8 12.5 12.5 31.2 6.2 0.0 100.0
28 N 0 0 0 0 0 1 0 1 7 5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 3
% row 33.3 33.3 0.0 0.0 33.3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 100.0
All N 461 311 764 1310 1363 3352 2548 5694 9308 5263 3922 83 34425
% row 1.3 0.9 2.2 3.8 4.0 9.7 7.4 16.5 27.0 15.3 11.4 0.2 100.0

Mission 11 - Produce a visual for stflife for the average by survey year

#Creating a year variable
 Germany_data$year <- NA
 replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
 for(i in 1:10){
 Germany_data$year[Germany_data$essround == i] <- replacements[i]
 }

stflife_by_year <- Germany_data %>%
  group_by(year) %>%
  filter(cntry == "DE") %>%
  summarize(avg_stflife = mean(stflife, na.rm = TRUE)) 
stflife_by_year
## # A tibble: 10 × 2
##     year avg_stflife
##    <dbl>       <dbl>
##  1  2002        6.81
##  2  2004        6.70
##  3  2006        6.71
##  4  2008        6.84
##  5  2010        7.11
##  6  2012        7.48
##  7  2014        7.42
##  8  2016        7.52
##  9  2018        7.66
## 10  2020        7.96
p_yearly_stflife_avg <- ggplot(stflife_by_year, aes(x = year, y = avg_stflife)) +
  geom_line(color = "lightblue", size = 1) +
  geom_point(color = "grey", size = 2) +
    labs(title = "Life statisfaction in Germany (2002-2020)",
         x = "Year of Survey",
         y = "Average level of General Life Satisfaction" 
         )+
  ylim(0, 8) 
## 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.
p_yearly_stflife_avg

## Mission 12 - Change the y-limit

p_yearly_stflife_avg_2 <- ggplot(stflife_by_year, aes(x = year, y = avg_stflife)) +
  geom_line(color = "lightblue", size = 1) +
  geom_point(color = "darkgrey", size = 2) +
    labs(title = "Life statisfaction in Germany (2002-2020)",
         x = "Year of Survey",
         y = "Average level of General Life Satisfaction" 
         ) +
   ylim(6.5, 8)  

p_yearly_stflife_avg_2

table(Germany_data$yrbrn)
## 
## 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 
##    1    2    3    4    5    7   11    7    9   17   22   19   43   51   47   43 
## 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 
##   62  102   95   91  141  159  135  204  192  196  235  309  301  342  414  454 
## 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 
##  520  494  428  401  474  333  385  437  464  546  606  574  594  560  547  610 
## 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 
##  643  647  640  646  675  708  739  699  754  694  708  638  651  573  589  503 
## 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 
##  415  399  417  421  414  391  410  425  492  440  434  406  438  454  498  505 
## 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 
##  434  402  394  329  316  332  290  286  254  261  214  241  176  155  134  117 
## 2004 2005 2006 7777 8888 9999 
##   96  108   43  180    4  497

Mission 17 - Producing a visual for the average of outcome of interests by year of birth

germany_stflife_avg_by_birthyear <- Germany_data %>%  
   mutate(yrbrn = case_when(
    yrbrn %in% c(7777, 8888, 9999) ~ NA_integer_,
    TRUE ~ as.integer(yrbrn)
  )) %>%
  group_by(yrbrn) %>%
  summarise(mean_stflife = mean(stflife, na.rm = TRUE)) 

p_stflife_avg_by_year_of_birth <- ggplot(germany_stflife_avg_by_birthyear, aes(x = yrbrn, y = mean_stflife)) +
  geom_line() +
  labs(title = "Average Life statisfaction by Year of Birth for Germany",
         x = "Year of Birth",
         y = "Average level of General Life Satisfaction" 
         ) 

p_stflife_avg_by_year_of_birth 
## Warning: Removed 1 row containing missing values (`geom_line()`).