# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable") # add any you need here
# 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.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: 'flextable'
##
##
## The following object is masked from 'package:purrr':
##
## compose
## [[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] "rmarkdown" "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] "ggridges" "rmarkdown" "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" "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"
##
## [[11]]
## [1] "questionr" "ggridges" "rmarkdown" "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] "flextable" "questionr" "ggridges" "rmarkdown" "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")
uk_data <- ess %>%
filter(cntry == "GB") #Use this if UK data does not work
#Quesion 2f.
uk_data_table_subset <- uk_data%>%
mutate(
inctxff = ifelse(inctxff %in% c(7, 8, 9), NA, inctxff),
ecohenv = ifelse(ecohenv %in% c(7, 8, 9), NA, ecohenv),
lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale),
impenv = ifelse(impenv %in% c(6, 7, 8, 9), NA, impenv),
impsafe = ifelse(impsafe %in% c(6, 7, 8, 9), NA, impsafe),
wrclmch = ifelse(wrclmch %in% c(6, 7, 8, 9), NA, wrclmch),
ccrdprs = ifelse(ccrdprs %in% c(77, 88, 99, 66), NA, ccrdprs),
)
uk_data_v2 <- uk_data_table_subset %>%
rename(
`Left-Right Scale` = lrscale,
`Important to Care for Nature and Environment` = impenv,
`Favour or Oppose Increasing Fossil Fuel Tax to Reduce Climate Change` = inctxff,
`Economic Growth always Ends up Harming Environment` = ecohenv,
`Important to live in secure and safe surroundings` = impsafe,
`How worried about climate change` = wrclmch,
`To what extent feel personal responsibility to reduce climate change` = ccrdprs
)
uk_summary_table <- datasummary_skim(uk_data_v2 %>%
select( `Left-Right Scale`, `Important to Care for Nature and Environment`, `Favour or Oppose Increasing Fossil Fuel Tax to Reduce Climate Change`, `Economic Growth always Ends up Harming Environment`, `Important to live in secure and safe surroundings`, `How worried about climate change`, `To what extent feel personal responsibility to reduce climate change`),
title = "Table 1: Descriptive Statistics for Core Variables",
output = "flextable"
)
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
uk_summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Left-Right Scale | 12 | 11 | 5.0 | 1.9 | 0.0 | 5.0 | 10.0 |
Important to Care for Nature and Environment | 6 | 3 | 2.2 | 1.1 | 1.0 | 2.0 | 5.0 |
Favour or Oppose Increasing Fossil Fuel Tax to Reduce Climate Change | 6 | 91 | 3.1 | 1.2 | 1.0 | 3.0 | 5.0 |
Economic Growth always Ends up Harming Environment | 6 | 91 | 3.0 | 0.9 | 1.0 | 3.0 | 5.0 |
Important to live in secure and safe surroundings | 6 | 4 | 2.3 | 1.2 | 1.0 | 2.0 | 5.0 |
How worried about climate change | 6 | 85 | 3.1 | 1.0 | 1.0 | 3.0 | 5.0 |
To what extent feel personal responsibility to reduce climate change | 12 | 86 | 6.3 | 2.5 | 0.0 | 7.0 | 10.0 |
flextable::save_as_docx(uk_summary_table, path = "uk_summary_table_v2.docx", # change name to whatever you would like
width = 7.0, height = 7.0)
set_flextable_defaults(fonts_ignore=TRUE)
print(uk_summary_table, preview = "docx")
## a flextable object.
## col_keys: ` `, `Unique (#)`, `Missing (%)`, `Mean`, `SD`, `Min`, `Median`, `Max`
## header has 1 row(s)
## body has 7 row(s)
## original dataset sample:
##
## 1 Left-Right Scale
## 2 Important to Care for Nature and Environment
## 3 Favour or Oppose Increasing Fossil Fuel Tax to Reduce Climate Change
## 4 Economic Growth always Ends up Harming Environment
## 5 Important to live in secure and safe surroundings
## Unique (#) Missing (%) Mean SD Min Median Max
## 1 12 11 5.0 1.9 0.0 5.0 10.0
## 2 6 3 2.2 1.1 1.0 2.0 5.0
## 3 6 91 3.1 1.2 1.0 3.0 5.0
## 4 6 91 3.0 0.9 1.0 3.0 5.0
## 5 6 4 2.3 1.2 1.0 2.0 5.0
#Question 2h.
library(ggplot2)
library(dplyr)
library(ggridges)
uk_data_table_subset%>%
filter(!is.na(lrscale), !is.na(inctxff)) %>%
mutate(
lrscale = case_when(
lrscale %in% 0:3 ~ "Left",
lrscale %in% c(4, 5, 6) ~ "Center",
lrscale %in% 7:10 ~ "Right",
TRUE ~ as.character(lrscale)
)
) %>%
ggplot(aes(x = inctxff, y = lrscale, fill = lrscale)) +
geom_density_ridges() +
labs(title = "Attitude Towareds Increasing Fossil Fuel Tax in relation to Political Ideology",
x = "Oppostion to Increasing Fossil Fuel Tax Scale",
y = "Political Identity") +
scale_fill_manual(name = "Political Idenity", values = c("Left" = "steelblue", "Right" = "navyblue", "Center" = "grey")) +
coord_cartesian(xlim = c(1, 5)) +
theme_minimal()
## Picking joint bandwidth of 0.316
#Question 2d.
uk_data %>%
mutate(yrbrn = case_when(
yrbrn %in% c(7777, 8888, 9999) ~ NA_integer_,
TRUE ~ as.integer(yrbrn),
inctxff %in% c(7, 8, 9) ~ NA_integer_,
TRUE ~ as.integer(inctxff)
)) %>%
group_by(yrbrn) %>%
summarise(avg_inctxff = mean(inctxff, na.rm = TRUE)) %>%
ggplot(aes(x = yrbrn, y = avg_inctxff)) +
geom_line() + # or geom_smooth() +
labs(title = "Average Attitude towards Increasing Fossil Fuel Tax by Birth Year",
x = "Birth Year", y = "Average Attitude towards Increasing Fossil Fuel Tax (1-5)" ) +
xlim (1916,2002) +
ylim (1,5) +
theme_minimal()
## Warning: Removed 23 rows containing missing values (`geom_line()`).
#Question 2e.
library(dplyr)
library(tidyr)
library(ggplot2)
avg_attitude_uk <- uk_data %>%
group_by(yrbrn) %>%
summarize(mean_attitude_uk = mean(inctxff, na.rm = TRUE))
avg_attitude_all_countries <- ess %>%
group_by(yrbrn) %>%
summarize(mean_attitude_all = mean(inctxff, na.rm = TRUE))
combined_data <- left_join(avg_attitude_uk, avg_attitude_all_countries, by = "yrbrn")
long_data <- combined_data %>%
pivot_longer(
cols = starts_with ("mean_attitude"),
names_to = "Attitude_Type",
values_to = "Mean_Attitude"
)
ggplot(long_data) +
geom_line(aes(x = yrbrn, y = Mean_Attitude, color = Attitude_Type), size = 1) +
geom_point(aes(x = yrbrn, y = Mean_Attitude, color = Attitude_Type), size = 3) +
labs(title = "Average Attitude towards Increasing Fossil Fuel Tax by Birth Year",
x = "Year",
y = "Mean Attitude (1-5 scale)") +
scale_color_manual(
values = c("steelblue", "navyblue"),
name = "Dataset",
labels = c("All ESS", "UK")
) +
xlim (1916,2002) +
ylim (1,5) +
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.
## Warning: Removed 42 rows containing missing values (`geom_line()`).
## Warning: Removed 44 rows containing missing values (`geom_point()`).
#Question 2g - Part 1
table(uk_data_table_subset$lrscale, uk_data_table_subset$inctxff) %>%
cprop()
##
## 1 2 3 4 5 All
## 0 7.5 3.3 1.8 1.8 4.5 3.0
## 1 9.2 0.8 0.9 0.7 2.0 1.5
## 2 12.5 6.1 3.1 4.0 4.9 5.1
## 3 13.3 11.4 8.7 6.9 8.6 9.3
## 4 13.3 13.2 13.0 10.6 8.6 11.9
## 5 25.0 36.2 47.3 42.8 41.4 40.7
## 6 6.7 10.0 11.9 12.4 11.1 11.0
## 7 5.8 10.4 7.6 9.1 6.6 8.5
## 8 0.8 7.3 3.4 7.5 7.4 5.9
## 9 2.5 1.0 0.2 2.0 1.2 1.2
## 10 3.3 0.4 2.0 2.2 3.7 1.9
## Total 100.0 100.0 100.0 100.0 100.0 100.0
table(uk_data_table_subset$impenv, uk_data_table_subset$inctxff) %>%
cprop()
##
## 1 2 3 4 5 All
## 1 42.2 27.7 24.8 23.6 30.0 27.2
## 2 40.6 44.3 34.6 39.1 35.4 39.0
## 3 9.4 19.1 23.2 22.7 19.5 20.5
## 4 4.7 7.2 13.0 10.2 9.7 9.7
## 5 3.1 1.7 4.3 4.3 5.4 3.7
## Total 100.0 100.0 100.0 100.0 100.0 100.0
table(uk_data_table_subset$impsafe, uk_data_table_subset$inctxff) %>%
cprop()
##
## 1 2 3 4 5 All
## 1 21.6 21.0 21.3 22.1 29.8 22.7
## 2 36.8 39.3 46.7 44.1 39.6 42.3
## 3 16.8 18.1 13.7 14.2 12.4 15.1
## 4 15.2 12.3 11.1 11.5 7.6 11.3
## 5 9.6 9.2 7.2 8.0 10.5 8.6
## Total 100.0 100.0 100.0 100.0 100.0 100.0
table(uk_data_table_subset$wrclmch, uk_data_table_subset$inctxff) %>%
cprop()
##
## 1 2 3 4 5 All
## 1 4.6 1.7 4.3 6.8 18.5 6.3
## 2 6.9 14.3 25.0 31.1 30.2 23.1
## 3 26.2 51.2 51.4 48.3 33.8 46.3
## 4 37.7 27.2 15.1 11.8 12.4 18.7
## 5 24.6 5.5 4.1 1.9 5.1 5.5
## Total 100.0 100.0 100.0 100.0 100.0 100.0
table(uk_data_table_subset$ccrdprs, uk_data_table_subset$inctxff) %>%
cprop()
##
## 1 2 3 4 5 All
## 0 3.8 1.9 5.0 4.4 12.5 5.0
## 1 0.0 1.0 2.7 3.8 5.1 2.7
## 2 1.5 2.3 3.8 4.4 6.2 3.7
## 3 1.5 2.1 5.0 4.2 8.1 4.2
## 4 3.1 7.1 7.1 6.9 7.3 6.8
## 5 6.9 13.6 22.5 21.5 23.4 18.8
## 6 12.3 13.8 15.6 15.7 8.4 13.8
## 7 13.1 20.8 16.5 18.0 11.4 17.1
## 8 20.8 22.6 13.5 13.8 8.1 15.8
## 9 11.5 7.6 3.5 3.3 3.3 5.1
## 10 25.4 7.3 4.8 4.2 6.2 6.9
## Total 100.0 100.0 100.0 100.0 100.0 100.0
#Question 2g - Part 2
uk_data_table_subset %>%
group_by(lrscale) %>%
summarise(
mean_inctxff = mean(inctxff, na.rm = TRUE),
std_inctxff = sd(inctxff, na.rm = TRUE)
)
## # A tibble: 12 × 3
## lrscale mean_inctxff std_inctxff
## <dbl> <dbl> <dbl>
## 1 0 2.92 1.43
## 2 1 2.52 1.58
## 3 2 2.80 1.32
## 4 3 2.91 1.20
## 5 4 2.97 1.12
## 6 5 3.22 1.10
## 7 6 3.23 1.11
## 8 7 3.05 1.11
## 9 8 3.31 1.15
## 10 9 3.19 1.36
## 11 10 3.53 1.28
## 12 NA 3.30 1.21
uk_data_table_subset %>%
group_by(impenv) %>%
summarise(
mean_inctxff = mean(inctxff, na.rm = TRUE),
std_inctxff = sd(inctxff, na.rm = TRUE)
)
## # A tibble: 6 × 3
## impenv mean_inctxff std_inctxff
## <dbl> <dbl> <dbl>
## 1 1 3.05 1.25
## 2 2 3.06 1.17
## 3 3 3.24 1.08
## 4 4 3.29 1.05
## 5 5 3.49 1.14
## 6 NA 3.29 1.45
uk_data_table_subset %>%
group_by(impsafe) %>%
summarise(
mean_inctxff = mean(inctxff, na.rm = TRUE),
std_inctxff = sd(inctxff, na.rm = TRUE)
)
## # A tibble: 6 × 3
## impsafe mean_inctxff std_inctxff
## <dbl> <dbl> <dbl>
## 1 1 3.25 1.21
## 2 2 3.17 1.13
## 3 3 3.00 1.16
## 4 4 2.98 1.15
## 5 5 3.15 1.24
## 6 NA 2.87 1.34
uk_data_table_subset %>%
group_by(impsafe) %>%
summarise(
mean_inctxff = mean(inctxff, na.rm = TRUE),
std_inctxff = sd(inctxff, na.rm = TRUE)
)
## # A tibble: 6 × 3
## impsafe mean_inctxff std_inctxff
## <dbl> <dbl> <dbl>
## 1 1 3.25 1.21
## 2 2 3.17 1.13
## 3 3 3.00 1.16
## 4 4 2.98 1.15
## 5 5 3.15 1.24
## 6 NA 2.87 1.34
uk_data_table_subset %>%
group_by(wrclmch) %>%
summarise(
mean_inctxff = mean(inctxff, na.rm = TRUE),
std_inctxff = sd(inctxff, na.rm = TRUE)
)
## # A tibble: 6 × 3
## wrclmch mean_inctxff std_inctxff
## <dbl> <dbl> <dbl>
## 1 1 3.95 1.17
## 2 2 3.51 1.05
## 3 3 3.09 1.07
## 4 4 2.68 1.18
## 5 5 2.46 1.36
## 6 NA 3.32 1.11
uk_data_table_subset %>%
group_by(ccrdprs) %>%
summarise(
mean_inctxff = mean(inctxff, na.rm = TRUE),
std_inctxff = sd(inctxff, na.rm = TRUE)
)
## # A tibble: 12 × 3
## ccrdprs mean_inctxff std_inctxff
## <dbl> <dbl> <dbl>
## 1 0 3.73 1.21
## 2 1 3.82 0.962
## 3 2 3.56 1.12
## 4 3 3.62 1.11
## 5 4 3.22 1.12
## 6 5 3.4 1.08
## 7 6 3.07 1.08
## 8 7 3.02 1.10
## 9 8 2.79 1.11
## 10 9 2.63 1.20
## 11 10 2.62 1.36
## 12 NA 3.41 1.07