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.

# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")

# 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"

Mission 2

Filter to your country of interest and save the dataset.

ess <- read_fst("All-ESS-Data.fst")
sweden_data <- ess %>% 
  filter(cntry == "SE")

write_fst(sweden_data, "~/Documents/__School/2324 UofT/SOC202/R/Homework_6_Project/sweden_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 1260719 67.4    2129845  113.8         NA    2129845   113.8
## Vcells 2152236 16.5 1257034592 9590.5      16384 1353821876 10328.9
sweden_df <- read_fst("~/Documents/__School/2324 UofT/SOC202/R/Homework_6_Project/sweden_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.

sweden_df$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)

for(i in 1:10){
  sweden_df$year[sweden_df$essround == i] <- replacements[i]}
  
sweden_df <- sweden_df %>% 
  mutate(
    ipcrtiv = ifelse(ipcrtiv %in% c(7, 8, 9), NA, ipcrtiv), 
    ipadvnt = ifelse(ipadvnt %in% c(7, 8, 9), NA, ipadvnt),
    impfun = ifelse(impfun %in% c(7, 8, 9), NA, impfun)
  )

datasummary_skim(sweden_df %>% select(ipcrtiv, ipadvnt, impfun), output = "flextable", 
                 title = "Table 1: Descriptive Statistics for Outcome Variables of Interest")
Table 1: Descriptive Statistics for Outcome Variables of Interest

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

ipcrtiv

7

20

2.5

1.2

1.0

3.0

6.0

ipadvnt

7

20

3.9

1.4

1.0

4.0

6.0

impfun

7

20

2.7

1.2

1.0

3.0

6.0

Mission 5

Produce and save a data summary output (using data summary skim) for socio-demographic variables. 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 socio-demographic variables. You can alter the title as you see fit.

new <- sweden_df %>%
  mutate(
    gndr = ifelse(gndr %in% c(9), NA, gndr),
    edulvlb = ifelse(edulvlb %in% c(5555, 7777, 8888, 9999), NA, edulvlb),
    domicil = ifelse(domicil %in% c(7, 8, 9), NA, domicil),
    brncntr = ifelse(brncntr %in% c(7, 8, 9), NA, brncntr)
    )

datasummary_skim(new %>% select(gndr, edulvlb, domicil, brncntr), output = "flextable",
  title ="Table 2: Descriptive Statistics for Socio-Demographic Variables")
Table 2: Descriptive Statistics for Socio-Demographic Variables

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

gndr

3

0

1.5

0.5

1.0

2.0

2.0

edulvlb

15

43

411.4

191.4

0.0

323.0

800.0

domicil

6

0

2.9

1.2

1.0

3.0

5.0

brncntr

3

0

1.1

0.3

1.0

1.0

2.0

Mission 6

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

fun_by_year <- sweden_df %>%
  group_by(year) %>%
  summarize(mean_fun = mean(impfun, na.rm = TRUE)) %>% 
  filter(!is.na(mean_fun))
fun_by_year
## # A tibble: 9 × 2
##    year mean_fun
##   <dbl>    <dbl>
## 1  2002     2.92
## 2  2004     2.82
## 3  2006     2.77
## 4  2008     2.78
## 5  2010     2.69
## 6  2012     2.53
## 7  2014     2.59
## 8  2016     2.62
## 9  2018     2.71
p1 <- ggplot(fun_by_year, aes(x = year, y = mean_fun)) +
  geom_line(color = "blue", size = 1) + 
  geom_point(color = "red", size = 3) + 
  labs(title = "Belief in the Importance of Having Fun by Year (2002-2018)", 
       x = "Survey Year", 
       y = "Belief (0-10)") +
  ylim(0, 10) +
  theme_minimal()

p1

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