Mission 1 & Setup

# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer") # 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"        
## 
## [[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"
setwd("C:/Users/2expl/Desktop/UToronto/Year 2/SOC202")

# read data
ess <- read_fst("All-ESS-Data.fst")

Mission 2

# Subsetting Great Britan from ESS
GB_data <- ess %>% 
  filter(cntry == "GB")

# Saving Great Britan subset to the project folder
write_fst(GB_data, "C:/Users/2expl/Desktop/UToronto/Year 2/SOC202/Chmelauskas_Adam_Project_202/GB_data.fst") 

Mission 3

#Clearing enviroment and loading chosen subset for easier manipulation
rm(list=ls()); gc()
##           used (Mb) gc trigger   (Mb)   max used    (Mb)
## Ncells 1258520 67.3    2273074  121.4    2273074   121.4
## Vcells 2128076 16.3 1256984332 9590.1 1361139193 10384.7
GB_df <- read_fst("C:/Users/2expl/Desktop/UToronto/Year 2/SOC202/Chmelauskas_Adam_Project_202/GB_data.fst")

Mission 4

#Subsetting the three Variables of interest and removing any non-answer values from the dataset.

GB_data_subset <- GB_df %>%
  filter(cntry == "GB") %>%
  mutate(
    cgtsmke = ifelse(cgtsmke %in% c(7, 8, 9), NA, cgtsmke),
    health = ifelse(health %in% c(7, 8, 9), NA, health),
    edulvla = ifelse(edulvla %in% c(55, 77, 88, 99), NA, edulvla)
  ) 
  
#renaming variables for easier understanding in output tables 
GB_data_v2 <- GB_data_subset %>%
  select(cgtsmke, health, edulvla) %>% 
  rename(
    `How Often Does the Respondent Smoke` = cgtsmke,
    `General Health` = health,
    `Highest Education Level` = edulvla
  )

#Creating summary table (via flexTable) using updated variables
summary_table_v2 <- datasummary_skim(GB_data_v2, 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.
#saving to project folder in .Docx format
flextable::save_as_docx(summary_table_v2, path = "C:/Users/2expl/Desktop/UToronto/Year 2/SOC202/Chmelauskas_Adam_Project_202/Homework5_summary_table_GB.docx", # change name to whatever you would like
                       width = 5.0, height = 5.0) 

summary_table_v2

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

How Often Does the Respondent Smoke

6

89

3.6

1.5

1.0

4.0

5.0

General Health

6

0

2.1

1.0

1.0

2.0

5.0

Highest Education Level

5

59

2.9

1.7

1.0

2.0

5.0

Mission 9

#set working directory to open ESS
setwd("C:/Users/2expl/Desktop/UToronto/Year 2/SOC202")

# read data
ess <- read_fst("All-ESS-Data.fst")

# Filter the data for Millennial and Boomers
millennials_data <- ess %>%
  filter(cntry == "GB", yrbrn > 1980 & yrbrn < 1997) 

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

# Cleaning the variables (millennial)
millennials_data_cleaned <- millennials_data %>%
  mutate(
    cgtsmke = ifelse(cgtsmke %in% c(7, 8, 9), NA, cgtsmke)
  ) %>%
  group_by(yrbrn) %>%
  summarise(
    avg_cgtsmke.M = mean(cgtsmke, na.rm = TRUE)
  )

#leaning the variables (Boomers)
boomers_data_cleaned <- boomers_data %>%
  mutate(
    cgtsmke = ifelse(cgtsmke %in% c(7, 8, 9), NA, cgtsmke)
  ) %>%
  group_by(yrbrn) %>%
  summarise(
    avg_cgtsmke.B = mean(cgtsmke, na.rm = TRUE),
  )

ggplot() +
  geom_point(data = millennials_data_cleaned, aes(x = yrbrn, y = avg_cgtsmke.M), color = "blue", alpha = 0.6) +
  geom_point(data = boomers_data_cleaned, aes(x = yrbrn, y = avg_cgtsmke.B), color = "red", alpha = 0.6) +
  geom_smooth(data = millennials_data_cleaned, aes(x = yrbrn, y = avg_cgtsmke.M), method = "lm", formula = y ~ poly(x, 1), color = "blue") +
  geom_smooth(data = boomers_data_cleaned, aes(x = yrbrn, y = avg_cgtsmke.B), method = "lm", formula = y ~ poly(x, 1), color = "red") +
  labs(
    title = "Average Smoking Habits by Birth Year in the United Kingdom (Millennials vs. Boomers)",
    x = "Year of Birth",
    y = "Average Frequancy of Smoking"
  ) +
  scale_color_manual(values = c("black", "black"))