Skip to content

Instantly share code, notes, and snippets.

@caub
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caub/9156637 to your computer and use it in GitHub Desktop.
Save caub/9156637 to your computer and use it in GitHub Desktop.
require(nnet)
require(caret)
y = read.csv('http://www-psych.stanford.edu/~andreas/Time-Series/SantaFe/A.dat', header=F)
y2 = read.csv('http://www-psych.stanford.edu/~andreas/Time-Series/SantaFe/A.cont', header=F)
k = 40
n=100
y = y$V1/256
y2 = y2$V1/256
dat = sapply(1:k, function(a) c(rep(NA,a),y[1:(length(y)-a)]) )
model <- train(dat,
y,
method='nnet',
linout=TRUE,
trace = FALSE)
ps <- predict(model, dat)
plot(seq(nrow(dat)), ps, type='l', col='blue')
# reinforced learning
nr = 10
for (i in 1:nr){
dat[1:(nrow(dat)-i),] = dat1[2:(nrow(dat)-i+1),] # shift up, overwrite oldest entry
dat[1:(nrow(dat)-i),1] = model$finalModel$fitted.values[2:(nrow(dat)-i+1)] # put newest predictions
y[1:(nrow(y1)-i),] = y[2:(nrow(y1)-i+1),,drop=F] # drop 1st row of older targets
model2 <- train(dat,
y,
method='nnet',
Wts = model$finalModel$weights,
linout=TRUE,
trace = FALSE)
print('if nmse of model2 increase stop and return model')
}
x = matrix(rev(y[(length(y)-k+1):length(y)]), nrow=1)
pred = rep(0, n)
for (i in seq(n)){
ps <- predict(model, x)
x[1,][2:k]=x[1,][1:(k-1)]
x[1,1] = pred[i] =ps
}
plot(seq(n), pred, type='l', col='blue')
lines(y2[1:n])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment