setwd("~/Desktop/SOC202 H1/Coding Tut 1")
getwd()
## [1] "/Users/marcohuang/Desktop/SOC202 H1/Coding Tut 1"
# List of packages
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")
##TASK1
Belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt),)
unique(Belgium_data$trstplt)
## [1] 8 6 7 4 NA 5 3 9 2 1 0 10
Belgium_data <- Belgium_data %>% filter(!is.na(trstplt))
Belgium_data <- Belgium_data %>%
mutate(
wrkprty = recode(as.character(wrkprty),
'1' = "Yes",
'2' = "No",
'3' = NA_character_ ,
'4' = NA_character_,
'5' = NA_character_,
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_),
)
table(Belgium_data$wrkprty)
##
## No Yes
## 15249 737
model1 <- lm(trstplt ~ wrkprty, data = Belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.1512230 0.6750999
tidy(model1)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 4.15 0.0176 236. 0
## 2 wrkprtyYes 0.675 0.0821 8.23 2.06e-16
tidy(model1) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 4.151 | 0.018 | 235.612 | 0 |
| wrkprtyYes | 0.675 | 0.082 | 8.227 | 0 |
summary(model1)
##
## Call:
## lm(formula = trstplt ~ wrkprty, data = Belgium_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.8263 -1.1512 0.8488 1.8488 5.8488
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.15122 0.01762 235.612 <2e-16 ***
## wrkprtyYes 0.67510 0.08206 8.227 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.176 on 15984 degrees of freedom
## (1350 observations deleted due to missingness)
## Multiple R-squared: 0.004217, Adjusted R-squared: 0.004155
## F-statistic: 67.69 on 1 and 15984 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
equatiomatic::extract_eq(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{trstplt}} = 4.15 + 0.68(\operatorname{wrkprty}_{\operatorname{Yes}}) \]
#Given equation: {trstplt}} = 4.15 + 0.68 (\operatorname{wrkprty}_{\operatorname{Yes}})
Interpretation: The intercept of 4.15 represents an expectation value of people who trust in politicians. In a scale of 0-10, where 0 is no trust at all and 10 being completely trust, an estimate of 4.15 tells us that unfortunately these people are not really trusting on the politicians. Nevertheless, with the variable of “wrkprty”, the coeficient of +0.68 represents that a higher trust level is noticeable if a person works in parties and they seem to be more trusting on politicians.
##TASK 2
BG_data <- ess %>%
filter(cntry == "BG") %>%
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),)
unique(BG_data$stfdem)
## [1] 0 1 NA 2 3 5 4 6 8 7 10 9
BG_data <- BG_data %>% filter(!is.na(stfdem))
BG_data <- BG_data %>%
mutate(
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_)
)
table(BG_data$native)
##
## No Yes
## 97 12239
model1 <- lm(stfdem ~ native, data = BG_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
tidy(model1)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 2.80 0.226 12.4 5.30e-35
## 2 nativeYes 0.119 0.227 0.523 6.01e- 1
tidy(model1) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 2.804 | 0.226 | 12.382 | 0.000 |
| nativeYes | 0.119 | 0.227 | 0.523 | 0.601 |
summary(model1)
##
## Call:
## lm(formula = stfdem ~ native, data = BG_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
equatiomatic::extract_eq(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{stfdem}} = 2.8 + 0.12(\operatorname{native}_{\operatorname{Yes}}) \] The expected average for people in Bulgaria satisfying with democracy is 2.80 with a possible increasing coeficient of 0.12 if there are people were to be native.
##TASK 3
#I have chosen “stfeco” as my outcome variable, United Kingdom as study country, and “gndr” as explanatory variable.
UK_data <- ess %>%
filter(cntry == "GB") %>%
mutate(stfeco = ifelse(stfdem %in% c(77, 88, 99), NA, stfeco),)
unique(UK_data$stfeco)
## [1] 8 4 1 6 5 0 NA 2 7 88 3 9 10 77
UK_data <- UK_data %>% filter(!is.na(stfeco))
UK_data <- UK_data %>%
mutate(
gndr = case_when(
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
gndr == 9 ~ NA_character_,
TRUE ~ as.character(gndr)
))
table(UK_data$gndr)
##
## Female Male
## 10836 9163
model1 <- lm(stfeco ~ gndr, data = UK_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) gndrMale
## 5.6083426 -0.2490716
tidy(model1) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 5.608 | 0.093 | 60.226 | 0.00 |
| gndrMale | -0.249 | 0.138 | -1.810 | 0.07 |
summary(model1)
##
## Call:
## lm(formula = stfeco ~ gndr, data = UK_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.608 -2.608 -0.608 0.641 82.641
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.60834 0.09312 60.23 <2e-16 ***
## gndrMale -0.24907 0.13757 -1.81 0.0702 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.694 on 19997 degrees of freedom
## (9 observations deleted due to missingness)
## Multiple R-squared: 0.0001639, Adjusted R-squared: 0.0001139
## F-statistic: 3.278 on 1 and 19997 DF, p-value: 0.07024
equatiomatic::extract_eq(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{stfeco}} = 5.61 - 0.25(\operatorname{gndr}_{\operatorname{Male}}) \]
Given equation: 5.61 - 0.25(_{} From a scale of 0-10 judging people’s satisfaction on their economy in United Kingdom, while 0 refers to extremely dissatisfied and 10 stands for extremely satisfied, it is found that an average score of 5.61 showing that people in UK are mostly satisfied with present economy. However, given the explanatory/predictor variable of gender, a decreased overall score of 5.61 -0.25 from the regresstion equation tells us that males in United Kingdom seems to be less satisfied compared to females in United Kingdom.