# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
# 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
## 载入需要的程辑包:viridisLite
##
##
## 载入程辑包:'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"
install.packages("fst")
## Warning: 正在使用'fst'这个程序包,因此不会被安装
library(fst)
ess <- read_fst("All-ESS-Data.fst")
Important: Must post link to your markdown on the discussion board (file and “knit” html), along with post detailing your general takeaways from exploring variables of interest, comparing to other countries, and looking into socio-demographics. You must also attach your R markdown file.
In your post, please link to the ESS website for the three variables you considered, as well as highlight the country you wish to focus on and for what main reason. Finally, please attach (or provide a screenshot) the visual that you produced while doing the tasks that is most interesting to you and explain why (just one visual not all).
You cannot use the same three variables we considered in the tutorial. You can use one of the three, but not all three.
Do a data summary table of three variables of interest. Discuss briefly what you note (i.e., add text in your markdown after the Task 1 code).
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]
}
france_data <- ess %>%
filter(cntry == "FR") %>%
mutate(
clsprty = ifelse(clsprty == 2, 0, ifelse(clsprty %in% c(7, 8, 9), NA, clsprty)),
stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco),
euftf = ifelse(euftf %in% c(77, 88, 99), NA, euftf),
)
table(france_data$clsprty)
##
## 0 1
## 9194 9507
table(france_data$stfeco)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 2031 1157 2775 3511 3004 3272 1629 888 374 57 69
table(france_data$euftf)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1047 505 1181 1399 1386 3713 1421 1589 1626 532 808
datasummary_skim(france_data %>% select(clsprty, stfeco, euftf))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| clsprty | 3 | 2 | 0.5 | 0.5 | 0.0 | 1.0 | 1.0 | |
| stfeco | 12 | 1 | 3.5 | 2.1 | 0.0 | 3.0 | 10.0 | |
| euftf | 12 | 20 | 5.0 | 2.6 | 0.0 | 5.0 | 10.0 |
##I noticed that clsprty has the same median and max due to its a binary variable, stfeco is slightly left skewed, and euftf is the most normal distributed variable of all.
Choose one of the three variables you just summarized in the table. This will be your current main outcome of interest.
Produce a visual that showcases the mean (average) for your outcome of interest by survey year (can be, e.g., point + line plot or ridge plot, depending on your variable). Discuss briefly what you note (i.e., add text in your markdown after the Task 2 code).
satisfy_eco_by_year <- france_data %>%
group_by(year) %>%
summarize(mean_satisfy_eco = mean(stfeco, na.rm = TRUE))
satisfy_eco_by_year
## # A tibble: 10 × 2
## year mean_satisfy_eco
## <dbl> <dbl>
## 1 2002 3.50
## 2 2004 3.76
## 3 2006 3.7
## 4 2008 2.99
## 5 2010 3.30
## 6 2012 3.13
## 7 2014 2.94
## 8 2016 3.49
## 9 2018 3.68
## 10 2020 4.61
ggplot(satisfy_eco_by_year, aes(x = year, y = mean_satisfy_eco)) +
geom_line(color = "blue", size = 1) +
geom_point(color = "red", size = 3) +
labs(title = "Satisfaction of Economy in France(2002-2020)",
x = "Survey Year",
y = "Satisfaction (0-10)") +
ylim(0, 10) +
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.
##I noticed that French people’s satisfaction on the nation economy
status are very steady between 3-5. ## Task 3
Provide a comparison visual of your outcome of interest with two other countries. You can choose the geom() you prefer. Discuss briefly what you note (i.e., add text in your markdown after the Task 3 code).
ess_selected <- ess %>%
filter(cntry %in% c("FR", "GB", "FI")) %>%
mutate(stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -stfeco, FUN=median), y = stfeco, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot comparison for statifaction of Economy (France, UK, Finland)",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 978 rows containing non-finite values (`stat_boxplot()`).
##The graph shows us that Finland and UK people are more satified with their economy by have median of around 6 and 5, where French people are less satisfied with their economy by having median around 3. At the sametime France are positively skewed and UK is negatively skewed, it means in France the mojority people are pretty satisfy with their eco stuation. Where UK has the opposite.
Produce a cross-tab between your outcome of interest and a socio-demographic variable (use datasummary_crosstab). Then, calculate column percentages using cprop(), making sure to pick a second socio-demographic variable. Discuss briefly what you note (i.e., add text in your markdown after the Task 4 code).
france_data <- france_data %>%
mutate(
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
table(france_data$educ_level)
##
## BA No BA
## 4235 14803
stfecoedu <- datasummary_crosstab(stfeco ~ educ_level, data = france_data)
stfecoedu
| stfeco | BA | No BA | All | |
|---|---|---|---|---|
| 0 | N | 243 | 1788 | 2031 |
| % row | 12.0 | 88.0 | 100.0 | |
| 1 | N | 215 | 942 | 1157 |
| % row | 18.6 | 81.4 | 100.0 | |
| 2 | N | 571 | 2204 | 2775 |
| % row | 20.6 | 79.4 | 100.0 | |
| 3 | N | 828 | 2683 | 3511 |
| % row | 23.6 | 76.4 | 100.0 | |
| 4 | N | 754 | 2250 | 3004 |
| % row | 25.1 | 74.9 | 100.0 | |
| 5 | N | 682 | 2590 | 3272 |
| % row | 20.8 | 79.2 | 100.0 | |
| 6 | N | 503 | 1126 | 1629 |
| % row | 30.9 | 69.1 | 100.0 | |
| 7 | N | 301 | 587 | 888 |
| % row | 33.9 | 66.1 | 100.0 | |
| 8 | N | 81 | 293 | 374 |
| % row | 21.7 | 78.3 | 100.0 | |
| 9 | N | 14 | 43 | 57 |
| % row | 24.6 | 75.4 | 100.0 | |
| 10 | N | 11 | 58 | 69 |
| % row | 15.9 | 84.1 | 100.0 | |
| All | N | 4235 | 14803 | 19038 |
| % row | 22.2 | 77.8 | 100.0 |
france_data <- france_data %>%
mutate(born_in_country = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_))
table(france_data$born_in_country)
##
## No Yes
## 1916 17119
table(france_data$brncntr)
##
## 1 2 7 8
## 17119 1916 1 2
table(france_data$stfeco, france_data$born_in_country) %>%
cprop()
##
## No Yes All
## 0 9.1 11.0 10.8
## 1 4.5 6.3 6.2
## 2 11.4 15.2 14.8
## 3 16.7 18.9 18.7
## 4 15.2 16.1 16.0
## 5 19.0 17.3 17.4
## 6 10.6 8.5 8.7
## 7 6.9 4.5 4.7
## 8 4.0 1.8 2.0
## 9 1.0 0.2 0.3
## 10 1.6 0.2 0.4
## Total 100.0 100.0 100.0
##The degree level people are holding have little affect on people’s satisfaction on the nation economy. People who don’t have BA are significantly more satisfy than people who holds BA, but for the people has low satification the proportion results are the same. For whether people are born in the country to have a higher satification on the status of economy, we can see that the results are pretty close, but people who are not born in the country have a lightly higher satisfaction than people whoa re bornint he country.
Choose one of the two socio-demographic variables you just worked with. Visualize the conditional probability (or column percentages) of your outcome given your selected socio-dem variable. Discuss briefly what you note (i.e., add text in your markdown after the Task 5 code).
france_clean <- france_data %>%
filter(!is.na(born_in_country) & !is.na(stfeco))
france_probs <- france_clean %>%
count(stfeco, born_in_country) %>%
group_by(born_in_country) %>%
mutate(prob = n / sum(n))
ggplot(france_probs, aes(x = as.factor(stfeco), y = prob, color = born_in_country)) +
geom_point() +
geom_line(aes(group = born_in_country)) +
labs(title = "Conditional Probabilities of satisfaction of economy",
subtitle = "Born in the Country",
x = "Satisfaction on Economy",
y = "Probability") +
theme_minimal()
##Looking at the visualization we notice that whether proplr born in the
country or not they share a very similar pattern on satisfaction on
Economy. And we can see that people who are not born in the country
override people who are from the selection 5 to 10 (very satisfy).
Overall is seems like people who are oborn in the country have a lower
satisfaction rate on the nation’s economy status.