getwd()
## [1] "C:/Users/Denis/Documents/RStudio"
setwd("C:/Users/Denis/Documents/RStudio")
italy_data <- ess %>%
  filter(cntry == "IT")
italy_data$weight <- italy_data$dweight * italy_data$pweight
survey_design <- svydesign(ids = ~1, data = italy_data, weights = ~weight)
italy_data <- italy_data %>%
  mutate(prtvtbit = case_when(
    trstprt == 1 ~ 1,    # Yes to 1
    trstprt == 2 ~ 0,    # No to 0
    TRUE ~ NA_real_    # Everything else to NA
  ))
italy_data <- italy_data %>% filter(!is.na(vote))

df <- italy_data
df$year <- NA
replacements <- c(1990, 1992, 1994, 1996, 1998, 2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  df$year[df$essround == i] <- replacements[i]
}

vote_avg <- df %>%
  group_by(year) %>%
  summarize(vote_avg = mean(vote, na.rm = TRUE))

vote_plot_IT <- ggplot(vote_avg, aes(x = year, y = vote_avg)) +
  geom_point(aes(size = vote_avg, color = vote_avg), alpha = 0.7) + 
  geom_line(aes(group = 1), color = "blue", linetype = "dashed") +  
  labs(
       x = "Survey Year",
       y = "Pr(vote)") +
  theme_minimal() +
  theme(legend.position = "none")

print(vote_plot_IT)

italy_data <- italy_data %>%
  # Recoding for feeling about trust in political parties
  mutate(trstprl = case_when(
    trstprt == 1 ~ "high",  # more trust to "high"
    trstprt %in% 2:4 ~ "low", # Other categories to "low"
    TRUE ~ NA_character_            # Everything else to NA
  )) %>%
  # Recoding age, adding age group, and cohort with generational cohorts
  mutate(
    age = ifelse(agea == 999, NA, agea),
    adulthood = ifelse(age >= 18 & age <= 30, "Early", ifelse(age >= 31, "Later", NA)),
    cohort = ifelse(yrbrn < 1930 | yrbrn > 2000, NA, yrbrn),
    gen = case_when(
      yrbrn %in% 1900:1945 ~ "Interwar",
      yrbrn %in% 1946:1964 ~ "Baby Boomers",
      yrbrn %in% 1965:1979 ~ "Gen X",
      yrbrn %in% 1980:1996 ~ "Millennials",
      yrbrn %in% 1997:2012 ~ "Gen Z",
      TRUE ~ as.character(cohort)
    ),
    gen = factor(gen, levels = c("Interwar", "Baby Boomers", "Gen X", "Millennials", "Gen Z"))
  ) %>%
  # Recoding political ID and voter behaviour
  mutate(
    polID = case_when(
      lrscale %in% 0:3 ~ "Left",                    
      lrscale %in% 7:10 ~ "Right",                     
      lrscale %in% 4:6 ~ "Moderate",                  
      lrscale %in% c(77, 88, 99) ~ NA_character_      
    ),
    prtvtbit = case_when(
      essround < 5 & trstplt == 5 ~ "high trust or more",
      essround >= 5 & trstprt > 600 ~ "high trust or more",
      TRUE ~ "low trust"
    ),
    trstplt = ifelse(trstplt %in% c(77, 88, 99), NA_integer_, trstplt),
    trstprt = ifelse(trstprt %in% c(5555, 7777, 8888), NA_integer_, trstprt),
    prtvtbit = factor(prtvtbit, levels = c("Low trust", "high trust or more"))
  )
# Setting values greater than 10 to NA
italy_data$trstplt <- ifelse(italy_data$trstplt > 10, NA, italy_data$trstplt)
italy_data$trstplt <- ifelse(italy_data$trstplt > 10, NA, italy_data$trstplt)
italy_data$trstprt <- ifelse(italy_data$trstprt > 10, NA, italy_data$trstprt)

# Creating and rescaling the trust variable
italy_data$trust <- scales::rescale(italy_data$trstplt + italy_data$trstplt + italy_data$trstprt, na.rm = TRUE, to = c(0, 100))

# Here's how you would "flip it" because populist is conceived by N + I as 'anti-trust'
# As Schafer explains there are some issues with that conceptualization, but N + I is also widely applied
italy_data$populist <- scales::rescale(italy_data$trust, na.rm = TRUE, to=c(100,0))

table(italy_data$populist)
## 
##                0 3.33333333333333 6.66666666666667               10 
##               14                2                4               25 
## 13.3333333333333 16.6666666666667               20 23.3333333333333 
##                7               13               66               30 
## 26.6666666666667               30 33.3333333333333 36.6666666666667 
##               38              177               85               89 
##               40 43.3333333333333 46.6666666666667               50 
##              407              185              147              737 
## 53.3333333333333 56.6666666666667               60 63.3333333333333 
##              205              174              702              217 
## 66.6666666666667               70 73.3333333333333 76.6666666666667 
##              178              682              263              170 
##               80 83.3333333333333 86.6666666666667               90 
##              746              201              160              626 
## 93.3333333333333 96.6666666666667              100 
##              233              124             2043
italydf <- italy_data %>%
  dplyr::select(trstprt, trstprl, gen, populist, polID, prtvtbit, weight)

# Check the first few rows to verify the selection
italydf <- na.omit(italydf)
head(italydf)
## [1] trstprt  trstprl  gen      populist polID    prtvtbit weight  
## <0 rows> (or 0-length row.names)
# Load italy data
italy_data <- read.fst("italy_data.fst")

# Setting values greater than 10 to NA for trust variables
italy_data$trstplt <- ifelse(italy_data$trstplt > 10, NA, italy_data$trstplt)
italy_data$trstprl <- ifelse(italy_data$trstprl > 10, NA, italy_data$trstprl)
italy_data$trstprt <- ifelse(italy_data$trstprt > 10, NA, italy_data$trstprt)

# Creating and rescaling the trust variable to create a populist scale
italy_data$trust <- scales::rescale(italy_data$trstplt + italy_data$trstprl + italy_data$trstprt, na.rm = TRUE, to = c(0, 100))

# Flip the scale as higher is now anti-trust (populist)
italy_data$populist <- scales::rescale(italy_data$trust, na.rm = TRUE, to = c(100, 0))

# Plot the distribution of the populist scale for italy
ggplot(italy_data, aes(x = populist)) +
  geom_histogram(bins = 30, fill = "blue", color = "black") +
  theme_minimal() +
  labs(title = "Distribution of Populist Scale for italy",
       x = "Populist Scale",
       y = "Count")
## Warning: Removed 1493 rows containing non-finite values (`stat_bin()`).

# Creating our trust in political party variable
italy_data <- italy_data %>%
  mutate(
    trstprt = case_when(
      essround < 5 & trstprt == 5 ~ "more trust",
      essround >= 5 & trstprt > 600 ~ "more trust",
      TRUE ~ "low trust"
    ),
    
    # Handle NAs for trust levels
    trstprt = ifelse(trstprt %in% c(77, 88, 99), NA_integer_, trstprt),
    trstplt = ifelse(trstplt %in% c(5555, 7777, 8888), NA_integer_, trstplt),
    
    # Explicitly making 'low trust' the reference category
    trstprt = factor(trstprt, levels = c("low trust", "more trust"))
  )

italy_data <- df %>%
  mutate(
    # Recoding cohort variable, setting years before 1930 and after 2012 to NA
    cohort = ifelse(yrbrn < 1930 | yrbrn > 2012, NA, yrbrn),

    # Recoding generational cohorts based on year of birth
    gen = case_when(
      yrbrn %in% 1900:1945 ~ "1",
      yrbrn %in% 1946:1964 ~ "2",
      yrbrn %in% 1965:1979 ~ "3",
      yrbrn %in% 1980:1996 ~ "4",
      yrbrn %in% 1997:2012 ~ "5",
      TRUE ~ as.character(cohort)  # Keeping other values as character if they do not fit the ranges
    ),

    # Converting cohort to a factor with labels
    gen = factor(gen,
                 levels = c("1", "2", "3", "4", "5"),
                 labels = c("Interwar", "Baby Boomers", "Gen X", "Millennials", "Gen Z"))
  )
table(italy_data$gen)
## 
##     Interwar Baby Boomers        Gen X  Millennials        Gen Z 
##         1470         3179         2636         2031          763
model1 <- lm(trstprt ~ gen, data = italy_data)
modelsummary(model1, fmt = 1,
  estimate  = c("{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_rename = c("genBaby Boomers" = "Baby Boomers", "genGen X" = "Gen X", "genMillennials" = "Millennials", "genGen Z" = "Gen Z"),
  title = 'Table 2. Regression model for authoritarian attitudes')
Table 2. Regression model for authoritarian attitudes
 (1)
(Intercept) 4.2 (0.4)***
Baby Boomers −0.6 (0.4)
Gen X 0.0 (0.4)
Millennials 0.9 (0.5)*
Gen Z 3.9 (0.6)***
Num.Obs. 8873
R2 0.010
R2 Adj. 0.009
AIC 69888.5
BIC 69931.1
Log.Lik. −34938.261
RMSE 12.41
italy <- df %>%
  mutate(ID = case_when(
    lrscale >= 0 & lrscale <= 4 ~ "Left",
    lrscale >= 6 & lrscale <= 10 ~ "Right",
    lrscale > 10 ~ NA_character_,  # Set values above 10 as NA
    TRUE ~ NA_character_  # Ensure value 5 and any other unexpected values are set as NA
  ))
table(df$ID)
## < table of extent 0 >
italy_data <- df %>%
  mutate(
    polID = case_when(
      lrscale %in% 0:3 ~ "Left",                    
      lrscale %in% 7:10 ~ "Right",                     
      lrscale %in% 4:6 ~ "Moderate",                  
      lrscale %in% c(77, 88, 99) ~ NA_character_      
    ),
   trust = case_when(
      trstprt %in% c(77, 88, 99) ~ NA_real_,
      TRUE ~ trstprt
    )
  )
italy_data$weight <- italy_data$dweight * italy_data$pweight 
survey_design <- svydesign(ids = ~1, data = italy_data, weights = ~weight)
df <- italy_data
df <- df %>%
  mutate(trustplt = trstplt,
         trustprt = trstprt,
         attitude = lrscale,
         prtcl = prtclcit) %>%
  mutate(across(c("trustplt", "trustprt", "attitude", "prtcl"),
                ~ na_if(.x, 7) %>% na_if(8) %>% na_if(9))) %>%
  # Apply the reverse coding
  mutate(across(c("trustplt", "trustprt", "attitude", "prtcl"), ~ 7 - .x ))

# Now you can calculate 'schwartzauth' after the NA recoding
df$auth <- scales::rescale(df$trustplt + 
                      df$trustprt + 
                      df$attitude + 
                      df$prtcl, to=c(0,100), na.rm=TRUE)


df <- df %>% filter(!is.na(auth))

df <- df %>%
  mutate(
    polID = case_when(
      lrscale %in% 0:3 ~ "Left",                    
      lrscale %in% 7:10 ~ "Right",                     
      lrscale %in% 4:6 ~ "Moderate",                  
      lrscale %in% c(77, 88, 99) ~ NA_character_      
    ),
   trust = case_when(
      trstprt %in% c(77, 88, 99) ~ NA_real_,
      TRUE ~ trstprt
    )
  )

# MLR without interaction
model2_italy <- lm(auth ~ polID + trust, data = df, weights = weight)

# MLR with interaction
model3_italy <- lm(auth ~ polID + trust + polID*trust, data = df, weights = weight)

# Displaying both models using modelsummary
modelsummary(list(model2_italy, model3_italy),
               fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL)
 (1)   (2)
(Intercept) 87.1 (0.6)*** 86.5 (0.7)***
polIDModerate −2.8 (0.6)*** −2.0 (0.9)*
polIDRight −0.5 (1.0) 0.8 (1.3)
trust −0.1 (0.1) 0.1 (0.2)
polIDModerate × trust −0.3 (0.3)
polIDRight × trust −0.9 (0.6)
Num.Obs. 1221 1221
R2 0.022 0.024
R2 Adj. 0.020 0.020
AIC 8905.0 8906.1
BIC 8930.5 8941.9
Log.Lik. −4447.503 −4446.068
RMSE 9.23 9.22
interaction_plot_italy <- effect("polID*trust", model3_italy, na.rm=TRUE)
plot(interaction_plot_italy,
     main="Interaction effect",
     xlab="Trust in Politicians",
     ylab="Authoritarian attitudes scale")