Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- title: "A22 By Noah Perry and Andrew Hall"
- output:
- pdf_document: default
- html_notebook: default
- ---
- Build a neural network for the Manheim data with a range of hidden layers and decide which model gives the best fit. Compare your result with a general linear model.
- ```{r}
- library(tidyverse)
- library(Metrics)
- library(neuralnet)
- library(ggplot2)
- url<-"http://www.richardtwatson.com/data/manheim.csv"
- t<-read_csv(url)
- #Create general linear model
- lm <- lm(t$price~t$miles +t$model+t$sale)
- summary(lm)
- predict.lm <- round(predict(lm),0)
- MSE.lm <- rmse(t$price,predict.lm)
- # Recoding
- t$saleCode <- case_when(
- t$sale == 'Auction' ~1,
- t$sale == 'Online' ~2
- )
- t$modelCode <- case_when(
- t$model == 'X' ~1,
- t$model == 'Y' ~2,
- t$model == 'Z' ~3
- )
- t$sale <- NULL
- t$model <- NULL
- # Normalize data
- maxs <- apply(t, 2, max)
- mins <- apply(t, 2, min)
- n <- as.data.frame(scale(t, center = mins, scale = maxs - mins))
- #Create Neural Nets with 2:6 Hidden layers
- mseList <- list()
- for (x in c(2,3,4,5,6)) {
- # Build neural net
- set.seed(2)
- net <- neuralnet(price ~ miles +
- saleCode + modelCode, data = n, hidden
- = x, linear.output = T)
- pr.net <- compute(net, n[,2:4])
- # rescale
- predict.net <- pr.net$net.result*(max(t$price)-min(t$price))+min(t$price)
- mseList[x] <- rmse(t$price, predict.net)
- }
- #Graph with x-axis as number of layers, y-axis MSE
- mseDf <- data.frame(matrix(unlist(mseList)))
- mseDf$layers = c(2,3,4,5,6)
- ggplot(mseDf, aes(mseDf$layers)) + geom_point(aes(y=mseDf$matrix.unlist.mseList..)) +
- xlab('Layers') +
- ylab('Mean Squared Error')
- #Neural Net with 4 Hidden Layers results in lowest MSE, so compare with linear model
- #First, recreate Neural Net
- set.seed(2)
- net <- neuralnet(price ~ miles +
- saleCode + modelCode, data = n, hidden
- = 4, linear.output = T)
- pr.net <- compute(net, n[,2:4])
- # rescale
- predict.net <- pr.net$net.result*(max(t$price)-min(t$price))+min(t$price)
- MSE.net <- rmse(t$price, predict.net)
- #Graph results
- t <- t %>% mutate(diff = predict.lm - predict.net)
- ggplot(t, aes(x=price)) +
- geom_point(aes(y=diff, color='Prediction difference')) +
- geom_point(aes(y=predict.lm, color='Linear model')) +
- geom_point(aes(y=predict.net,color='Neural net')) +
- geom_abline(intercept = 0, slope = 1) +
- xlab('Actual price') +
- ylab('Predicted price')
- # Compare the two models' mean square error
- paste('MSEs for linear regression and neural net ',round(MSE.lm,
- 0),round(MSE.net,0))
- paste('Percent difference ', round(((MSE.net - MSE.lm)/MSE.lm*100),2))
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement