This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE
parameter was added to the code chunk to prevent printing of the R code that generated the plot.
install.packages(“Quandl”)
library(“Quandl”) #(1)model identification
rcpg <- Quandl(“FRED/WCOILBRENTEU”, type=“zoo”)
summary(rcpg)
plot(rcpg)
str(rcpg)
head(rcpg)
tail(rcpg)
g=log(rcpg)
Y=diff(g) Y plot(Y)
acf(as.data.frame(Y),type=‘correlation’,lag=24)
acf(as.data.frame(Y),type=‘partial’,lag=24, main=“”)
m1 <- arima(y, order=c(1,0,0))
str(m1)
m1
tsdiag(m1,gof.lag=24) # we prefer MA(2) to MA(1) B1=arima(Y,order=c(0,0,2),method=“ML”) B1 str(B1)
B2=arima(Y,order=c(0,0,1),method=“ML”) B2 str(B2)
tsdiag(A,gof.lag=24) # estimate an AR(2) model m2 <- arima(Y, order=c(2,0,0)) m2 # diagnostics for the AR(2) model - tests for corelation 1- L-B, 2-B-p ,from L-B test we find that transformed data are not correlated(not significant) tsdiag(m2,gof.lag=24)
m3 <- arima(Y, order=c(3,0,0)) m3 # diagnostics for the AR(3) model,tests for corelation 1- L-B, 2-B-p ,from L-B test we find that transformed data are correlated( significant) tsdiag(m3,gof.lag=24)
m1=ar(Y,method=“mle”)
m1
m1$order
A=arima(Y,order=c(1,0,0),method=“ML”)
m2=arima(Y, order=c(2,0,0))
m2
m3=arima(Y, order=c(3,0,0))
m3
m3\(coef/sqrt(diag(m3\)var.coef)) # p values (1-pnorm(abs(m3\(coef)/sqrt(diag(m3\)var.coef))))*2
m <- ar(Y,method=“mle”) str(m) m m\(order # note that AIC prefers AR(3) to AR(2) m\)aic
BIC(m2) BIC(m3)
m2.LB.lag12 <- Box.test(m2\(residuals, lag=12, type="Ljung") str(m2.LB.lag12) m2.LB.lag12 1-pchisq(m2.LB.lag12\)statistic,10)
m2.LB.lag16 <- Box.test(m2\(residuals, lag=16, type="Ljung") m2.LB.lag16 1-pchisq(m2.LB.lag16\)statistic,14)
(1-0.1866+0.0215-0.0907)*0.0004
sqrt(m2$sigma2)
p1=c(1, -m2$coef[1:3])
roots=polyroot(p1)
roots
Mod(roots)
k=2*pi/acos(1.989104/2.354474)
k
ord=ar(Y,method=“mle”)
ord$aic
ord$order
m3=arima(Y,order=c(3,0,0))
m3
(1-0.1866+0.0215-0.0907)*mean(Y)
sqrt(m3$sigma2)
Box.test(m3$residuals,lag=12,type=“Ljung”)
pv=1-pchisq(9.9132,9)
pv