2014년 3월 12일 수요일

ar2kospi

library(quantmod)

library(fGarch)

URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KS11"

dat <- read.csv(URL)

dat$Date <- as.Date(dat$Date, "%Y-%m-%d")

odat<-dat[c(1:100),]

attach(odat)

x<-odat[order(Date),]

fit <- arima(diff(log(x$Close)), order=c(2,0,0))

sim <- diff(log(abs(x$Close)))[98]

sim <- c(sim, diff(log(abs(x$Close)))[99])

for (t in 3:20) {

    sim[t] <- fit$coef[2]*sim[t-2]+ fit$coef[1]*sim[t-1]

}


dev.new()

par(mfrow=c(3,1))

par(mar=c(3,3,1,2))

plot(x$Close, type='l', xlim=c(0, 120), xaxt='n',

    panel.first=grid(col = "gray", lty = "dotted"), pch=20)

t <- c(101:120)

points(x$Close, pch = 20, col = "red")

ye <- x$Close[100]

err <- rnorm(20, 0, sqrt(sd(x$Close[length(x$Close)-50:length(x$Close)])))

for (j in 2:20) {

ye[j] <- exp(sim[j-1])*ye[j-1]+err[j]

}

ye

points(t, ye, col='red', pch=20, type='b')

##########################################################

library(forecast)

fit <- auto.arima(x$Close[1:80])
#library(dataframes2xls)
#write.xls(forecast(fit, h=20), "c:/mydata.xls")

write.table(x, "c:/kospi.txt")

plot(forecast(fit, h=20))




댓글 없음:

댓글 쓰기