Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Machine Learning by (4.2k points)

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.)

1 Answer

+1 vote
by (6.8k points)

The caret package, 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. 

#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 conjointly predicts on the correct scale, therefore you'll directly compare results. If you're interested in neural networks, you must additionally take a glance at the neural net and RSNNS packages. the caret will presently tune nnet and neuralnet models but doesn't however have an interface for RSNNS.

caret now has an interface for RSS. It also now supports Bayesian regularization for feed-forward neural networks from the brnn package.

For more details on nnet, check out the Machine Learning Online Course by Intellipaat. Also, study the Machine Learning Algorithms for more broad research.

Browse Categories

...