rm(list = ls())      # Clear all files from your environment
         gc()            # Clear unused memory
##          used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 526511 28.2    1169607 62.5         NA   669420 35.8
## Vcells 970630  7.5    8388608 64.0      16384  1851883 14.2
         cat("\f")       # Clear the console
 graphics.off()      # Clear all graphs

Packages

# install.packages('bea.R')
library(bea.R)
## Loading required package: data.table
## Note: As of February 2018, beaGet() requires 'TableName' for NIPA and NIUnderlyingDetail data instead of 'TableID.' See https://github.us-bea/bea.R for details.
library(ggplot2)
# Install necessary packages if not installed
#install.packages("dplyr")
#install.packages("tidyverse")

# Load required libraries
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
## 
##     between, first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ✔ readr     2.1.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::between()     masks data.table::between()
## ✖ dplyr::filter()      masks stats::filter()
## ✖ dplyr::first()       masks data.table::first()
## ✖ lubridate::hour()    masks data.table::hour()
## ✖ lubridate::isoweek() masks data.table::isoweek()
## ✖ dplyr::lag()         masks stats::lag()
## ✖ dplyr::last()        masks data.table::last()
## ✖ lubridate::mday()    masks data.table::mday()
## ✖ lubridate::minute()  masks data.table::minute()
## ✖ lubridate::month()   masks data.table::month()
## ✖ lubridate::quarter() masks data.table::quarter()
## ✖ lubridate::second()  masks data.table::second()
## ✖ purrr::transpose()   masks data.table::transpose()
## ✖ lubridate::wday()    masks data.table::wday()
## ✖ lubridate::week()    masks data.table::week()
## ✖ lubridate::yday()    masks data.table::yday()
## ✖ lubridate::year()    masks data.table::year()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

2) API Key and Data

# Setup API key
beaKey  <- '1D5B09D7-4704-4A3F-9C7F-A06D7F5679AA'
# Create a specs list to use inside beaGet
beadata <- list(
  "UserID" = beaKey,
  "Method" = "GetData",
  "datasetname" = "NIPA",
  "TableName" = "T10705",
  "Frequency" = "A",
  "Year" = "X")

beadata2 <- beaGet(beadata, asWide = FALSE)
## No encoding supplied: defaulting to UTF-8.

4) Filter Data to GDP

# Filter the data for GDP
beadataGDP <- beadata2 %>%
  filter(LineDescription == "Gross domestic product (GDP)")

3) Graph data

plot(beadataGDP$TimePeriod, 
     beadataGDP$DataValue, 
     type = "l", 
     col = "darkgreen",
     xlab = "Year", 
     ylab = "GDP", 
     main = "GDP Over Time")

5) Covid-19 and 2008 Financial Crisis impact

During recessionary periods such as the 2008 financial crises and the pandemic, we saw a slow down in GDP levels. While both were recessions they had different root causes. In the 2008 financial crises the high interest rates drove financial pressure on unstable underwritten loans, causing for high defaults on mortgages. it also drove for financial institutions to fail which reduced consumer and business access to capital. Overall you saw less money circulating, decreasing the amount for spending on goods and services.

During the Pandemic on the other hand, businesses were all shut down, creating for supply shortages of goods and services. High lay offs and lockdowns also drove demand to decrease. What is interesting is the bounce back to GDP after the inital dip, mainly driven by the high stimulus spread to consumers.

6) Graph data with GGPlot

# Convert 'TimePeriod' column from characters to numeric
beadataGDP$TimePeriod <- as.numeric(beadataGDP$TimePeriod)

# verify class
class(beadataGDP$TimePeriod)
## [1] "numeric"
#Graph
ggplot(beadataGDP, aes(x = TimePeriod, y = DataValue)) +
  geom_line(color = "blue") +  # Line plot with blue color
  labs(title = "GDP Over Time", x = "Time Period", y = "GDP") +
  theme_minimal()