Advertisement
Guest User

A22

a guest
Apr 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. ---
  2. title: "A22 By Noah Perry and Andrew Hall"
  3. output:
  4. pdf_document: default
  5. html_notebook: default
  6. ---
  7.  
  8. 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.
  9.  
  10. ```{r}
  11. library(tidyverse)
  12. library(Metrics)
  13. library(neuralnet)
  14. library(ggplot2)
  15.  
  16. url<-"http://www.richardtwatson.com/data/manheim.csv"
  17. t<-read_csv(url)
  18.  
  19. #Create general linear model
  20. lm <- lm(t$price~t$miles +t$model+t$sale)
  21. summary(lm)
  22. predict.lm <- round(predict(lm),0)
  23. MSE.lm <- rmse(t$price,predict.lm)
  24.  
  25. # Recoding
  26. t$saleCode <- case_when(
  27. t$sale == 'Auction' ~1,
  28. t$sale == 'Online' ~2
  29. )
  30. t$modelCode <- case_when(
  31. t$model == 'X' ~1,
  32. t$model == 'Y' ~2,
  33. t$model == 'Z' ~3
  34. )
  35. t$sale <- NULL
  36. t$model <- NULL
  37.  
  38. # Normalize data
  39. maxs <- apply(t, 2, max)
  40. mins <- apply(t, 2, min)
  41. n <- as.data.frame(scale(t, center = mins, scale = maxs - mins))
  42.  
  43. #Create Neural Nets with 2:6 Hidden layers
  44. mseList <- list()
  45. for (x in c(2,3,4,5,6)) {
  46. # Build neural net
  47. set.seed(2)
  48. net <- neuralnet(price ~ miles +
  49. saleCode + modelCode, data = n, hidden
  50. = x, linear.output = T)
  51. pr.net <- compute(net, n[,2:4])
  52. # rescale
  53. predict.net <- pr.net$net.result*(max(t$price)-min(t$price))+min(t$price)
  54. mseList[x] <- rmse(t$price, predict.net)
  55. }
  56.  
  57. #Graph with x-axis as number of layers, y-axis MSE
  58. mseDf <- data.frame(matrix(unlist(mseList)))
  59. mseDf$layers = c(2,3,4,5,6)
  60. ggplot(mseDf, aes(mseDf$layers)) + geom_point(aes(y=mseDf$matrix.unlist.mseList..)) +
  61. xlab('Layers') +
  62. ylab('Mean Squared Error')
  63.  
  64. #Neural Net with 4 Hidden Layers results in lowest MSE, so compare with linear model
  65. #First, recreate Neural Net
  66. set.seed(2)
  67. net <- neuralnet(price ~ miles +
  68. saleCode + modelCode, data = n, hidden
  69. = 4, linear.output = T)
  70. pr.net <- compute(net, n[,2:4])
  71. # rescale
  72. predict.net <- pr.net$net.result*(max(t$price)-min(t$price))+min(t$price)
  73. MSE.net <- rmse(t$price, predict.net)
  74.  
  75. #Graph results
  76. t <- t %>% mutate(diff = predict.lm - predict.net)
  77. ggplot(t, aes(x=price)) +
  78. geom_point(aes(y=diff, color='Prediction difference')) +
  79. geom_point(aes(y=predict.lm, color='Linear model')) +
  80. geom_point(aes(y=predict.net,color='Neural net')) +
  81. geom_abline(intercept = 0, slope = 1) +
  82. xlab('Actual price') +
  83. ylab('Predicted price')
  84.  
  85. # Compare the two models' mean square error
  86. paste('MSEs for linear regression and neural net ',round(MSE.lm,
  87. 0),round(MSE.net,0))
  88. paste('Percent difference ', round(((MSE.net - MSE.lm)/MSE.lm*100),2))
  89.  
  90.  
  91. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement