setwd("~/Desktop/r/first one")
# 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.3 ✔ 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[ess$cntry == "BE",]
belgium_data <- belgium_data %>%
mutate(lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale)) %>%
# Recode Political Interest
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)
)) %>%
# Recode work for party
mutate(wrkprty = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkprty)
)) %>%
# Recode worked for civil org/association
mutate(workcivil = case_when(
wrkorg == 1 ~ "Yes",
wrkorg == 2 ~ "No",
wrkorg %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkorg)
)) %>%
# Recode participation in protest
mutate(protest = case_when(
pbldmn == 1 ~ "Yes",
pbldmn == 2 ~ "No",
pbldmn %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(pbldmn)
))
table(belgium_data$wrkprty)
##
## No Yes
## 15354 738
model1 <- lm(trstplt ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.7282141 0.2108102
#My answer: #For the explanatory variable when the level is 1, there is an increase of 0.2108 on the outcome (trstplt) variable. When the explanatory variable level is 0, the outcome is 4.73.
#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?
BG_data <- ess[ess$cntry == "BG",]
BG_data <- BG_data %>%
mutate(
# Recoding place of residence
geo = recode(as.character(domicil),
'1' = "Urban",
'2' = "Peri-Urban",
'3' = "Rural",
'4' = "Rural",
'5' = "Rural",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_),
# Recoding if born in the country
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_)
)
table(BG_data$native)
##
## No Yes
## 104 13134
model2 <- lm(stfdem ~ native, data = BG_data)
summary(model2)
##
## 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
#The expected average satisfaction with democracy for respondents that were not born in Bulgaria is 2*0.1654 + 8.5385=8.8693.
#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.
IT_data <- ess[ess$cntry == "IT",]
IT_data <- IT_data %>%
mutate(lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale)) %>%
# Recode Political Interest
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)
)) %>%
# Recode work for party
mutate(wrkprty = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkprty)
)) %>%
# Recode worked for civil org/association
mutate(workcivil = case_when(
wrkorg == 1 ~ "Yes",
wrkorg == 2 ~ "No",
wrkorg %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkorg)
)) %>%
# Recode participation in protest
mutate(protest = case_when(
pbldmn == 1 ~ "Yes",
pbldmn == 2 ~ "No",
pbldmn %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(pbldmn)
))
table(IT_data$polintr)
##
## Interested Not Interested
## 3218 6910
model3 <- lm(trstlgl ~ polintr, data = IT_data)
coefficients3 <- coef(model3)
coefficients3
## (Intercept) polintrNot Interested
## 6.3610938 0.9710335
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.73 + 0.21(\operatorname{wrkprty}_{\operatorname{Yes}}) \]
#My answer: This suggest that the individuals who are interested in politics are expected to have a trust level in the legal system that is 0.97 points higher than the baseline group.