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.
Setting up the Environment
# 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")
Task 1: 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(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep),
)
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 %>%
mutate(wrkprty = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7,8,9) ~ NA_character_,
TRUE ~ as.character(wrkprty)
)
)
table(belgium_data$wrkprty)
##
## No Yes
## 14871 727
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.9405554 0.4308338
Response: Using the code above, I was able to extract the intercept and coefficient values from a regression model. The outcome variable was trust in the European Parliament and the explanatory variable was working in a political party. The regression equation for predicting trust in the European Parliament solely on if one has worked in a political party is trstep = 4.94 + 0.43 [wrkprty(yes)]. The intercept value of 4.94 represents the predicted average for trust level in European Parliament for those that did not work in a political party. The coefficient 0.43 is associated with this as it is the predicted increase value for if there is trust in the European Parliament vs. not working in a political party.
Task 2: 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(
stfdem = ifelse(stfdem %in% c(77,88,99), NA, stfdem),
native = recode(brncntr,
'1' = "Yes",
'2' = "No",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_)
)
table(bulgaria_data$stfdem)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 2247 1589 1778 1969 1597 1742 640 400 206 86 84
table(bulgaria_data$native)
##
## No Yes
## 104 13134
model1 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
Response: The average satisfaction with democracy for respondents that were not born in Bulgaria is the intercept value which is 2.804, rounded to an average of 2.8.
Task 3: 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.
netherlands_data <- ess %>%
filter(cntry == "NL") %>%
mutate(
ppltrst = ifelse(ppltrst %in% c(7, 8, 9), NA, ppltrst),
rlgblg = ifelse(rlgblg %in% c(7, 8), NA, rlgblg),
religion_binary = ifelse(rlgblg == 1, "Yes", "No")
)
table(netherlands_data$ppltrst)
##
## 0 1 2 3 4 5 6 10 77 88
## 379 261 587 1170 1258 2815 2784 209 8 15
table(netherlands_data$religion_binary)
##
## No Yes
## 11289 7008
model3 <- lm(ppltrst ~ religion_binary, data = netherlands_data)
coefficients <- coef(model3)
print(coefficients)
## (Intercept) religion_binaryYes
## 4.6562937 0.1453614
summary(model3)
##
## Call:
## lm(formula = ppltrst ~ religion_binary, data = netherlands_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.802 -1.656 0.198 1.198 83.344
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.6563 0.0563 82.704 <2e-16 ***
## religion_binaryYes 0.1454 0.0895 1.624 0.104
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.258 on 9464 degrees of freedom
## (8863 observations deleted due to missingness)
## Multiple R-squared: 0.0002787, Adjusted R-squared: 0.000173
## F-statistic: 2.638 on 1 and 9464 DF, p-value: 0.1044
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(model3, use_coefs = TRUE)
\[ \operatorname{\widehat{ppltrst}} = 4.66 + 0.15(\operatorname{religion\_binary}_{\operatorname{Yes}}) \] Response: Intercept: The value 4.66 represents the estimated average of trust that people have in others for those that do not follow a religion and the intercept term also represents the trust individuals have in others when the other variables equate to zero. The coefficent value of 0.15 represent that unit increase of trust that people have and not having identified as believing in a religion. Furthermore, the 0.15 value suggests that not being associated with a religion is associated with a unit increase of 0.15 for the predicted levels of trust.
Summary: Overall, this equation suggests that the trust in others for people that are not associated with their religion is 4.66, and the individuals that are associated with their relgion, on average, are assumed to have a trust level that is 0.15 units higher than those who are not.