# List of packages
packages <- c("infer", "fst", "modelsummary", "effects", "tidyr", "dplyr", "ggplot2") # 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)
## Loading required package: carData
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## [[1]]
## [1] "infer"     "stats"     "graphics"  "grDevices" "utils"     "datasets" 
## [7] "methods"   "base"     
## 
## [[2]]
## [1] "fst"       "infer"     "stats"     "graphics"  "grDevices" "utils"    
## [7] "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "modelsummary" "fst"          "infer"        "stats"        "graphics"    
##  [6] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [11] "methods"      "base"        
## 
## [[5]]
##  [1] "tidyr"        "effects"      "carData"      "modelsummary" "fst"         
##  [6] "infer"        "stats"        "graphics"     "grDevices"    "utils"       
## [11] "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "dplyr"        "tidyr"        "effects"      "carData"      "modelsummary"
##  [6] "fst"          "infer"        "stats"        "graphics"     "grDevices"   
## [11] "utils"        "datasets"     "methods"      "base"        
## 
## [[7]]
##  [1] "ggplot2"      "dplyr"        "tidyr"        "effects"      "carData"     
##  [6] "modelsummary" "fst"          "infer"        "stats"        "graphics"    
## [11] "grDevices"    "utils"        "datasets"     "methods"      "base"
ess <- read_fst("All-ESS-Data.fst")
uk_data <- ess %>%
  filter(cntry == "GB") %>%
   mutate(
    age = ifelse(agea == 999, NA, agea),
    hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
    vote = ifelse(vote == 2, 0, ifelse(vote %in% c(3, 7, 8, 9), NA, vote)),
    clsprty = case_when(
    clsprty == 1 ~ 1,              # Keep 1 as numeric
    clsprty == 2 ~ 0,              # Recode 2 as numeric 0
    clsprty %in% c(7, 8, 9) ~ NA_real_,  # Set 7, 8, 9 as NA keeping the type numeric
    TRUE ~ clsprty ))                 # Keep all other values as they are
uk_data$clsprty <- as.factor(uk_data$clsprty)
model1 <- glm(vote ~ age, family = binomial, data = uk_data)
model2 <- glm(vote ~ age + clsprty, family = binomial, data = uk_data)
model3 <- glm(vote ~ age + clsprty + age*clsprty, family = binomial, data = uk_data)



modelsummary(
  list(model1, model2, model3),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept")
 (1)   (2)   (3)
age 0.0 (0.0)*** 0.0 (0.0)*** 0.0 (0.0)***
clsprty1 1.2 (0.0)*** 1.1 (0.1)***
age × clsprty1 0.0 (0.0)
Num.Obs. 19706 19574 19574
AIC 21074.0 19866.9 19868.8
BIC 21089.8 19890.5 19900.3
Log.Lik. −10535.024 −9930.425 −9930.392
RMSE 0.42 0.41 0.41
exp(coef(model1))
## (Intercept)         age 
##   0.3902372   1.0407395
exp(coef(model2))
## (Intercept)         age    clsprty1 
##   0.3067683   1.0350929   3.1821625
exp(coef(model3))
##  (Intercept)          age     clsprty1 age:clsprty1 
##    0.3097139    1.0348741    3.0994407    1.0005561
test_stat <- uk_data %>%
  specify(explanatory = age, # change variable name for explanatory variable
          response = vote) %>% # change variable name for outcome of interest
  hypothesize(null = "independence") %>%
  calculate(stat = "slope") # replace in between quotation marks appropriate test statistic
## Warning: Removed 1273 rows containing missing values.
## Message: The independence null hypothesis does not inform calculation of the
## observed statistic (a slope) and will be ignored.
print(test_stat$stat)
##        age 
## 0.00710142
null_distribution <- uk_data %>%
  specify(explanatory = age,
          response = vote) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 100, type = "permute") %>% # only line we are adding, use reps = 1000 as standard
  calculate(stat = "slope")
## Warning: Removed 1273 rows containing missing values.
p_val <- null_distribution %>% # replace name here if you assigned something other than null_distribution above
  get_pvalue(obs_stat = test_stat, direction = "two-sided") # would only replace test_stat if assigned another name in Step 1
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
p_val
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
null_distribution %>%
  visualize() +
  shade_p_value(obs_stat = test_stat, direction = "two-sided")

equatiomatic::extract_eq(model2, use_coefs = TRUE)

\[ \log\left[ \frac { \widehat{P( \operatorname{vote} = \operatorname{1} )} }{ 1 - \widehat{P( \operatorname{vote} = \operatorname{1} )} } \right] = -1.18 + 0.03(\operatorname{age}) + 1.16(\operatorname{clsprty}_{\operatorname{1}}) \]