# Install tidyverse for dplyr and ggplot
# install.packages("tidyverse")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.4 ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
# Install bea R for API Access to BEA
# install.package("bea.R")
library(bea.R)
## Loading required package: data.table
##
## Attaching package: 'data.table'
##
## The following objects are masked from 'package:dplyr':
##
## between, first, last
##
## The following object is masked from 'package:purrr':
##
## transpose
##
## Creating a generic function for 'toJSON' from package 'jsonlite' in package 'googleVis'
## 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.
# Setup API key
APIKey <- "3611C9EE-9804-4C82-B776-C4854127AB83"
# Search for GDP
beaParams(beaKey = APIKey, "NIPA")
## No encoding supplied: defaulting to UTF-8.
## $Parameter
## ParameterName ParameterDataType
## 1 Frequency string
## 2 ShowMillions string
## 3 TableID integer
## 4 TableName string
## 5 Year integer
## ParameterDescription
## 1 A - Annual, Q-Quarterly, M-Monthly
## 2 A flag indicating that million-dollar data should be returned.
## 3 The standard NIPA table identifier
## 4 The new NIPA table identifier
## 5 List of year(s) of data to retrieve (X for All)
## ParameterIsRequiredFlag ParameterDefaultValue MultipleAcceptedFlag AllValue
## 1 1 1
## 2 0 N 0
## 3 0 <NA> 0
## 4 0 <NA> 0
## 5 1 1 X
##
## attr(,"params")
## ParameterName ParameterValue
## 1 USERID 3611C9EE-9804-4C82-B776-C4854127AB83
## 2 METHOD GETPARAMETERLIST
## 3 DATASETNAME NIPA
## 4 RESULTFORMAT JSON
head(beaSearch("gdp", beaKey = APIKey))
## 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("gdp", beaKey = APIKey): Regional metadata is missing from
## C:/Users/Ryan McNulty/AppData/Local/R/win-library/4.2/beaR/data and may be
## locked for updating on the BEA API; searching national metadata only.
## SeriesCode RowNumber LineDescription LineNumber
## 1: A191RO 10 Gross domestic product (GDP) 1
## 2: PE000009 330 Average of GDP and GDI 32
## 3: A191RL 10 Gross domestic product (GDP) 1
## 4: PB000009 130 Average of GDP and GDI 12
## 5: PA000009 200 Average of GDP and GDI, current dollars 19
## 6: A191RC 10 Gross domestic product (GDP) 1
## ParentLineNumber Tier Path TableID DatasetName
## 1: 0 1 T10111 NIPA
## 2: 0 32 T10111 NIPA
## 3: 0 1 T10701 NIPA
## 4: 0 12 T10701 NIPA
## 5: 0 19 T10701 NIPA
## 6: 0 1 T10705 NIPA
## TableName
## 1: Table 1.1.11. Real Gross Domestic Product: Percent Change From Quarter One Year Ago
## 2: Table 1.1.11. Real Gross Domestic Product: Percent Change From Quarter One Year Ago
## 3: Table 1.7.1. Percent Change From Preceding Period in Real Gross Domestic Product, Real Gross National Product, and Real Net National Product
## 4: Table 1.7.1. Percent Change From Preceding Period in Real Gross Domestic Product, Real Gross National Product, and Real Net National Product
## 5: Table 1.7.1. Percent Change From Preceding Period in Real Gross Domestic Product, Real Gross National Product, and Real Net National Product
## 6: Table 1.7.5. Relation of Gross Domestic Product, Gross National Product, Net National Product, National Income, and Personal Income
## 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
## 6: Feb 28 2019 8:30AM Mar 28 2019 8:30AM 2019-03-06T10:13:29.923 National
## apiCall
## 1: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10111', ...))
## 2: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10111', ...))
## 3: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10701', ...))
## 4: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10701', ...))
## 5: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10701', ...))
## 6: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10705', ...))
# Create a specs list to use inside beaGet
beaSpecsNIPA <- list(
"UserID" = APIKey, # Set up API key
"Method" = "GetData", # Method
"datasetname" = "NIPA", # Specify dataset
"TableName" = "T10705", # Specify table within the dataset
"Frequency" = "A", # Frequency of data sets
"Year" = "X" # Years to include X is all
)
# Get the Data (in long form) and Check
gdp <- beaGet(beaSpecsNIPA, asWide = FALSE)
## No encoding supplied: defaulting to UTF-8.
knitr::kable(head(gdp))
| TableName | SeriesCode | LineNumber | LineDescription | TimePeriod | METRIC_NAME | CL_UNIT | UNIT_MULT | DataValue | NoteRef |
|---|---|---|---|---|---|---|---|---|---|
| T10705 | A191RC | 1 | Gross domestic product (GDP) | 1929 | Current Dollars | Level | 6 | 104556 | T10705 |
| T10705 | A191RC | 1 | Gross domestic product (GDP) | 1930 | Current Dollars | Level | 6 | 92160 | T10705 |
| T10705 | A191RC | 1 | Gross domestic product (GDP) | 1931 | Current Dollars | Level | 6 | 77391 | T10705 |
| T10705 | A191RC | 1 | Gross domestic product (GDP) | 1932 | Current Dollars | Level | 6 | 59522 | T10705 |
| T10705 | A191RC | 1 | Gross domestic product (GDP) | 1933 | Current Dollars | Level | 6 | 57154 | T10705 |
| T10705 | A191RC | 1 | Gross domestic product (GDP) | 1934 | Current Dollars | Level | 6 | 66800 | T10705 |
# Filter and reshape the data
gdp <- gdp %>% filter(LineNumber == 1)
gdp <- gdp %>% select(TimePeriod,DataValue)
gdp <- gdp %>% rename(Year = TimePeriod,
GDP_Dollar = DataValue)
gdp <- gdp %>% filter(Year > 1999)
# Plot the GDP over Year
ggplot(data = gdp,
aes(x = Year, y = GDP_Dollar/1000000)) +
geom_col(fill='skyblue') +
ggtitle("United States Annual GDP (Current Dollars)") +
theme_minimal() +
theme(plot.title=element_text( hjust=0.5, vjust=0.5, face='bold')) +
labs(x = "Years", y = "GDP ($ in Millions)")
Do you find that GDP dropped during Covid - 19 and 2008 Financial Crisis? Why could that be happening?
For the 2008 crisis the GDP dropped in the following year 2009, and for Covid-19, the GDP dropped in 2020. This is likely due to each crisis affecting the United States ability to work (either from workers losing their jobs or from having to quarantine).