rm(list = ls())
#Importing libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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(bea.R)
## Loading required package: data.table
## 
## Attaching package: 'data.table'
## 
## The following objects are masked from 'package:lubridate':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## 
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## 
## The following object is masked from 'package:purrr':
## 
##     transpose
## 
## 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.
  1. Setup
# Setup API key
beaKey <- "485487AB-425F-4275-BB33-5E522472A238"
#Assigning the key
beaSets(beaKey = beaKey)
## No encoding supplied: defaulting to UTF-8.
## $Dataset
##                DatasetName                                 DatasetDescription
## 1                     NIPA                               Standard NIPA tables
## 2       NIUnderlyingDetail               Standard NI underlying detail tables
## 3                      MNE                          Multinational Enterprises
## 4              FixedAssets                       Standard Fixed Assets tables
## 5                      ITA                International Transactions Accounts
## 6                      IIP                  International Investment Position
## 7              InputOutput                                  Input-Output Data
## 8            IntlServTrade                       International Services Trade
## 9              IntlServSTA International Services Supplied Through Affiliates
## 10           GDPbyIndustry                                    GDP by Industry
## 11                Regional                                 Regional data sets
## 12 UnderlyingGDPbyIndustry                         Underlying GDP by Industry
## 13      APIDatasetMetaData                  Metadata about other API datasets
## 
## attr(,"params")
##   ParameterName                       ParameterValue
## 1        USERID 485487AB-425F-4275-BB33-5E522472A238
## 2        METHOD                       GETDATASETLIST
## 3  RESULTFORMAT                                 JSON
beadf <- list(
  "UserID" = beaKey,
  "Method" = "GetData",
  "datasetname" = "NIPA",
  "TableName" = "T10705",
  "Frequency" = "A",
  "Year" = "X")
beartd <- beaGet(beadf, asWide = FALSE)
## No encoding supplied: defaulting to UTF-8.
?beaGet
#Studying dataset
unique(beartd$LineDescription)
##  [1] "Gross domestic product (GDP)"                                                  
##  [2] "Plus: Income receipts from the rest of the world"                              
##  [3] "Less: Income payments to the rest of the world"                                
##  [4] "Equals: Gross national product"                                                
##  [5] "Less: Consumption of fixed capital"                                            
##  [6] "Private"                                                                       
##  [7] "Domestic business"                                                             
##  [8] "Capital consumption allowances"                                                
##  [9] "Less: Capital consumption adjustment"                                          
## [10] "Households and institutions"                                                   
## [11] "Government"                                                                    
## [12] "General government"                                                            
## [13] "Government enterprises"                                                        
## [14] "Equals: Net national product"                                                  
## [15] "Less: Statistical discrepancy"                                                 
## [16] "Equals: National income"                                                       
## [17] "Corporate profits with inventory valuation and capital consumption adjustments"
## [18] "Taxes on production and imports less subsidies"                                
## [19] "Contributions for government social insurance, domestic"                       
## [20] "Net interest and miscellaneous payments on assets"                             
## [21] "Business current transfer payments (net)"                                      
## [22] "Current surplus of government enterprises"                                     
## [23] "Plus: Personal income receipts on assets"                                      
## [24] "Plus: Personal current transfer receipts"                                      
## [25] "Equals: Personal income"                                                       
## [26] "Gross domestic income (GDI)"                                                   
## [27] "Average of GDP and GDI"                                                        
## [28] "Gross national income"                                                         
## [29] "Gross national factor income"                                                  
## [30] "Net domestic product"                                                          
## [31] "Net domestic income"                                                           
## [32] "Net national factor income"                                                    
## [33] "Net domestic purchases"                                                        
## [34] "Statistical discrepancy as a percentage of GDP"
# Filter the data for GDP
beadf_GDP <- beartd %>%
  filter(LineDescription == "Gross domestic product (GDP)")
range(beadf_GDP$TimePeriod)
## [1] "1929" "2022"
# Set up the plotting area
plot(beadf_GDP$TimePeriod, 
     beadf_GDP$DataValue, 
     type = "l",                   # Type: "l" for lines
     col = "darkgreen",            # Line color
     xlab = "Year", 
     ylab = "GDP", 
     main = "GDP Over Time")

# Add grid lines for better readability
grid()

# Customize the legend if needed
legend("topright", legend = "GDP", col = "darkgreen", lty = 1)

Yes.

COVID-19 Pandemic (2020):

- Lockdowns and disruptions led to a decline in economic activity.

- Global supply chains were disrupted, affecting production and distribution.

- Decreased consumer spending due to job losses and reduced confidence.

2008 Financial Crisis:

- Triggered by the collapse of financial institutions and the subprime mortgage market.

- Financial market instability led to a freeze in credit markets.

- Housing market collapse resulted in reduced consumer spending and a global recession.

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

# verify class
class(beadf_GDP$TimePeriod)
## [1] "numeric"
#Checking for missing values
any(is.na(beadf_GDP$TimePeriod))
## [1] FALSE
#Omit missing values
beadf_GDP <- na.omit(beadf_GDP)
library(ggplot2)

#Graph
ggplot(beadf_GDP, aes(x = TimePeriod, y = DataValue)) +
  geom_line(color = "red") + 
  labs(title = "GDP Track", x = "Year", y = "GDP") +
  theme_minimal()