SETUP

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

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
## 
## 
## 
## Attaching package: 'ggdist'
## 
## 
## The following objects are masked from 'package:ggridges':
## 
##     scale_point_color_continuous, scale_point_color_discrete,
##     scale_point_colour_continuous, scale_point_colour_discrete,
##     scale_point_fill_continuous, scale_point_fill_discrete,
##     scale_point_size_continuous
## [[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"        
## 
## [[13]]
##  [1] "ggdist"       "questionr"    "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"        
## 
## [[14]]
##  [1] "ggdist"       "questionr"    "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"
ess <- read_fst("All-ESS-Data.fst")

TASK 1

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]
}

table(ess$happy)
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##   3933   3540   7343  13759  17016  52117  43707  87869 130802  76229  51006 
##     77     88     99 
##    330   2163    741
table(ess$imprich)
## 
##      1      2      3      4      5      6      7      8      9 
##  18611  48058  84325  98659 144488  58341   1480   4164   4367
table(ess$impenv)
## 
##      1      2      3      4      5      6      7      8      9 
## 138248 179255  88182  33878  10030   2484   1414   4380   4615
poland_data <- ess %>%
  filter(cntry == "PL") %>%
  mutate(
    imprich = ifelse(imprich %in% c(7, 8, 9), NA, imprich),
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
    impenv = ifelse(impenv %in% c(7, 8, 9), NA, impenv), 
  )

table(poland_data$happy)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##  149  151  303  552  714 2545 1625 3003 4194 2188 2038
table(poland_data$imprich)
## 
##    1    2    3    4    5    6 
##  580 2150 3515 3277 4701 1183
table(poland_data$impenv)
## 
##    1    2    3    4    5    6 
## 4351 7396 2638  745  214   50
datasummary_skim(poland_data %>% select(happy, imprich, impenv))
Unique (#) Missing (%) Mean SD Min Median Max
happy 12 1 7.0 2.1 0.0 7.0 10.0
imprich 7 13 3.8 1.3 1.0 4.0 6.0
impenv 7 13 2.0 0.9 1.0 2.0 6.0

TASK 1 COMMENTS

The three variables selected for this assignment are happy (“how happy are you”), imprich (“how important is it to be rich”), and impenv (“how important is it to care about nature & the environment”). Poland is the country of interest as according to the IMF it has a low GDP per capita & has also answered the ESS survey every year. GDP will be the metric of interest in selecting countries to compare. Essentially this will look at the interaction of a country’s residents happiness in relation to how the residents value material things and the environment based on the GDP of a country.

The summary table of all three variables Poland has a high level of happiness as evidenced by the high mean of 7.0 out of a max of 10 being extremely happy, a mid level of valuing being rich as evidenced by the mean of 3.8 out of a max of 6 being ‘not like me at all’, and a high level of caring for nature/the environment as evidenced by the low mean of 2.0 out of 6 being ‘not like me at all’.

Happiness had a higher standard deviation meaning despite the high level of happiness responses were slightly spread out with some people responding as being unhappy. Both valuing wealth and valuing nature had lower standard deviations meaning that the responses were more concentrated toward the extreme answer (‘a little’ for valuing wealth and ‘like me’ for valuing nature).

TASK 2

nature_by_year <- poland_data %>%
  group_by(year) %>%
  summarize(mean_nature = mean(impenv, na.rm = TRUE))
nature_by_year
## # A tibble: 10 × 2
##     year mean_nature
##    <dbl>       <dbl>
##  1  2002        2.07
##  2  2004        2.02
##  3  2006        2.07
##  4  2008        2.12
##  5  2010        2.08
##  6  2012        1.91
##  7  2014        1.96
##  8  2016        2.03
##  9  2018        2.10
## 10  2020      NaN
year_to_remove <- 2020
nature_by_year <- nature_by_year %>%
  filter(year != year_to_remove)

ggplot(nature_by_year, aes(x = year, y = mean_nature)) +
  geom_line(color = "#90EE90", size = 1) +
  geom_point(color = "#023020", size = 3) +
  labs(title = "Importance of Caring for Nature in Poland (2002-2018)", 
       x = "Survey Year", 
       y = "Importance (0-10)") +
  ylim(0, 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.

TASK 2 COMMENTS

Note: Poland did not respond to this question in the 2020 survey and therefore 2020 was removed from the data.

Valuing the importance of nature and the environment was chosen is the variable of interest. In Poland there was a steady response to this question over each survey year of approximately 2 (‘like me’). In 2012 the issue appears to have been slightly more salient with a very small dip, but overall this variable is consistent.

TASK 3

ess_selected <- ess %>%
  filter(cntry %in% c("PL", "IE", "DE")) %>%
  mutate(impenv = ifelse(impenv %in% c(7, 8, 9), NA, impenv))


task3hwplot <- ggplot(ess_selected, aes(x = reorder(cntry, -impenv, FUN=median), y = impenv, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Boxplot comparison for valuing nature (Germany, Ireland, Poland)", 
       x = "Country", 
       y = "Scale (0-6)")

task3hwplot
## Warning: Removed 12967 rows containing non-finite values (`stat_boxplot()`).

TASK 3 COMMENTS

Interestingly Germany and Ireland have identical responses to valuing nature, and all three countries have the same median of 2 (‘like me’) for Germany and Ireland the majority 50% of responses are evenly spread out between 1 (‘very much like me’) and 3 (‘somewhat like me’), with the last quartile spreadout between 3 (‘somewhat like me’) and 6 (‘not like me at all’). Poland, while similar, appears to have a higher concentration of 2 (‘like me’) responses and the last quartile is spread out between 2 (‘like me’) and 3 (‘somewhat like me’) with outliers at 4 (‘a little like me’), 5 (‘not like me’), and 6 (‘not like me at all’).

TASK 4

poland_data <- poland_data %>%
  mutate(
    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"
    )
  )

table(poland_data$educ_level)
## 
##    BA No BA 
##  3414 14275
poland_data <- poland_data %>%
  mutate(geo = recode(as.character(domicil), 
                      '1' = "Urban", 
                      '2' = "Urban",
                      '3' = "Rural", 
                      '4' = "Rural", 
                      '5' = "Rural",
                      '7' = NA_character_,
                      '8' = NA_character_,
                      '9' = NA_character_))

table(poland_data$geo)
## 
## Rural Urban 
## 12577  4984
impenvedu <- datasummary_crosstab(impenv ~ educ_level, data = poland_data)
impenvgeo <- datasummary_crosstab(impenv ~ geo, data = poland_data)

impenvedu
impenv BA No BA All
1 N 911 3440 4351
% row 20.9 79.1 100.0
2 N 1268 6128 7396
% row 17.1 82.9 100.0
3 N 385 2253 2638
% row 14.6 85.4 100.0
4 N 109 636 745
% row 14.6 85.4 100.0
5 N 31 183 214
% row 14.5 85.5 100.0
6 N 6 44 50
% row 12.0 88.0 100.0
All N 3414 14275 17689
% row 19.3 80.7 100.0
impenvgeo
impenv Rural Urban All
1 N 3009 1332 4351
% row 69.2 30.6 100.0
2 N 5325 2057 7396
% row 72.0 27.8 100.0
3 N 1960 675 2638
% row 74.3 25.6 100.0
4 N 536 206 745
% row 71.9 27.7 100.0
5 N 139 74 214
% row 65.0 34.6 100.0
6 N 30 20 50
% row 60.0 40.0 100.0
All N 12577 4984 17689
% row 71.1 28.2 100.0
table(poland_data$impenv, poland_data$educ_level) %>%
  cprop()
##        
##         BA    No BA All  
##   1      33.6  27.1  28.3
##   2      46.8  48.3  48.0
##   3      14.2  17.8  17.1
##   4       4.0   5.0   4.8
##   5       1.1   1.4   1.4
##   6       0.2   0.3   0.3
##   Total 100.0 100.0 100.0
table(poland_data$impenv, poland_data$geo) %>%
  cprop()
##        
##         Rural Urban All  
##   1      27.4  30.5  28.3
##   2      48.4  47.1  48.1
##   3      17.8  15.5  17.2
##   4       4.9   4.7   4.8
##   5       1.3   1.7   1.4
##   6       0.3   0.5   0.3
##   Total 100.0 100.0 100.0

TASK 4 COMMENTS

Education level and Rural/Urban divide were selected to see if there was any differences in valuing nature in Poland. However, any differences for both sociodemographics were minimal.

In terms of education people with BAs were slightly more likely to really resonate with valuing nature with 33.6% compared to 27.1%. People without BAs were more likely to not resonate as much by selecting 2 (48.3% vs 46.8%) and 3 (17.8% vs 14.2%) more, similar patterns were seen with selecting 4, 5, & 6. Both people w/ BAs and without followed the same portional spread and it can be seen that there are minimal differences with education.

Likewise with Urban dwellers were slightly more likely to resonate with valuing nature (30.5% vs 27.4%). Both urban and rural dwellers had very similar porportional spreads throughout all answers.

TASK 5

poland_clean <- poland_data %>%
  filter(!is.na(geo) & !is.na(impenv))

poland_probs <- poland_clean %>%
  count(impenv, geo) %>%
  group_by(geo) %>%
  mutate(prob = n / sum(n))

ggplot(poland_probs, aes(x = as.factor(impenv), y = prob, color = geo)) +
  geom_point() +
  geom_line(aes(group = geo)) +
  labs(title = "Conditional Probabilities of Valuing Nature",
       subtitle = "by Place of Residence",
       x = "Importance of Nature", 
       y = "Probability") +
  theme_minimal()

TASK 5 COMMENTS

When looking at a visualization of whether Urban vs Rural dwellers in Poland resonate with nature being important there is little difference. Urban dwellers were actually more likely to highly resonate with nature by selection 1 (‘very much like me’), although Rural dwellers were slightly more likely to select 2 (‘like me’) and 3 (‘somewhat like me’). For the negative responses to answering the questions the differences were negligable, answer 4 and 6 were visibly the same and answer 5 Urban dwellers were slightly more likely to select. Overall it seems like people living in both settings rate nature and the environment as being fairly important.