html link: file:///Users/sss/Desktop/soc202/HW8/HW8-Ruoci.html
##Task1 Extract the coefficients and intercept using trust in the European Parliament (trstep, as the outcome) and worked in a political party (wrkprty, as the explanatory), for the country of Belgium. Write the regression equation and interpret.
# 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.4 ✔ 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")
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep)) %>%
filter(!is.na(trstep))
# Fit linear regression model
model_belgium <- lm(trstep ~ wrkprty, data = belgium_data)
# Print coefficients
coefficients_belgium <- coef(model_belgium)
print(coefficients_belgium)
## (Intercept) wrkprty
## 5.5366318 -0.2943243
trstep=5.54−0.29×wrkprty
(Intercept) 5.54: 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” in wrkprty (thus, worked in political party), the expected value of trstplt is approximately 5.54.
wrkprty(not worked) -0.29:
Specifically, chose “Yes”(1) is associated with a decrease of approximately 0.51 in the predicted value of trust in politicians, compared to having a “No”(2).
I wrote an explanation following the tut file, but I think it was not clear in this case, so I asked chatGPT and rewrite my explanation ⬇️
This coefficient represents the change in the predicted value of Trust in the European Parliament for a one-unit change in the variable wrkprty. In this context, where wrkprty is binary (1=yes, 2=no), it implies a change from not working in a political party (wrkprty = 2) to working in one (wrkprty = 1). Holding all other variables constant, there is an associated decrease of approximately 0.29 in the predicted value of Trust in the European Parliament when an individual moves from not working in a political party to working in one.
In summary:
If an individual has not worked in a political party (wrkprty=2), their predicted average Trust in the European Parliament is approximately 5.54. If an individual has worked in a political party (wrkprty=1), there is an associated decrease of approximately 0.29 in the predicted average Trust in the European Parliament.
So, the model suggests that working in a political party (wrkprty = 1) is associated with a lower predicted average Trust in the European Parliament compared to not working in a political party (wrkprty = 2), as indicated by the negative coefficient for the variable wrkprty.
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(BG). 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)) %>%
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_)
)
model_bulgaria <- lm(stfdem ~ native, data = bulgaria_data)
# Print coefficients
coefficients_bulgaria <- coef(model_bulgaria)
print(coefficients_bulgaria)
## (Intercept) nativeYes
## 2.8041237 0.1189909
summary(model_bulgaria)
##
## 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
If a respondent is not born in Bulgaria (native=“No”), there is an associated increase of approximately 0.12 in the predicted average Satisfaction with Democracy.
Interpretation (Intercept) 2.80: The intercept represents the predicted value of the outcome variable, Satisfaction with Democracy (stfdem), when the explanatory variable (native) is at its reference level or, in this case, when the respondent is born in Bulgaria (native=“No”). The expected value of Satisfaction with Democracy is approximately 2.80 for respondents who were not born in Bulgaria.
nativeYes 0.12: This coefficient represents the change in the predicted value of Satisfaction with Democracy for a one-unit change in the variable native, where native is recoded as “Yes” for respondents born in Bulgaria and “No” for those not born in Bulgaria. There is an associated increase of approximately 0.12 in the predicted value of Satisfaction with Democracy when a respondent is born in Bulgaria (native=“Yes”) compared to those not born in Bulgaria.
So, if a respondent is not born in Bulgaria (native=“No”), there is an associated increase of approximately 0.12 in the predicted average Satisfaction with Democracy.
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.
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_bulgaria, use_coefs = TRUE)
\[ \operatorname{\widehat{stfdem}} = 2.8 + 0.12(\operatorname{native}_{\operatorname{Yes}}) \]
stfdem(hat) =2.8+0.12×(native Yes) + error term
The regression equation provides a way to predict the satisfaction with democracy (stfdem^) based on the values of the predictor variable, native. The intercept term (2.8) represents the predicted satisfaction with democracy when the native variable is at its reference level, and The coefficient for native Yes (0.12) represents the change in the predicted satisfaction with democracy for individuals born in Bulgaria compared to those not born in Bulgaria.
Appendix: I ask ChatGPT about the explanation of the equation and change some my my words to make them easy to understand