In this week’s data dive, we will be looking at correlation and covariance between variables
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ 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
library(ggplot2)
library(dplyr)
library(boot)
cdata <- read.csv("/Users/cadgo/Downloads/data_aging_congress.csv")
cdf <- data.frame(cdata)
Here below, I will create a couple of columns to show the age difference of each member in congress between each terms, the total amount of years they served in congress, and a column to represent the cumulative years in congress they have been in.
The first graph will show the trend between congress and the amount of years served in congress.
cdf1 <- cdf |>
group_by(bioguide_id) |>
mutate(year_lag = lag(age_years, n = 1, order_by = congress),
age_difference = age_years - year_lag,
years_in_congress_at_start = cmltv_cong*2 - 2,
total_terms_in_congress = max(cmltv_cong),
total_years_in_congress = total_terms_in_congress*2)
cdf2 <- cdf1 |>
group_by(congress) |>
summarise(mean_years_served = mean(years_in_congress_at_start))
cdf2 |>
ggplot(mapping = aes(x = congress, y=mean_years_served)) +
geom_point(color = "gray") +
geom_smooth(se = FALSE) +
theme_classic() +
labs(title = "Average Years Served in Each Congress",
x = "Congress Number",
y = "Years Served")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
From the graph, we can see a general positive correlation between congress and years served. The data points are spread out, but there are no outliers within the data.
We will now calculate the correlation coefficient.
round(cor(cdf2$congress, cdf2$mean_years_served), 2)
## [1] 0.83
This value shows that there is a strong positive correlation between our variables. This makes sense as our graph also shows a strong positive correlation.
In the next graph, I will show the average total years served by members of congress in each generation. For the graph, each generation is represented by their ID_Number where the oldest generation, the Gilded Generation, is number 1 and the youngest, Generation Z, is 10.
cdf1 <- cdf1 |>
mutate(generation_id = case_when(
endsWith(generation, "Gilded") ~ 1,
endsWith(generation, "Progressive") ~ 2,
endsWith(generation, "Missionary") ~ 3,
endsWith(generation, "Lost") ~ 4,
endsWith(generation, "Greatest") ~ 5,
endsWith(generation, "Silent") ~ 6,
endsWith(generation, "Boomers") ~ 7,
endsWith(generation, "X") ~ 8,
endsWith(generation, "Millennial") ~ 9,
endsWith(generation, "Z") ~ 10
))
cdf3 <- cdf1
cdf3 <- cdf3 |>
distinct(bioguide_id, .keep_all = TRUE)
cdf3 <- cdf3 |>
group_by(generation_id) |>
summarise(mean_years_served = mean(total_years_in_congress))
cdf3 |>
ggplot(mapping = aes(x = generation_id, y=mean_years_served)) +
geom_point(color = "gray") +
geom_smooth(se = FALSE) +
theme_classic() +
labs(title = "Average Years Served by Generation",
x = "Generation",
y = "Years Served")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
The graph shows a general negative correlation between generations and years served. This is to be expected as the younger generations have had less time to serve in congress, but interesting to see that Greatest and Silent Generations had more tenured politicians than the Progressive and Missionary Generations.
Now we will calculate the correlation coefficient for the variables.
round(cor(cdf3$generation_id, cdf3$mean_years_served), 2)
## [1] -0.84
This coefficient makes sense compared to the trend we see in the graph.
In the next graph, I will show the correlation between age and average terms served in congress.
cdf4 <- cdf1
cdf4$age_years <- as.integer(cdf4$age_years)
cdf4 <- cdf4 |>
group_by(age_years) |>
summarise(mean_years_served = mean(total_years_in_congress))
cdf4 |>
ggplot(mapping = aes(x = age_years, y=mean_years_served)) +
geom_point(color = "gray") +
geom_smooth(se = FALSE) +
theme_classic() +
labs(title = "Average Years Served by Age",
x = "Age in Years",
y = "Years Served")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
This graph shows a general positive correlation between age and years served in congress. This makes sense as one gets older, they are then able to serve more terms.
Now we will calculate the correlation coefficient.
round(cor(cdf4$age_years, cdf4$mean_years_served), 2)
## [1] 0.9
This correlation shows a very strong correlation between the variables, which makes sense regarding our graph and conclusions from above.
cdf5 <- cdf1
cdf5 <- cdf5 |>
distinct(bioguide_id, .keep_all = TRUE)
smean <- mean(cdf5$total_years_in_congress)
print(smean)
## [1] 11.77135
confidence_interval <- lm(total_years_in_congress ~ 1, cdf5)
confint(confidence_interval, level=0.95)
## 2.5 % 97.5 %
## (Intercept) 11.51763 12.02508
Looking at the confidence interval for the total years a congressman has spent in congress, it looks that the average years spent in congress is at 11.77 with not much deviation around the mean. This means that we can be 95% sure that the true mean of the average time spent in congress by politicians will be within the interval 11.51 and 12.02. Since this data goes until 2023, we can correctly assume that when additional data is added in future congress sessions, that the true mean will still lie within this interval.