packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
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
## Loading required package: viridisLite
##
##
## Attaching package: 'kableExtra'
##
##
## The following object is masked from 'package:dplyr':
##
## group_rows
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[3]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[4]]
## [1] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [16] "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[6]]
## [1] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[7]]
## [1] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [6] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
##
## [[8]]
## [1] "kableExtra" "knitr" "viridis" "viridisLite" "fst"
## [6] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [11] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [16] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [21] "utils" "datasets" "methods" "base"
##
## [[9]]
## [1] "rmarkdown" "kableExtra" "knitr" "viridis" "viridisLite"
## [6] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
##
## [[10]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[11]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[12]]
## [1] "questionr" "ggridges" "rmarkdown" "kableExtra" "knitr"
## [6] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [11] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [16] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
setwd("/Users/tenzinyingsel/Documents/SOC202/Homework_5_Project")
ess <- read_fst("All-ESS-Data.fst")
table (ess$essround)
##
## 1 2 3 4 5 6 7 8 9 10
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
ess$year[ess$essround == i] <- replacements[i]
}
norway_data <- ess %>%
filter(cntry == "NO") %>%
mutate(
domicil = ifelse(domicil %in% c(7, 8, 9), NA, domicil),
aesfdrk = ifelse(aesfdrk %in% c(7, 8, 9), NA, aesfdrk),
impsafe = ifelse(impsafe %in% c(7, 8, 9), NA, impsafe),
)
datasummary_skim(norway_data %>% select(domicil,aesfdrk,impsafe))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| domicil | 6 | 0 | 3.1 | 1.3 | 1.0 | 3.0 | 5.0 | |
| aesfdrk | 5 | 0 | 1.6 | 0.7 | 1.0 | 1.0 | 4.0 | |
| impsafe | 7 | 5 | 3.0 | 1.3 | 1.0 | 3.0 | 6.0 |
By observing this table, we see that the mean of variable “domicil” is 3.1 meaning that the majority of respondents from Norway live in a town or small city. This then correlates to the fact that their feelings of safety when walking alone in a local area (variable “aesfdrk”) is between the range of “very safe” to “safe” as the mean is 1.6. However, living in secure and safe surroundings (variable “impsafe”) is not of great importance to them. This may be due to them being accustomed to a safe environment as Norway is known as a very safe country.
safety_by_year <- norway_data %>%
group_by(year) %>%
summarize(mean_safety = mean(aesfdrk, na.rm = TRUE))
safety_by_year
## # A tibble: 10 × 2
## year mean_safety
## <dbl> <dbl>
## 1 2002 1.65
## 2 2004 1.64
## 3 2006 1.60
## 4 2008 1.60
## 5 2010 1.60
## 6 2012 1.59
## 7 2014 1.56
## 8 2016 1.52
## 9 2018 1.42
## 10 2020 1.44
ggplot(safety_by_year, aes(x = year, y = mean_safety)) +
geom_line(color = "pink", size = 1) +
geom_point(color = "green", size = 3) +
labs(title = "Feeling of Safety of Walking Alone in Local Area After Dark
in Norway (2002-2020)",
x = "Survey Year",
y = "Feeling Very Safe - Feeling Very Unsafe (0- 4)") +
ylim(0, 5) +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
We can see that throuhgout the years, feelings of safety when walking alone in a local area in Norway has remained constant, with little to no significant change. After the year of 2015, there is a slight increase in feelings of being safe, resulting in a minor dip/trough in the graph.
ess_selected <- ess %>%
filter(cntry %in% c("NO", "RU", "IS")) %>%
mutate(aesfdrk = ifelse(aesfdrk %in% c(7, 8, 9), NA, aesfdrk))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -aesfdrk, FUN=median), y = aesfdrk, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot Comparison for Feelings of Safety of Walking Alone in Local Area After Dark (Norway, Russia, Iceland)",
x = "Country",
y = "Scale (0-4)")
task3plot
## Warning: Removed 315 rows containing non-finite values (`stat_boxplot()`).
From this figure we notice that Russia’s maximum value is much higher on the scale (at 4 - very unsafe) than Iceland and Norway’s maximum values (3 - unsafe). Additionally, Norway and Iceland’s medians are both 1 (very safe), while Russia’s median line lies on 2 (safe).From this we can conclude that respondents from Russia tend to feel less safe walking alone in a local area after dark than respondents from Norway and Iceland.
norway_data <- norway_data %>%
mutate(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_))
table(norway_data$geo)
##
## Peri-Urban Rural Urban
## 2903 10700 2441
Crosstab
aesfdrkgeo <- datasummary_crosstab(aesfdrk ~ geo, data = norway_data)
aesfdrkgeo
| aesfdrk | Peri-Urban | Rural | Urban | All | |
|---|---|---|---|---|---|
| 1 | N | 1466 | 6106 | 1024 | 8601 |
| % row | 17.0 | 71.0 | 11.9 | 100.0 | |
| 2 | N | 1183 | 3698 | 1099 | 5988 |
| % row | 19.8 | 61.8 | 18.4 | 100.0 | |
| 3 | N | 218 | 728 | 263 | 1211 |
| % row | 18.0 | 60.1 | 21.7 | 100.0 | |
| 4 | N | 34 | 158 | 54 | 249 |
| % row | 13.7 | 63.5 | 21.7 | 100.0 | |
| All | N | 2903 | 10700 | 2441 | 16065 |
| % row | 18.1 | 66.6 | 15.2 | 100.0 |
norway_data <- norway_data %>%
mutate(born_in_country = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_))
table(norway_data$born_in_country)
##
## No Yes
## 1506 14534
aesfdrkbrncntr <- datasummary_crosstab(aesfdrk ~ born_in_country, data = norway_data)
aesfdrkbrncntr
| aesfdrk | No | Yes | All | |
|---|---|---|---|---|
| 1 | N | 712 | 7880 | 8601 |
| % row | 8.3 | 91.6 | 100.0 | |
| 2 | N | 642 | 5335 | 5988 |
| % row | 10.7 | 89.1 | 100.0 | |
| 3 | N | 126 | 1083 | 1211 |
| % row | 10.4 | 89.4 | 100.0 | |
| 4 | N | 24 | 224 | 249 |
| % row | 9.6 | 90.0 | 100.0 | |
| All | N | 1506 | 14534 | 16065 |
| % row | 9.4 | 90.5 | 100.0 |
Column Percentages
table(norway_data$aesfdrk, norway_data$born_in_country) %>%
cprop()
##
## No Yes All
## 1 47.3 54.3 53.6
## 2 42.7 36.7 37.3
## 3 8.4 7.5 7.5
## 4 1.6 1.5 1.5
## Total 100.0 100.0 100.0
The crosstab results show that the number of respondents living in rural areas is significantly higher than those living in urban and peri-urban areas/residences. These results are not of great significance as they only show how many respondents from the certain places of residence took the survey, but not their feelings regarding safety when walking alone in the dark. After looking at the column percentage table, we can see that the respondents who live in the country have better feelings regarding safety when walking alone, whereas the respondents who answered “no” and do not live in the country clearly will be less safe as they are not from there. Overall, there seems to be an average feeling of being very safe to safe within Norway.
df <- norway_data %>%
filter(!is.na(geo) & !is.na(aesfdrk))
df <- df %>%
mutate(aesfdrk = case_when(
aesfdrk == 1 ~ "Very safe",
aesfdrk == 2 ~ "Safe",
aesfdrk == 3 ~ "Unsafe",
aesfdrk == 4 ~ "Very unsafe",
TRUE ~ as.character(aesfdrk)
))
table(df$aesfdrk)
##
## Safe Unsafe Very safe Very unsafe
## 5980 1209 8596 246
table(df$aesfdrk, df$geo) %>%
cprop() %>%
as.data.frame() %>%
filter(Var1 != "Total",
Var2 != "All") %>%
ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
geom_col(position = "dodge") +
labs(title="Feeling of Safety of Walking Alone in Local Area After
Dark in Norway",
y = "Conditional Percentage",
x = "Feeling of Safety",
fill = "Urban vs. Rural vs. Peri-Urban")
From this graph, we can see that the majority of respondents from rural and peri-urban locations tend to feel very safe walking alone in a local area after dark while the majority of respondents from urban locations do not feel as safe. We can conclude that individuals from Norway living in rural areas feel the safest.