setwd("~/Downloads/R /Homework_8_Alessia_DeFoa") # Replace /path/to/your/folder with the actual path you just copied.
#run
getwd()
## [1] "/Users/alessiamacbook/Downloads/R /Homework_8_Alessia_DeFoa"
# 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.4 ✔ 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")
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep), ## Example of a trust variable
)
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 %in% c(7, 8, 9) ~ NA_character_,
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
TRUE ~ as.character(wrkprty)
)
)
table(belgium_data$wrkprty)
##
## No Yes
## 14871 727
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
summary(model1) #added later by me
##
## Call:
## lm(formula = trstep ~ wrkprty, 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 ***
## wrkprtyYes 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
## (1332 observations deleted due to missingness)
## Multiple R-squared: 0.00158, Adjusted R-squared: 0.001516
## F-statistic: 24.68 on 1 and 15596 DF, p-value: 6.832e-07
# model 1 refers to the name where you will store the output
# then it goes lm(outcome ~ explanatory, data = dataset_name)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.9405554 0.4308338
tidy(model1)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 4.94 0.0187 264. 0
## 2 wrkprtyYes 0.431 0.0867 4.97 0.000000683
tidy(model1) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 4.941 | 0.019 | 263.892 | 0 |
| wrkprtyYes | 0.431 | 0.087 | 4.968 | 0 |
summary(model1)
##
## Call:
## lm(formula = trstep ~ wrkprty, 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 ***
## wrkprtyYes 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
## (1332 observations deleted due to missingness)
## Multiple R-squared: 0.00158, Adjusted R-squared: 0.001516
## F-statistic: 24.68 on 1 and 15596 DF, p-value: 6.832e-07
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{trstep}} = 4.94 + 0.43(\operatorname{wrkprty}_{\operatorname{Yes}}) \]
y(hat) = intercept + B(hat)1 + B(hat)2 * x1 + epsilon trstep(hat) = 4.94 - 0.43(No) + error term
Interpretation: The intercept is 4.94 and there is a 0.43 value associated with if peolpe work with a political party in the past 12 months. the regression equation indicates that there is a signfigant trust in politicians which corresponds with a slightly high value for wrkprty.
TASK 2
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 %>% filter(!is.na(stfdem))
bulgaria_data <- bulgaria_data %>%
mutate(
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_)
)
table(bulgaria_data$brncntr)
##
## 1 2 7
## 12239 97 2
model2 <- lm(stfdem ~ brncntr, data = bulgaria_data)
tidy(model2)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 3.06 0.175 17.5 1.01e-67
## 2 brncntr -0.134 0.172 -0.778 4.36e- 1
coefficients <- coef(model2)
print(coefficients)
## (Intercept) brncntr
## 3.0571188 -0.1339063
tidy(model2)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 3.06 0.175 17.5 1.01e-67
## 2 brncntr -0.134 0.172 -0.778 4.36e- 1
tidy(model2)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 3.06 0.175 17.5 1.01e-67
## 2 brncntr -0.134 0.172 -0.778 4.36e- 1
tidy(model2) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 3.057 | 0.175 | 17.496 | 0.000 |
| brncntr | -0.134 | 0.172 | -0.778 | 0.436 |
summary(model2)
##
## Call:
## lm(formula = stfdem ~ brncntr, data = bulgaria_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.9232 -1.9232 0.0768 2.0768 7.2107
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.0571 0.1747 17.496 <2e-16 ***
## brncntr -0.1339 0.1721 -0.778 0.436
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.23 on 12336 degrees of freedom
## Multiple R-squared: 4.91e-05, Adjusted R-squared: -3.196e-05
## F-statistic: 0.6057 on 1 and 12336 DF, p-value: 0.4364
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(model2, use_coefs = TRUE)
\[ \operatorname{\widehat{stfdem}} = 3.06 - 0.13(\operatorname{brncntr}) \]
y(hat) = intercept + B(hat)1 + B(hat)2 * x1 + epsilon stfdem(hat) = 3.06 - 0.13(No) + error term
Interpretation: With the intercept being 3.05 displaying the satisfaction with democracy and whether one was born in bulgaria being 0.13, (3.06- 0.13= 2.93) meaning that the average satisfaction for people not born in Bulgaria is 2.93. 2.93 indicates that there is low satisfaction with democracy for individuals not born in Bulgaria.
TASK 3
italy_data <- ess %>%
filter(cntry == "IT") %>%
mutate(stfeco = ifelse(stfdem %in% c(77, 88, 99), NA, stfeco), ## Example of a trust variable
)
unique(italy_data$stfeco)
## [1] 3 4 8 5 0 6 7 9 1 NA 2 88 10 99 77
italy_data <- italy_data %>% filter(!is.na(stfeco))
italy_data <- italy_data %>%
mutate(
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_)
)
table(italy_data$brncntr)
##
## 1 2 7 8 9
## 9061 701 5 5 3
model3 <- lm(stfeco ~ brncntr, data = italy_data)
tidy(model3)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 4.68 0.356 13.2 3.34e-39
## 2 brncntr 0.564 0.313 1.80 7.15e- 2
coefficients <- coef(model3)
print(coefficients)
## (Intercept) brncntr
## 4.6846196 0.5636388
tidy(model3)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 4.68 0.356 13.2 3.34e-39
## 2 brncntr 0.564 0.313 1.80 7.15e- 2
tidy(model3) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 4.685 | 0.356 | 13.157 | 0.000 |
| brncntr | 0.564 | 0.313 | 1.802 | 0.072 |
summary(model3)
##
## Call:
## lm(formula = stfeco ~ brncntr, data = italy_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.757 -3.248 -1.248 -0.248 93.752
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.6846 0.3561 13.157 <2e-16 ***
## brncntr 0.5636 0.3127 1.802 0.0715 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11.07 on 9773 degrees of freedom
## Multiple R-squared: 0.0003323, Adjusted R-squared: 0.00023
## F-statistic: 3.249 on 1 and 9773 DF, p-value: 0.07151
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{stfeco}} = 4.68 + 0.56(\operatorname{brncntr}) \]
y(hat) = intercept + B(hat)1 + B(hat)2 * x1 + epsilon stfeco(hat) = 4.68 - 0.56(No) + error term
Interpretation: for this task, I chose to look at the satisfatcion of democracy for individuals who were not born in Italy (stfeco & brncntr). This resulted in there being 4.68 displaying the signfigant satisfaction in economy. Further, it can be seen that there is a signfigant number of the avergae people who were not born in italy. Thus yeilding the average individual not born in italy having a (4.68-0.56=4.12).