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))
In this week’s data dive, I will be building a linear model using age, congress, party and chamber as variables.
I will test each variable in the model to see which one is the best. Before doing that, I will be converting the party and chamber variables to binary columns. In the IsRepublican column, a 0 represents a democrat and a 1 represents a republican. In the IsSenate column, a 0 represents a member of the House and a 1 represents a member of the Senate. The changes to the dataframe is represented below.
w11 <- w7cdf |>
filter(party_code == 100 | party_code == 200) |>
mutate(IsRepublican = (party_code-100)/100,
IsSenate = case_when(
endsWith(chamber, 'House') ~ 0,
endsWith(chamber, 'Senate') ~ 1
))
sample(w11)
## # A tibble: 28,985 × 19
## # Groups: bioguide_id [5,233]
## state_abbrev bioguide_id age_days generation party_code start_date bioname
## <chr> <chr> <int> <chr> <int> <chr> <chr>
## 1 ND A000001 19626 Lost 200 1951-01-03 AANDAHL, …
## 2 VA A000002 14106 Greatest 100 1947-01-03 ABBITT, W…
## 3 VA A000002 14837 Greatest 100 1949-01-03 ABBITT, W…
## 4 VA A000002 15567 Greatest 100 1951-01-03 ABBITT, W…
## 5 VA A000002 16298 Greatest 100 1953-01-03 ABBITT, W…
## 6 VA A000002 17028 Greatest 100 1955-01-03 ABBITT, W…
## 7 VA A000002 17759 Greatest 100 1957-01-03 ABBITT, W…
## 8 VA A000002 18489 Greatest 100 1959-01-03 ABBITT, W…
## 9 VA A000002 19220 Greatest 100 1961-01-03 ABBITT, W…
## 10 VA A000002 19950 Greatest 100 1963-01-03 ABBITT, W…
## # ℹ 28,975 more rows
## # ℹ 12 more variables: birthday <chr>, chamber <chr>, age_years <dbl>,
## # cmltv_cong <int>, IsSenate <dbl>, cmltv_chamber <int>,
## # years_in_congress_at_start <dbl>, congress <int>, before_2000 <dbl>,
## # IsRepublican <dbl>, total_terms_in_congress <int>,
## # total_years_in_congress <dbl>
model1 <- lm(age_years ~ congress, data = w11)
summary(model1)
##
## Call:
## lm(formula = age_years ~ congress, data = w11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -30.397 -7.833 -0.246 7.435 43.095
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 46.02734 0.38206 120.47 <2e-16 ***
## congress 0.08373 0.00410 20.42 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.68 on 28983 degrees of freedom
## Multiple R-squared: 0.01418, Adjusted R-squared: 0.01415
## F-statistic: 417 on 1 and 28983 DF, p-value: < 2.2e-16
Looking at model 1, the coefficient for congress is positive, which shows that there is a slight general positive correlation between congress and age, showing that the average age has been increasing over the years. The R Squared value shows that the model has a low goodness-to-fit.
model2 <- lm(age_years ~ congress + IsRepublican, data = w11)
summary(model2)
##
## Call:
## lm(formula = age_years ~ congress + IsRepublican, data = w11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -30.418 -7.837 -0.248 7.436 43.073
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 46.011552 0.385254 119.432 <2e-16 ***
## congress 0.083700 0.004101 20.410 <2e-16 ***
## IsRepublican 0.040202 0.125999 0.319 0.75
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.68 on 28982 degrees of freedom
## Multiple R-squared: 0.01419, Adjusted R-squared: 0.01412
## F-statistic: 208.6 on 2 and 28982 DF, p-value: < 2.2e-16
Looking at model 2, the p value for the democrat or republican variable is insignificant. The R-squared of this model is also lower than model 1.
model3 <- lm(age_years ~ congress + IsSenate, data = w11)
summary(model3)
##
## Call:
## lm(formula = age_years ~ congress + IsSenate, data = w11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.448 -7.715 -0.228 7.313 38.960
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 45.049158 0.376620 119.61 <2e-16 ***
## congress 0.083975 0.004029 20.84 <2e-16 ***
## IsSenate 5.085840 0.157791 32.23 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.49 on 28982 degrees of freedom
## Multiple R-squared: 0.0483, Adjusted R-squared: 0.04823
## F-statistic: 735.4 on 2 and 28982 DF, p-value: < 2.2e-16
Looking at model 3, the coefficient of the Senate or House variable is a larger positive value, showing that there is a larger average increase in age if the congressman is in the senate compared to the house of representatives. The R-squared value is higher here than in models 1 and 2, but still has a poor goodness-of-fit.
model4 <- lm(age_years ~ congress + IsRepublican + IsSenate, data = w11)
summary(model4)
##
## Call:
## lm(formula = age_years ~ congress + IsRepublican + IsSenate,
## data = w11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.463 -7.715 -0.230 7.304 38.946
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 45.03815 0.37974 118.603 <2e-16 ***
## congress 0.08396 0.00403 20.835 <2e-16 ***
## IsRepublican 0.02810 0.12380 0.227 0.82
## IsSenate 5.08573 0.15779 32.230 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.49 on 28981 degrees of freedom
## Multiple R-squared: 0.0483, Adjusted R-squared: 0.0482
## F-statistic: 490.3 on 3 and 28981 DF, p-value: < 2.2e-16
For the final model, we combined all of the variables from before. The Republican and Democrat variable is still insignificant and the coefficient is near zero, the other values are very similar or close to the values shown in model 3. It looks as Model 3 is the best out of the bunch, but is still not a great regression model. For our data, it looks that we cannot create a significant and very representative linear regression model to accurately describe and predict the data.