library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(bea.R)
## Warning: package 'bea.R' was built under R version 4.3.2
## Loading required package: data.table
##
## Attaching package: 'data.table'
##
## The following objects are masked from 'package:lubridate':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
##
## The following objects are masked from 'package:dplyr':
##
## between, first, last
##
## The following object is masked from 'package:purrr':
##
## transpose
##
## 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(ggplot2)
beaKey <- "C42BB66A-63CC-4836-8384-656390151D66"
beaSpecs <- list(
'UserID' = beaKey,
'Method' = 'GetData',
'datasetname' = 'NIPA',
'TableName' = 'T10705',
'Frequency' = 'A',
'Year' = 'X'
)
#asWide = FALSE will give us long data format. GGplots likes long data.
bea_data <- beaGet(beaSpecs, asWide = FALSE)
## No encoding supplied: defaulting to UTF-8.
#check for NAs
NA_Number <- sum(is.na(beaSpecs$DataValue))
NA_Number
## [1] 0
#Filter for only GDP data
data_filtered <- bea_data %>%
filter(LineDescription == 'Gross domestic product (GDP)')
#as we can see, TimePeriod is acting as characters, not numbers.
str(data_filtered)
## Classes 'data.table' and 'data.frame': 94 obs. of 10 variables:
## $ TableName : chr "T10705" "T10705" "T10705" "T10705" ...
## $ SeriesCode : chr "A191RC" "A191RC" "A191RC" "A191RC" ...
## $ LineNumber : chr "1" "1" "1" "1" ...
## $ LineDescription: chr "Gross domestic product (GDP)" "Gross domestic product (GDP)" "Gross domestic product (GDP)" "Gross domestic product (GDP)" ...
## $ TimePeriod : chr "1929" "1930" "1931" "1932" ...
## $ METRIC_NAME : chr "Current Dollars" "Current Dollars" "Current Dollars" "Current Dollars" ...
## $ CL_UNIT : chr "Level" "Level" "Level" "Level" ...
## $ UNIT_MULT : chr "6" "6" "6" "6" ...
## $ DataValue : num 104556 92160 77391 59522 57154 ...
## $ NoteRef : chr "T10705" "T10705" "T10705" "T10705" ...
## - attr(*, "params")='data.frame': 8 obs. of 2 variables:
## ..$ ParameterName : chr [1:8] "USERID" "METHOD" "DATASETNAME" "TABLENAME" ...
## ..$ ParameterValue: chr [1:8] "C42BB66A-63CC-4836-8384-656390151D66" "GETDATA" "NIPA" "T10705" ...
## - attr(*, "detail")=List of 4
## ..$ Statistic : chr "NIPA Table"
## ..$ UTCProductionTime: chr "2023-11-21T01:21:32.257"
## ..$ Dimensions :'data.frame': 9 obs. of 4 variables:
## .. ..$ Ordinal : chr [1:9] "1" "2" "3" "4" ...
## .. ..$ Name : chr [1:9] "TableName" "SeriesCode" "LineNumber" "LineDescription" ...
## .. ..$ DataType: chr [1:9] "string" "string" "numeric" "string" ...
## .. ..$ IsValue : chr [1:9] "0" "0" "0" "0" ...
## ..$ Notes :'data.frame': 5 obs. of 2 variables:
## .. ..$ NoteRef : chr [1:5] "T10705" "T10705.1" "T10705.2" "T10705.3" ...
## .. ..$ NoteText: chr [1:5] "Table 1.7.5. Relation of Gross Domestic Product, Gross National Product, Net National Product, National Income,"| __truncated__ "1. Prior to 1959, current surplus of government enterprises (line 22) is not shown separately; subsidies are in"| __truncated__ "2. The arithmetic average of gross domestic product and of gross domestic income." "3. Consists of compensation of employees, proprietors' income with inventory valuation adjustment (IVA) and cap"| __truncated__ ...
## - attr(*, ".internal.selfref")=<externalptr>
## - attr(*, "is.wide")= logi FALSE
#turn TimePeriod into numeric data ... now we can plot with ggplot
data_filtered$TimePeriod <- as.numeric(data_filtered$TimePeriod)
ggplot(data = data_filtered) +
(mapping = aes(x = TimePeriod, y = DataValue)) +
geom_line()
labs(title = "GDP by Year", x = "Year", y = "GDP")
## $x
## [1] "Year"
##
## $y
## [1] "GDP"
##
## $title
## [1] "GDP by Year"
##
## attr(,"class")
## [1] "labels"
Note: At first ggplots was not working. I was not getting a line plot and the x-axis was not readable with all the close year labels. I realized that the TimePeriod column that contained the years (x-variable) was acting as a character data type. I had to change this with a simple as.numeric function. Now ggplots sees the Years as numbers and is able to plot the time series as a line plot.
From the plot we can see the long term GDP. GDP did drop during the 2008 financial crisis and during COVID19.
In 2008, the market crashed largely due to predatory lending that targeted low-income home buyers. Financial institutions took on too much risk by lending large sums of money to people that could not pay it back. This was done because there was a housing bubble, and people thought prices were only going to continue to rise. Money was loaned to people that could not pay it back and the banks planned to sell the houses at a higher value to recover their loan. House values crashed and people began to default on their loans, resulting in foreclosure, banks sold the houses at a loss resulting in tons of money lost. The markets crashed. Whether you lost your money in the stock market or defaulted on a loan and lost your house, almost everyone got hit hard. People were fighting to survive, saving what they had left, and not spending money on entertainment, goods, services, etc. US GDP declined as people had no money to spend, massive layoffs, and other factors.
http://economyria.com/the-financial-crisis-2008-explained/
In early 2020, the world shut down due to COVID. This was an unprecedented event. Governments across the world, including the US had a lock down in place. At least at the beginning of the shutdown working from home was not that common and that infrastructure was not fully set up. Companies had massive layoffs and only essential workers were working. The amount of goods and services being produced plummeted and there was not much movement of money in the country or the world. The rebound was stronger (steeper) in the short term for COVID 19. This can possibly be attributed to stimulus checks, overvalued stock market during parts of the pandemic (especially tech), a booming housing market, among other reasons.