Loading Data

packages <- c("tidyverse", "infer", "fst", "modelsummary", "survey", "interactions", "scales", "broom", "dplyr") 

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.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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: grid
## 
## Loading required package: Matrix
## 
## 
## Attaching package: 'Matrix'
## 
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## 
## Loading required package: survival
## 
## 
## Attaching package: 'survey'
## 
## 
## The following object is masked from 'package:graphics':
## 
##     dotchart
## 
## 
## 
## Attaching package: 'scales'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
## 
## 
## Registered S3 methods overwritten by 'broom':
##   method            from  
##   tidy.glht         jtools
##   tidy.summary.glht jtools
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "infer"     "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "fst"       "infer"     "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"     
## 
## [[4]]
##  [1] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "survey"       "survival"     "Matrix"       "grid"         "modelsummary"
##  [6] "fst"          "infer"        "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "interactions" "survey"       "survival"     "Matrix"       "grid"        
##  [6] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[7]]
##  [1] "scales"       "interactions" "survey"       "survival"     "Matrix"      
##  [6] "grid"         "modelsummary" "fst"          "infer"        "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[8]]
##  [1] "broom"        "scales"       "interactions" "survey"       "survival"    
##  [6] "Matrix"       "grid"         "modelsummary" "fst"          "infer"       
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[9]]
##  [1] "broom"        "scales"       "interactions" "survey"       "survival"    
##  [6] "Matrix"       "grid"         "modelsummary" "fst"          "infer"       
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"
rm(list=ls()); gc()
##           used  (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
## Ncells 2513656 134.3    4483331 239.5         NA  4483331 239.5
## Vcells 4295610  32.8   10146329  77.5      16384  6857883  52.4
ess <- read_fst("All-ESS-Data.fst")
hungary_data <- read_fst("hungary_data.fst")
hungary_data <- hungary_data %>%
  filter(cntry == "HU")
hungary_data$weight <- hungary_data$dweight * hungary_data$pweight
survey_design <- svydesign(ids = ~1, data = hungary_data, weights = ~weight)
hungary_data <- hungary_data %>%
  mutate(healthcare = case_when(
    medtrnp == 0 ~ 0,    # Not marked to 0
    medtrnp == 1 ~ 1,    # Marked to 1
    TRUE ~ NA_real_    # Everything else to NA
  ))
hungary_data <- hungary_data %>% filter(!is.na(healthcare))
hungary_data <- hungary_data %>%
  # Recoding income and education
  mutate(
    income = case_when(
      hinctnta %in% 1:2 ~ "Low-income",                    
      hinctnta %in% 3:5 ~ "Medium-income",                     
      hinctnta %in% 6:10 ~ "High-income",                  
      hinctnta %in% c(77, 88, 99) ~ NA_character_      
    ),
    educ.ba = case_when(
      essround == 5 & edulvla == 5 ~ "BA or more",
      essround >= 5 & edulvlb > 600 ~ "BA or more",
      TRUE ~ "No BA"
    ),
    edulvla = ifelse(edulvla %in% c(77, 88, 99), NA_integer_, edulvla),
    edulvlb = ifelse(edulvlb %in% c(5555, 7777, 8888), NA_integer_, edulvlb),
    educ.ba = factor(educ.ba, levels = c("No BA", "BA or more"))
  )
hungarydf <- hungary_data %>%
  dplyr::select(healthcare, income, educ.ba)

hungarydf <- na.omit(hungarydf)
head(hungarydf)
##   healthcare        income    educ.ba
## 1          0 Medium-income BA or more
## 2          0   High-income      No BA
## 3          0   High-income      No BA
## 4          0 Medium-income      No BA
## 5          0 Medium-income      No BA
## 6          0 Medium-income      No BA

Descriptive table

hungary_data <- hungary_data %>%
  mutate(
    Healthcare = healthcare,
    Income = case_when(
      hinctnta %in% 1:2 ~ 1, # Low-income                   
      hinctnta %in% 3:5 ~ 2, # Medium-income                     
      hinctnta %in% 6:10 ~ 3, # High-income                  
      hinctnta %in% c(77, 88, 99) ~ NA_integer_,     
      TRUE ~ NA_real_
    ),
    Educ.ba = case_when(
      essround == 5 & edulvla == 5 ~ 1, # BA or more
      essround >= 5 & edulvlb > 600 ~ 1, # BA or more"
      TRUE ~ 0 # No BA
    ),
  )
df <- hungary_data
df <- df %>% filter(!is.na(Income))
df <- df %>% filter(!is.na(educ.ba))

table1 <- datasummary_skim(df  %>% dplyr::select(Healthcare, Income, Educ.ba), title = "Table 1. Descriptive Statistics for Main Variables (Healthcare, Income, and Education)", 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.
table1
Table 1. Descriptive Statistics for Main Variables (Healthcare, Income, and Education)

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

Healthcare

2

0

0.0

0.1

0.0

0.0

1.0

Income

3

0

2.5

0.7

1.0

3.0

3.0

Educ.ba

2

0

0.2

0.4

0.0

0.0

1.0

Null Distribution Model

ess <- ess %>%
  filter(cntry == "HU")
ess$weight <- ess$dweight * ess$pweight
survey_design <- svydesign(ids = ~1, data = ess, weights = ~weight)
ess <- ess %>%
  mutate(healthcare = case_when(
    medtrnp == 1 ~ 1,    # Marked
    medtrnp == 0 ~ 0,    # Not Marked
    TRUE ~ NA_real_    
  ))
ess <- ess %>% filter(!is.na(healthcare))
test_stat <- ess %>%
  specify(explanatory = hinctnta , 
          response = medtrnp) %>% 
  calculate(stat = "slope") 
print(test_stat$stat)
##     hinctnta 
## -0.000101764
null_dist <- ess %>%
  specify(explanatory = hinctnta, 
          response = medtrnp) %>%
  generate(reps = 1000, type = "bootstrap") %>%
  calculate(stat = "slope")

null_dist
## Response: medtrnp (numeric)
## Explanatory: hinctnta (numeric)
## # A tibble: 1,000 × 2
##    replicate        stat
##        <int>       <dbl>
##  1         1 -0.000212  
##  2         2 -0.000159  
##  3         3 -0.0000478 
##  4         4 -0.0000149 
##  5         5  0.00000972
##  6         6 -0.000166  
##  7         7 -0.0000891 
##  8         8 -0.000118  
##  9         9 -0.0000433 
## 10        10 -0.000158  
## # ℹ 990 more rows
p_val <- null_dist %>% # 
  get_pvalue(obs_stat = test_stat, direction = "two-sided")
p_val
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1   0.978
conf_int <- null_dist%>%
  get_confidence_interval(level = 0.95, type = "percentile")


null_dist %>%
  visualize(data, bins = 10, method = "simulation", dens_color = "black") +
  shade_p_value(obs_stat = test_stat, direction = "two-sided") +
  shade_confidence_interval(endpoints = conf_int)

Regression Models

ess <- ess %>% 
  mutate(healthcare = if_else(medtrnp == 1, 1, if_else(medtrnp == 0, 0, NA_integer_))) %>%
  filter(!is.na(healthcare))
ess <- ess %>%
  mutate(
    income = case_when(
    hinctnta %in% 1:2 ~ "Low-income",
    hinctnta %in% 3:5 ~ "Middle Income",
    hinctnta %in% 6:10 ~ "High-income",
    hinctnta %in% c(77, 88, 99) ~ NA_character_
    ),
    
    educ.ba = case_when(
      essround == 7 & edulvla == 7 ~ "BA or more",
      essround == 7 & edulvlb > 600 ~ "BA or more",
      TRUE ~ "No BA"
    ),
    
    edulvla = ifelse(edulvla %in% c(77, 88, 99), NA_integer_, edulvla),
    edulvlb = ifelse(edulvlb %in% c(5555, 7777, 8888), NA_integer_, edulvlb),
    educ.ba = factor(educ.ba, levels = c("No BA", "BA or more")),
  )
ess <- ess %>%
  dplyr::select(healthcare, income, educ.ba)
ess <-ess %>%
  mutate(healthcare = case_when(
    healthcare == 0 ~ "Not Marked",
    healthcare == 1 ~ "Marked",
    TRUE ~ NA_character_
  ))
ess <- ess %>%
  mutate(
    healthcare == healthcare,
    income == income,
    educ.ba == educ.ba
  )

With this portion of the assignment I did a lot of trial and error to get the codes to run properly, but I kept getting “object ‘weight’ not found” error messages, despite my attempts to define and correct them and go over them with the TA. I wanted to include this regardless to show that I did try as I have confidence in my codes and what they should have looked like had it run properly. - Breanna

’’’ ess <- ess mutate(weight = dweight * pweight) survey_design <- svydesign(ids = ~1, data = ess, weights = ~weight) ’’’

’’’ model1 <- glm(healthcare ~ income, data = ess, weights = round(weight)) # single predictor

model2 <- glm(healthcare ~ income + educ.ba, data = ess, weights = round(weight)) # two predictors

model3 <- glm(healthcare ~ income + educ.ba + income*educ.ba, data = ess, weights = round(weight)) # both predictors + interaction ’’’

’’’ modelsummary( list(model1, model2, model3), fmt = 1, estimate = c( “{estimate} ({std.error}){stars}”, “{estimate} ({std.error}){stars}”, “{estimate} ({std.error}){stars}”), statistic = NULL) ’’’

Visual Of Choice

’’’ hist(ess$hinctnta) ’’’