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.
Create a new working directory by starting a new R project. Go to ‘File’, then ‘New Project’, then ‘New Directory’. Provide the following name for your directory: “Homework_5_Project”.
Go to the top again to ‘File’, then new file, then do R markdown and save it as “Homework_5_FirstName_LastName”. Go find your new directory, open it to confirm that you have your .Rproj file, and your .Rmd file.
Setting up the Environment.
# 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.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"
Copy-paste the class dataset (wherever it is located on your computer) into your current project directory.
Load class dataset.
ess <- read_fst("All-ESS-Data.fst")
Note: if you get a message like OpenMp was not detected, it is fine. As long as you see the dataset loaded in the top right window, you are golden.
Move your mouse near the Run symbol where you see a plus sign with a C. Click on it, then on R.
You will see a a grey bar appear with the following symbol at the top: ```{r}
In there copy the following and run it by clicking the green play button that is at the extreme right of the box:
print(“hell yeah this works!”)
Everything that is in that box is R code. Everything outside is text.
You can test a code chunk by clicking the run button as we just did. If it does not work, it will give you an error code.
Want to see what it looks like?
Add some text here.
Some with two hashtags (for titles)
Others just straight typing. Anything you would like.
When you are ready: Go to knit, then click knit to html.
Wait, it will take some time. If it works, it will open a new window and you can click publish and create an account to be able to post html docs online for free.
In this tutorial, we will walk you through fundamentals of exploratory data analysis (i.e., describing data) that will be part and parcel of what you will need to do yourself for Homework 5.
We will use this later. Create from corresponding essrounds.
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]
}
To understand data and prepare it for analysis, we need to initially examine individual variables.
Investigating clsprty The variable clsprty represents a respondent’s feeling of closeness to a particular political party. Let’s inspect its distribution, noting values that need to be removed.
Variable info here: https://ess-search.nsd.no/en/variable/query/clsprty/1
table(ess$clsprty)
##
## 1 2 7 8 9
## 231368 246470 2391 9334 992
Noting values 7, 8, 9 need to be treated as missing. We’ll also recode for analytic purposes: from 1 (Yes) and 2 (No) to 1 (Yes) and 0 (No).
Investigating stfeco (trust in politicians)
Info here: https://ess-search.nsd.no/variable/query/stfeco/1
Examine its distribution and values to remove.
table(ess$stfeco)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 39574 25671 45330 59142 55182 75639 57084 58459 41634 13226 8012 424 10562
## 99
## 616
The values 77, 88, and 99 will be treated as missing.
Finally, let’s check stfgov (satisfaction with democracy).
Variable info here:
https://ess-search.nsd.no/variable/query/stfgov/1
table(ess$stfgov)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 50082 30238 46909 58918 55276 80173 54097 51400 30634 8648 6253 1012 14087
## 99
## 782
Same as trstplt.
Let’s undertake a comprehensive cleaning of specified variables, as well as a recod of clsprty so that Yes corresponds to 1 and No to 0. We will also filter to France.
france_data <- ess %>% # Start with the original 'ess' dataset
filter(cntry == "FR") %>% # Filter the data to only include rows where country (cntry) is France ("FR")
mutate(
clsprty = ifelse(clsprty == 2, 0, ifelse(clsprty %in% c(7, 8, 9), NA, clsprty)), # If 'clsprty' is 2, set it to 0. If it's 7, 8, or 9, set it to NA.
stfeco = ifelse(stfdem %in% c(77, 88, 99), NA, stfeco), # For 'stfeco', set values 77, 88, and 99 to NA.
stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov), # For 'stfgov', set values 77, 88, and 99 to NA.
)
Check what you did and what was done worked as intended. A quick way to do so:
table(france_data$clsprty)
##
## 0 1
## 9194 9507
We’ve done this in the past. We will do it only for our three variables of interest and use the select function to “select out” this time the year and yrbrn variables from our summary.
datasummary_skim(france_data %>% select(clsprty, stfeco, stfgov))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| clsprty | 3 | 2 | 0.5 | 0.5 | 0.0 | 1.0 | 1.0 | |
| stfeco | 14 | 1 | 4.3 | 8.6 | 0.0 | 4.0 | 88.0 | |
| stfgov | 12 | 1 | 3.7 | 2.3 | 0.0 | 4.0 | 10.0 |
We will do so two ways. One for the mean across survey years, and the other to compare the distribution across survey years as way to get a sense of how it shifted over time.
You will do something similar for your Task 2.
First, let’s calculate the average by year.
satisfaction_by_year <- france_data %>%
group_by(year) %>%
summarize(mean_satisfaction = mean(stfeco, na.rm = TRUE))
satisfaction_by_year
## # A tibble: 10 × 2
## year mean_satisfaction
## <dbl> <dbl>
## 1 2002 4.41
## 2 2004 4.75
## 3 2006 4.26
## 4 2008 3.31
## 5 2010 3.72
## 6 2012 3.57
## 7 2014 3.51
## 8 2016 4.10
## 9 2018 4.91
## 10 2020 6.90
Before visualizing, what do you note?
ggplot(satisfaction_by_year, aes(x = year, y = mean_satisfaction)) +
geom_line(color = "blue", size = 1) + # Line to show the trend
geom_point(color = "red", size = 3) + # Points to highlight each year's value
labs(title = "Satisfaction with the current economic state in France (2002-2020)",
x = "Survey Year",
y = "Satisfaction (0-10)") +
ylim(0, 10) + # Setting the y-axis limits from 0 to 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.
Look at the appendix for a deeper exploration of this graph and more on visualization!
Now let’s visualize the distribution of stfdem across survey years.
france_data %>%
ggplot(aes(x = stfgov, y = as.factor(year), fill = as.factor(year))) +
geom_density_ridges() + # geometry used for visualizing
labs(title = "Satisfaction with national government Across Survey Years For France",
x = "Satisfaction (0-10)", y = "Survey Year",
fill = "Survey Year") +
theme_minimal()
## Picking joint bandwidth of 0.435
## Warning: Removed 280 rows containing non-finite values
## (`stat_density_ridges()`).
What do you note?
You will do something similar for Task 3. There are many options, depending on your variable of interest.
If you were to choose a variable from 0 to 10, here’s an option:
ess_selected <- ess %>%
filter(cntry %in% c("FR", "IT", "GB")) %>%
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 satisfaction with current economy (France, Italy, Great Britain)",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 1082 rows containing non-finite values (`stat_boxplot()`).
If you were to compare a binary outcome, like clsprty, with added proportion calculation:
# Compute proportions for each country
country_proportions <- ess %>%
filter(!clsprty %in% c(7, 8, 9)) %>% # Exclude 7, 8, 9 responses
group_by(cntry) %>% # Group by country
summarize(
total_respondents = n(),
yes_count = sum(clsprty == 1),
proportion_yes = yes_count / total_respondents
) %>%
filter(cntry %in% c("FR", "IT", "GB")) %>% # Filter for the desired countries
arrange(-proportion_yes) # Sort by proportion in descending order
# Plot
p4 <- ggplot(country_proportions, aes(x = reorder(cntry, -proportion_yes), y = proportion_yes, fill = cntry)) +
# Add bar layer
geom_bar(stat = "identity") +
# Define labels and title for the plot
labs(
title = "Proportion of People Feeling Close to a Party (France, Lithuania, Sweden)",
x = "Country",
y = "Proportion",
fill = "Country"
) +
# Use viridis color palette
scale_fill_viridis_d() + # using viridis package to color
# Apply a minimal theme
theme_minimal()
# Display the plot
p4
You will refer to the following for Task 4.
First, let’s set up five socio-demographic variables. There are many more, which you can explore and recode as you need. But here are five for reference:
We’ll create a binary variable indicating whether the respondent has attained a bachelor’s degree or above vs. not.
info here: https://ess-search.nsd.no/variable/query/edulvlb/1
# Re-coding education
france_data <- france_data %>%
mutate(
# Set 'other' to NA for rounds 1-4
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
# Set 'other' to NA for rounds 5 and later
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
# Create educ_level column
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
# View the table of educ_level
table(france_data$educ_level)
##
## BA No BA
## 4235 14803
We’ll recode the domicil variable into a more understandable categorization.
info here: https://ess-search.nsd.no/variable/query/domicil/1
france_data <- france_data %>%
mutate(geo = recode(as.character(domicil),
'1' = "Urban",
'2' = "Peri-Urban", # or set to Urban | Regardless decision needs to be justified
'3' = "Rural",
'4' = "Rural",
'5' = "Rural",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_))
# check
table(france_data$geo)
##
## Peri-Urban Rural Urban
## 2361 13087 3584
# compare
table(france_data$domicil)
##
## 1 2 3 4 5 7 8
## 3584 2361 6257 5668 1162 3 3
Variable details https://ess-search.nsd.no/variable/ffdd4171-10bf-4f0e-b943-b1a472ac0c55
Paid work in last 7 days.
Note: in a future tutorial, we will create a class variable for occupation codes. There are a few reasons for this, but it involves pretty extensive recoding and data manipulation + cleaning. For now, let’s just do paid work status.
france_data <- france_data %>%
mutate(pdwrk_recode = case_when(
pdwrk == 1 ~ 'yes',
pdwrk == 0 ~ 'no',
))
# check the recoded variable
table(france_data$pdwrk_recode)
##
## no yes
## 9466 9572
# compare with the original variable
table(france_data$pdwrk)
##
## 0 1
## 9466 9572
Create a binary variable based on whether someone belongs to a religion or not
https://ess-search.nsd.no/variable/query/rlgblg/1
france_data <- france_data %>%
mutate(religion = case_when(
rlgblg == 2 ~ "No",
rlgblg == 1 ~ "Yes",
rlgblg %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(rlgblg)
))
# check
table(france_data$religion)
##
## No Yes
## 9434 9509
# compare to check (original variable name)
table(france_data$rlgblg)
##
## 1 2 7 8
## 9509 9434 53 42
Let’s create a binary variable to distinguish those born in the country from those who weren’t.
# renaming to born_in_country and recoding to Y/N
france_data <- france_data %>%
mutate(born_in_country = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_))
# always check what you did
table(france_data$born_in_country)
##
## No Yes
## 1916 17119
table(france_data$brncntr)
##
## 1 2 7 8
## 17119 1916 1 2
Especially useful to explore the association between two categorical variables (but you can also do a 0-10 type variable x categorical). Here we are choosing an outcome of interest and one socio-demographic variable.
stfecoedu <- datasummary_crosstab(stfeco ~ educ_level, data = france_data)
stfecogeo <- datasummary_crosstab(stfeco ~ geo, data = france_data)
stfecopdwrk <- datasummary_crosstab(stfeco ~ pdwrk_recode, data = france_data)
stfecoedu
| stfeco | BA | No BA | All | |
|---|---|---|---|---|
| 0 | N | 242 | 1759 | 2001 |
| % row | 12.1 | 87.9 | 100.0 | |
| 1 | N | 214 | 934 | 1148 |
| % row | 18.6 | 81.4 | 100.0 | |
| 2 | N | 568 | 2180 | 2748 |
| % row | 20.7 | 79.3 | 100.0 | |
| 3 | N | 827 | 2655 | 3482 |
| % row | 23.8 | 76.2 | 100.0 | |
| 4 | N | 751 | 2233 | 2984 |
| % row | 25.2 | 74.8 | 100.0 | |
| 5 | N | 681 | 2546 | 3227 |
| % row | 21.1 | 78.9 | 100.0 | |
| 6 | N | 503 | 1115 | 1618 |
| % row | 31.1 | 68.9 | 100.0 | |
| 7 | N | 301 | 577 | 878 |
| % row | 34.3 | 65.7 | 100.0 | |
| 8 | N | 80 | 290 | 370 |
| % row | 21.6 | 78.4 | 100.0 | |
| 9 | N | 14 | 43 | 57 |
| % row | 24.6 | 75.4 | 100.0 | |
| 10 | N | 10 | 51 | 61 |
| % row | 16.4 | 83.6 | 100.0 | |
| 77 | N | 3 | 7 | 10 |
| % row | 30.0 | 70.0 | 100.0 | |
| 88 | N | 25 | 152 | 177 |
| % row | 14.1 | 85.9 | 100.0 | |
| All | N | 4235 | 14803 | 19038 |
| % row | 22.2 | 77.8 | 100.0 |
stfecogeo
| stfeco | Peri-Urban | Rural | Urban | All | |
|---|---|---|---|---|---|
| 0 | N | 205 | 1444 | 351 | 2001 |
| % row | 10.2 | 72.2 | 17.5 | 100.0 | |
| 1 | N | 130 | 806 | 210 | 1148 |
| % row | 11.3 | 70.2 | 18.3 | 100.0 | |
| 2 | N | 307 | 1970 | 470 | 2748 |
| % row | 11.2 | 71.7 | 17.1 | 100.0 | |
| 3 | N | 424 | 2406 | 652 | 3482 |
| % row | 12.2 | 69.1 | 18.7 | 100.0 | |
| 4 | N | 383 | 2004 | 597 | 2984 |
| % row | 12.8 | 67.2 | 20.0 | 100.0 | |
| 5 | N | 395 | 2230 | 601 | 3227 |
| % row | 12.2 | 69.1 | 18.6 | 100.0 | |
| 6 | N | 247 | 1032 | 338 | 1618 |
| % row | 15.3 | 63.8 | 20.9 | 100.0 | |
| 7 | N | 132 | 570 | 176 | 878 |
| % row | 15.0 | 64.9 | 20.0 | 100.0 | |
| 8 | N | 55 | 239 | 76 | 370 |
| % row | 14.9 | 64.6 | 20.5 | 100.0 | |
| 9 | N | 12 | 33 | 12 | 57 |
| % row | 21.1 | 57.9 | 21.1 | 100.0 | |
| 10 | N | 10 | 32 | 19 | 61 |
| % row | 16.4 | 52.5 | 31.1 | 100.0 | |
| 77 | N | 3 | 6 | 1 | 10 |
| % row | 30.0 | 60.0 | 10.0 | 100.0 | |
| 88 | N | 29 | 117 | 31 | 177 |
| % row | 16.4 | 66.1 | 17.5 | 100.0 | |
| All | N | 2361 | 13087 | 3584 | 19038 |
| % row | 12.4 | 68.7 | 18.8 | 100.0 |
stfecopdwrk
| stfeco | no | yes | All | |
|---|---|---|---|---|
| 0 | N | 1030 | 971 | 2001 |
| % row | 51.5 | 48.5 | 100.0 | |
| 1 | N | 572 | 576 | 1148 |
| % row | 49.8 | 50.2 | 100.0 | |
| 2 | N | 1356 | 1392 | 2748 |
| % row | 49.3 | 50.7 | 100.0 | |
| 3 | N | 1625 | 1857 | 3482 |
| % row | 46.7 | 53.3 | 100.0 | |
| 4 | N | 1349 | 1635 | 2984 |
| % row | 45.2 | 54.8 | 100.0 | |
| 5 | N | 1689 | 1538 | 3227 |
| % row | 52.3 | 47.7 | 100.0 | |
| 6 | N | 789 | 829 | 1618 |
| % row | 48.8 | 51.2 | 100.0 | |
| 7 | N | 420 | 458 | 878 |
| % row | 47.8 | 52.2 | 100.0 | |
| 8 | N | 224 | 146 | 370 |
| % row | 60.5 | 39.5 | 100.0 | |
| 9 | N | 29 | 28 | 57 |
| % row | 50.9 | 49.1 | 100.0 | |
| 10 | N | 28 | 33 | 61 |
| % row | 45.9 | 54.1 | 100.0 | |
| 77 | N | 8 | 2 | 10 |
| % row | 80.0 | 20.0 | 100.0 | |
| 88 | N | 131 | 46 | 177 |
| % row | 74.0 | 26.0 | 100.0 | |
| All | N | 9466 | 9572 | 19038 |
| % row | 49.7 | 50.3 | 100.0 |
Representing conditional percentages using cprop()
Column percentages of a two-way frequency table (value for conditional probabilities would just be divided by 100)
table(france_data$stfeco, france_data$educ_level) %>%
cprop()
##
## BA No BA All
## 0 5.7 12.1 10.7
## 1 5.1 6.4 6.1
## 2 13.5 15.0 14.6
## 3 19.6 18.3 18.6
## 4 17.8 15.4 15.9
## 5 16.1 17.5 17.2
## 6 11.9 7.7 8.6
## 7 7.1 4.0 4.7
## 8 1.9 2.0 2.0
## 9 0.3 0.3 0.3
## 10 0.2 0.4 0.3
## 77 0.1 0.0 0.1
## 88 0.6 1.0 0.9
## Total 100.0 100.0 100.0
Now let’s visualize. First relying on (conditional) column percentages.
# double check clean
df <- france_data %>%
filter(!is.na(educ_level) & !is.na(stfeco))
df <- df %>%
mutate(stfeco = case_when(
stfeco == 1 ~ "Yes",
stfeco == 0 ~ "No",
TRUE ~ as.character(stfeco) # or "Other", or NA_character_, depending on your needs
))
table(df$stfeco)
##
## 10 2 3 4 5 6 7 77 8 88 9 No Yes
## 61 2748 3482 2984 3227 1618 878 10 370 177 57 2001 1148
# visualize
table(df$stfeco, df$educ_level) %>%
cprop() %>%
as.data.frame() %>%
filter(Var1 != "Total",
Var2 != "All") %>%
ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
geom_col(position = "dodge") +
labs(title="Satisfaction with current economic state in France",
y = "Conditional Percentage",
x = "Satisfaction",
fill = "At least BA vs. Not")
Creating visual for variable with 0-10 scale
# double check clean
france_clean <- france_data %>%
filter(!is.na(geo) & !is.na(stfgov))
# calculate conditional probabilities
france_probs <- france_clean %>%
count(stfgov, geo) %>%
group_by(geo) %>%
mutate(prob = n / sum(n))
# plot
ggplot(france_probs, aes(x = as.factor(stfgov), y = prob, color = geo)) +
geom_point() +
geom_line(aes(group = geo)) +
labs(title = "Satisfaction with national government",
subtitle = "by Place of Residence",
x = "Satisfaction",
y = "Probability") +
theme_minimal()
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).
My Observations: There is a much higher average amount of individuals who are satisfied with the national government and who are satisfied with the present state of the economy in their country, then there are people who feel closer to a particular party compared to other parties.
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).
My Observations: Since 2015, there has been a positive increase in the amount of French citizens that feel satisfied with their present state of the economy in France. Furthermore, it has been roughly 10 years since there has been an increase of satisfaction in this particular category.
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).
My Observations: In this visual, there are two outliers. The first belonging to France, and the second belonging to Italy, both being at the same point in the scale. Furthermore, Great Britain has the highest median, while France has the lowest.
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).
My Observations: When looking at the data based on those who received a bachelors degree or above vs. not, those who said they have not received a bachelors degree were the ones who felt the most that they were satisfied with the state of economy in France. When looking at the data based on those paid work status, there was a majority higher amount of individuals in the yes category who felt that they were satisfied with the state of the economy in France.
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).
My Observations: The results of those who feel satisfied with the current state of the economy in France based on whether they have received a Bachelors degree or higher vs. not, reveals that those who have not recieved a Bachelors degree tend to feel more satisfied with the current state of the economy in France, compared to those who have recieved a Bachelors degree or higher.