SHARE
TWEET

Untitled

a guest Feb 26th, 2020 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### question 4 hint ####
  2. library(MASS)
  3. library(class)
  4. Boston$medv01 <- ifelse(Boston$medv > median(Boston$medv),1,0)
  5. names(Boston)
  6.  
  7. # Choice of subsets
  8. plot(Boston)
  9.  
  10. #subset 1: "lstat","rm"
  11. #subset 2: "lstat", "rm","age","rad"
  12. #subset 3: all predictors
  13.  
  14. Boston$medv <- NULL
  15. names(Boston)
  16.  
  17. ### try three values of K #######
  18. ##### K=1 #######
  19. x.train <- Boston[,c("lstat","rm")]
  20. y.train <- Boston[,"medv01"]
  21. test.err <- mean(knn.cv(x.train, y.train, k=1) !=y.train)
  22. test.err
  23.  
  24. x.train <- Boston[,c("lstat", "rm","age","rad")]
  25. y.train <- Boston[,14]
  26. test.err <- mean(knn.cv(x.train, y.train, k=1) !=y.train)
  27. test.err
  28.  
  29. x.train <- Boston[,-14]### using all the predictors
  30. y.train <- Boston[,14]
  31. test.err <- mean(knn.cv(x.train, y.train, k=1) !=y.train)
  32. test.err
  33.  
  34. x.train <- Boston[,c("lstat","rm")]
  35. y.train <- Boston[,"medv01"]
  36. test.err <- mean(knn.cv(x.train, y.train, k=3) !=y.train)
  37. test.err
  38.  
  39. x.train <- Boston[,c("lstat", "rm","age","rad")]
  40. y.train <- Boston[,14]
  41. test.err <- mean(knn.cv(x.train, y.train, k=3) !=y.train)
  42. test.err
  43.  
  44. x.train <- Boston[,-14]### using all the predictors
  45. y.train <- Boston[,14]
  46. test.err <- mean(knn.cv(x.train, y.train, k=3) !=y.train)
  47. test.err
  48.  
  49. x.train <- Boston[,c("lstat","rm")]
  50. y.train <- Boston[,"medv01"]
  51. test.err <- mean(knn.cv(x.train, y.train, k=10) !=y.train)
  52. test.err
  53.  
  54. x.train <- Boston[,c("lstat", "rm","age","rad")]
  55. y.train <- Boston[,14]
  56. test.err <- mean(knn.cv(x.train, y.train, k=10) !=y.train)
  57. test.err
  58.  
  59. x.train <- Boston[,-14]### using all the predictors
  60. y.train <- Boston[,14]
  61. test.err <- mean(knn.cv(x.train, y.train, k=10) !=y.train)
  62. test.err
  63.  
  64. ###SCALED DATA####
  65. X.train <- Boston[,c("lstat","rm")]
  66. X.train.scaled <- scale(X.train)
  67. y.train <- Boston[,"medv01"]
  68. test.err <- mean(knn.cv(X.train.scaled, y.train, k=1) !=y.train)
  69. test.err
  70.  
  71. X.train <- Boston[,c("lstat", "rm","age","rad")]
  72. X.train.scaled <- scale(X.train)
  73. y.train.scaled <- Boston[,14]
  74. test.err <- mean(knn.cv(X.train.scaled, y.train, k=1) !=y.train.scaled)
  75. test.err
  76.  
  77. X.train <- Boston[,-14]### using all the predictors
  78. X.train.scaled <- scale(X.train)
  79. y.train.scaled <- Boston[,14]
  80. test.err <- mean(knn.cv(X.train.scaled, y.train, k=1) !=y.train)
  81. test.err
  82.  
  83. X.train <- Boston[,c("lstat","rm")]
  84. X.train.scaled <- scale(X.train)
  85. y.train <- Boston[,"medv01"]
  86. test.err <- mean(knn.cv(X.train.scaled, y.train, k=3) !=y.train)
  87. test.err
  88.  
  89. X.train <- Boston[,c("lstat", "rm","age","rad")]
  90. X.train.scaled <- scale(X.train)
  91. y.train.scaled <- Boston[,14]
  92. test.err <- mean(knn.cv(X.train.scaled, y.train, k=3) !=y.train.scaled)
  93. test.err
  94.  
  95. X.train <- Boston[,-14]### using all the predictors
  96. X.train.scaled <- scale(X.train)
  97. y.train.scaled <- Boston[,14]
  98. test.err <- mean(knn.cv(X.train.scaled, y.train, k=3) !=y.train)
  99. test.err
  100.  
  101. X.train <- Boston[,c("lstat","rm")]
  102. X.train.scaled <- scale(X.train)
  103. y.train <- Boston[,"medv01"]
  104. test.err <- mean(knn.cv(X.train.scaled, y.train, k=10) !=y.train)
  105. test.err
  106.  
  107. X.train <- Boston[,c("lstat", "rm","age","rad")]
  108. X.train.scaled <- scale(X.train)
  109. y.train.scaled <- Boston[,14]
  110. test.err <- mean(knn.cv(X.train.scaled, y.train, k=10) !=y.train.scaled)
  111. test.err
  112.  
  113. X.train <- Boston[,-14]### using all the predictors
  114. X.train.scaled <- scale(X.train)
  115. y.train.scaled <- Boston[,14]
  116. test.err <- mean(knn.cv(X.train.scaled, y.train, k=10) !=y.train)
  117. test.err
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top