-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBEA_CorpProfits.Rmd
155 lines (106 loc) · 5.88 KB
/
BEA_CorpProfits.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---
title: "BEA Corporate Profits"
author: "Jesse Wade"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r Libraries, include=FALSE}
library(tidyverse)
library(httr)
library(jsonlite)
beakey <- "DA37A0AD-8100-4FA9-A813-CDF16F9E6AE9"
```
# Analysis of BEA Corporate Profits Data
Within the BEA data, we have access to 13 differnet datasets. Corporate profits is associated with the GDP releases. They are released with the second release of GDP, as an initial esimtate at a SAAR.
```{r getData, include=FALSE}
ListofData <- GET("https://apps.bea.gov/api/data?&UserID=DA37A0AD-8100-4FA9-A813-CDF16F9E6AE9&method=GETDATASETLIST&ResultFormat=JSON")
listofData <- fromJSON(rawToChar(ListofData$content))
Possible_Datasets <- listofData$BEAAPI$Results$Dataset
```
```{r BEA_DataSets}
knitr::kable(Possible_Datasets)
```
Since we are downloaded in the NIPA dataset, we also need to know what the required parameters are for this data set. The table below tells us what the API will expect when we request the data.
```{r NIPAParam, include=FALSE}
NIPA_DataSet <- GET("https://apps.bea.gov/api/data?&UserID=DA37A0AD-8100-4FA9-A813-CDF16F9E6AE9&method=GETPARAMETERLIST&datasetname=NIPA&ResultFormat=JSON")
NIPA_table <- fromJSON(rawToChar(NIPA_DataSet$content))
NIPA_Param <- NIPA_table$BEAAPI$Results$Parameter
```
```{r NIPAParameters}
knitr::kable(NIPA_Param[,c(1,3)])
```
Within the paramets, there a 325 possible tableIDs to download from the NIPA. We need to find which one has the corporate profits data. It may be multiple tables that we will need to use.
```{r AllowedValues, include=FALSE}
TableName_DataSet <- GET("https://apps.bea.gov/api/data?&UserID=DA37A0AD-8100-4FA9-A813-CDF16F9E6AE9&method=GETPARAMETERVALUES&datasetname=NIPA¶metername=TableName&ResultFormat=JSON")
TableName_table <- fromJSON(rawToChar(TableName_DataSet$content))
TableName_Param <- TableName_table$BEAAPI$Results$ParamValue
TableName_Param %>%
filter(grepl('corporate profits',tolower(Description))) -> CorpProfits_Tables
```
```{r AllowedValues_View}
nrow(TableName_Param)
knitr::kable(head(CorpProfits_Tables))
```
Since looking for Corporate Profits, need to search for this in the Description column of the table parameter table
```{r GetDataFromBEA, include=F}
CorpProfitByIndustry <- GET("https://apps.bea.gov/api/data?&UserID=DA37A0AD-8100-4FA9-A813-CDF16F9E6AE9&method=GETDATA&datasetname=NIPA&FREQUENCY=Q&TABLENAME=T61600D&YEAR=X&ResultFormat=JSON")
CorpProfits_Data_JSON <- fromJSON(rawToChar(CorpProfitByIndustry$content))
CorpProfits_Data <- CorpProfits_Data_JSON$BEAAPI$Results
CorpProfits_DataTable = CorpProfits_Data$Data
```
After finally finding the table, we now have the Corporate profits with inventory valuation and capital consumption adjustments dating back to the first Quarter of 2021. We will likely need to edit the TimePeriod column because R does not deal well with the Q in the time period.
```{r ViewData}
CorpProfits_DataTable %>%
filter(LineNumber==1) %>%
select(TimePeriod,DataValue) -> CorpProfits_Line1_SAAR
knitr::kable(head(CorpProfits_Line1_SAAR))
```
```{r ChangeDate, include=FALSE}
CorpProfits_Line1_SAAR %>%
mutate(Date=as.Date(if_else(substr(TimePeriod,5,6)=="Q1",paste(substr(TimePeriod,1,4),"/03/31",sep=''),
if_else(substr(TimePeriod,5,6)=="Q2",paste(substr(TimePeriod,1,4),"/06/30",sep=''),
if_else(substr(TimePeriod,5,6)=="Q3",paste(substr(TimePeriod,1,4),"/09/30",sep=''),
if_else(substr(TimePeriod,5,6)=="Q4",paste(substr(TimePeriod,1,4),"/12/31",sep=''),"ERROR"))))),CorpProfit=as.integer(gsub(',',"",DataValue))) %>%
select(CorpProfit,Date)->CorpProfits_Line1_SAAR
CorpProfitsPlot <- ggplot(data = CorpProfits_Line1_SAAR,aes(x=Date,y=CorpProfit)) + geom_line(linewidth=1.25,color="red") + ylab("Corporate Profits [Millions;SAAR]") +labs(title="Corporate profits with inventory valuation and capital consumption adjustments")
```
```{r BEA_Corp_Profits_Gr}
CorpProfitsPlot
```
```{r CorpProfitsByIndustry, include = FALSE }
CorpProfits_DataTable %>% mutate(Date=as.Date(if_else(substr(TimePeriod,5,6)=="Q1",paste(substr(TimePeriod,1,4),"/03/31",sep=''),
if_else(substr(TimePeriod,5,6)=="Q2",paste(substr(TimePeriod,1,4),"/06/30",sep=''),
if_else(substr(TimePeriod,5,6)=="Q3",paste(substr(TimePeriod,1,4),"/09/30",sep=''),
if_else(substr(TimePeriod,5,6)=="Q4",paste(substr(TimePeriod,1,4),"/12/31",sep=''),"ERROR"))))),CorpProfit=as.integer(gsub(',',"",DataValue))) %>%
select(Date,LineDescription,CorpProfit,LineNumber) -> All_industry_table
```
Within the corporate profits table, there is 29 unique line items. The main line items that This will focus on for now are first 7 line items
```{r Unique_Line_Items}
knitr::kable(unique(All_industry_table$LineDescription))
All_industry_table %>%
filter(as.numeric(LineNumber) < 8) -> CCAADj_CorpProfits
```
```{r CCAAdj_CorpProfits_gph, include=FALSE}
CCAAdj_CorpProfits_gph <- ggplot(data=CCAADj_CorpProfits,aes(x=Date,y=CorpProfit)) +
geom_line(linewidth=.75,aes(color=LineNumber)) +ylab("Corporate Profits [Millions;SAAR]") +labs(title="Corporate profits with inventory valuation and capital consumption adjustments By industry")
```
```{r CCAAdj_CorpProfits_gph_1}
knitr::kable(unique(CCAADj_CorpProfits[,c(2,4)]))
CCAAdj_CorpProfits_gph
```
```{r NonDurableGoods_dta, include = FALSE}
All_industry_table %>%
filter(LineNumber %in% c('23','24','25','26','27')) -> NonDurable_Goods
```
```{r NonDurableGoods_gph, include = FALSE}
NonDurable_Goods_Gph <- ggplot(data=NonDurable_Goods,aes(x=Date,y=CorpProfit)) +
geom_line(linewidth=.75,aes(color=LineNumber)) +ylab("Corporate Profits [Millions;SAAR]") +labs(title="Corporate profits of NonDurable Goods")
NonDurable_Goods_Gph
```
```{r NoDurable_Image}
knitr::kable(unique(NonDurable_Goods[,c(2,4)]))
NonDurable_Goods_Gph
```