O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To use neural networks for regression, follow these steps:

  1. Load the nnet and caret packages:
> library(nnet) 
> library(caret) 
> library(devtools) 
  1. Read the data:
> bh <- read.csv("BostonHousing.csv")
  1. Partition the data:
> set.seed(1000) 
> t.idx <- createDataPartition(bh$MEDV, p=0.7, list=FALSE) 
  1. Find the range of the response variable to be able to scale it to [01]:
> summary(bh$MEDV) 
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.  
   5.00   17.02   21.20   22.53   25.00   50.00 
  1. Build the model:
> fit <- nnet(MEDV/50 ~ ., data=bh[t.idx,], size=6, decay = 0.1, maxit = 1000, linout = TRUE) 
  1. In preparation for plotting the network, get the code for the plotting function plot.nnet from fawda123's GitHub page. The following GitHub page ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required