# HighYieldSpreadAutoregressiveFit
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(xts)
names0<-load(file="casestudy_1_0.RData")
#apply(is.na(fred.data00),2,sum)
fred.data0.0<-na.omit(fred.data0)
dim(fred.data0.0)
## [1] 4399    7
Spread.HY=fred.data0.0[,"DBAA"]-fred.data0.0[,"DGS10"]

Spread.HY.1<-window(Spread.HY, 
                    start = as.Date("2010-01-01"),
                    end=as.Date("2017-08-28"))



plot((time(Spread.HY.1)), Spread.HY.1,col='black',type="l",
     xlab="Date", ylab="Spread",
     main="High-Yield Spread\nMoody's Baa  Yield minus US 10-Year Yield")
x=as.numeric(time(Spread.HY.1))
y=as.numeric(Spread.HY.1)

abline(h=mean(y),col='gray')

# 
plot((time(Spread.HY.1)), Spread.HY.1,col='black',type="l",
     xlab="Date", ylab="Spread",
     main="High-Yield Spread\nMoody's Baa  Yield minus US 10-Year Yield")
x=as.numeric(time(Spread.HY.1))
y=as.numeric(Spread.HY.1)

abline(h=mean(y),col='gray')

y.lag1<-c(NA,y[1:(length(y)-1)])
lmfitar1=lm(y~y.lag1)
lines(x[-1],lmfitar1$fitted.values,col='blue')

summary(lmfitar1)
## 
## Call:
## lm(formula = y ~ y.lag1)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.089554 -0.010525  0.000072  0.010821  0.139739 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.003604   0.004080   0.883    0.377    
## y.lag1      0.998613   0.001469 679.720   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0234 on 1910 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.9959, Adjusted R-squared:  0.9959 
## F-statistic: 4.62e+05 on 1 and 1910 DF,  p-value: < 2.2e-16
hist(lmfitar1$residuals,nclass=50)

acf(lmfitar1$residuals,type="partial")