Mission 1

Install packages

packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "tinytex")
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
## 
## 
## 
## Attaching package: 'flextable'
## 
## 
## The following objects are masked from 'package:kableExtra':
## 
##     as_image, footnote
## 
## 
## 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] "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"        
## 
## [[13]]
##  [1] "flextable"    "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [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"        
## 
## [[14]]
##  [1] "tinytex"      "flextable"    "questionr"    "ggridges"     "rmarkdown"   
##  [6] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
## [11] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [16] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [21] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [26] "utils"        "datasets"     "methods"      "base"

Import ESS Data

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

Mission 2

Filter for Russian data

russian_data <- ess %>%
  filter(cntry == "RU")

Save the data (Skill 1)

write_fst(russian_data, "/Users/michaelsu/Desktop/SOC202/Su_Michael_Project_202/russian_data.fst")

Mission 3

Clean environment (SKill 2)

rm(list=ls()); gc()
##           used (Mb) gc trigger   (Mb) limit (Mb)   max used    (Mb)
## Ncells 1263979 67.6    2126972  113.6         NA    2126972   113.6
## Vcells 2157078 16.5 1257021608 9590.4     102400 1338554454 10212.4

Load filtered dataset (Skill 3)

rus_frame <- read_fst("/Users/michaelsu/Desktop/SOC202/Su_Michael_Project_202/russian_data.fst")

Mission 4 (Skill 4)

Create the year variables

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

Produce a data summary output/table

russian_data <- rus_frame
russian_data_table_subset <- russian_data %>%
  mutate(
    trstlgl = ifelse(trstlgl %in% c(77, 88, 99), NA, trstlgl),
    trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl),
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy)
  )
summary_table <- datasummary_skim(russian_data_table_subset %>% select(trstlgl, trstprl, happy), 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

trstlgl

12

6

3.9

2.7

0.0

4.0

10.0

trstprl

12

6

3.7

2.7

0.0

4.0

10.0

happy

12

2

6.1

2.2

0.0

6.0

10.0

Rename variables

russian_data_v2 <- russian_data_table_subset %>%
  rename(
    `Trust in the legal system` = trstlgl,
    `Trust in country's parliament` = trstprl,
    `How happy are you` = happy
  )

Reproduce the data summary table

summary_table_v2 <- datasummary_skim(russian_data_v2 %>% select(`Trust in the legal system`, `Trust in country's parliament`, `How happy are you`), 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

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

Trust in the legal system

12

6

3.9

2.7

0.0

4.0

10.0

Trust in country's parliament

12

6

3.7

2.7

0.0

4.0

10.0

How happy are you

12

2

6.1

2.2

0.0

6.0

10.0

Save as word file

flextable::save_as_docx(summary_table_v2, path = "Table 1: Descriptive Statistics for outcome variables.docx", width = 10.0, height = 6.0)

*Note: Changed title of the table in the word doc

Save as PDF file

set_flextable_defaults(fonts_ignore=TRUE)
print(summary_table_v2, preview = "pdf")
## a flextable object.
## col_keys: ` `, `Unique (#)`, `Missing (%)`, `Mean`, `SD`, `Min`, `Median`, `Max` 
## header has 1 row(s) 
## body has 3 row(s) 
## original dataset sample: 
##                                 Unique (#) Missing (%) Mean  SD Min Median  Max
## 1     Trust in the legal system         12           6  3.9 2.7 0.0    4.0 10.0
## 2 Trust in country's parliament         12           6  3.7 2.7 0.0    4.0 10.0
## 3             How happy are you         12           2  6.1 2.2 0.0    6.0 10.0

Mission 5

Data summary table for socio-demographic variables

russian_data <- rus_frame
russian_data_table_sdv <- russian_data %>%
  mutate(
    chldhhe = ifelse(chldhhe == 2, 0, ifelse(chldhhe %in% c(6, 7, 8, 9), NA, chldhhe)),
    lvgptnea = ifelse(lvgptnea == 2, 0, ifelse(lvgptnea %in% c(6, 7, 8, 9), NA, lvgptnea)),
    hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta)
  )

russian_data_v3 <- russian_data_table_sdv %>%
  rename(
    `Ever had children living in household` = chldhhe,
    `Ever lived with a partner, without being married` = lvgptnea,
    `Household's total net income, all sources` = hinctnta
  )

sdv_table <- datasummary_skim(russian_data_v3 %>% select(`Ever had children living in household`, `Ever lived with a partner, without being married`, `Household's total net income, all sources`), 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.
sdv_table

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

Ever had children living in household

3

37

0.5

0.5

0.0

1.0

1.0

Ever lived with a partner, without being married

3

46

0.3

0.5

0.0

0.0

1.0

Household's total net income, all sources

11

35

5.3

2.8

1.0

5.0

10.0

Save as word file

flextable::save_as_docx(summary_table_v2, path = "Table 1: Descriptive Statistics for socio-demographic variables.docx", width = 10.0, height = 6.0)

*Note: Changed title of the table in the word doc