Building Indicators

Harold Nelson

2026-11-10

Intro

Our goal is to create an anlysis file focusing on the values of economic indicators for every presidential term beginning with Ronald Reagan in the first quarter of 1981 and ending with the second term of Donald Trump through the third quarter of 2026.

Setup

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.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(fredr)

Load Presidents.csv

Use the Import Dataset utility and select the readr option. Run str() to verify that the first column is a date and the second column is a string.

Solution

library(readr)
Presidents = read_csv("Presidents.csv")
## Rows: 183 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Date, President
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Add unrate to the Dataframe

Use the code I supplied in class with your own API key to create the vector unrate.

Make the following changes:

  1. Make the observations start January 1981.

  2. Make the observations end September 2026.

Run str on unratef to verify your work.

Solution

start = as.Date("1981-01-01")
end = as.Date("2026-09-01")
unratef = fredr_series_observations(
  series_id = "UNRATE",
  observation_start = start,
  observation_end = end,
  frequency = "q")

unrate = unratef$value

Add unrate to Indicators

Use cbind to create the dataframe indicators from the vector unrate and the Presidents dataframe.

Run str() to verify.

# Your code goes here.
indicators = cbind(Presidents,unrate)
str(indicators)
## 'data.frame':    183 obs. of  3 variables:
##  $ Date     : chr  "1/1/81" "4/1/81" "7/1/81" "10/1/81" ...
##  $ President: chr  "Reagan" "Reagan" "Reagan" "Reagan" ...
##  $ unrate   : num  7.4 7.4 7.4 8.2 8.8 9.4 9.9 10.7 10.4 10.1 ...

Add the Rest

Repeat the process above to add the following variables

Run str() to verify your work.

Real GDP

realgdp the series_id = “A191RL1Q225SBEA”.

Solution

realgdpf = fredr_series_observations(
  series_id = "A191RL1Q225SBEA",
  observation_start = start,
  observation_end = end,
  frequency = "q")

realgdp = realgdpf$value

indicators = cbind(indicators,realgdp)

New Jobs

newjobs series_id = “PAYEMS”

Solution

newjobsf = fredr_series_observations(
  series_id = "PAYEMS",
  observation_start = start,
  observation_end = end, 
  frequency = "q",
  units = "chg")

newjobs = newjobsf$value

indicators = cbind(indicators,newjobs)
str(indicators)
## 'data.frame':    183 obs. of  5 variables:
##  $ Date     : chr  "1/1/81" "4/1/81" "7/1/81" "10/1/81" ...
##  $ President: chr  "Reagan" "Reagan" "Reagan" "Reagan" ...
##  $ unrate   : num  7.4 7.4 7.4 8.2 8.8 9.4 9.9 10.7 10.4 10.1 ...
##  $ realgdp  : num  8.1 -2.9 4.9 -4.3 -6.1 1.8 -1.5 0.2 5.4 9.4 ...
##  $ newjobs  : num  389 240 191 -399 -628 ...

Inflation

infcpi series_id = “CPIAUCSL”

Solution

infcpif = fredr_series_observations(
  series_id = "CPIAUCSL",
  observation_start = start,
  observation_end = end, 
  frequency = "q",
  units = "pca")

infcpi = infcpif$value

indicators = cbind(indicators,infcpi)

Labor Force Pariticpation 25-54

lfpr25_54f series_id = “LNS11300060”

Solution

lfpr25_54f = fredr_series_observations(
  series_id = "LNS11300060",
  observation_start = start,
  observation_end = end, 
  frequency = "q",
  units = "chg")

lfpr25_54 = lfpr25_54f$value

indicators = cbind(indicators,lfpr25_54)

str(indicators)
## 'data.frame':    183 obs. of  7 variables:
##  $ Date     : chr  "1/1/81" "4/1/81" "7/1/81" "10/1/81" ...
##  $ President: chr  "Reagan" "Reagan" "Reagan" "Reagan" ...
##  $ unrate   : num  7.4 7.4 7.4 8.2 8.8 9.4 9.9 10.7 10.4 10.1 ...
##  $ realgdp  : num  8.1 -2.9 4.9 -4.3 -6.1 1.8 -1.5 0.2 5.4 9.4 ...
##  $ newjobs  : num  389 240 191 -399 -628 ...
##  $ infcpi   : num  11.53 8.6 11.61 6.66 3.6 ...
##  $ lfpr25_54: num  0.3667 0.3333 -0.3 0.3333 0.0667 ...

Save

Save the dataframe as an Rdata file.

Solution

save(indicators,file = "indicators.Rdata")