packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom")
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.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"
ess <- read_fst("All-ESS-Data.fst")
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.
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))
belgium_data <- belgium_data %>%
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
## 14871 727
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.9405554 0.4308338
The regression equation can be written as: trstep = 4.94 + 0.43 * wrkprty. Where: trstep is the dependent variable, wrkprty is the independent variable, with “Yes”, “No” as its categories, beta_0 is the intercept of the model, beta_1 is the coefficient for the wrkprty variable. Intercept of beta_0: This represents the expected value of trstep when wrkprty is at the reference category (usually the one not included in the coefficients list, often the first alphabetically, which could be “No” in this case). Coefficient of wrkprty (beta_1): This indicates the expected change in trstep for the “Yes” category of wrkprty, compared to the reference category (likely “No”).
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?
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))
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
model1 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
summary(model1)
##
## 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 expected average satisfaction with democracy for respondents not born in Bulgaria is approximately 2.8041, based on the model’s intercept. This value reflects the average level of satisfaction on the scale used in the survey, assuming that the scale is likely to be from 0 to 10 or similar.
Given the high p-value for the nativeYes coefficient and the very low R-squared values, it’s important to note that this model explains very little of the variation in satisfaction with democracy and that the effect of being native or not is not statistically significant in this data.
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.
germany_data <- ess %>%
filter(cntry == "GR") %>%
mutate(impfree = ifelse(impfree %in% c(7, 8, 9), NA, impfree), ## Example of a trust variable
)
unique(germany_data$impfree)
## [1] 1 5 2 4 3 NA 6
germany_data <- germany_data %>% filter(!is.na(impfree))
germany_data <- germany_data %>%
mutate(polintr = case_when(
polintr %in% c(1, 2) ~ "Interested",
polintr %in% c(3, 4) ~ "Not Interested",
polintr %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(polintr)
))
table(germany_data$polintr)
##
## Interested Not Interested
## 3870 8564
model1 <- lm(impfree ~ polintr, data = germany_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) polintrNot Interested
## 1.9475452 0.1731928
summary(model1)
##
## Call:
## lm(formula = impfree ~ polintr, data = germany_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.1207 -0.9475 -0.1207 0.8793 4.0525
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.94755 0.01670 116.647 <2e-16 ***
## polintrNot Interested 0.17319 0.02012 8.609 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.039 on 12432 degrees of freedom
## (32 observations deleted due to missingness)
## Multiple R-squared: 0.005926, Adjusted R-squared: 0.005846
## F-statistic: 74.11 on 1 and 12432 DF, p-value: < 2.2e-16
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
force = TRUE
equatiomatic::extract_eq(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{impfree}} = 1.95 + 0.17(\operatorname{polintr}_{\operatorname{Not\ Interested}}) \] Regression equation: impfree = 1.94_0.17*polintrnotinterested.
Intercept (beta_0=1.94755): This value represents the expected level of importance of freedom for respondents who are interested in politics (as “Interested” is likely the reference category). It means that if a person is politically interested, their average importance of freedom score would be approximately 1.95, based on the scale used in the survey.
Coefficient for polintrNot Interested (beta_1=0.17319): This coefficient shows the expected change in the importance of freedom for respondents who are not interested in politics compared to those who are interested. Specifically, it suggests that respondents who are not interested in politics rate the importance of freedom on average 0.17319 points higher than those who are interested in politics, holding other factors constant.
While there is a statistically significant relationship between political interest and the importance of freedom, the effect size is small, and the overall explanatory power of the model is quite limited. This implies that other factors not included in this model likely play a more substantial role in determining how people in this dataset view the importance of freedom.