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(ggthemes)
library(ggrepel)
library(effsize)
library(pwrss)
##
## Attaching package: 'pwrss'
##
## The following object is masked from 'package:stats':
##
## power.t.test
library(ggplot2)
library(xts)
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
library(tsibble)
## Warning: package 'tsibble' was built under R version 4.3.2
##
## Attaching package: 'tsibble'
##
## The following object is masked from 'package:zoo':
##
## index
##
## The following object is masked from 'package:lubridate':
##
## interval
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
cdata <- read.csv("/Users/cadgo/Downloads/data_aging_congress.csv")
cdf <- data.frame(cdata)
w7cdf <- cdf |>
group_by(bioguide_id) |>
mutate(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,
before_2000 = floor(congress/107))
In this week’s data dive, we will be ooking at time-based data. We will be analyzing the average age of congress over time.
w7cdf$start_date <- as.Date(w7cdf$start_date)
sda <- w7cdf |>
group_by(start_date) |>
summarise(avg_age = mean(age_years))
sda_ts <- as_tsibble(sda, index=start_date) |>
index_by(date = date(start_date))
sda_ts
## # A tsibble: 53 x 3 [1D]
## # Groups: @ date [53]
## start_date avg_age date
## <date> <dbl> <date>
## 1 1919-03-04 51.7 1919-03-04
## 2 1921-03-04 52.6 1921-03-04
## 3 1923-03-04 52.6 1923-03-04
## 4 1925-03-04 53.2 1925-03-04
## 5 1927-03-04 54.0 1927-03-04
## 6 1929-03-04 54.6 1929-03-04
## 7 1931-03-04 54.4 1931-03-04
## 8 1933-03-04 53.5 1933-03-04
## 9 1935-01-03 52.6 1935-01-03
## 10 1937-01-03 52.5 1937-01-03
## # ℹ 43 more rows
sda_xts <- xts(x = sda_ts$avg_age,
order.by = sda_ts$date)
sda_xts <- setNames(sda_xts, "Average_Age")
head(sda_xts)
## Average_Age
## 1919-03-04 51.73002
## 1921-03-04 52.60360
## 1923-03-04 52.56625
## 1925-03-04 53.22577
## 1927-03-04 53.95525
## 1929-03-04 54.61430
sda_xts %>%
ggplot(mapping = aes(x = Index, y = Average_Age)) +
geom_line() +
labs(title = "Average Age of Congress Over Time",
subtitle = "Grouped by age at start date") +
theme_hc()
What stands out immediately is that there is a dip in the average age at the end of the 1970’s until the early 1980’s, and has spiked significantly until current day.
Let’s run a linear regression model to detect any trend.
sda |>
ggplot(aes(start_date, avg_age)) +
geom_point() +
geom_smooth(method ='lm', se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'
As we can see above, there is a general upwards trend showing that the average age has been increasing.
Let’s see how strong the linear trend is to the data.
model1 <- lm(avg_age ~ start_date, data = sda)
summary(model1)
##
## Call:
## lm(formula = avg_age ~ start_date, data = sda)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.6433 -1.1264 0.2178 1.3366 3.0787
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.369e+01 2.700e-01 198.869 < 2e-16 ***
## start_date 1.165e-04 2.418e-05 4.818 1.34e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.964 on 51 degrees of freedom
## Multiple R-squared: 0.3128, Adjusted R-squared: 0.2993
## F-statistic: 23.22 on 1 and 51 DF, p-value: 1.341e-05
The R squared value is .2993, showing that the trend does not have a great fit, but still shows some strength among the data.
Since my data is grouped by each year, as it is the only way to accurately compare the average age of each congress, there isn’t a way to detect a season for the data.