Translate

Thursday, May 13, 2021

S&P/ Case-Shiller U.S. City Price Index as of 02-2021

 


































library(ggplot2)
library(Quandl)
library(zoo)
library(quantmod)
library(TTR)
library(forecast)
library(ggfortify)
library(psych)
library(pastecs)
library(xts)

start <- as.Date("1990-01-01")

getSymbols(c(
  
  'NYXRSA',
  'BOXRSA',
  'SDXRSA',
  'CHXRSA',
  'DNXRSA',
  'LVXRSA',
  'DAXRSA',
  'WDXRSA',
  'MIXRSA',
  'ATXRSA',
  'SFXRSA',
  'LXXRSA',
  'SEXRSA',
  'CSUSHPISA'
  
), from=start, src='FRED')

names(NYXRSA)<-"New York"
names(BOXRSA)<-"Boston"
names(SDXRSA)<-"San Diego"
names(CHXRSA)<-"Chicago"
names(DNXRSA)<-"Denver"
names(LVXRSA)<-"Las Vegas"
names(WDXRSA)<-"DC"
names(MIXRSA)<-"Miami"
names(DAXRSA)<-"Dallas"
names(ATXRSA)<-"Atlanta"
names(SFXRSA)<-"San Francisco"
names(LXXRSA)<-"Los Angeles"
names(SEXRSA)<-"Seattle"
names(CSUSHPISA)<-"National" 

Mortgage<-getSymbols('MORTGAGE30US', src='FRED')

Housing_Price=ATXRSA

Compare_Max=Housing_Price/max(Housing_Price)*100

Diff_Housing=Delt(Housing_Price,k=12)*100
Annual_Changes=annualReturn(Housing_Price)*100

par(mfrow=c(2,1))
plot(Diff_Housing)
plot(Annual_Changes)

summary(last(Annual_Changes,'7 years'))

describe(Annual_Changes)

par(mfrow=c(3,1))
plot(ATXRSA, main="ATLANTA")
plot(Delt(ATXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(ATXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(BOXRSA, main="BOSTON")
plot(Delt(BOXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(BOXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(CHXRSA, main="CHICAGO")
plot(Delt(CHXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(CHXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(DAXRSA, main="DALLAS")
plot(Delt(DAXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(DAXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(DNXRSA, main="DENVER")
plot(Delt(DNXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(DNXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)



par(mfrow=c(3,1))
plot(LVXRSA, main="LAS VEGAS")
plot(Delt(LVXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(LVXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(MIXRSA, main="MIAMI")
plot(Delt(MIXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(MIXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(NYXRSA, main="NEW YORK")
plot(Delt(NYXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(NYXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(SDXRSA, main="SAN DIEGO")
plot(Delt(SDXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(SDXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(SEXRSA, main="SEATTLE")
plot(Delt(SEXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(SEXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(SFXRSA, main="SAN FRANSICO")
plot(Delt(SFXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(SFXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


par(mfrow=c(3,1))
plot(WDXRSA, main="WASHINGTON DC")
plot(Delt(WDXRSA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(WDXRSA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)



par(mfrow=c(3,1))
plot(CSUSHPISA, main="US NATIONAL")
plot(Delt(CSUSHPISA,  k=12)*100, main="Percent changes from previous year ", col="red")
barplot(annualReturn(CSUSHPISA), main="Annual Changes ", col="blue", cex.names=0.8, las=2)


dev.off()

barplot(Annual_Changes, main=" Annual Changes ", col="blue", cex.names=0.8, las=2)





Monthly_Price<-monthlyReturn(Housing_Price)
Monthly_Rate<-MORTGAGE30US

Housing<-na.omit(merge(Monthly_Price,MORTGAGE30US))
head(Housing)

# Scatter chart for Price chagnes

scatter.smooth(last(Housing, "10 years"))
abline(lm(Housing$monthly.returns~Housing$MORTGAGE30US), col="blue")

# Regressiion Model

alli.mod1=lm(Combined$monthly.returns~Combined$MORTGAGE30US, data=Combined)

summary(alli.mod1)


autoplot(MORTGAGE30US)
y<-(Housing_Price)

library(ggfortify)
library(forecast)
autoplot(y)

d.arima<-auto.arima(y)
d.arima

d.forecast<-forecast(d.arima)
d.forecast

autoplot(forecast(d.arima, 36))


fit<-ets(y)
autoplot(stl(y, plot=FALSE))
ggtsdiag(auto.arima(y))


autoplot(forecast(fit,36))



Housing_Prices <- as.xts(data.frame(
  
  NYXRSA,
  BOXRSA,
  SDXRSA,
  CHXRSA,
  DNXRSA,
  LVXRSA,
  WDXRSA,
  MIXRSA,
  LXXRSA,
  SFXRSA
))



Housing_return = apply(Housing_Prices, 1, function(x) {x / Housing_Prices[1,]}) %>% 
  t %>% as.xts


head(Housing_return)

Summary_Stat<-summary(Housing_return)

cor(Housing_return)

write.table(Summary_Stat, "Summary.xls")

plot(as.zoo(Housing_return), screens = 1, lty = 1:10, xlab = "Date", ylab = "Home Price Index")
legend("topleft", c("New York", "Boston", "San Diego","Chicago",
                    "Denver", "Las Vegas","DC",
                    "Miami", "Los Angeles", "San Francisco"), lty = 1:10, cex = 0.5)


plot(Housing_return,  xlab = "Date", ylab = "Index (1987=100)")
legend("right", c("New York", "Boston", "San Diego","Chicago",
                    "Denver", "Las Vegas","DC",
                    "Miami","Los Angeles","San Francisco"), 
       fill=c("blue","red","green","black","sky blue","purple","yellow",
              "white", "black","red"
                                                                   
                    ) )


ggplot(Housing_return, aes( x=))

tail(Housing_return)





Wednesday, May 12, 2021

U.S. Consumer Price Index (CPI) Analysis as of 04-2021

 



















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

#Food and Beverages (CPIFABSL)
#Housing (CPIHOSNS)
#Apparel (CPIAPPSL)
#Transportation (CPITRNSL)
#Medical Care (CPIMEDSL)
#Recreation (CPIRECSL)
#Education and Communication (CPIEDUSL)
# Other Goods and Services (CPIOGSSL)
#Commodities (CUSR0000SAC)
# Services (CUSR0000SAS)

start <- as.Date("1990-01-01")

getSymbols(c('CPIFABSL','CPIHOSNS','CPIAPPSL','CPITRNSL','CPIMEDSL','CPIRECSL',
             
             'CPIEDUSL','CPIOGSSL','CUSR0000SAC','CUSR0000SAS',"CPIAUCSL","CPILFESL"), from=start, src='FRED')

CPI<-merge (CPIFABSL,CPIHOSNS,CPIAPPSL,CPITRNSL,CPIMEDSL,CPIRECSL,
             
           CPIEDUSL,CPIOGSSL,CUSR0000SAC,CUSR0000SAS,CPIAUCSL,CPILFESL)

Diff_CPI <- (CPI/lag(CPI)-1)*100  
Diff_CPI[1,] <- 0


Diff_CPIAUCSL=Delt(CPIAUCSL,k=12)*100
Diff_CPIFESL=Delt(CPILFESL,k=12)*100

Diff_CPIFABSL=Delt(CPI$CPIFABSL,k=12)*100
Diff_CPIHOSNS=Delt(CPI$CPIHOSNS,k=12)*100
Diff_CPIAPPSL=Delt(CPI$CPIAPPSL,k=12)*100
Diff_CPITRNSL=Delt(CPI$CPITRNSL,k=12)*100
Diff_CPIMEDSL=Delt(CPI$CPIMEDSL,k=12)*100
Diff_CPIRECSL=Delt(CPI$CPIRECSL,k=12)*100
Diff_CPIEDUSL=Delt(CPI$CPIEDUSL,k=12)*100
Diff_CPIOGSSL=Delt(CPI$CPIOGSSL,k=12)*100
Diff_CUSR0000SAC=Delt(CPI$CUSR0000SAC,k=12)*100
Diff_CUSR0000SAS=Delt(CPI$CUSR0000SAS,k=12)*100

Diff_12<-merge(Diff_CPIFABSL, Diff_CPIHOSNS, Diff_CPIAPPSL, Diff_CPITRNSL, 
               Diff_CPIMEDSL, Diff_CPIRECSL, Diff_CPIEDUSL, Diff_CPIOGSSL,
               Diff_CUSR0000SAC, Diff_CUSR0000SAS)


Diff_12=window(Diff_12,start=as.Date("2001-01-01"), end=as.Date("2020-12-31"))

cor.distance <- cor(Diff_12)
corrplot::corrplot(cor.distance)

plot(Diff_CPIAPPSL)

plot(Diff_12)



plot(Diff_CPIAUCSL, main='Changes from previous year -CPI for All',las=2, subset='2000-01-01/')

plot(Diff_CPIFESL, main='Changes from previous year - Core CPI',las=2,subset='2000-01-01/')


plot(Diff_CPIFABSL, main='Changes from previous year -Food and Beverage',las=2, subset='2000-01-01/')

plot(Diff_CPIHOSNS, main='Changes from previous year -Housing',las=2,subset='2000-01-01/')


plot(Diff_CPIAPPSL, main='Changes from previous year -APPAREL', las=2,subset='2000-01-01/')

plot(Diff_CPITRNSL, main='Changes from previous year -Transportation',las=2,subset='2000-01-01/')


plot(Diff_CPIMEDSL, main='Changes from previous year -Medical Care',las=2,subset='2000-01-01/')


plot(Diff_CPIRECSL, main='Changes from previous year -Recreation',las=2,subset='2000-01-01/')


plot(Diff_CPIEDUSL, main='Changes from previous year -Education and Communication',las=2,subset='2000-01-01/')


plot(Diff_CPIOGSSL, main='Changes from previous year -Other Goods and Services',las=2,subset='2000-01-01/')

plot(Diff_CUSR0000SAC, main='Changes from previous year -Commodities',las=2,subset='2000-01-01/')


plot(Diff_CUSR0000SAS, main='Changes from previous year -Services',las=2,subset='2000-01-01/')






U.S. Employment Analysis by Sectors as of 04-2021

 
















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

#Service-Providing Industries (SRVPRD)
#USGOOD -  All Employees: Goods-Producing Industries
#Private Service-Providing (CES0800000001)
#Mining and Logging: Oil and Gas Extraction (CES1021100001)
#Construction (USCONS)
#Manufacturing (MANEMP)
#Durable Goods (DMANEMP)
#Nondurable goods (NDMANEMP)
#Trade, Transportation and Utilities (USTPU)
#Wholesale Trade (USWTRADE)
#Transportation and Warehousing (CES4300000001)
#Retail Trade (USTRADE)
#Utilities (CES4422000001)
#Information Services (USINFO)
#Financial Activities (USFIRE)
#Professional and Business Services (USPBS)
#Education and Health Services (USEHS)
#Leisure and Hospitality (USLAH)
#Other Services (USSERV)
#Government (USGOVT)

getSymbols(c('PAYEMS','USGOOD','SRVPRD','CES0800000001','CES1021100001','USCONS','MANEMP',
             'DMANEMP','NDMANEMP','USTPU','USWTRADE','USTRADE','CES4300000001','CES4422000001',
            'USINFO','USFIRE','USPBS','USEHS','USLAH','USSERV','USGOVT'), from="1990-01-01",src='FRED')



EMP<-merge (PAYEMS,USGOOD,SRVPRD,CES0800000001,CES1021100001,USCONS,MANEMP,
            DMANEMP,NDMANEMP,USTPU,USWTRADE,USTRADE,CES4300000001,CES4422000001,
            USINFO,USFIRE,USPBS,USEHS,USLAH,USSERV,USGOVT)

names(EMP)<-c('PAYEMS','USGOOD','SRVPRD','CES0800000001','CES1021100001',
              'USCONS','MANEMP','DMANEMP','NDMANEMP','USTPU',
              'USWTRADE','USTRADE','CES4300000001','CES4422000001',
              'USINFO','USFIRE','USPBS','USEHS','USLAH',
              'USSERV','USGOVT')

# % in Total Nonfarm
PER_Good=USGOOD/PAYEMS*100
PER_Service=SRVPRD/PAYEMS*100
PER_CES0800000001=CES0800000001/PAYEMS*100
PER_CES1021100001=CES1021100001/PAYEMS*100

PER_USCONS=USCONS/PAYEMS*100
PER_MANEMP=MANEMP/PAYEMS*100
PER_DMANEMP=DMANEMP/MANEMP*100
PER_NDMANEMP=NDMANEMP/MANEMP*100

PER_USTPU=USTPU/PAYEMS*100
PER_USTRADE=USTRADE/PAYEMS*100
PER_USINFO=USINFO/PAYEMS*100
PER_USPBS=USPBS/PAYEMS*100
PER_USEHS=USEHS/PAYEMS*100

PER_USWTRADE=USWTRADE/PAYEMS*100

PER_USFIRE=USFIRE/PAYEMS*100
PER_USLAH=USLAH/PAYEMS*100
PER_USSERV=USSERV/PAYEMS*100
PER_USGOVT=USGOVT/PAYEMS*100

plot(PER_CES1021100001)

PER_USPBS


summary(PER_USCONS)

hist(PER_USCONS)


# Chart for Total Nonfarm

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

plot(x = EMP_PER, xlab = "Year", ylab = "% in Total Nonfarm",
     main = "% in Total Nonfarm Employment",col=myColors,   screens = 1)
legend(x = "bottom", legend = c('USGOOD','SRVPRD'),
       lty = 1, col=myColors)



plot(x = EMP_MAN, xlab = "Year", ylab = "% in Manufacturing",
     main = "% in Total Manufacturing Employment",col=myColors,   screens = 1)
legend(x = "bottom", legend = c('Durable','NonDurable'),
       lty = 1, col=myColors)


write.table(EMP, file='EMP.xls')

myColors <- c("red", "darkblue","brown","yellow","darkred","blue","pink","green","violet","black")

plot(x = last(EMP, "30 years"), xlab = "Year", ylab = "Index",
     main = "CPI", col=myColors,  screens = 1)
legend(x = "topleft", legend = c('PAYEMS','USGOOD','SRVPRD','CES0800000001','CES1021100001',
                                 'USCONS','MANEMP','DMANEMP','NDMANEMP','USTPU',
                                 'USWTRADE','USTRADE','CES4300000001','CES4422000001',
                                 'USINFO','USFIRE','USPBS','USEHS','USLAH',
                                 'USSERV','USGOVT'),
       lty = 1, col=myColors)


Diff_EMP <- (EMP/lag(EMP)-1)  
Diff_EMP[1,] <- 0

Diff_EMP12<-diff(EMP,lag =12)

write.table(Diff_EMP, file='Diff_EMP.xls')

tail(Diff_EMP12)



# Retail Trade
par(mfrow=c(3,1))
plot(USTRADE, main="Retail Trade", col="black")
barplot(Diff_EMP12$USTRADE, main="Changes from previous year ", col="red")
barplot(annualReturn(USTRADE), main=" Annual Changes ", col="blue")



# Financial Activities
par(mfrow=c(3,1))
plot(USFIRE, main="Financial Activities", col="black")
barplot(Diff_EMP12$USFIRE, main="Changes from previous year ", col="red")
barplot(annualReturn(USFIRE), main=" Annual Changes ", col="blue")


# Professional and Business Services
par(mfrow=c(3,1))
plot(USPBS, main="Professional and Business Services", col="black")
barplot(Diff_EMP12$USPBS, main="Changes from previous year ", col="red")
barplot(annualReturn(USPBS), main=" Annual Changes ", col="blue")


# Education and Health Services
par(mfrow=c(3,1))
plot(USEHS, main="Education and Health Services", col="black")
barplot(Diff_EMP12$USEHS, main="Changes from previous year ", col="red")
barplot(annualReturn(USEHS), main=" Annual Changes ", col="blue")


# Leisure and Hospitality
par(mfrow=c(3,1))
plot(USLAH, main="Leisure and Hospitality", col="black")
barplot(Diff_EMP12$USLAH, main="Changes from previous year ", col="red")
barplot(annualReturn(USLAH), main=" Annual Changes ", col="blue")


# Other Services
par(mfrow=c(3,1))
plot(USSERV, main="Other Services", col="black")
barplot(Diff_EMP12$USSERV, main="Changes from previous year ", col="red")
barplot(annualReturn(USSERV), main=" Annual Changes ", col="blue")



# US Government
par(mfrow=c(3,1))
plot(USGOVT, main="Government", col="black")
barplot(Diff_EMP12$USGOVT, main="Changes from previous year ", col="red")
barplot(annualReturn(USGOVT), main=" Annual Changes ", col="blue")




# Wholesale Trade
par(mfrow=c(3,1))
plot(USWTRADE, main="Wholesale Trade", col="black")
barplot(Diff_EMP12$USWTRADE, main="Changes from previous year ", col="red")
barplot(annualReturn(USWTRADE), main=" Annual Changes ", col="blue")



# Mining and Logging: Oil and Gas Extraction
par(mfrow=c(3,1))
plot(CES1021100001, main="Mining and Logging: Oil and Gas Extraction", col="black")
barplot(Diff_EMP12$CES1021100001, main="Changes from previous year ", col="red")
barplot(annualReturn(CES1021100001), main=" Annual Changes ", col="blue")