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)








Monday, November 30, 2020

Housing Top 20 cities with R as of 10-2020

 




















download.file("https://s3-us-west-2.amazonaws.com/econresearch/Reports/Core/RDC_InventoryCoreMetrics_State.csv",
              destfile = "State.csv")

download.file("https://s3-us-west-2.amazonaws.com/econresearch/Reports/Core/RDC_InventoryCoreMetrics_Metro.csv",
              destfile = "Metro.csv")

library(ggplot2)
State_data<-read.csv("State.csv")
Metro_data<-read.csv("Metro.csv")
taland

Metro_prices<-Metro_data [which(Metro_data$Hhrank<21),]

Metro_top<-subset(Metro_data,Metro_data$Hrank <21)

Metro_top


ratio_yy<-Metro_prices$price_increased_count_yy/Metro_prices$price_reduced_count_yy


ratio_mm<-Metro_prices$price_increased_count_mm/Metro_prices$price_reduced_count_mm



barplot(ratio_yy, main='Ratio of price increase to decrease in Top 20 cities Y to Y', col="blue",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7, cex.names = 0.6, las=2)

barplot(ratio_mm, main='Ratio of price increase to decrease in Top 20 cities M to M', col="red",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7, cex.names = 0.6, las=2)

        
barplot(Metro_prices$Median.Listing.Price, main='Median Prices for Top 20 cities', col="blue",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7,
        
        
        cex.names = 0.5, las=2)


barplot(Metro_prices$Median.Listing.Price.Y.Y, main='Median Prices for Top 20 cities Y to Y', col="red",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7,
        cex.names = 0.6, las=2)



barplot(Metro_prices$Median.Listing.Price.M.M, main='Median Prices for Top 20 cities M to M', col="blue",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7,
        cex.names = 0.6, las=2)



barplot(Metro_prices$Active.Listing.Count.Y.Y, main='Active Listing for Top 20 cities Y to Y', col="blue",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7,
        cex.names = 0.6, las=2)


barplot(Metro_prices$Active.Listing.Count.M.M, main='Active Listing for Top 20 cities M to M', col="blue",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7,
        cex.names = 0.6, las=2)


barplot(Metro_prices$Days.on.Market.Y.Y, main='Days on Market for Top 20 cities Y to Y', col="red",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7,
        cex.names = 0.6, las=2)



barplot(Metro_prices$price_increased_count_yy, main='Price Increased count for Top 20 cities Y to Y', col="green",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7,
        cex.names = 0.6, las=2)


barplot(Metro_prices$pending_ratio_yy, main='Price Pending ratio for Top 20 cities Y to Y', col="green",
        names.arg=Metro_prices$cbsa_title, cex.axis=0.7,
        cex.names = 0.5, las=2)





ggplot(Metro_prices, aes(x=factor(Hhrank), y=Median.Listing.Price.Y.Y)) +
     geom_col(fill="lightblue", colour="red")
     geom_text(aes(label=Median.Listing.Price.Y.Y), vjust=-0.2)


ggplot(Metro_prices, aes(x=Metro_prices$Median.Listing.Price.Y.Y)) +
  geom_histogram()



barplot(Metro_prices[order(Metro_prices$Median.Listing.Price.Y.Y)], horiz=T)

hist(Metro_prices$Median.Listing.Price)
hist(Metro_data$Median.Listing.Price.Y.Y)

summary(Metro_data$Median.Listing.Price.Y.Y)


barplot(Metro_prices$Median.Listing.Price.Y.Y, cex.names = 0.9, las=2)



# Select Housing Prices for top 20 cities

Housing<- subset(Metro_data, Metro_data$Hhrank <'20') 
Housing<-Housing[order(Housing$Month),]
ratio=Housing$Avg.Listing.Price/Housing$Median.Listing.Price

barplot(Housing$Median.Listing.Price)


d <- density(ratio)
plot(d, main="Average Prices / Median Prices")
polygon(d, col="red", border="blue")




var0<-Housing$Median.Listing.Price
var1<-Housing$Avg.Listing.Price
date <- seq(as.Date("2012-05-01"), by="1 month", length.out=84) 

# Creating Charts

ggplot() + geom_line(aes(x=date,y=var0),color='red') +
  
  geom_line(aes(x=date,y=var1),color='blue') + 
  
  ylab('Housing Prices')+xlab('Date')+
  
  labs(title=" Median Listing Prices (in Red) and Average Listing Prices (in Blue)")




# Percent Chagnes

var2<-Housing$Median.Listing.Price.Y.Y
var3<-Housing$Avg.Listing.Price.Y.Y
date <- seq(as.Date("2012-05-01"), by="1 month", length.out=84) 

ggplot() + geom_line(aes(x=date,y=var2),color='red') +
  
  geom_line(aes(x=date,y=var3),color='blue') + 
  
  ylab('Housing Prices')+xlab('Date')+
  
  labs(title=" Median Listing Prices (in Red) and Average Listing Prices Y to Y (in Blue)")



barplot(Housing$Days.on.Market, main="Days on Market"
        ,
        names.arg = Housing$Month, cex.names = 0.3 )


barplot(Housing$Days.on.Market.Y.Y, main="Days on Market Y to Y"
        ,
        names.arg = Housing$Month, cex.names = 0.3 )


barplot(Housing$Total.Listing.Count.Y.Y, main="Total Listing Y to Y"
        ,
        names.arg = Housing$Month, cex.names = 0.5 )



barplot(Housing$Pending.Listing.Count.Y.Y, main="Pending Listing Y to Y"
        ,
        names.arg = Housing$Month, cex.names = 0.5 )


# Basic line plot with points
ggplot(data=Housing, aes(x=Housing$Month, y=ratio, group=1)) +
  geom_line()+
  geom_point()+
  labs(title=" Ratio ( Average Price / Median Price) ")



# Basic line plot with points
ggplot(data=Housing, aes(x=Housing$Month, y=Housing$Active.Listing.Count.Y.Y, group=1)) +
  geom_line(linetype="dashed")+
  geom_point()+
  labs(title=" Active Listing Count")










U.S. Employment analysis with R as of 10-2020

 















library(Quandl)