1. Set up

# key
beakey <- "073D1606-20C5-4EDB-A502-FEB84C7E2461"

# search codes
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 073D1606-20C5-4EDB-A502-FEB84C7E2461
## 2        METHOD                       GETDATASETLIST
## 3  RESULTFORMAT                                 JSON
beaSearch('gross domestic', beakey)
## Creating first-time local copy of metadata for all datasets - only done once.
## Datasets will be updated only if timestamps indicate metadata obsolete in future searches,
## and only obsolete metadata sets will be updated (it's faster this way).
## 
## No encoding supplied: defaulting to UTF-8.
## Warning in beaSearch("gross domestic", beakey): Regional metadata is missing
## from
## /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/beaR/data
## and may be locked for updating on the BEA API; searching national metadata
## only.
##       SeriesCode RowNumber                LineDescription LineNumber
##    1:     A191RL        10         Gross domestic product          1
##    2:     A009RL       100                     Structures         10
##    3:     Y033RL       110                      Equipment         11
##    4:     Y001RL       120 Intellectual property products         12
##    5:     A011RL       130                    Residential         13
##   ---                                                               
## 2982:     B935RY        70       Private fixed investment          5
## 2983:     SWSNXY        80                    Net exports          6
## 2984:     SWSHXY        90                        Exports          7
## 2985:     SWSHMY       100                        Imports          8
## 2986:     SWSHGY       110                     Government          9
##       ParentLineNumber Tier Path TableID        DatasetName
##    1:                     0    1  T10101               NIPA
##    2:                     0   10  T10101               NIPA
##    3:                     0   11  T10101               NIPA
##    4:                     0   12  T10101               NIPA
##    5:                     0   13  T10101               NIPA
##   ---                                                      
## 2982:                     0    5  U90500 NIUnderlyingDetail
## 2983:                     0    6  U90500 NIUnderlyingDetail
## 2984:                     0    7  U90500 NIUnderlyingDetail
## 2985:                     0    8  U90500 NIUnderlyingDetail
## 2986:                     0    9  U90500 NIUnderlyingDetail
##                                                                                                                                              TableName
##    1:                                                                 Table 1.1.1. Percent Change From Preceding Period in Real Gross Domestic Product
##    2:                                                                 Table 1.1.1. Percent Change From Preceding Period in Real Gross Domestic Product
##    3:                                                                 Table 1.1.1. Percent Change From Preceding Period in Real Gross Domestic Product
##    4:                                                                 Table 1.1.1. Percent Change From Preceding Period in Real Gross Domestic Product
##    5:                                                                 Table 1.1.1. Percent Change From Preceding Period in Real Gross Domestic Product
##   ---                                                                                                                                                 
## 2982: Table 9.5U. Contributions to Percent Change in Real Gross Domestic Product From Final Sales of Computers, Software, and Communications Equipment
## 2983: Table 9.5U. Contributions to Percent Change in Real Gross Domestic Product From Final Sales of Computers, Software, and Communications Equipment
## 2984: Table 9.5U. Contributions to Percent Change in Real Gross Domestic Product From Final Sales of Computers, Software, and Communications Equipment
## 2985: Table 9.5U. Contributions to Percent Change in Real Gross Domestic Product From Final Sales of Computers, Software, and Communications Equipment
## 2986: Table 9.5U. Contributions to Percent Change in Real Gross Domestic Product From Final Sales of Computers, Software, and Communications Equipment
##               ReleaseDate     NextReleaseDate         MetaDataUpdated  Account
##    1: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
##    2: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
##    3: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
##    4: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
##    5: Feb 28 2019  8:30AM Mar 28 2019  8:30AM 2019-03-06T10:13:29.923 National
##   ---                                                                         
## 2982: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:32.840 National
## 2983: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:32.840 National
## 2984: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:32.840 National
## 2985: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:32.840 National
## 2986: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:32.840 National
##                                                                                                                              apiCall
##    1:               beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10101', ...))
##    2:               beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10101', ...))
##    3:               beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10101', ...))
##    4:               beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10101', ...))
##    5:               beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10101', ...))
##   ---                                                                                                                               
## 2982: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'U90500', ...))
## 2983: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'U90500', ...))
## 2984: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'U90500', ...))
## 2985: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'U90500', ...))
## 2986: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'U90500', ...))

2. Load GDP Data

# set up
beaSpecs <- list(
  "UserID" = beakey, 
  "Method" = "GetData",
  "datasetname" = "NIPA", 
  "TableName" = "T10705",
  "Frequency" = "A",
  "Year" = "X")

bea <- beaGet(beaSpecs, asWide = FALSE)
## No encoding supplied: defaulting to UTF-8.
# print data options 
unique(bea$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"
# extract data 
gdp_data <- bea |>
  filter( LineDescription == "Gross domestic product (GDP)")

# print data
head(gdp_data,5)
##    TableName SeriesCode LineNumber              LineDescription TimePeriod
## 1:    T10705     A191RC          1 Gross domestic product (GDP)       1929
## 2:    T10705     A191RC          1 Gross domestic product (GDP)       1930
## 3:    T10705     A191RC          1 Gross domestic product (GDP)       1931
## 4:    T10705     A191RC          1 Gross domestic product (GDP)       1932
## 5:    T10705     A191RC          1 Gross domestic product (GDP)       1933
##        METRIC_NAME CL_UNIT UNIT_MULT DataValue NoteRef
## 1: Current Dollars   Level         6    104556  T10705
## 2: Current Dollars   Level         6     92160  T10705
## 3: Current Dollars   Level         6     77391  T10705
## 4: Current Dollars   Level         6     59522  T10705
## 5: Current Dollars   Level         6     57154  T10705

3. Graph GDP

# change variable type from "chr" -> "num"
gdp_data$TimePeriod <- as.numeric(gdp_data$TimePeriod)

# plot gdp time-series data 
ggplot(data = gdp_data, aes(x = TimePeriod, y = DataValue)) +
  geom_line(color = "aquamarine3") +
  labs(x = "Year", y = "Real GDP") +
  theme_classic()

4. Explanation of Economic downturn