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.

Task 1

# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom") # 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
## [[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"
setwd("~/Desktop/SOC_202_YAY/")

getwd()  
## [1] "/Users/apple/Desktop/SOC_202_YAY"
ess <- read_fst("All-ESS-Data.fst")
belgium_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep), ## Example of a trust variable
  )
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))
belgium_data <- ess %>%
  filter(cntry == "BE") %>%
mutate(
    wrkprty = case_when(
      wrkprty == 1 ~ "Yes",  # Recode 1 to "Yes"
      wrkprty == 2 ~ "No",   # Recode 2 to "No"
      wrkprty %in% c(7, 8, 9) ~ NA_character_,  # Handle other specific cases where you want to set it as NA
      TRUE ~ as.character(wrkprty)  # Keep other values as-is but ensure they are characters
    ),
    trstep_recode = case_when(
      trstep == 0 ~ "No trust at all",
      trstep == 1 ~ "1",
      trstep == 2 ~ "2",
      trstep == 3 ~ "3",
      trstep == 4 ~ "4",
      trstep == 5 ~ "5",
      trstep == 6 ~ "6",
      trstep == 7 ~ "7",
      trstep == 8 ~ "8",
      trstep == 9 ~ "9",
      trstep == 10 ~ "Complete trust",
      trstep %in% c(77, 88, 99) ~ NA_character_,
      TRUE ~ as.character(trstep)  # Keep other values as characters
    )
  )
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   7.5491077  -0.9610318

Interpretation:

(Intercept) 7.55:

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 “No BA” in educ_level (thus, holding a “BA”), the expected value of trstplt is approximately 7.55.

educ_levelNo BA -0.96:

This coefficient represents the difference in the predicted value of trust in politicians between the “No BA” category and the reference category (“BA”) for the educ_level variable.

Specifically, having “No BA” is associated with a decrease of approximately 0.96 in the predicted value of trust in politicians, compared to having a “BA”.

In summary:

If a person has a “BA” (assuming that’s the reference category), their predicted average trust in politicians is 7.55. If a person does not have a “BA” (i.e., “No BA”), their predicted average trust in politicians is 6.59 (given 7.55 - 0.96)

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 <- ess %>%
  filter(cntry == "BE") %>%
mutate(
    native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_),
    stfdem_recode = case_when(
      stfdem == 0 ~ "Extremely dissatisfied",
      stfdem == 1 ~ "1",
      stfdem == 2 ~ "2",
      stfdem == 3 ~ "3",
      stfdem == 4 ~ "4",
      stfdem == 5 ~ "5",
      stfdem == 6 ~ "6",
      stfdem == 7 ~ "7",
      stfdem == 8 ~ "8",
      stfdem == 9 ~ "9",
      stfdem == 10 ~ "Extremely satisfied",
      stfdem %in% c(77, 88, 99) ~ NA_character_,
      TRUE ~ as.character(stfdem)  # Keep other values as characters
    )
  )
model2 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept)   nativeYes 
##    7.610496   -1.043938
summary(model2)
## 
## Call:
## lm(formula = stfdem ~ native, data = bulgaria_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -7.610 -2.567 -0.610  0.433 92.433 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   7.6105     0.2253  33.777  < 2e-16 ***
## nativeYes    -1.0439     0.2401  -4.349 1.38e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.27 on 17445 degrees of freedom
##   (4 observations deleted due to missingness)
## Multiple R-squared:  0.001083,   Adjusted R-squared:  0.001026 
## F-statistic: 18.91 on 1 and 17445 DF,  p-value: 1.378e-05

Task 3

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
# remotes::install_github("datalorax/equatiomatic"

\[ \operatorname{\widehat{stfdem}} = 7.61 - 1.04(\operatorname{native}_{\operatorname{Yes}}) \] y(hat) = intercept + B(hat)0 + B(hat)1 * x1 + epsilon

trstplt(hat) = 7.61 - 1.04 (No BA) + error term