library(quantmod)
library(fGarch)
URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KQ11"
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:100),]
attach(odat)
x<-odat[order(Date),]
#kospiGarch <- garchFit(~arma(1, 2) + garch(1, 1), data=as.ts(x$Close))
fit <- arima(diff(log(x$Close)), order=c(1,0,1))
#fore <- predict(fit, n.ahead=10, doplot=F)
err <- rnorm(30, 0, sd(diff(log(x$Close))))
sim <- diff(log(abs(x$Close)))[99]
for (t in 2:30) {
sim[t] <- fit$coef[1]*sim[t-1] + fit$coef[2]*err[t-1]
}
dev.new()
par(mfrow=c(3,1))
par(mar=c(3,3,1,2))
x$newday <- as.Date(x$Date, "%m/%d/%Y")
plot(x$newday, x$Close, type='l', col='red',
panel.first=grid(col = "gray", lty = "dotted"), pch=20)
points(x$newday, x$Close, pch = 20, col = "red")
ye <- x$Close[100]
td<-x$newday[length(x$newday)]+1
for (j in 2:30) {
td[j] <- x$newday[length(x$newday)] + j
ye[j] <- exp(sim[j-1])*ye[j-1]
}
td<-as.Date(td)
ye
points(td, ye, col='red', pch=20, type='b')
##########################################################
library(forecast)
fit <- arima(x$Close, order=c(1,1,10))
fore <- forecast(fit, h=15)
plot(fore)
fore

댓글 없음:
댓글 쓰기