Task 1

# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom", "remotes") # 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)
## ── 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
## Warning: package 'remotes' was built under R version 4.3.2
## [[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] "broom"        "modelsummary" "fst"          "infer"        "lubridate"   
##  [6] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [11] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"        
## 
## [[6]]
##  [1] "remotes"      "broom"        "modelsummary" "fst"          "infer"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"
setwd("C:/Users/KRonh/OneDrive/Desktop/UNI/SOC 202 R files/ESS DATA") 

ess <- read_fst("All-ESS-Data.fst")
belgium_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl), ## Example of a trust variable
  ) #--> is i need to remove NA's REMEBER THIS LINE!!!! 
unique(belgium_data$trstprl)
##  [1] 10  7  6 NA  5  9  8  4  3  2  0  1
belgium_data <- belgium_data %>% filter(!is.na(trstprl))
belgium_data <- belgium_data %>%
 mutate(lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale)) %>%
  mutate(wrkprty = case_when(
    wrkprty == 1 ~ "Yes",
    wrkprty == 2 ~ "No",
    wrkprty %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(wrkprty)
  ))

table(belgium_data$wrkprty)
## 
##    No   Yes 
## 15067   732
model1 <- lm(trstprl ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##    4.766111    0.692905

Intercept: 4.766

When looking at a category that is not “Yes” in wrkprty, the expected value of trstprl is approximately 4.76.

Having worked in a in a political party is associated with a 0.692 increase in the predicted value of people’s trust in the European parliament.

If they have not worked with a political party, the average expected trust in European parliament is roughly 4.76, and if they have worked in a political party their average expected trust in parliament is roughly 5.4590

tidy(model1)
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)    4.77     0.0180    265.   0       
## 2 wrkprtyYes     0.693    0.0836      8.29 1.26e-16
tidy(model1) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 4.766 0.018 264.806 0
wrkprtyYes 0.693 0.084 8.287 0
summary(model1)
## 
## Call:
## lm(formula = trstprl ~ wrkprty, data = belgium_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.4590 -1.7661  0.2339  1.2339  5.2339 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.76611    0.01800 264.806   <2e-16 ***
## wrkprtyYes   0.69291    0.08362   8.287   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.209 on 15797 degrees of freedom
##   (1334 observations deleted due to missingness)
## Multiple R-squared:  0.004328,   Adjusted R-squared:  0.004265 
## F-statistic: 68.67 on 1 and 15797 DF,  p-value: < 2.2e-16
equatiomatic::extract_eq(model1, use_coefs = TRUE)

\[ \operatorname{\widehat{trstprl}} = 4.77 + 0.69(\operatorname{wrkprty}_{\operatorname{Yes}}) \]

Based on our estimation of peoples trust in parliament, the causal variable (if they worked in a political party) does increase the average trust in politicians.

There is an increasing slope of 0.69. SO the estimate of average trust in Parliament (4.77) would increase when counting for out explanatory variable (wrkprty ‘yes’).

The calculation shows us that people who have not worked in a political party have an estimated average trust of 4.77. The average expected trust in Parliament for respondents who have worked in a political party is 5.46,

Task 2

bulgaria_data <- ess %>%
  filter(cntry == "BG") %>%
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), ## Example of a trust variable
  )

unique(bulgaria_data$stfdem)
##  [1]  0  1 NA  2  3  5  4  6  8  7 10  9
bulgaria_data <- bulgaria_data %>% filter(!is.na(stfdem))
bulgaria_data <- bulgaria_data %>%
  mutate(
    native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_)
  )

table(bulgaria_data$native)
## 
##    No   Yes 
##    97 12239
model2 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept)   nativeYes 
##   2.8041237   0.1189909

Intercept: 2.80

The intercept represents the average satisfaction with democracy in Bulgaria. There is an associated increase of satisfaction around 0.12 if people are native/ born in the country.
If the person is not native, the average trust in democracy in Bulgaria is roughly 2.80, but if the person is “native” their estimated average trust is around 2.92

summary(model2)
## 
## Call:
## lm(formula = stfdem ~ native, data = bulgaria_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9231 -1.9231  0.0769  2.0769  7.1959 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.8041     0.2265  12.382   <2e-16 ***
## nativeYes     0.1190     0.2274   0.523    0.601    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.231 on 12334 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  2.22e-05,   Adjusted R-squared:  -5.887e-05 
## F-statistic: 0.2739 on 1 and 12334 DF,  p-value: 0.6007
equatiomatic::extract_eq(model2, use_coefs = TRUE)

\[ \operatorname{\widehat{stfdem}} = 2.8 + 0.12(\operatorname{native}_{\operatorname{Yes}}) \] Intercept: 2.8 The expected average satisfaction with democracy for respondents that were not born in Bulgaria is 2.8.

The intercept gives us the average satisfaction of people who are NOT native to Bulgaria. Since in order to get the expected average of satisfaction, we need to add the slope/ value of ‘nativeYES’ to the intercept, the intercept left alone if the opposite value of the slope. In this case that is ‘nativeNO’

Task 3

Based on your own outcome and country of interest, one of the predictors re-coded in the tutorial, equatiomatic regression equation.

unitedkingdom_data <- ess %>%
  filter(cntry == "GB") %>%
  mutate(trstplc = ifelse(trstplc %in% c(77, 88, 99), NA, trstplc), ## Example of a trust variable
  )
unique(unitedkingdom_data$trstplc)
##  [1]  7  6  8  9  4  3  2  0  5 10  1 NA
uk_data <- unitedkingdom_data %>% filter(!is.na(trstplt))
uk_data <- uk_data %>%
  mutate( 
  current_union_member = case_when(
      mbtru == 1 ~ "Yes",
      mbtru %in% c(7, 8, 9) ~ NA_character_,
      TRUE ~ "No"
  ))
table(uk_data$current_union_member)
## 
##    No   Yes 
## 17882  3026
model3 <- lm(trstplc ~ current_union_member, data = uk_data)
coefficients <- coef(model3)
print(coefficients)
##             (Intercept) current_union_memberYes 
##               6.2759377               0.1055193

Intercept: 6.275

Intercept is the reference category. The refernce category being peoples trust in police in the UK.

The average estimated trust in police is roughly 6.28 if people are not apart of a union at work.

However, estimated average trust in police increases to around 6.39 if people are apart of a union at work.

tidy(model3)
## # A tibble: 2 × 5
##   term                    estimate std.error statistic p.value
##   <chr>                      <dbl>     <dbl>     <dbl>   <dbl>
## 1 (Intercept)                6.28     0.0178    353.    0     
## 2 current_union_memberYes    0.106    0.0467      2.26  0.0238
tidy(model3) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 6.276 0.018 352.883 0.000
current_union_memberYes 0.106 0.047 2.261 0.024
summary(model3)
## 
## Call:
## lm(formula = trstplc ~ current_union_member, data = uk_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.3815 -1.2759  0.7241  1.7241  3.7241 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)              6.27594    0.01778 352.883   <2e-16 ***
## current_union_memberYes  0.10552    0.04668   2.261   0.0238 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.372 on 20801 degrees of freedom
##   (176 observations deleted due to missingness)
## Multiple R-squared:  0.0002456,  Adjusted R-squared:  0.0001976 
## F-statistic:  5.11 on 1 and 20801 DF,  p-value: 0.02379
equatiomatic::extract_eq(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{trstplc}} = 6.28 + 0.11(\operatorname{current\_union\_member}_{\operatorname{Yes}}) \] The equation shows us the relationship in the trust in police. It shows us that the average trust in police for people in the UK is around 6.28 (y intercept). this is the baseline value for peoples trust in police, which is influenced by the slope we have chosen –> slope = if apart of a union.

The slope has a value of +0.11. This shows that the average trust in police increases by 0.11 for people who are currently apart of a union at work in the UK. The average trust for people who are apart of a union is 6.39

The expected average of people’s trust in police if they are not apart of a union is 6.28.

This is a pretty small slope variable, it does not have a very large/ significant affect on peoples trust in police. It is good to note that there is an increase, but it isnt very large, this tells us that there are other factors that could have larger influences over peoples trust in police and other similar institutions. Like education for example. Perhaps being in a union increases trust because it makes people feel that they have a voice in their place of work and therefore have a more significant voice in the rest of society.