Task 1

ess <- read_fst("All-ESS-Data.fst")
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep), )
unique(belgium_data$trstep)
##  [1]  0  7  8  5  6 NA  4  9  3  1 10  2
belgium_data <- belgium_data %>% filter(!is.na(trstep))
unique(belgium_data$trstep)
##  [1]  0  7  8  5  6  4  9  3  1 10  2
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(wrkprty = ifelse(wrkprty %in% c(7, 8, 9), NA, wrkprty),
)
unique(belgium_data$wrkprty)
## [1]  2  1 NA
belgium_data <- belgium_data %>% filter(!is.na(wrkprty))
unique(belgium_data$wrkprty)
## [1] 2 1
belgium_data <- ess %>%
filter(cntry == "BE") %>%
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 
## 15354   738
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   7.5491077  -0.9610318
options(repos = c(CRAN = "https://cloud.r-project.org"))

install.packages("broom")
## 
## The downloaded binary packages are in
##  /var/folders/bb/gzxzs8ns3fz91pbm5bmtt0c00000gn/T//Rtmpilk7VP/downloaded_packages
library(broom)
   
tidy(model1)
## # A tibble: 2 × 5
##   term        estimate std.error statistic p.value
##   <chr>          <dbl>     <dbl>     <dbl>   <dbl>
## 1 (Intercept)    7.55      0.117     64.6   0     
## 2 wrkprtyYes    -0.961     0.546     -1.76  0.0782
tidy(model1) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 7.549 0.117 64.609 0.000
wrkprtyYes -0.961 0.546 -1.761 0.078
summary(model1)
## 
## Call:
## lm(formula = trstep ~ wrkprty, data = belgium_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -7.549 -3.549 -2.549 -0.549 91.451 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   7.5491     0.1168  64.609   <2e-16 ***
## wrkprtyYes   -0.9610     0.5456  -1.761   0.0782 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.48 on 16090 degrees of freedom
##   (1359 observations deleted due to missingness)
## Multiple R-squared:  0.0001928,  Adjusted R-squared:  0.0001306 
## F-statistic: 3.103 on 1 and 16090 DF,  p-value: 0.07819
remotes::install_github("datalorax/equatiomatic")
## Skipping install of 'equatiomatic' from a github remote, the SHA1 (29ff168f) has not changed since last install.
##   Use `force = TRUE` to force installation
library(equatiomatic)

model1equation <- equatiomatic::extract_eq(model1, use_coefs = TRUE)
model1equation

\[ \operatorname{\widehat{trstep}} = 7.55 - 0.96(\operatorname{wrkprty}_{\operatorname{Yes}}) \] #Interpretation: The intercept represents the estimated average trust in the European Parliament when the explanatory variable (working in a political party) is zero. Because wrkprty is a categorical variable, it does not mean anything except it provides a reference point of avg trust without taking into account a relationship with another variable. the “wrkprtyYes” coefficient is -0.9610318. this means if someone has worked in political party or action group last 12 months, there would be a decrease in trust in the European Parliament by 0.9610318 compared to those who did not work in a party.Someone who did not work in a party has avg trust of 6.589. The p-value is 0.0782, meaning there is not enough evidence to reject the null.

Task 2

bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
)
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))
unique(bulgaria_data$stfdem)
##  [1]  0  1  2  3  5  4  6  8  7 10  9
bulgaria_data <- bulgaria_data %>%
mutate(
native = recode(brncntr,
                 `1` = "Yes",
                `2` = "No",
                 `7` = NA_character_,
                 `8` = NA_character_,
                 `9` = NA_character_)
 )
table(bulgaria_data$brncntr)
## 
##     1     2     7 
## 12239    97     2
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
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

#The average trust in democracy for respondents that were not born in Bulgaria is 2.8041237.

Task 3

portugal_data <- ess %>%
filter(cntry == "PT") %>%
  mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
)
unique(portugal_data$happy)
##  [1]  5  4  3  2  6  9  8 10  7  0  1 NA
 portugal_data <- portugal_data %>%
filter(!is.na(happy))
unique(portugal_data$happy)
##  [1]  5  4  3  2  6  9  8 10  7  0  1
portugal_data <- portugal_data %>%
mutate(
retired_status = case_when(
rtrd == 1 ~ "Yes",
rtrd == 0 ~ "No",
TRUE ~ NA_character_
)
)
table(portugal_data$retired_status)
## 
##    No   Yes 
## 12139  5641
model3 <- lm(happy ~ retired_status, data = portugal_data)
coefficients <- coef(model3)
print(coefficients)
##       (Intercept) retired_statusYes 
##         6.9201746        -0.6895418
summary(model3)
## 
## Call:
## lm(formula = happy ~ retired_status, data = portugal_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.9202 -1.2306  0.0798  1.0798  3.7694 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        6.92017    0.01795  385.46   <2e-16 ***
## retired_statusYes -0.68954    0.03187  -21.63   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.978 on 17778 degrees of freedom
## Multiple R-squared:  0.02565,    Adjusted R-squared:  0.0256 
## F-statistic:   468 on 1 and 17778 DF,  p-value: < 2.2e-16
tidy(model3)
## # A tibble: 2 × 5
##   term              estimate std.error statistic   p.value
##   <chr>                <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept)          6.92     0.0180     385.  0        
## 2 retired_statusYes   -0.690    0.0319     -21.6 1.81e-102
equatiomatic::extract_eq(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{happy}} = 6.92 - 0.69(\operatorname{retired\_status}_{\operatorname{Yes}}) \] #When someone is not retired, their happiness score is avg 6.9201746, when status changes to retired, their score drops to an avg of 6.2306328. The p value is < 2.2e-16, an extremely small value, meaning there is enough evidence to reject the null hypothesis. There is strong evidence of a meaningful correlation in the data.