#SETUP
setwd("/Users/shuhruhakhand/Desktop/SOC202")
# 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"
ess <- read_fst("All-ESS-Data.fst")
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(workprty = recode(as.character(wrkprty),
'1' = "Yes",
'2' = "No",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_),
trstep = case_when(
trstep == '0' ~ "0",
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' ~ "10",
trstep == '77' ~ NA_character_,
trstep == '88' ~ NA_character_,
trstep == '99' ~ NA_character_,
)
)
unique(belgium_data$workprty)
## [1] "No" "Yes" NA
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 %>% filter(!is.na(workprty))
model2 <- lm(trstep ~ workprty, data = belgium_data)
summary(model2)
##
## Call:
## lm(formula = trstep ~ workprty, data = belgium_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.3714 -0.9406 0.0594 2.0594 5.0594
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.94056 0.01872 263.892 < 2e-16 ***
## workprtyYes 0.43083 0.08672 4.968 6.83e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.283 on 15596 degrees of freedom
## Multiple R-squared: 0.00158, Adjusted R-squared: 0.001516
## F-statistic: 24.68 on 1 and 15596 DF, p-value: 6.832e-07
coefficients <- coef(model2)
print(coefficients)
## (Intercept) workprtyYes
## 4.9405554 0.4308338
equatiomatic::extract_eq(model2, use_coefs = TRUE)
\[ \operatorname{\widehat{trstep}} = 4.94 + 0.43(\operatorname{workprty}_{\operatorname{Yes}}) \]
Interpretation:
People who have worked in a political party have an average of 4.94 of trust in the European Parliament out of a 10 scale. The slope is 0.43, meaning that there is an increase of 0.43 in the predicted value of trust in european parliament of those who worked in a party ( those who worked in a party is associated with a increase of approx. 0.43).
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(
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_),
stfdem = case_when(
stfdem == '0' ~ "0",
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' ~ "10",
stfdem == '77' ~ NA_character_,
stfdem == '88' ~ NA_character_,
stfdem == '99' ~ NA_character_,
)
)
unique(bulgaria_data$stfdem)
## [1] "0" "1" NA "2" "3" "5" "4" "6" "8" "7" "10" "9"
unique(bulgaria_data$native)
## [1] "Yes" "No" NA
bulgaria_data <- bulgaria_data %>% filter(!is.na(stfdem))
bulgaria_data <- bulgaria_data %>% filter(!is.na(native))
model <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
summary(model)
##
## 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
## 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 who were not born in Bulgaria can be estimated from the intercept. The intercept is given as 2.8041, meaning out of a scale of 10, the averahe satifiscation is 2.8.
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.
denmark_data <- ess %>%
filter(cntry == "DK") %>%
mutate(
rlgblg = ifelse(rlgblg %in% c(7, 8), NA, rlgblg),
stfgov = case_when(
stfgov == '0' ~ "0",
stfgov == '1' ~ "1",
stfgov == '2' ~ "2",
stfgov == '3' ~ "3",
stfgov == '4' ~ "4",
stfgov == '5' ~ "5",
stfgov == '6' ~ "6",
stfgov == '7' ~ "7",
stfgov == '8' ~ "8",
stfgov == '9' ~ "9",
stfgov == '10' ~ "10",
stfgov == '77' ~ NA_character_,
stfgov == '88' ~ NA_character_,
stfgov == '99' ~ NA_character_,
)
)
unique(denmark_data$stfgov)
## [1] "5" "7" "8" "3" "6" "4" "1" "0" "9" "2" "10" NA
unique(denmark_data$rlgblg)
## [1] 2 1 9 NA
denmark_data <- denmark_data %>% filter(!is.na(stfgov))
denmark_data <- denmark_data %>% filter(!is.na(rlgblg))
model_denmark <- lm(stfgov ~ rlgblg, data = denmark_data)
summary(model_denmark)
##
## Call:
## lm(formula = stfgov ~ rlgblg, data = denmark_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.5351 -1.5351 0.4649 1.9201 6.1066
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.99028 0.05903 101.5 <2e-16 ***
## rlgblg -0.45520 0.03889 -11.7 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.334 on 12061 degrees of freedom
## Multiple R-squared: 0.01123, Adjusted R-squared: 0.01115
## F-statistic: 137 on 1 and 12061 DF, p-value: < 2.2e-16
# model 1 refers to the name where you will store the output
# then it goes lm(outcome ~ explanatory, data = dataset_name)
coefficients <- coef(model_denmark)
print(coefficients)
## (Intercept) rlgblg
## 5.990284 -0.455204
equatiomatic::extract_eq(model_denmark, use_coefs = TRUE)
\[ \operatorname{\widehat{stfgov}} = 5.99 - 0.46(\operatorname{rlgblg}) \]
This regression equation means that the intercept of 5.99 is the average satisfaction in government for those who are in a religious denomination. The satisfaction decreases by 0.456 (slope is -0.46). Essentially, this show the value of satisfcation in government between the “no” category and the reference category (“yes”) for the relgious belonging variable, in other words, not belonging to a religion is associated to a decrease of 0.46 in the predicted satisfaction, compared to being in a religous group.