This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
#Task 1
packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom", "remotes") # 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"
##
## [[6]]
## [1] "remotes" "broom" "modelsummary" "fst" "infer"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(wrkprty= ifelse(wrkprty %in% c(77, 88, 99), NA, wrkprty), ## Example of a trust variable
)
unique(belgium_data$wrkprty)
## [1] 2 1 9 8 7 NA
belgium_data <- belgium_data %>% filter(!is.na(wrkprty))
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprty
## 5.276821 1.140297
Interpretation:
Intercept: 5.28
When combined with the category of having worked in a political party (wrkprty), the outcome variable (trust in the European Parliament) which represents the intercept, is approximately 5.28.
wrkprty: 1.14
This value represents the difference in the predicted value of trust in the European Parliament in the “worked in a political party” versus the “not worked in a poitical party” category
This means that having “worked in a political party” is associated with a decrease of approximately 1.14 in the predicted value of trust in the European Parliament, compared to “not having worked in a political party”.
Therefore, an individual who has worked in a political party, has a predicted 5.28 level of trust in the European Parliament, if the person has not worked in a political party, their predicted level of trust in the European Parliament is 4.14
bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), ## Example of a trust variable
)
unique(bulgaria_data$stfdem)
## [1] 0 1 NA 2 3 5 4 6 8 7 10 9
bulgaria_data <- bulgaria_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(bulgaria_data$native)
##
## No Yes
## 104 13134
model2 <- lm(stfdem ~ domicil, data = bulgaria_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept) domicil
## 2.9199420059 0.0008523524
tidy(model2)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 2.92 0.0426 68.6 0
## 2 domicil 0.000852 0.0153 0.0556 0.956
tidy(model2) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 2.920 | 0.043 | 68.567 | 0.000 |
| domicil | 0.001 | 0.015 | 0.056 | 0.956 |
(Intercept) estimate: 2.92 Domicil estimate: 0.001 Difference: 2.91
If a person was born in Bulgaria, their predicted level of satisfaction with democracy is approximately 2.92, but given the difference between people born in Bulgaria as opposed to people who were not born in bulgaria (0.001), the estimated level of satisfaction with democracy for people not born in Bulgaria is approximately 2.91.
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
imacrlv (if immigrants commit a crime should they leave? - outcome) imgfrnd (how many immigrant friends? - explanatory)
spain_data <- ess %>%
filter(cntry == "ES") %>%
mutate(imacrlv = ifelse(imacrlv %in% c(77, 88, 99), NA, imacrlv), ## Example of a trust variable
)
unique(spain_data$imacrlv)
## [1] 2 3 1 4 5 8 7 NA
spain_data <- spain_data %>%
# Handle NA values for maritalb
mutate(maritalb = na_if(maritalb, 77),
maritalb = na_if(maritalb, 88),
maritalb = na_if(maritalb, 99)) %>%
# Recode marital status
mutate(
marital_status = case_when(
maritalb %in% c(1, 2) ~ "Married",
!is.na(maritalb) ~ "Not married",
TRUE ~ NA_character_
),
# Determine if there are children in the household based on multiple rshipaX variables
children = if_any(starts_with("rshipa"), ~.x == 2),
# Use chldhhe as a fallback for children
chldhhe = as.logical(chldhhe == 1),
# Combine both indicators into a single children variable
children = case_when(
!is.na(children) ~ children,
!is.na(chldhhe) ~ chldhhe,
TRUE ~ FALSE
),
# Recode into categories
children_cat = if_else(children, "Children", "No Children")
)
# Display tables of the new and old children variables
table(spain_data$children_cat)
##
## Children No Children
## 11656 7796
table(spain_data$marital_status)
##
## Married Not married
## 4921 4295
model3 <- lm(imacrlv ~ children_cat, data = spain_data)
equatiomatic::extract_eq(model3, use_coefs = TRUE)
\[ \operatorname{\widehat{imacrlv}} = 3.13 - 0.15(\operatorname{children\_cat}_{\operatorname{No\ Children}}) \]