Prediction

Let's predict the medv values for the test data and store the results in pred using the following code:

# Prediction
pred <- model %>% predict(test)
cbind(pred[1:10], testtarget[1:10])

OUTPUT
[,1] [,2]
[1,] 33.18942 36.2
[2,] 18.17827 20.4
[3,] 17.89587 19.9
[4,] 13.07977 13.9
[5,] 14.17268 14.8
[6,] 19.09264 18.4
[7,] 19.81316 18.9
[8,] 21.00356 24.7
[9,] 30.50263 30.8
[10,] 19.75816 19.4

We can take a look at the first 10 predicted and actual values using the cbind function. The first column in the output shows the predicted values based on the model and the second column shows the actual values. We can make the following observations from the output:

  • The prediction for the first sample in the test data is about 33.19 and the actual value is 36.2. The model underestimates the response by about 3 points.
  • For the second sample, the model underestimates the response by over 2 points.
  • For the tenth sample, the predicted and actual values are very close.
  • For the sixth sample, the model overestimates the response.

To get an overall picture of the prediction performance, we can develop a scatter plot of the predicted versus the actual values. We will use the following code:

plot(testtarget, pred,
xlab = 'Actual',
ylab = 'Prediction')
abline(a=0,b=1)

The scatter plot shows the predicted versus the actual response values based on the test data:

From the preceding graph, we can see the overall performance of the prediction model. The relationship between the actual and predicted values is positive and approximately linear. Although we can see that the model has decent performance, clearly there is scope for further improvement that makes data points closer to the ideal line that has zero intercepts and a slope of 1. We will further explore making improvements to the model by developing a deeper neural network model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset