Setting up environment

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

Homework 8 (2.5%) due Nov. 20

Submit on Quercus using assignment tab – not on the discussion board. Make sure to provide both your markdown file and R pubs link (submit as comment if you cannot submit both files at once). If you do not submit both, you will be penalized 1 pt. out of the 2.5 total.

Task 1

Extract the coefficients and intercept using trust in the European Parliament (as the outcome) and worked in a political party (wrkprty, as the explanatory), for the country of Belgium. Write the regression equation and interpret.

#Cleaning for trstep
bel_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep), 
  )

#Cleaning for wrkprty
bel_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(bel_data$wrkprty)
## 
##    No   Yes 
## 15354   738
table(bel_data$trstep)
## 
##    0    1    2    3    4    5    6    7    8    9   10   77   88   99 
## 1139  577  997 1419 1562 3850 2688 2759 1493  314  132   14  499    8
unique(bel_data$trstep)
##  [1]  0  7  8  5  6 99 88  4  9  3  1 10  2 77
unique(bel_data$wrkprty)
## [1] "No"  "Yes" NA
bel_data <- bel_data %>% 
            filter(!is.na(trstep)) %>%
            filter(!is.na(wrkprty))

First steps in linear modelling

This is the line of code in R to produce a linear regression model.

model1 <- lm(trstep ~ wrkprty, data = bel_data)
# model 1 refers to the name where you will store the output
# then it goes lm(outcome ~ explanatory, data = dataset_name)

Now, let’s extract just the coefficients and intercept

coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   7.5491077  -0.9610318

Interpretation:

(Intercept) 7.5491:

The intercept in this model represents the predicted level of trust in the European Parliament for someone from Belgium who hasn’t worked in a political party。

For participants who have not worked in a political party, the average level of trust in the European Parliament is predicted to be about 7.55.

wrkprtyYes -0.9610:

This coefficient tells the predicted difference in trust in the European Parliament between those who have worked in a political party and those who haven’t (our reference group).

Specifically, having worked in a political party (“Yes”) is associated with a decrease of approximately 0.96 in the predicted level of trust in the European Parliament, compared to those who have not worked in a political party (“No”).

In summary:

If a person has not worked in a political party, their predicted average trust in the European Parliament is 7.5491. If a person has worked in a political party, their predicted average trust in the European Parliament is about 6.5891 (which is 7.5491 - 0.9610).

Task 2

Produce a model summary output using either tidy() or summary() (your choice). Use stfdem (satisfaction with democracy) as the outcome and born in country as a predictor (which we recoded/renamed as ‘native’ in the tutorial), for the country of Bulgaria. What is the expected average satisfaction with democracy for respondents that were not born in Bulgaria?

#Cleaning for stfdem
bg_data <- ess %>%
  filter(cntry == "BG") %>%
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), 
  )

#Cleaning for brncntr
bg_data <- ess %>%
  filter(cntry == "BG") %>%
   mutate(native = recode(brncntr,
                   `1` = "Yes",
                   `2` = "No",
                   `7` = NA_character_,
                   `8` = NA_character_,
                   `9` = NA_character_

  ))

table(bg_data$stfdem)
## 
##    0    1    2    3    4    5    6    7    8    9   10   77   88   99 
## 2247 1589 1778 1969 1597 1742  640  400  206   86   84   23  876    3
table(bg_data$brncntr)
## 
##     1     2     7 
## 13134   104     2
model_bg <- lm(stfdem ~ native, data = bg_data)

A first crack at a table output

tidy(model_bg)
## # A tibble: 2 × 5
##   term        estimate std.error statistic   p.value
##   <chr>          <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept)    8.54       2.11    4.05   0.0000512
## 2 nativeYes      0.165      2.12    0.0782 0.938

Striving to improve table presentation

tidy(model_bg) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 8.538 2.107 4.052 0.000
nativeYes 0.165 2.116 0.078 0.938

The full summary output

summary(model_bg)
## 
## Call:
## lm(formula = stfdem ~ native, data = bg_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -8.704 -7.704 -5.704 -3.704 90.296 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   8.5385     2.1074   4.052 5.12e-05 ***
## nativeYes     0.1654     2.1158   0.078    0.938    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 21.49 on 13236 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  4.615e-07,  Adjusted R-squared:  -7.509e-05 
## F-statistic: 0.006108 on 1 and 13236 DF,  p-value: 0.9377

ANS: The expected average satisfaction with democracy in Bulgaria for respondents that were not born in Bulgaria is 8.5385.

Task 3

Based on your own outcome and country of interest, as well as one of the predictors recoded in the tutorial, produce an equatiomatic regression equation. Interpret the regression equation.

First need to install in a special way

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(model_bg, use_coefs = TRUE)

\[ \operatorname{\widehat{stfdem}} = 8.54 + 0.17(\operatorname{native}_{\operatorname{Yes}}) \]

y(hat) = intercept + B(hat)0 + B(hat)1 * x1 + epsilon stfdem(hat) = 8.54 + 0.17 (nativeYes) + error term

Interpretation:

For respondents not born in Bulgaria, the average satisfaction with democracy score is expected to be 8.5385, as indicated by the intercept.

Being born in Bulgaria (when native is “Yes”) is associated with an increase of 0.1654 points in the satisfaction with democracy score, compared to those not born in Bulgaria. However, this effect is not statistically significant (p-value: 0.938), suggesting that we do not have sufficient evidence to claim a real difference in the population based on the sample data.

In the context of this model, the nativeYes coefficient is not statistically significant, being born in Bulgaria does not have a statistically significant effect on how respondents rate their satisfaction with democracy.

End