R Markdown
# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr") # 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: '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")
This is my first programming line
print("hell yeah this works!")
## [1] "hell yeah this works!"
Mission 1
table(ess$cntry)
##
## AL AT BE BG CH CY CZ DE DK EE ES FI FR
## 1201 15225 17451 13240 16925 6065 20090 34425 12408 16856 19452 19532 19038
## GB GR HR HU IE IL IS IT LT LU LV ME MK
## 20979 12558 6535 16642 22233 16218 3975 10178 11652 3187 3921 2478 1429
## NL NO PL PT RO RS RU SE SI SK TR UA XK
## 18329 16065 17689 17881 2146 3548 12458 18216 13484 11292 4272 9987 1295
sweden_data <- ess[ess$cntry == "SE", ]
table(sweden_data$basinc)
##
## 1 2 3 4 7 8
## 511 392 463 80 4 101
sweden_basinc_cleaned <- sweden_data %>%
filter(cntry == "SE") %>%
mutate(
basinc = ifelse(basinc %in% c(7, 8, 9), NA, basinc)) %>%
select(basinc)
table(sweden_basinc_cleaned)
## basinc
## 1 2 3 4
## 511 392 463 80
Mission 2
table(ess$mbtru)
##
## 1 2 3 7 8 9
## 84012 113377 286391 697 1948 2241
mbtru_cleaned <- ess %>%
mutate(
mbtru = ifelse(mbtru %in% c(7, 8, 9), NA, mbtru)) %>%
select(mbtru)
table(mbtru_cleaned)
## mbtru
## 1 2 3
## 84012 113377 286391
Mission 3
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]
}
sweden_data <- ess %>% filter(cntry == "SE") %>% select(cntry, yrbrn, basinc, year)
sweden_data_cleaned <- sweden_data %>%
filter(cntry == "SE", year == 2016) %>%
select(basinc, yrbrn)
sweden_data_cleaned <- sweden_data_cleaned %>%
mutate(basinc = ifelse(basinc %in% c(7, 8, 9), NA, basinc))
sweden_data_cleaned <- sweden_data_cleaned %>%
mutate(yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn))
table(sweden_data_cleaned)
## yrbrn
## basinc 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
## 1 4 1 4 2 2 1 6 1 5 7 4 5 8 4
## 2 4 0 1 1 0 2 2 1 2 2 3 1 9 6
## 3 6 1 0 3 0 2 0 2 3 3 3 0 6 6
## 4 0 0 1 0 1 0 1 0 1 1 0 0 1 0
## yrbrn
## basinc 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
## 1 5 3 7 7 7 15 12 12 15 15 8 9 15 12
## 2 4 5 6 6 6 4 6 7 7 7 4 9 8 9
## 3 5 1 7 3 5 7 12 15 9 9 5 7 9 7
## 4 1 0 0 2 0 1 1 2 1 2 1 2 0 1
## yrbrn
## basinc 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
## 1 8 7 14 6 9 5 10 11 8 11 10 15 6 10
## 2 7 9 6 8 4 6 4 3 4 5 6 5 3 6
## 3 9 4 7 7 4 5 11 10 4 6 9 3 13 5
## 4 2 1 0 3 3 0 2 0 4 2 1 4 0 0
## yrbrn
## basinc 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
## 1 7 13 3 10 9 5 6 13 10 5 3 4 7 5
## 2 11 9 8 2 8 6 5 7 7 9 11 5 10 4
## 3 4 6 4 5 8 11 9 8 11 11 5 5 5 8
## 4 1 0 0 1 0 1 3 1 1 1 1 2 2 0
## yrbrn
## basinc 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
## 1 6 9 5 7 5 6 7 3 4 9 4 3 5 1
## 2 1 7 3 5 4 3 2 4 5 5 8 3 6 7
## 3 8 6 4 11 5 8 6 6 10 3 7 11 1 4
## 4 0 3 4 1 1 1 1 2 2 2 1 2 0 2
## yrbrn
## basinc 1996 1997 1998 1999 2000 2001
## 1 2 2 1 2 0 2
## 2 7 6 6 3 2 5
## 3 11 6 5 7 3 8
## 4 1 0 0 1 0 0
Mission 4
datasummary_skim(sweden_data_cleaned)
Unique (#)
Missing (%)
Mean
SD
Min
Median
Max
basinc
5
7
2.1
0.9
1.0
2.0
4.0
yrbrn
77
0
1964.5
19.1
1926.0
1964.0
2001.0
Mission 8
table(sweden_data_cleaned$basinc, sweden_data_cleaned$yrbrn) %>%
cprop() %>%
as.data.frame() %>%
filter(Var1 != "Total",
Var2 != "All") %>%
ggplot(aes(x=Var2 %>% as.character() %>% as.integer(),
y=Freq,
color=Var1)) +
geom_point() +
labs(title="Attitude Towards a Basic Income Scheme by Birth Year",
x = "Birth Year",
color = "Attitude Towards a Basic Income Scheme")
Mission 10
# double check clean
sweden_data_cleaned <- sweden_data_cleaned %>%
filter(!is.na(yrbrn) & !is.na(basinc))
# visualize
table(sweden_data_cleaned$basinc, sweden_data_cleaned$yrbrn) %>%
cprop() %>%
as.data.frame() %>%
filter(Var1 != "Total",
Var2 != "All") %>%
ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
geom_col(position = "dodge") +
labs(title="Attitude Towards a Basic Income Scheme", x = "Not in Favor/In Favor of a Basic Income Scheme")
Mission 13
ess_data_cleaned <- ess %>%
filter(year == 2016) %>%
select(basinc, yrbrn)
ess_data_cleaned <- ess_data_cleaned %>%
mutate(basinc = ifelse(basinc %in% c(7, 8, 9), NA, basinc))
ess_data_cleaned <- ess_data_cleaned %>%
mutate(yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn))
avg_basinc_sweden <- sweden_data_cleaned %>%
group_by(yrbrn) %>%
summarize(mean_basinc_sweden = mean(basinc, na.rm = TRUE))
avg_basinc_ess <- ess_data_cleaned %>%
summarize(mean_basinc_ess = mean(basinc, na.rm = TRUE))
combined_data <- cross_join(avg_basinc_sweden, avg_basinc_ess)
long_data <- combined_data %>%
pivot_longer(
cols = starts_with("mean_basinc"),
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 = "Mean Attitude Towards a Basic Income Scheme by Birth Year",
x = "Birth Year",
y = "Mean Attitude Towards a Basic Income Scheme (0-4 scale)") +
ylim(2, 4)
## 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 26 rows containing missing values (`geom_point()`).
Mission 19
sweden_data_cleaned <- sweden_data_cleaned %>%
mutate(birth_decade = case_when(
yrbrn >= 1920 & yrbrn <= 1929 ~ "1920-1929",
yrbrn >= 1930 & yrbrn <= 1939 ~ "1930-1939",
yrbrn >= 1940 & yrbrn <= 1949 ~ "1940-1949",
yrbrn >= 1950 & yrbrn <= 1959 ~ "1950-1959",
yrbrn >= 1960 & yrbrn <= 1969 ~ "1960-1969",
yrbrn >= 1970 & yrbrn <= 1979 ~ "1970-1979",
yrbrn >= 1980 & yrbrn <= 1989 ~ "1980-1989",
yrbrn >= 1990 & yrbrn <= 2000 ~ "1990-2000",
TRUE ~ NA_character_
))
filtered_data <- sweden_data_cleaned %>% filter(!is.na(birth_decade))
hist2d_data <- filtered_data %>%
count(basinc, birth_decade)
ggplot(hist2d_data, aes(x = basinc, y = birth_decade, fill = n)) +
geom_tile() +
scale_fill_viridis_c() +
labs(title = "Heatmap of Attitudes Towards a Basic Income Scheme across Birth Decades",
x = "Attitude Towards a Basic Income Scheme", y = "Birth Decade") +
theme_minimal()