R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

packages <- c("tidyverse", "infer", "fst") # add any you need here

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.4
## ✔ 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
## [[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"
ess <- read_fst("All-ESS-Data.fst")

Task 1

Belgium_data <- ess %>%
  filter(cntry == "BE") %>%
mutate(
    trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt  
    ),
    wrkprty = case_when(
      wrkprty == 1 ~ "Yes",  
      wrkprty == 2 ~ "No",  
      wrkprty %in% c(7, 8, 9) ~ NA_character_,  
      TRUE ~ as.character(wrkprty)  
    )
  )
unique(Belgium_data$trstplt)
##  [1]  8  6  7  4 NA  5  3  9  2  1  0 10
Belgium_data <- Belgium_data %>% filter(!is.na(trstplt))
unique(Belgium_data$wrkprty)
## [1] "No"  "Yes" NA
Belgium_data <- Belgium_data %>% filter(!is.na(wrkprty))
model1 <- lm(trstplt ~ wrkprty, data = Belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.1512230   0.6750999

The intercept represents the predicted value of the outcome variable (in this case, Trust in Politicians, 0-10) when the explanatory variable is zero or, in the case of categorical variables, at their reference level.

Put differently, when looking at the category that’s NOT worked in a political party, the expected value of trstplt is approximately 4.151.

worked in a political party 0.675:

This coefficient represents the difference in the predicted value of trust in politicians between worked in a political party and not worked in a political party for the wrkprty variable.

Specifically, having “not worked in a political party” is associated with a decrease of approximately 0.675 in the predicted value of trust in politicians, compared to having a “worked in a political party”.

In summary: If a person who worked in a political party, their predicted average trust in politicians is 4.15. If a person not worked in a political party, their predicted average trust in politicians is 3.48.

Task2

Bulgaria_data <- ess %>%
  filter(cntry == "BG") %>%
  mutate(
      native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_),
    
    stfdem= case_when(
      stfdem == 0 ~ "Extremely dissatisfied",  
      stfdem == 1 ~ "Extremely satisfied",  
      stfdem %in% c(77, 88, 99) ~ NA_character_,  
      TRUE ~ as.character(stfdem)  
    )
  )
model2 <- lm(brncntr~ stfdem, data = Bulgaria_data)
summary(model1)
## 
## Call:
## lm(formula = trstplt ~ wrkprty, data = Belgium_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.8263 -1.1512  0.8488  1.8488  5.8488 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.15122    0.01762 235.612   <2e-16 ***
## wrkprtyYes   0.67510    0.08206   8.227   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.176 on 15984 degrees of freedom
## Multiple R-squared:  0.004217,   Adjusted R-squared:  0.004155 
## F-statistic: 67.69 on 1 and 15984 DF,  p-value: < 2.2e-16

Tsk3

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
equatiomatic::extract_eq(model1, use_coefs = TRUE)

\[ \operatorname{\widehat{trstplt}} = 4.15 + 0.68(\operatorname{wrkprty}_{\operatorname{Yes}}) \] y(hat) = intercept + B(hat)0 + B(hat)1 * x1 + epsilon

trstplt(hat) = 4.15 +0.68 (Not wrkprty ) + error term