rm(list = ls())
library(tidyverse)
library(bea.R)
# Setup API key
beaKey <- "A31F4917-2490-488D-AFFD-86A2D5BCFF74"
beaSets(beaKey = beaKey)
## $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 A31F4917-2490-488D-AFFD-86A2D5BCFF74
## 2        METHOD                       GETDATASETLIST
## 3  RESULTFORMAT                                 JSON
beaSearch("nonfarm proprietors", beaKey = beaKey)
##      SeriesCode RowNumber
##   1:     A1646C       750
##   2:     B046RC        10
##   3:     Q1808C       100
##   4:     Q1809C       110
##   5:     H1810C       120
##  ---                     
## 212:     B1203C        60
## 213:     B1205C        70
## 214:     B1206C        80
## 215:     B1207C        90
## 216:     Y370RC       100
##                                                                      LineDescription
##   1:                                  Nonfarm proprietors' income with IVA and CCAdj
##   2:                                                     Nonfarm proprietors' income
##   3:                                                                   Communication
##   4:                                            Electric, gas, and sanitary services
##   5:                                                                 Wholesale trade
##  ---                                                                                
## 212: Adjustment to depreciate expenditures for mining exploration, shafts, and wells
## 213:                                                                Bad debt expense
## 214:                                                  Income received by fiduciaries
## 215:                                               Income of tax-exempt cooperatives
## 216:        Adjustment to depreciate expenditures for intellectual property products
##      LineNumber ParentLineNumber Tier   Path TableID        DatasetName
##   1:         72                     0     72  T11300               NIPA
##   2:          1                0    0      1 T61200A               NIPA
##   3:         10                8    2 1.8.10 T61200A               NIPA
##   4:         11                8    2 1.8.11 T61200A               NIPA
##   5:         12                1    1   1.12 T61200A               NIPA
##  ---                                                                   
## 212:          5                0    0      5  T71400 NIUnderlyingDetail
## 213:          6                0    0      6  T71400 NIUnderlyingDetail
## 214:          7                0    0      7  T71400 NIUnderlyingDetail
## 215:          8                0    0      8  T71400 NIUnderlyingDetail
## 216:          9                0    0      9  T71400 NIUnderlyingDetail
##                                                                                                                                                                   TableName
##   1:                                                                                  Table 1.13. National Income by Sector, Legal Form of Organization, and Type of Income
##   2:                                                                                                                   Table 6.12A. Nonfarm Proprietors' Income by Industry
##   3:                                                                                                                   Table 6.12A. Nonfarm Proprietors' Income by Industry
##   4:                                                                                                                   Table 6.12A. Nonfarm Proprietors' Income by Industry
##   5:                                                                                                                   Table 6.12A. Nonfarm Proprietors' Income by Industry
##  ---                                                                                                                                                                       
## 212: Table 7.14. Relation of Nonfarm Proprietors' Income in the National Income and Product Accounts to Corresponding Measures as Published by the Internal Revenue Service
## 213: Table 7.14. Relation of Nonfarm Proprietors' Income in the National Income and Product Accounts to Corresponding Measures as Published by the Internal Revenue Service
## 214: Table 7.14. Relation of Nonfarm Proprietors' Income in the National Income and Product Accounts to Corresponding Measures as Published by the Internal Revenue Service
## 215: Table 7.14. Relation of Nonfarm Proprietors' Income in the National Income and Product Accounts to Corresponding Measures as Published by the Internal Revenue Service
## 216: Table 7.14. Relation of Nonfarm Proprietors' Income in the National Income and Product Accounts to Corresponding Measures as Published by the Internal Revenue Service
##              ReleaseDate     NextReleaseDate         MetaDataUpdated  Account
##   1: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:29.923 National
##   2: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:29.923 National
##   3: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:29.923 National
##   4: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:29.923 National
##   5: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:29.923 National
##  ---                                                                         
## 212: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:32.840 National
## 213: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:32.840 National
## 214: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:32.840 National
## 215: Jul 31 2018  8:30AM Jan  1 1900 12:00AM 2019-03-06T10:13:32.840 National
## 216: 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' = 'T11300', ...))
##   2:              beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T61200A', ...))
##   3:              beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T61200A', ...))
##   4:              beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T61200A', ...))
##   5:              beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIPA', 'TableName' = 'T61200A', ...))
##  ---                                                                                                                               
## 212: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'T71400', ...))
## 213: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'T71400', ...))
## 214: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'T71400', ...))
## 215: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'T71400', ...))
## 216: beaGet(list('UserID' = '[your_key]', 'Method' = 'GetData', 'DatasetName' = 'NIUnderlyingDetail', 'TableName' = 'T71400', ...))
data <- beaGet(list('UserID' = beaKey,
            'Method' = 'GetData',
            'DatasetName' = 'NIPA',
            'TableName' = 'T10705',
            'Frequency' = 'A',
            'Year' = 'X'))
library(dplyr)
datanew <- data %>% head(1)
datanew <- pivot_longer(datanew,cols = 8:101,names_to = 'year',values_to = 'GDP')
datanew$year <- gsub("DataValue_","",datanew$year)
datanew$year <- as.numeric(datanew$year)
plot(datanew$year,datanew$GDP)

library(ggplot2)
ggplot(data=datanew, aes(x=year, y=GDP, group=1)) +
  geom_line()+
  geom_point(color = 'blue') +
  ggtitle('Plot on the GDP over Time') + 
  xlab('Year')  +
  ylab('GDP')

We can find there exists a decrease of GDP during the finacial crisis in 2008-2009 and Covid-19 in 2019-2020. The financial crisis caused the economic situation to decline, unemployment to increase, and therefore GDP to decrease. The COVID-19 affected many industries, especially manufacturing and services, resulting in lower GDP.