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")
table(ess$wrclmch)
## 
##     1     2     3     4     5     6     7     8     9 
##  4793 16034 44210 28111  7832  1954    63   645   430
table(ess$yrbrn)
## 
## 1885 1893 1900 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 
##    1    1    5    1    1    3    5    3    7    9   33   41   53   91  109  140 
## 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 
##  157  218  267  332  445  695  811 1093 1169 1323 1660 1815 1929 2439 2604 3071 
## 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 
## 3280 3445 3625 3869 4280 4620 4802 5206 5634 6190 5848 5884 6055 6366 6642 7347 
## 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 
## 7797 7961 8017 8586 7963 8293 8008 8206 8401 8449 8303 8423 8250 9020 8483 8652 
## 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 
## 8347 8705 8836 8543 8313 8407 8273 8627 8054 8153 7656 7820 7879 7691 7463 7482 
## 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 
## 7388 8020 7173 7272 6902 6947 7059 6963 6616 6364 6020 5651 4970 4550 4372 3567 
## 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 7777 8888 9999 
## 3454 3062 2764 2318 2095 1884 1476 1243 1059  698  574  294   10  962  245 1898
table(ess$impenv)
## 
##      1      2      3      4      5      6      7      8      9 
## 138248 179255  88182  33878  10030   2484   1414   4380   4615

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).

Vairable details: 1. wrclmch - https://ess-search.nsd.no/variable/query/wrclmch/1 2. yrbrn - https://ess-search.nsd.no/variable/query/yrbrn/1 3. impenv- https://ess-search.nsd.no/en/variable/query/impenv/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]
}

france_data <- ess %>% 
  filter(cntry == "FR") %>% 
  mutate(
    wrclmch = ifelse(wrclmch %in% c(7, 8, 9), NA, wrclmch),
    
    yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn), 
    
    impenv = ifelse(impenv %in% c(7, 8, 9), NA, impenv), 
  )

datasummary_skim(france_data %>% select(wrclmch, yrbrn, impenv))
Unique (#) Missing (%) Mean SD Min Median Max
wrclmch 7 79 3.3 1.0 1.0 3.0 6.0
yrbrn 99 0 1961.9 19.1 1905.0 1962.0 2005.0
impenv 7 2 2.2 1.2 1.0 2.0 6.0

My three chosen variables were wrclmch (How worried about climate change), yrbrn (year born), and impenv (Important to care for nature and environment). By observing the data summary table of my three variables of interest, I notice that the average year born of 1961~1962 responds as they somewhat care for the environment, but does not very worry about the climate change. I believe the reason for this outcome is because year born of 1961~1962 had less alertness and seriousness of climate change compared to this generation. While people still cared for the nature and environment, the issue of climate change did not impact much to people born in 1961~1962.

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).

care_by_year <- france_data %>%
  group_by(year) %>%
  summarize(mean_care = mean(impenv, na.rm = TRUE))
care_by_year
## # A tibble: 10 × 2
##     year mean_care
##    <dbl>     <dbl>
##  1  2002      2.16
##  2  2004      2.10
##  3  2006      2.07
##  4  2008      2.21
##  5  2010      2.31
##  6  2012      2.23
##  7  2014      2.30
##  8  2016      2.31
##  9  2018      2.08
## 10  2020      2.18
ggplot(care_by_year, aes(x = year, y = mean_care)) +
  geom_line(color = "blue", size = 1) +  
  geom_point(color = "red", size = 3) +  
  labs(title = "Care for nature and environment in France (2002-2020)", 
       x = "Survey Year", 
       y = "Care (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.

Firstly, I chose a point + line plot, because it allows to give more cleaer visualization. This plot allows me to observe that the care for the nature and environment hasn’t changed dramatically throughout the survey year in France. It is visible that most people in France do care about the nature and environment, however, it has slightly fallen during the year 2017~2018.

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("FR", "ES", "NL")) %>%
  mutate(impenv = ifelse(impenv %in% c(7, 8, 9), NA, impenv))


task3plot <- 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 care in environment (France, Spain, Netherlands)", 
       x = "Country", 
       y = "Scale (1-6)")

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

The median care level in Spain appears to be around 3, which is the lowest among the three countries.Spain has a relatively smaller interquartile range (IQR) indicating less variability in the responses. There are several outliers above the upper whisker, suggesting that a small percentage of respondents in Spain have significantly higher care levels than most of the population. France’s median care level appears to be around 4. The IQR for France is larger than that of Spain, suggesting more variability in care levels among respondent, and there don’t seem to be any outliers from France. The median care level in the Netherlands is slightly above 4, making it the highest among the three countries. The Netherlands has an IQR similar to France’s, indicating comparable variability. There are a few outliers above the upper whisker, indicating a small percentage of respondents in the Netherlands with significantly higher care levels.

When looking at the median levels of care for nature and environment, the Netherlands leads, followed by France, and lastly Spain. Spain shows a tighter concentration of responses, but also a notable number of individuals with high care, whereas responses from France and the Netherlands are more spread out.

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).

france_data <- france_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(france_data$educ_level)
## 
##    BA No BA 
##  4235 14803
france_data <- france_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(france_data$geo)
## 
## Peri-Urban      Rural      Urban 
##       2361      13087       3584
impenvedulevel <- datasummary_crosstab(impenv ~ educ_level, data = france_data)
impenvgeo <- datasummary_crosstab(impenv ~ geo, data = france_data)

impenvedulevel
impenv BA No BA All
1 N 1444 5063 6507
% row 22.2 77.8 100.0
2 N 1351 4466 5817
% row 23.2 76.8 100.0
3 N 823 2571 3394
% row 24.2 75.8 100.0
4 N 434 1809 2243
% row 19.3 80.7 100.0
5 N 86 486 572
% row 15.0 85.0 100.0
6 N 21 127 148
% row 14.2 85.8 100.0
All N 4235 14803 19038
% row 22.2 77.8 100.0
impenvgeo
impenv Peri-Urban Rural Urban All
1 N 735 4575 1196 6507
% row 11.3 70.3 18.4 100.0
2 N 768 3975 1072 5817
% row 13.2 68.3 18.4 100.0
3 N 435 2311 648 3394
% row 12.8 68.1 19.1 100.0
4 N 291 1526 426 2243
% row 13.0 68.0 19.0 100.0
5 N 81 368 122 572
% row 14.2 64.3 21.3 100.0
6 N 20 91 37 148
% row 13.5 61.5 25.0 100.0
All N 2361 13087 3584 19038
% row 12.4 68.7 18.8 100.0
table(france_data$impenv, france_data$educ_level) %>%
  cprop()
##        
##         BA    No BA All  
##   1      34.7  34.9  34.8
##   2      32.5  30.8  31.1
##   3      19.8  17.7  18.2
##   4      10.4  12.5  12.0
##   5       2.1   3.3   3.1
##   6       0.5   0.9   0.8
##   Total 100.0 100.0 100.0
table(france_data$impenv, france_data$geo) %>%
  cprop()
##        
##         Peri-Urban Rural Urban All  
##   1      31.5       35.6  34.2  34.8
##   2      33.0       30.9  30.6  31.1
##   3      18.7       18.0  18.5  18.2
##   4      12.5       11.9  12.2  12.0
##   5       3.5        2.9   3.5   3.1
##   6       0.9        0.7   1.1   0.8
##   Total 100.0      100.0 100.0 100.0

In the data, both BA (34.7%) and No BA (34.9%) groups largely identify with strongly valuing environmental care. Across all respondents, 34.8% feel this sentiment is “very much like them”, when it comes to caring for the nature and environment. There are less respondents as the levels increase, which indicates the less care for the nature. The data does not show a clear different when it comes to the education level (BA and without). However, it seems visible that respondents with BA tend to care more for the nature and notice the importance of the environment compared to respondents with no BA (observable in levels 4-6). Along with the place of residence, there is not much of a difference when it comes to rural areas and urabn areas. Most respondents show a care for the nature taking 35.6% (rural), and 34.2% (urabn), and shows a rapid decrease from level 5 which indicates ’ Not like me’ when it comes to caring for the environment.

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).

df <- france_data %>%
  filter(!is.na(educ_level) & !is.na(impenv))

table(df$impenv)
## 
##    1    2    3    4    5    6 
## 6507 5817 3394 2243  572  148
table(df$impenv, df$educ_level) %>%
  cprop() %>%
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge") +
  labs(title="Caring for the environment in France",
       y = "Conditional Percentage",
       x = "Caring to not caring",
       fill = "At least BA vs. Not")

In the graph “Caring for the environment in France”, those with at least a BA degree and those without display similar sentiments towards environmental care. Both groups show a high percentage of individuals who strongly value environmental care (level 1), but the inclination slightly diminishes as we move to level 2. By level 3, a noticeable drop is evident in both categories. For the higher levels, indicating lesser concern for the environment, the percentages are significantly lower, highlighting that a majority in France, regardless of educational background, lean towards valuing environmental care.

Throughout the Homework assignment 5, I chose the country France with the variables wrclmch, yrbrn, and impenv. I chose France because I thought France was always involved in the issues of climate change and the environment. While my other major is on Environmental Studies, I wanted to do a research on France, and if the respondents actually took a care for the nature and climate change issues.

Throughout the tasks, I was able to notice quite an interesting observations. However, among the tasks, I personally would like to highlight the last graph, which is on the scale of caring for the nature among people with different education levels (BA and no BA). I thought that this graph was interesting because I believed that the importance of education allows people to be aware of the issues of the environment. Nonetheless, the produced visual representation displays that education level does not display a clear difference of sentiments towards environmental care. Therefore, throughout this observation, I was able to learn that education level does not clearly matter when it comes to people’s care for environment.