packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "aod", "interactions", "kableExtra", "flextable", "scales", "ggeffects", "marginaleffects", "viridis", "knitr", "rmarkdown", "ggridges", "questionr", "performance", "broom") 

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.5
## ✔ 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: carData
## 
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## 
## 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: 'MASS'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## 
## 
## Attaching package: 'aod'
## 
## 
## The following object is masked from 'package:survival':
## 
##     rats
## 
## 
## 
## 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
## 
## 
## 
## Attaching package: 'scales'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
## 
## 
## 
## Attaching package: 'ggeffects'
## 
## 
## The following object is masked from 'package:interactions':
## 
##     johnson_neyman
## 
## 
## Loading required package: viridisLite
## 
## 
## Attaching package: 'viridis'
## 
## 
## The following object is masked from 'package:scales':
## 
##     viridis_pal
## 
## 
## 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] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[6]]
##  [1] "survey"       "survival"     "Matrix"       "grid"         "effects"     
##  [6] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[7]]
##  [1] "MASS"         "survey"       "survival"     "Matrix"       "grid"        
##  [6] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[8]]
##  [1] "aod"          "MASS"         "survey"       "survival"     "Matrix"      
##  [6] "grid"         "effects"      "carData"      "modelsummary" "fst"         
## [11] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "interactions" "aod"          "MASS"         "survey"       "survival"    
##  [6] "Matrix"       "grid"         "effects"      "carData"      "modelsummary"
## [11] "fst"          "infer"        "lubridate"    "forcats"      "stringr"     
## [16] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [21] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [26] "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "kableExtra"   "interactions" "aod"          "MASS"         "survey"      
##  [6] "survival"     "Matrix"       "grid"         "effects"      "carData"     
## [11] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
## [16] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [21] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [26] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[11]]
##  [1] "flextable"    "kableExtra"   "interactions" "aod"          "MASS"        
##  [6] "survey"       "survival"     "Matrix"       "grid"         "effects"     
## [11] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [16] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [21] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [26] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [31] "base"        
## 
## [[12]]
##  [1] "scales"       "flextable"    "kableExtra"   "interactions" "aod"         
##  [6] "MASS"         "survey"       "survival"     "Matrix"       "grid"        
## [11] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [16] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [21] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [26] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [31] "methods"      "base"        
## 
## [[13]]
##  [1] "ggeffects"    "scales"       "flextable"    "kableExtra"   "interactions"
##  [6] "aod"          "MASS"         "survey"       "survival"     "Matrix"      
## [11] "grid"         "effects"      "carData"      "modelsummary" "fst"         
## [16] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [21] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [26] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [31] "datasets"     "methods"      "base"        
## 
## [[14]]
##  [1] "marginaleffects" "ggeffects"       "scales"          "flextable"      
##  [5] "kableExtra"      "interactions"    "aod"             "MASS"           
##  [9] "survey"          "survival"        "Matrix"          "grid"           
## [13] "effects"         "carData"         "modelsummary"    "fst"            
## [17] "infer"           "lubridate"       "forcats"         "stringr"        
## [21] "dplyr"           "purrr"           "readr"           "tidyr"          
## [25] "tibble"          "ggplot2"         "tidyverse"       "stats"          
## [29] "graphics"        "grDevices"       "utils"           "datasets"       
## [33] "methods"         "base"           
## 
## [[15]]
##  [1] "viridis"         "viridisLite"     "marginaleffects" "ggeffects"      
##  [5] "scales"          "flextable"       "kableExtra"      "interactions"   
##  [9] "aod"             "MASS"            "survey"          "survival"       
## [13] "Matrix"          "grid"            "effects"         "carData"        
## [17] "modelsummary"    "fst"             "infer"           "lubridate"      
## [21] "forcats"         "stringr"         "dplyr"           "purrr"          
## [25] "readr"           "tidyr"           "tibble"          "ggplot2"        
## [29] "tidyverse"       "stats"           "graphics"        "grDevices"      
## [33] "utils"           "datasets"        "methods"         "base"           
## 
## [[16]]
##  [1] "knitr"           "viridis"         "viridisLite"     "marginaleffects"
##  [5] "ggeffects"       "scales"          "flextable"       "kableExtra"     
##  [9] "interactions"    "aod"             "MASS"            "survey"         
## [13] "survival"        "Matrix"          "grid"            "effects"        
## [17] "carData"         "modelsummary"    "fst"             "infer"          
## [21] "lubridate"       "forcats"         "stringr"         "dplyr"          
## [25] "purrr"           "readr"           "tidyr"           "tibble"         
## [29] "ggplot2"         "tidyverse"       "stats"           "graphics"       
## [33] "grDevices"       "utils"           "datasets"        "methods"        
## [37] "base"           
## 
## [[17]]
##  [1] "rmarkdown"       "knitr"           "viridis"         "viridisLite"    
##  [5] "marginaleffects" "ggeffects"       "scales"          "flextable"      
##  [9] "kableExtra"      "interactions"    "aod"             "MASS"           
## [13] "survey"          "survival"        "Matrix"          "grid"           
## [17] "effects"         "carData"         "modelsummary"    "fst"            
## [21] "infer"           "lubridate"       "forcats"         "stringr"        
## [25] "dplyr"           "purrr"           "readr"           "tidyr"          
## [29] "tibble"          "ggplot2"         "tidyverse"       "stats"          
## [33] "graphics"        "grDevices"       "utils"           "datasets"       
## [37] "methods"         "base"           
## 
## [[18]]
##  [1] "ggridges"        "rmarkdown"       "knitr"           "viridis"        
##  [5] "viridisLite"     "marginaleffects" "ggeffects"       "scales"         
##  [9] "flextable"       "kableExtra"      "interactions"    "aod"            
## [13] "MASS"            "survey"          "survival"        "Matrix"         
## [17] "grid"            "effects"         "carData"         "modelsummary"   
## [21] "fst"             "infer"           "lubridate"       "forcats"        
## [25] "stringr"         "dplyr"           "purrr"           "readr"          
## [29] "tidyr"           "tibble"          "ggplot2"         "tidyverse"      
## [33] "stats"           "graphics"        "grDevices"       "utils"          
## [37] "datasets"        "methods"         "base"           
## 
## [[19]]
##  [1] "questionr"       "ggridges"        "rmarkdown"       "knitr"          
##  [5] "viridis"         "viridisLite"     "marginaleffects" "ggeffects"      
##  [9] "scales"          "flextable"       "kableExtra"      "interactions"   
## [13] "aod"             "MASS"            "survey"          "survival"       
## [17] "Matrix"          "grid"            "effects"         "carData"        
## [21] "modelsummary"    "fst"             "infer"           "lubridate"      
## [25] "forcats"         "stringr"         "dplyr"           "purrr"          
## [29] "readr"           "tidyr"           "tibble"          "ggplot2"        
## [33] "tidyverse"       "stats"           "graphics"        "grDevices"      
## [37] "utils"           "datasets"        "methods"         "base"           
## 
## [[20]]
##  [1] "performance"     "questionr"       "ggridges"        "rmarkdown"      
##  [5] "knitr"           "viridis"         "viridisLite"     "marginaleffects"
##  [9] "ggeffects"       "scales"          "flextable"       "kableExtra"     
## [13] "interactions"    "aod"             "MASS"            "survey"         
## [17] "survival"        "Matrix"          "grid"            "effects"        
## [21] "carData"         "modelsummary"    "fst"             "infer"          
## [25] "lubridate"       "forcats"         "stringr"         "dplyr"          
## [29] "purrr"           "readr"           "tidyr"           "tibble"         
## [33] "ggplot2"         "tidyverse"       "stats"           "graphics"       
## [37] "grDevices"       "utils"           "datasets"        "methods"        
## [41] "base"           
## 
## [[21]]
##  [1] "broom"           "performance"     "questionr"       "ggridges"       
##  [5] "rmarkdown"       "knitr"           "viridis"         "viridisLite"    
##  [9] "marginaleffects" "ggeffects"       "scales"          "flextable"      
## [13] "kableExtra"      "interactions"    "aod"             "MASS"           
## [17] "survey"          "survival"        "Matrix"          "grid"           
## [21] "effects"         "carData"         "modelsummary"    "fst"            
## [25] "infer"           "lubridate"       "forcats"         "stringr"        
## [29] "dplyr"           "purrr"           "readr"           "tidyr"          
## [33] "tibble"          "ggplot2"         "tidyverse"       "stats"          
## [37] "graphics"        "grDevices"       "utils"           "datasets"       
## [41] "methods"         "base"
ess <- read.fst("All-ESS-Data.fst")
spain_data <- read.fst("spain_data.fst")
spain_data <- ess %>%
  filter(cntry == "HR")
spain_data$weight <- spain_data$dweight * spain_data$pweight
survey_design <- svydesign(ids = ~1, data = spain_data, weights = ~weight)

Table of Descriptive Statistics

spain_descriptive <- spain_data %>%
  mutate(
    imptrad = case_when (
      imptrad == 1 ~ "Very Much Like Me",
      imptrad == 2 ~ "Like Me",
      imptrad == 3 ~ "Somewhat Like Me",
      imptrad == 4 ~ "A Little Like Me",
      imptrad == 5 ~ "Not Like Me",
      imptrad == 6 ~ "Not Like Me At All",
      imptrad %in% c(7,8,9) ~ NA_character_,
      TRUE ~ as.character(imptrad)
  )) %>%
  filter (!is.na(imptrad))
spain_descriptive <- spain_data %>%
  mutate(
       imptrad = ifelse(imptrad %in% c(7, 8, 9), NA, imptrad)
  ) %>%
    filter(!is.na(imptrad))
spain_descriptive <- spain_descriptive %>%
 mutate(
   lrscale = ifelse(lrscale %in% c(77,88,99), NA, lrscale)) %>%
  filter(!is.na(lrscale))
spain_descriptive <- spain_descriptive %>%
  mutate(
    gndr = ifelse(gndr %in% c(9), NA, gndr)
  ) %>%
  filter(!is.na(gndr))
spain_descriptive <- spain_descriptive %>%
  mutate(
    Traditions = imptrad,
    PoliticalID = lrscale,
    Gender = gndr
  )
spain_table1 <- datasummary_skim(spain_descriptive %>% dplyr::select(Traditions, PoliticalID, Gender), title = "Table 1. Descriptive statistics of view on traditions and customs, political ideology, and gender", 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.
spain_table1
Table 1. Descriptive statistics of view on traditions and customs, political ideology, and gender

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

Traditions

6

0

2.3

1.3

1.0

2.0

6.0

PoliticalID

11

0

5.0

2.5

0.0

5.0

10.0

Gender

2

0

1.6

0.5

1.0

2.0

2.0

flextable::save_as_docx(spain_table1, path = "Spain_Table1.docx", width = 7.0, height = 9.0)

#Null Distribution with Confidence Intervals

spain_null <- spain_data %>%
  mutate(
    imptrad = case_when (
      imptrad == 1 ~ "Very Much Like Me",
      imptrad == 2 ~ "Like Me",
      imptrad == 3 ~ "Somewhat Like Me",
      imptrad == 4 ~ "A Little Like Me",
      imptrad == 5 ~ "Not Like Me",
      imptrad == 6 ~ "Not Like Me At All",
      imptrad %in% c(7,8,9) ~ NA_character_,
      TRUE ~ as.character(imptrad)
  )) %>%
  filter (!is.na(imptrad))
spain_null <- spain_null %>%
  mutate(
    lrscale = case_when(
      lrscale %in% 0:4 ~ "Left",
      lrscale %in% 6:10 ~ "Right",
      lrscale %in% c(5, 77, 88, 99) ~ NA_character_)
    ) %>%
  filter(!is.na(lrscale))
spain_null <- spain_null %>%
  mutate (gndr = case_when (
    gndr == 1 ~ "Male",
    gndr == 2 ~ "Female",
    gndr %in% c(9) ~ NA_character_,
  )) %>%
  filter(!is.na(gndr))
test_stat1 <- spain_null %>%
  specify(explanatory = lrscale,
          response = imptrad) %>%
  hypothesize (null = "independence") %>%
  calculate (stat = "Chisq")

print(test_stat1$stat)
## X-squared 
##  188.1765
spain_tab <- spain_null

HRtab <- table(spain_tab$lrscale, spain_tab$imptrad)

rsum <- rowSums(HRtab)

csums <- colSums(HRtab)

N <- sum(HRtab)

ptab <- tcrossprod(rsum/N, csums/N)
cat("Table of Expected Proportions:\n")
## Table of Expected Proportions:
print(ptab)
##            [,1]      [,2]       [,3]       [,4]       [,5]      [,6]
## [1,] 0.04574059 0.1817626 0.02924398 0.01484695 0.07963362 0.1499691
## [2,] 0.04552213 0.1808945 0.02910431 0.01477603 0.07925328 0.1492529
ftab <- N*ptab
cat("Table of Expected Frequencies:\n")
## Table of Expected Frequencies:
print(ftab)
##          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]
## [1,] 152.8651 607.4506 97.73339 49.61849 266.1355 501.1969
## [2,] 152.1349 604.5494 97.26661 49.38151 264.8645 498.8031
alpha <- 0.05
c_val <- qchisq(alpha, df = 1, lower.tail = FALSE)
cat("Critical Value:", round(c_val, 3), "\n")
## Critical Value: 3.841
null_distribution <- spain_null %>%
  specify(explanatory = lrscale,
          response = imptrad) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>%
  calculate(stat="Chisq")
p_val <- null_distribution %>%
  get_pvalue(obs_stat = test_stat1, direction = "two-sided")
## 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()` (`?infer::get_p_value()`) for more information.
p_val
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
conf_int <- null_distribution %>%
  get_confidence_interval(level = 0.95, type = "percentile")

null_distribution %>%
  visualize() +
  shade_p_value(obs_stat = test_stat1, direction = "two-sided") +
  shade_confidence_interval(endpoints = conf_int)
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.

null_distribution
## Response: imptrad (factor)
## Explanatory: lrscale (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
##    replicate  stat
##        <int> <dbl>
##  1         1  2.70
##  2         2  4.86
##  3         3  3.68
##  4         4  5.76
##  5         5  2.79
##  6         6  4.09
##  7         7  3.41
##  8         8 10.4 
##  9         9  7.88
## 10        10  2.71
## # ℹ 990 more rows

Regression Models (3 Models)

spain_figure1 <- spain_data %>%
  mutate(imptrad = na_if(imptrad, 7) %>% na_if(8) %>% na_if(9)) %>%
  mutate(imptrad = 7 - imptrad) %>%
  filter(!is.na(imptrad))
spain_figure1 <- spain_figure1 %>%
  mutate(
    lrscale = case_when(
      lrscale %in% 0:4 ~ "Left",
      lrscale %in% 6:10 ~ "Right",
      lrscale %in% c(5, 77, 88, 99) ~ NA_character_
    )) %>%
  filter(!is.na(lrscale))
spain_figure1 <- spain_figure1 %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      gndr %in% c(9) ~ NA_character_,
      TRUE ~ as.character(gndr)
  )) %>%
  filter(!is.na(gndr))
figure1 <- lm(imptrad ~ lrscale, data = spain_figure1, weights = weight)
modelsummary(
  list(figure1),
  fmt = 3,
  estimate = c("{estimate} ({std.error}){stars}"),
  statistic = NULL,
  title = "Figure 1: Regression model for Traditions and Customs, Political Ideology, and Gender in Spain")
Figure 1: Regression model for Traditions and Customs, Political Ideology, and Gender in Spain
 (1)
(Intercept) 4.327 (0.031)***
lrscaleRight 0.663 (0.044)***
Num.Obs. 3342
R2 0.064
R2 Adj. 0.064
AIC 11404.7
BIC 11423.0
Log.Lik. −5699.326
RMSE 1.27
figure2 <- lm(imptrad ~ lrscale + gndr, data = spain_figure1, weights = weight)
figure3 <- lm(imptrad ~ lrscale + gndr + lrscale*gndr, data = spain_figure1, weights = weight)
modelsummary(
  list(figure1, figure2, figure3),
  fmt = 3,
  estimate = c("{estimate} ({std.error}){stars}",
               "{estimate} ({std.error}){stars}",
               "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  title = "Figure 2: Regression model for Traditions and Customs, Political Ideology, and Gender in Spain")
Figure 2: Regression model for Traditions and Customs, Political Ideology, and Gender in Spain
 (1)   (2)   (3)
(Intercept) 4.327 (0.031)*** 4.373 (0.036)*** 4.399 (0.041)***
lrscaleRight 0.663 (0.044)*** 0.674 (0.044)*** 0.616 (0.061)***
gndrMale −0.105 (0.044)* −0.165 (0.062)**
lrscaleRight × gndrMale 0.119 (0.088)
Num.Obs. 3342 3342 3342
R2 0.064 0.066 0.066
R2 Adj. 0.064 0.065 0.066
AIC 11404.7 11400.9 11401.1
BIC 11423.0 11425.4 11431.7
Log.Lik. −5699.326 −5696.459 −5695.540
RMSE 1.27 1.27 1.27
figure1.2 <- glm(imptrad ~ lrscale, data = spain_figure1, family = poisson)
figure2.2 <- glm(imptrad ~ lrscale + gndr, data = spain_figure1, family = poisson)
figure3.2 <- glm(imptrad ~ lrscale + gndr + lrscale*gndr, data = spain_figure1, family = poisson)
modelsummary(
  list(figure1.2, figure2.2, figure3.2),
  fmt = 3,
  estimate = c("{estimate} ({std.error}){stars}",
               "{estimate} ({std.error}){stars}",
               "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  title = "Figure 3: Regression model for Traditions and Customs, Political Ideology, and Gender in Spain")
Figure 3: Regression model for Traditions and Customs, Political Ideology, and Gender in Spain
 (1)   (2)   (3)
(Intercept) 1.472 (0.012)*** 1.482 (0.014)*** 1.488 (0.015)***
lrscaleRight 0.131 (0.016)*** 0.133 (0.016)*** 0.121 (0.022)***
gndrMale −0.023 (0.016) −0.037 (0.024)
lrscaleRight × gndrMale 0.027 (0.032)
Num.Obs. 3342 3342 3342
AIC 12605.6 12605.7 12607.0
BIC 12617.9 12624.0 12631.4
Log.Lik. −6300.819 −6299.834 −6299.489
RMSE 1.27 1.27 1.27

#Visualization of Choice

spain_plot <- spain_data %>%
   mutate(
    imptrad = case_when(
      imptrad %in% c(7,8,9) ~ NA_character_,
      TRUE ~ as.character(imptrad))
  ) %>%
  filter (!is.na(imptrad))
spain_plot <- spain_plot %>%
  mutate(
    lrscale = case_when(
      lrscale %in% 0:4 ~ "Left",
      lrscale %in% 6:10 ~ "Right",
      lrscale %in% c(5, 77, 88, 99) ~ NA_character_)
    ) %>%
  filter(!is.na(lrscale))
spain_plot <- spain_plot %>%
  mutate (gndr = case_when (
    gndr == 1 ~ "Male",
    gndr == 2 ~ "Female",
    gndr %in% c(9) ~ NA_character_,
  )) %>%
  filter(!is.na(gndr))
table(spain_plot$imptrad, spain_plot$lrscale) %>%
  cprop() %>% 
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge", ) +
  labs(title="Importance of following Tradtions and Values in Spain by Political Ideology",
       y = "Conditional Proportion",
       x = "Very much to Not like me at all",
       fill = "Pol ID")

table(spain_plot$imptrad, spain_plot$gndr) %>%
  cprop() %>% 
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge", ) +
  labs(title="Importance of following Tradtions and Values in Spain by Gender",
       y = "Conditional Proportion",
       x = "Very much to Not like me at all",
       fill = "Gender")