Using an API (Application Programming Interface)

The data API provides programmatic access to BEA published economic statistics using industry-standard methods and procedures. BEA’s data API includes methods for retrieving a subset of our statistical data and the meta-data that describes it.

  1. Go top Bureau of Economic Analysis (BEA) Data Application Programming Interface (API) and create an account to get your public key.

    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.
    BEA_Key <-'703E50E9-4E5F-48CF-A861-E32A940297AC'
  2. Next, read API guides or online blogs on how to use BEA API to import data from the web into R directly.  You may find other useful resources too on platforms like GitHub like this. Feel free to use add on packages like tidyverse for this discussion, where you will often see the pipingoperator %>% for logical sequencing. 

    HINT:  Try reading up on the help commands for BEA functions to see what arguments they take - in particular ?beaGet and ?beaSpecs to ensure you do not the error - “The dataset request requires parameters that were missing from the request”

    BEA_specs <- list(
      "UserID" = BEA_Key,
      "Method" = "GetData",
      "datasetname" = "NIPA",
      "TableName" = "T10705",
      "Frequency" = "A",
      "Year" = "X")
    
    
    BEA_df <- 
      beaGet(
        BEA_specs, 
        asWide = FALSE)
    ## No encoding supplied: defaulting to UTF-8.
  3. Plot the current or real GDP over time (you can try to go as long back in time as possible) - share your graph as a picture in your submission along with your R script.  Make sure the axis are well labelled, graph has clear labels, et cetra / is presentable.  Submit a R Markdown fileoutput, like usual. Cheatsheet

    library(tidyverse)
    ## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
    ## ✔ dplyr     1.1.4     ✔ readr     2.1.5
    ## ✔ forcats   1.0.0     ✔ stringr   1.5.1
    ## ✔ ggplot2   3.5.0     ✔ 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(dplyr)
    
    BEA_GDP <- BEA_df %>%
      filter(LineDescription == "Gross domestic product (GDP)")
    
    plot(BEA_GDP$TimePeriod, 
         BEA_GDP$DataValue, 
         type = "l", 
         col = "turquoise",
         xlab = "Time (Years)", 
         ylab = "GDP (Dollars)", 
         main = "BEA Gross Domestic Product (GDP) Over Time")

  4. Do you find that GDP dropped during Covid - 19 and 2008 Financial Crisis ?  Why could that be happening ?

    Yes, there are two sharp dips (i.e., change in slopes from positive to negative) around 2008 and 2020. These drops in GDP show major financial turndown events, signifying that the amount of dollars spent in the US spent for those years decreased significantly.

  5. If you have time, use ggplot2 to create the plot this time.  Google is your best friend, and will take you to online blogs like STHDA, Analytics Vidhya, and of course you can look for cheatsheets for popular packages like from DataCamp or RStudio.

    library(ggplot2)
    BEA_GDP$TimePeriod <- 
      as.numeric(BEA_GDP$TimePeriod)
    BEA_GDP$DataValue <- 
      as.numeric(BEA_GDP$DataValue)
    
    ggplot(
      BEA_GDP, 
      aes(x = TimePeriod, y = DataValue)) +
      geom_line(color = "maroon") +  
      labs(
        title = "Change in GDP Over the Years", 
        x = "Time (Years)", 
        y = "GDP (Dollars)")