Week 8 Data Dive

In this week’s data dive, we will devise a null hypotheis and conduct an ANOVA test with the most valuable parts of the data. For my data, this would be the age of each congressman and the generation they are from.

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)
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))
head(w7cdf)
## # A tibble: 6 × 17
## # Groups:   bioguide_id [2]
##   congress start_date chamber state_abbrev party_code bioname        bioguide_id
##      <int> <chr>      <chr>   <chr>             <int> <chr>          <chr>      
## 1       82 1951-01-03 House   ND                  200 AANDAHL, Fred… A000001    
## 2       80 1947-01-03 House   VA                  100 ABBITT, Watki… A000002    
## 3       81 1949-01-03 House   VA                  100 ABBITT, Watki… A000002    
## 4       82 1951-01-03 House   VA                  100 ABBITT, Watki… A000002    
## 5       83 1953-01-03 House   VA                  100 ABBITT, Watki… A000002    
## 6       84 1955-01-03 House   VA                  100 ABBITT, Watki… A000002    
## # ℹ 10 more variables: birthday <chr>, cmltv_cong <int>, cmltv_chamber <int>,
## #   age_days <int>, age_years <dbl>, generation <chr>,
## #   years_in_congress_at_start <dbl>, total_terms_in_congress <int>,
## #   total_years_in_congress <dbl>, before_2000 <dbl>

Null Hypothesis for Anova Testing

The null hypothesis for anova testing that I have devised is that the average age of each congressman across each generation is equal.

w7cdf$generation <- factor(w7cdf$generation)

anovaresults <- aov(age_years ~ generation , data = w7cdf)
summary(anovaresults)
##                Df  Sum Sq Mean Sq F value Pr(>F)    
## generation      9  348209   38690   372.3 <2e-16 ***
## Residuals   29110 3025065     104                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

As seen above, there is enough evidence to conclude that there is a group that significantly varies from another. I will create a table below to pick out which groups that may be.

genage <- cdf |>
  group_by(generation) |>
  summarise(avg_age = mean(age_years))
genage
## # A tibble: 10 × 2
##    generation  avg_age
##    <chr>         <dbl>
##  1 Boomers        53.1
##  2 Gen X          44.8
##  3 Gen Z          26.0
##  4 Gilded         82.6
##  5 Greatest       52.1
##  6 Lost           53.4
##  7 Millennial     36.2
##  8 Missionary     57.9
##  9 Progressive    68.1
## 10 Silent         54.0

As we can see above, the average age of Gen Z varies significantly from the Gilded generation, so it is safe to assume to reject the null hypothesis.

Linear Regression Plot

Here I will plot the average age of congress over each congress.

conage <- cdf |>
  group_by(congress) |>
  summarise(avg_age = mean(age_years))
conage
## # A tibble: 53 × 2
##    congress avg_age
##       <int>   <dbl>
##  1       66    51.7
##  2       67    52.6
##  3       68    52.6
##  4       69    53.2
##  5       70    54.0
##  6       71    54.6
##  7       72    54.4
##  8       73    53.5
##  9       74    52.6
## 10       75    52.5
## # ℹ 43 more rows
conage |>  
  ggplot(aes(congress, avg_age)) +
  geom_point() +
  geom_smooth(method ='lm', se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

With the plot above, we can get a sense of a general linear trend between average age and each congress, where age is increasing.

m <- lm(age_years ~ congress, data = cdf)
summary(m)
## 
## Call:
## lm(formula = age_years ~ congress, data = cdf)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -30.430  -7.842  -0.242   7.437  43.070 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 45.96449    0.38094  120.66   <2e-16 ***
## congress     0.08454    0.00409   20.67   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.69 on 29118 degrees of freedom
## Multiple R-squared:  0.01446,    Adjusted R-squared:  0.01443 
## F-statistic: 427.4 on 1 and 29118 DF,  p-value: < 2.2e-16

The coefficients for the linear regression model represent that the X-intercept is at 45.96 and the slope of the line is .085. In context of the data, the intercept represents what the model thinks would have been the average age of the first ever congress. The slope represents the average change in the average age over each congress since the first congress. The p-values shows that both of these values are significant, so its safe to assume these are reasonable estimates.

We will now run another regression model that includes the generation variable as well as the congress variable.

cdf1 <- cdf |>
  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
    ))

congen <- cdf1 |>
  group_by(congress, generation_id) |>
  summarise(avg_age = mean(age_years))
## `summarise()` has grouped output by 'congress'. You can override using the
## `.groups` argument.
congen |>  
  ggplot(aes(congress, avg_age)) +
  geom_point() +
  geom_smooth(method ='lm', se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

m1 <- lm(age_years ~ congress + generation_id, data = cdf1)
summary(m1)
## 
## Call:
## lm(formula = age_years ~ congress + generation_id, data = cdf1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.982  -4.118  -0.019   4.072  19.894 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    -3.108710   0.265664   -11.7   <2e-16 ***
## congress        1.479152   0.005441   271.9   <2e-16 ***
## generation_id -15.394701   0.055240  -278.7   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.58 on 29117 degrees of freedom
## Multiple R-squared:  0.7313, Adjusted R-squared:  0.7313 
## F-statistic: 3.962e+04 on 2 and 29117 DF,  p-value: < 2.2e-16

When looking at the above graph and the results from the model, it does not make sense to add the generation variable to the linear regression model. This does not help our understanding of the data, and it is tough to add more variables to the model from my data.