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 looking at a binary column data and building a regression model for the variable. What I want to investigate is the average age of congressmen and if it has changed before/after the year 2000. The binary column is called before_2000, and it contains a 0 if the congress was before the year 2000 and a 1 if it was after.
The model built with the variables can be seen below.
model <- glm(before_2000 ~ age_years, data = w7cdf,
family = binomial(link = 'logit'))
model$coefficients
## (Intercept) age_years
## -3.55001721 0.04202516
The coefficient for age_years means that, on the graph, for each year increase, the likelihood that the age is present past the year 2000 increases by .04. For the context of what we want to know, it shows that the average age of congress after 2000 is slightly older than before 2000.
Now I will put the coefficients into a logistic regression graph below to view the model.
sigmoid <- \(x) 1 / (1 + exp(-(-3.55 + 0.04 * x)))
w7cdf |>
ggplot(mapping = aes(x = age_years, y = before_2000)) +
geom_jitter(width = 0, height = 0.1, shape = 'O', size = 3) +
geom_function(fun = sigmoid, color = 'blue', linewidth = 1) +
labs(title = "Modeling a Binary Response with Sigmoid") +
scale_y_continuous(breaks = c(0, 1)) +
theme_minimal()
Now I will do a transformation for the age variable, where I will
instead take the average age of each congress as the explanatory
variable instead of the individual age of each congressman.
loggraph <- w7cdf |>
group_by(congress) |>
mutate(avg_age = mean(age_years))
loggraph1 <- loggraph |>
ungroup(congress)
head(loggraph1)
## # A tibble: 6 × 18
## 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
## # ℹ 11 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>, avg_age <dbl>
model1 <- glm(before_2000 ~ avg_age, data = loggraph1,
family = binomial(link = 'logit'))
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
model1$coefficients
## (Intercept) avg_age
## -18962.6064 346.8909
After trying to do the transformation, it does not make sense and it is not needed to create a logistic regression with the variables presented.