Mission 1

Launch a new R project and R markdown file. Name it “Lastname_Firstname_Project_202”. Set up your environment with packages you will use.

packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer") 
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: '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"        
## 
## [[13]]
##  [1] "infer"        "flextable"    "questionr"    "ggridges"     "rmarkdown"   
##  [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")

Mission 2

Filter to your country of interest and save the dataset.

gb_data <- ess %>% 
  filter(cntry == "GB") 
write_fst(gb_data, "~/Documents/R/Liana_Liu_Project_202/gb_data.fst") 

Mission 3

Clean your environment and load in the filtered dataset.

rm(list=ls()); gc()
##           used (Mb) gc trigger   (Mb) limit (Mb)   max used    (Mb)
## Ncells 1263834 67.5    2131380  113.9         NA    2131380   113.9
## Vcells 2154288 16.5 1257029638 9590.4      16384 1361184694 10385.1
dgb <- read_fst("~/Documents/R/Liana_Liu_Project_202/gb_data.fst")

Mission 4

Produce and save a data summary output (using data summary skim) for potential outcomes of interest on a similar scale (e.g., 0-10, or 1 to 6, or binary). Add a title. You can do so while coding (explore package information for flextable and/or modelsummary) or add it directly in the word file. Title should be something like: Table 1: Descriptive Statistics for outcome variables. You can alter the title as you see fit.

Variable References:

happy: https://ess-search.nsd.no/variable/f38e3867-0b7a-46de-9105-639b7a20a981

imprich: https://ess-search.nsd.no/variable/a5a8b42e-cf73-42f4-b7dc-6b027df1c093

netusoft: https://ess-search.nsd.no/variable/08ae7dcc-4c71-4537-b7fc-c322371b095c

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

gb_data <- dgb

gb_data_table_subset <- gb_data %>%
  mutate(
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy), 
    imprich = ifelse(imprich %in% c(7, 8, 9), NA, imprich), 
    netusoft = ifelse(netusoft %in% c(7, 8, 9), NA, netusoft) 
  ) 

summary_table <- datasummary_skim(gb_data_table_subset %>% select(happy, imprich, netusoft), 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.
summary_table

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

happy

12

0

7.5

1.9

0.0

8.0

10.0

imprich

7

2

4.3

1.3

1.0

5.0

6.0

netusoft

6

75

4.2

1.4

1.0

5.0

5.0

gb_data_v2 <- gb_data_table_subset %>%
  rename(
    `How happy are you` = happy,
    `Important to be rich, have money and expensive things` = imprich,
    `Internet use, how often` = netusoft
  )
summary_table_v2 <- datasummary_skim(gb_data_v2 %>% select(`How happy are you`,`Important to be rich, have money and expensive things`, `Internet use, how often`), 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.
summary_table_v2 <- add_header_lines(summary_table_v2, values = "Table 1: Descriptive Statistics for outcome variables")

summary_table_v2

Table 1: Descriptive Statistics for outcome variables

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

How happy are you

12

0

7.5

1.9

0.0

8.0

10.0

Important to be rich, have money and expensive things

7

2

4.3

1.3

1.0

5.0

6.0

Internet use, how often

6

75

4.2

1.4

1.0

5.0

5.0

Mission 6

Produce and save a plot as a PDF for your outcome of interest (any will do here).

avg_happy_by_year <- aggregate(happy ~ year + imprich, data=gb_data_table_subset, mean)

p1 <- ggplot(avg_happy_by_year, aes(x=year, y=happy, color=as.factor(imprich))) + 
  geom_line(aes(group=imprich)) +
  labs(title="Mean of Happiness by year in relation to", 
       subtitle = "the importance of being rich in the United Kingdom",
       x="Survey Year", 
       y="Happiness 0 = extremely unhappy - 10 = extremely happy") +
  scale_color_discrete(name="Importance of being rich", labels=c("Very much like me", "Like me", "Somewhat like me", "A little like me", "Not like me", "Not like me at all")) +
  theme_minimal()

p1

ggsave(filename = "plot1.pdf", plot = p1, device = "pdf", width = 8, height = 6)

Mission 9

Produce a visualization between birth year and your outcome, but only visualize the years for Millennials and Boomers (you can also visualize other birth year ranges, or truncate specifically to say 1950 to 1980 – the idea is to practice visualizing only certain aspects of interest).

ess <- read_fst("All-ESS-Data.fst")
millennials_data <- ess %>%
  filter(cntry == "GB", yrbrn > 1980 & yrbrn < 1997) 


boomers_data <- ess %>%
  filter(cntry == "GB", yrbrn > 1945 & yrbrn < 1965)


millennials_data_cleaned <- millennials_data %>%
  mutate(
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy)
  ) %>%
  group_by(yrbrn) %>%
  summarise(
    avg_happy = mean(happy, na.rm = TRUE)
  )

boomers_data_cleaned <- boomers_data %>%
  mutate(
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy)
  ) %>%
  group_by(yrbrn) %>%
  summarise(
    avg_happy = mean(happy, na.rm = TRUE)
  )
  
ggplot() +
  geom_point(data = millennials_data_cleaned, aes(x = yrbrn, y = avg_happy), color = "purple", alpha = 0.6) +
  geom_point(data = boomers_data_cleaned, aes(x = yrbrn, y = avg_happy), color = "purple", alpha = 0.6) +
  geom_smooth(data = millennials_data_cleaned, aes(x = yrbrn, y = avg_happy), method = "lm", formula = y ~ poly(x, 1), color = "purple") +
  geom_smooth(data = boomers_data_cleaned, aes(x = yrbrn, y = avg_happy), method = "lm", formula = y ~ poly(x, 1), color = "purple") +
  labs(
    title = "Average happiness Score by Birth Year (Millennials vs. Boomers)",
    x = "Year of Birth",
    y = "Average happiness 0 = Extremely unhappy - 10 = Extremely happy"
  ) +
  scale_color_manual(values = c("purple", "pirple")) +
  theme_bw()