https://github.com/us-bea/bea.R
# Clear the workspace
rm(list = ls()) # Clear environment -remove all files from your workspace
gc() # Clear unused memory
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 527654 28.2 1172937 62.7 NA 669291 35.8
## Vcells 969134 7.4 8388608 64.0 32768 1840392 14.1
cat("\f") # Clear the console
# graphics.off() # Clear all graphs
library("bea.R") # for getting data
## Loading required package: data.table
## 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.
library("tidyverse") # data manipulation/wrangling
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── 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
library(ggplot2) # graphing
library(dplyr)
library(lubridate) # date manipulation
Replace with your own unique key that you got on signup.
my_beaKey <- '293963AB-94AA-4234-AF30-43EB482D85CB'
?beaSearch
beaSearch(searchTerm = 'GDP',
beaKey = my_beaKey,
asHtml = TRUE
)
## 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(searchTerm = "GDP", beaKey = my_beaKey, asHtml = TRUE):
## Regional metadata is missing from
## /Users/arvindsharma/Library/R/x86_64/4.2/library/beaR/data and may be locked
## for updating on the BEA API; searching national metadata only.
# beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T10705', ...))
?beaGet
my_beaSpecs <- list(
'UserID' = my_beaKey ,
'Method' = 'GetData',
'datasetname' = 'NIPA',
'TableName' = 'T10705',
'Frequency' = 'Q',
'Year' = 'X',
'ResultFormat' = 'json'
)
beaPayload <- beaGet(beaSpec = my_beaSpecs)
## No encoding supplied: defaulting to UTF-8.
beaLong <- beaGet(beaSpec = my_beaSpecs,
asWide = FALSE
)
## No encoding supplied: defaulting to UTF-8.
head(beaLong)
## TableName SeriesCode LineNumber LineDescription TimePeriod
## 1: T10705 A191RC 1 Gross domestic product (GDP) 1947Q1
## 2: T10705 A191RC 1 Gross domestic product (GDP) 1947Q2
## 3: T10705 A191RC 1 Gross domestic product (GDP) 1947Q3
## 4: T10705 A191RC 1 Gross domestic product (GDP) 1947Q4
## 5: T10705 A191RC 1 Gross domestic product (GDP) 1948Q1
## 6: T10705 A191RC 1 Gross domestic product (GDP) 1948Q2
## METRIC_NAME CL_UNIT UNIT_MULT DataValue NoteRef
## 1: Current Dollars Level 6 243164 T10705
## 2: Current Dollars Level 6 245968 T10705
## 3: Current Dollars Level 6 249585 T10705
## 4: Current Dollars Level 6 259745 T10705
## 5: Current Dollars Level 6 265742 T10705
## 6: Current Dollars Level 6 272567 T10705
table(beaLong$LineDescription)
##
## Average of GDP and GDI
## 308
## Business current transfer payments (net)
## 309
## Capital consumption allowances
## 309
## Contributions for government social insurance, domestic
## 309
## Corporate profits with inventory valuation and capital consumption adjustments
## 308
## Current surplus of government enterprises
## 261
## Domestic business
## 309
## Equals: Gross national product
## 308
## Equals: National income
## 308
## Equals: Net national product
## 308
## Equals: Personal income
## 309
## General government
## 309
## Government
## 309
## Government enterprises
## 309
## Gross domestic income (GDI)
## 308
## Gross domestic product (GDP)
## 309
## Gross national factor income
## 308
## Gross national income
## 308
## Households and institutions
## 309
## Less: Capital consumption adjustment
## 309
## Less: Consumption of fixed capital
## 309
## Less: Income payments to the rest of the world
## 308
## Less: Statistical discrepancy
## 308
## Net domestic income
## 308
## Net domestic product
## 309
## Net domestic purchases
## 309
## Net interest and miscellaneous payments on assets
## 309
## Net national factor income
## 308
## Plus: Income receipts from the rest of the world
## 308
## Plus: Personal current transfer receipts
## 309
## Plus: Personal income receipts on assets
## 309
## Private
## 309
## Statistical discrepancy as a percentage of GDP
## 308
## Taxes on production and imports less subsidies
## 309
df <- beaLong |> filter(LineDescription =="Gross domestic product (GDP)")
df$year <- substring(text = df$TimePeriod,
first = 1,
last = 4
)
ggplot(data = df,
mapping = aes(x = year,
y = DataValue / 1000000 )
) + geom_line() + labs(title = "US GDP over time",
x = "Year",
y = "GDP (in trillions)"
) +
theme(axis.text.x = element_text(angle=90,
vjust=.5,
size = 7
)
)
An import skill to be able to aggregate monthly/daily/quarterly data to annual frequency
# Extract year and quarter from TimePeriod
df <- df %>%
mutate(year = as.numeric(substring(TimePeriod, first = 1, last = 4)),
quarter = as.numeric(substring(TimePeriod, first = 6, last = 6)))
# Aggregate quarterly data to annual
df_annual <- df %>%
group_by(year) %>%
summarize(DataValue_annual = mean(DataValue))
# Plot the annual data
ggplot(data = df_annual,
mapping = aes(x = as.factor(year),
y = DataValue_annual / 1000000,
group = 1) # Specify group = 1
)+ geom_line() +
labs(title = "US GDP over time (Annual)",
x = "Year",
y = "GDP (in trillions)"
) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, size = 7)
) + geom_point(color='red')
Can also read up on ggplot basics here.
And here. ggplot2 charts