Translate

Monday, December 14, 2020

Analyzing GDP by Industry with R

 






































library(quantmod)
library(Quandl)
library(xts)
library(zoo)

Industry_Data <- read.csv("Industry Data.csv")
View(Industry_Data)

GDP<-ts(Industry_Data, start=c(2005,1), end=c(2020,2), frequency=4)

data<-GDP

data

# Percent changes from previous

percent_GDP<-diff(data, lag=4)/lag(data, k=-4)*100


Hw# Percentage of each industry in total industry

pct_GDP<-(data/data[,1])*100

summary(percent_GDP)


plot(pct_GDP)

par(mfrow=c(2,1))
plot(percent_GDP[,2], main="Private Industry")
plot(pct_GDP[,2])


par(mfrow=c(2,1))
plot(percent_GDP[,3], main="  Agriculture, forestry, fishing, and hunting")
plot(pct_GDP[,3])


par(mfrow=c(2,1))
plot(percent_GDP[,4], main="      Farms")
plot(pct_GDP[,4])


par(mfrow=c(2,1))
plot(percent_GDP[,6], main="       Mining")
plot(pct_GDP[,6])


par(mfrow=c(2,1))
plot(percent_GDP[,7], main="        Oil and gas extraction")
plot(pct_GDP[,7])


par(mfrow=c(2,1))
plot(percent_GDP[,11], main=" Construction")
plot(pct_GDP[,11])



par(mfrow=c(2,1))
plot(percent_GDP[,12], main="   Manufacturing")
plot(pct_GDP[,12])



par(mfrow=c(2,1))
plot(percent_GDP[,13], main="     Durable goods")
plot(pct_GDP[,13])


par(mfrow=c(2,1))
plot(percent_GDP[,19], main="Computer and electronic products")
plot(pct_GDP[,19])

par(mfrow=c(2,1))
plot(percent_GDP[,20], main="Electrical equipment, appliances, and components")
plot(pct_GDP[,20])

par(mfrow=c(2,1))
plot(percent_GDP[,21], main="Motor vehicles, bodies and trailers, and parts")
plot(pct_GDP[,21])


par(mfrow=c(2,1))
plot(percent_GDP[,27], main="    Textile mills and textile product mills")
plot(pct_GDP[,27])



par(mfrow=c(2,1))
plot(percent_GDP[,28], main="         Apparel and leather and allied products")
plot(pct_GDP[,28])

par(mfrow=c(2,1))
plot(percent_GDP[,34], main="    Wholesale trade")
plot(pct_GDP[,34])

par(mfrow=c(2,1))
plot(percent_GDP[,35], main="     Retail trade")
plot(pct_GDP[,35])

par(mfrow=c(2,1))
plot(percent_GDP[,40], main="   Transportation and warehousing")
plot(pct_GDP[,40])


par(mfrow=c(2,1))
plot(percent_GDP[,41], main="      Air transportation")
plot(pct_GDP[,41])



par(mfrow=c(2,1))
plot(percent_GDP[,42], main=" Rail transportation")
plot(pct_GDP[,42])


par(mfrow=c(2,1))
plot(percent_GDP[,49], main="Information")
plot(pct_GDP[,49])

par(mfrow=c(2,1))
plot(percent_GDP[,55], main="    Finance and insurance")
plot(pct_GDP[,55])


par(mfrow=c(2,1))
plot(percent_GDP[,60], main=" Real estate")
plot(pct_GDP[,60])


par(mfrow=c(2,1))
plot(percent_GDP[,62], main=" Housing")
plot(pct_GDP[,62])









Analyzing Gross Domestic Product (GDP) as of 2020-07-01

 




library(Quandl)
library(ggplot2)
library(tseries);library(timeseries);library(xts);library(forecast)
library (quantmod)
library(psych)
library(plotly) #install.package(plotly)

getSymbols(c('GDPDEF','GDP','PCEC','GPDI','NETEXP','GCE'), src='FRED')

ratio_PCEC=PCEC/GDP*100
ratio_GPDI=GPDI/GDP*100
ratio_NETEXP=NETEXP/GDP*100
ratio_GCE=GCE/GDP*100


basket<-cbind(ratio_PCEC, ratio_GPDI, ratio_NETEXP, ratio_GCE)

summary(basket)

tail(basket)
myColors <- c("red", "darkgreen", "goldenrod", "darkblue")

plot(x = basket, xlab = "Year", ylab = "Percent",
       main = "Percent in GDP", col = myColors, screens = 1, subset = "1980-01-04/")
legend(x = "topleft", legend = c("PCEC", "GDPI", "NETEXP", "GCE"),
       lty = 1, col = myColors)

describe(ratio_GPDI)


plot(ratio_PCEC, main="% of Personal Consumption Expenditure in GDP", ylab="Percent")
lines(mean_PCEC, col='red')

plot(ratio_GPDI, main="% of Gross Private Domestic Investment in GDP")

plot(ratio_NETEXP, main="% of Net Export in GDP")

plot(ratio_GCE, main="% of Government Consumption Expenditure in GDP")





Diff_GDP=Delt(GDP, k=4)*100
Diff_PCEC=Delt(PCEC, k=4)*100
Diff_GPDI=Delt(GPDI, k=4)*100
Diff_NETEXP=Delt(NETEXP, k=4)*100
Diff_GCE=Delt(GCE, k=4)*100



Diff_basket<-cbind(Diff_GDP, Diff_PCEC, Diff_GPDI, Diff_GCE)

myColors <- c("red", "darkgreen", "goldenrod", "darkblue")

plot(x = Diff_basket, xlab = "Year", ylab = "Percent",
     main = "Percent in GDP", col = myColors, screens = 1, subset = "1980-01-04/")
legend(x = "topleft", legend = c("GDP", "PCEC", "GDPI",  "GCE"),
       lty = 1, col = myColors)



Diff_GDPDEF=Delt(GDPDEF, k=4)*100

Real_GDP=Diff_GDP-Diff_GDPDEF

plot(Real_GDP)