19

I'm still pretty new to R and AI / ML techniques. I would like to use a neural net for prediction, and since I'm new I would just like to see if this is how it should be done.

As a test case, I'm predicting values of sin(), based on 2 previous values. For training I create a data frame withy = sin(x), x1 = sin(x-1), x2 = sin(x-2), then use the formula y ~ x1 + x2.

It seems to work, but I am just wondering if this is the right way to do it, or if there is a more idiomatic way.

This is the code:

require(quantmod) #for Lag()
requre(nnet)
x <- seq(0, 20, 0.1)
y <- sin(x)
te <- data.frame(y, Lag(y), Lag(y,2))
names(te) <- c("y", "x1", "x2")
p <- nnet(y ~ x1 + x2, data=te, linout=TRUE, size=10)
ps <- predict(p, x1=y)
plot(y, type="l")
lines(ps, col=2)

Thanks

[edit]

Is this better for the predict call?

t2 <- data.frame(sin(x), Lag(sin(x)))
names(t2) <- c("x1", "x2")
vv <- predict(p, t2)
plot(vv)

I guess I'd like to see that the nnet is actually working by looking at its predictions (which should approximate a sin wave.)

Rob
  • 5,223
  • 5
  • 41
  • 62
dizzy
  • 381
  • 1
  • 2
  • 10
  • The `predict()` call looks suspicious. Aren't you just predicting 'y' with 'y'? On the other hand it might be failing to actually be supplying newdata, since it is not a dataframe. So you would just be "predicting" with the lagged values in 'te'. You might look at `expand` to avoid needing "pkg:quantmod" – IRTFM Oct 12 '11 at 17:16
  • i added a better example for predicting, do you think that would be actually using the nnet model? it seems to give a sin when plotted – dizzy Oct 12 '11 at 17:34
  • You can check by plotting the original on the same scale as the predicted: `plot(x, vv); lines(x, y)` and you see there is a lag (which it seems you would expect.) – IRTFM Oct 12 '11 at 18:32
  • An example in [here](http://www.parallelr.com/r-deep-neural-network-from-scratch/) about R + DNN for better understand. – Patric Feb 22 '16 at 04:02

1 Answers1

48

I really like the caret package, as it provides a nice, unified interface to a variety of models, such as nnet. Furthermore, it automatically tunes hyperparameters (such as size and decay) using cross-validation or bootstrap re-sampling. The downside is that all this re-sampling takes some time.

#Load Packages
require(quantmod) #for Lag()
require(nnet)
require(caret)

#Make toy dataset
y <- sin(seq(0, 20, 0.1))
te <- data.frame(y, x1=Lag(y), x2=Lag(y,2))
names(te) <- c("y", "x1", "x2")

#Fit model
model <- train(y ~ x1 + x2, te, method='nnet', linout=TRUE, trace = FALSE,
                #Grid of tuning parameters to try:
                tuneGrid=expand.grid(.size=c(1,5,10),.decay=c(0,0.001,0.1))) 
ps <- predict(model, te)

#Examine results
model
plot(y)
lines(ps, col=2)

It also predicts on the proper scale, so you can directly compare results. If you are interested in neural networks, you should also take a look at the neuralnet and RSNNS packages. caret can currently tune nnet and neuralnet models, but does not yet have an interface for RSNNS.

/edit: caret now has an interface for RSNNS. It turns out if you email the package maintainer and ask that a model be added to caret he'll usually do it!

/edit: caret also now supports Bayesian regularization for feed-forward neural networks from the brnn package. Furthermore, caret now also makes it much easier to specify your own custom models, to interface with any neural network package you like!

Zach
  • 29,791
  • 35
  • 142
  • 201
  • 5
    thanks very much, that looks great. Sorry for the delay in response somehow i missed the notification of a reply – dizzy Dec 09 '11 at 09:55
  • 4
    + Nice example. Your answer is first hit for search on '[r] nnet predict'. – IRTFM Jan 23 '13 at 19:56