Question
2) This question should be answered using the Marketing data set, which is provided on Blackboard. Consider observations 1 to 120 as training data and
2) This question should be answered using the Marketing data set, which is provided on Blackboard.
Consider observations 1 to 120 as training data and rest of them as testing data.
Use the training data set to perform a k-nearest neighbor model with pop_density as
the response and rest of the variables as predictors (google_adwords. Facebook,
twitter, marketing_total, revenues, employees). Where choose k as 5.
Calculate the training error of k-nearest neighbor method by using R.
Calculate the testing error of k-nearest neighbor method by using R.
Observation | google_adwords | marketing_total | revenues | employees | pop_density | ||
1 | 65.66 | 47.86 | 52.46 | 165.98 | 39.26 | 5 | High |
2 | 39.1 | 55.2 | 77.4 | 171.7 | 38.9 | 7 | Medium |
3 | 174.81 | 52.01 | 68.01 | 294.83 | 49.51 | 11 | Medium |
4 | 34.36 | 61.96 | 86.86 | 183.18 | 40.56 | 7 | High |
5 | 78.21 | 40.91 | 30.41 | 149.53 | 40.21 | 9 | Low |
6 | 34.19 | 15.09 | 12.79 | 62.07 | 38.09 | 3 | High |
7 | 225.71 | 15.91 | 33.31 | 274.93 | 44.21 | 10 | Low |
8 | 90.03 | 17.13 | 34.33 | 141.49 | 40.23 | 6 | High |
9 | 238.4 | 35.1 | 13.9 | 287.4 | 48.8 | 6 | Medium |
10 | 43.53 | 42.23 | 71.83 | 157.59 | 36.63 | 4 | Low |
11 | 118.24 | 15.74 | 14.14 | 148.12 | 38.14 | 7 | Medium |
12 | 224.64 | 40.84 | 52.74 | 318.22 | 47.24 | 9 | High |
13 | 219.51 | 59.21 | 63.21 | 341.93 | 54.21 | 10 | High |
14 | 89.79 | 45.99 | 122.19 | 257.97 | 42.19 | 6 | Low |
15 | 306.09 | 51.69 | 66.69 | 424.47 | 56.79 | 12 | Medium |
16 | 91.98 | 30.68 | 27.28 | 149.94 | 41.78 | 6 | High |
17 | 242.72 | 39.42 | 63.92 | 346.06 | 50.02 | 10 | Medium |
18 | 263.64 | 18.74 | 35.94 | 318.32 | 46.44 | 6 | Low |
19 | 35.16 | 25.26 | 57.76 | 118.18 | 35.26 | 5 | Low |
20 | 253.04 | 29.04 | 37.14 | 319.22 | 47.94 | 10 | High |
21 | 88.14 | 25.84 | 30.34 | 144.32 | 43.24 | 6 | High |
22 | 163.68 | 37.48 | 19.58 | 220.74 | 43.48 | 6 | Low |
23 | 263.96 | 27.96 | 32.96 | 324.88 | 47.46 | 9 | Medium |
24 | 268.98 | 34.68 | 29.28 | 332.94 | 46.78 | 9 | Low |
25 | 314.07 | 36.87 | 50.57 | 401.51 | 50.27 | 9 | Low |
26 | 134.9 | 26.8 | 46.8 | 208.5 | 41.6 | 8 | High |
27 | 284.99 | 26.79 | 5.89 | 317.67 | 44.49 | 10 | Medium |
28 | 117.68 | 10.78 | 15.58 | 144.04 | 39.18 | 3 | Low |
29 | 316.09 | 16.89 | 20.09 | 353.07 | 45.89 | 10 | Medium |
30 | 288.58 | 52.88 | 12.88 | 354.34 | 54.78 | 10 | Low |
31 | 97.37 | 59.47 | 54.57 | 211.41 | 45.07 | 10 | Medium |
32 | 66.6 | 37.6 | 44.8 | 149 | 41.3 | 7 | High |
33 | 250.75 | 47.85 | 40.95 | 339.55 | 51.95 | 12 | Medium |
34 | 223.1 | 30.3 | 38.4 | 291.8 | 44.9 | 6 | High |
35 | 202.09 | 45.89 | 49.99 | 297.97 | 49.89 | 9 | Medium |
36 | 317.58 | 39.08 | 11.98 | 368.64 | 52.38 | 9 | High |
37 | 231.76 | 20.66 | 37.46 | 289.88 | 45.46 | 8 | High |
38 | 45.16 | 33.16 | 49.56 | 127.88 | 36.26 | 6 | Medium |
39 | 199.47 | 34.27 | 42.07 | 275.81 | 46.97 | 8 | Low |
40 | 262.08 | 51.08 | 26.88 | 340.04 | 53.08 | 9 | Low |
41 | 224.85 | 15.55 | 45.85 | 286.25 | 44.15 | 9 | Low |
42 | 124.23 | 20.83 | 13.63 | 158.69 | 42.23 | 8 | Medium |
43 | 241.18 | 53.88 | 50.58 | 345.64 | 55.08 | 9 | Low |
44 | 205.77 | 56.77 | 68.07 | 330.61 | 52.07 | 10 | Medium |
45 | 285.71 | 39.21 | 25.11 | 350.03 | 50.91 | 10 | Low |
46 | 32.13 | 40.33 | 52.43 | 124.89 | 38.03 | 6 | Low |
47 | 155.66 | 26.06 | 22.26 | 203.98 | 40.36 | 9 | High |
48 | 233.44 | 59.64 | 46.54 | 339.62 | 54.14 | 10 | Low |
49 | 235.13 | 41.33 | 19.93 | 296.39 | 50.53 | 12 | High |
50 | 77.65 | 13.55 | 31.75 | 122.95 | 39.95 | 7 | Medium |
51 | 283.94 | 52.74 | 63.54 | 400.22 | 54.54 | 10 | Medium |
52 | 128.03 | 42.33 | 19.93 | 190.29 | 47.03 | 7 | High |
53 | 153.47 | 52.57 | 37.47 | 243.51 | 48.07 | 9 | Low |
54 | 90.01 | 17.71 | 8.11 | 115.83 | 38.01 | 4 | Low |
55 | 51.29 | 31.79 | 8.19 | 91.27 | 36.99 | 4 | Medium |
56 | 159.3 | 21.9 | 16.4 | 197.6 | 41.1 | 9 | Medium |
57 | 258.91 | 36.41 | 18.71 | 314.03 | 48.11 | 10 | High |
58 | 239.73 | 54.23 | 36.33 | 330.29 | 52.93 | 11 | Low |
59 | 133.73 | 25.63 | 41.83 | 201.19 | 44.03 | 8 | Medium |
60 | 48.95 | 42.55 | 27.65 | 119.15 | 38.65 | 3 | Low |
61 | 155.09 | 18.79 | 43.19 | 217.07 | 44.39 | 8 | High |
62 | 234.76 | 33.36 | 20.66 | 288.78 | 46.06 | 8 | High |
63 | 50.01 | 11.51 | 29.41 | 90.93 | 37.11 | 3 | Low |
64 | 142.13 | 37.53 | 22.03 | 201.69 | 43.53 | 10 | High |
65 | 29.26 | 41.16 | 19.46 | 89.88 | 36.86 | 6 | High |
66 | 137.11 | 16.21 | 30.41 | 183.73 | 39.81 | 3 | Medium |
67 | 262.45 | 14.15 | 45.75 | 322.35 | 42.65 | 7 | Medium |
68 | 99.96 | 32.36 | 43.36 | 175.68 | 43.66 | 7 | Low |
69 | 88.29 | 51.79 | 41.69 | 181.77 | 41.19 | 7 | Low |
70 | 238.93 | 55.83 | 45.43 | 340.19 | 54.83 | 11 | Medium |
71 | 214.87 | 27.47 | 73.57 | 315.91 | 44.57 | 6 | Medium |
72 | 101.48 | 40.08 | 27.38 | 168.94 | 44.88 | 7 | High |
73 | 132.43 | 49.73 | 71.13 | 253.29 | 45.43 | 8 | High |
74 | 109.94 | 34.54 | 81.24 | 225.72 | 42.24 | 6 | Low |
75 | 132.43 | 57.83 | 60.23 | 250.49 | 47.03 | 10 | High |
76 | 159.85 | 17.85 | 21.05 | 198.75 | 44.45 | 8 | Medium |
77 | 243.05 | 46.25 | 70.55 | 359.85 | 52.45 | 10 | Medium |
78 | 272.93 | 45.93 | 80.53 | 399.39 | 51.93 | 9 | Medium |
79 | 132.14 | 26.14 | 21.84 | 180.12 | 43.94 | 10 | High |
80 | 189.32 | 45.02 | 65.12 | 299.46 | 50.62 | 12 | Medium |
81 | 219.94 | 13.24 | 14.44 | 247.62 | 41.74 | 8 | Low |
82 | 313.99 | 53.99 | 61.69 | 429.67 | 57.39 | 12 | Low |
83 | 157.3 | 51.2 | 54.2 | 262.7 | 47 | 7 | Low |
84 | 243.98 | 13.28 | 57.58 | 314.84 | 40.98 | 7 | Medium |
85 | 321 | 48.3 | 111.7 | 481 | 56.1 | 12 | High |
86 | 300.92 | 18.22 | 28.32 | 347.46 | 43.22 | 9 | Medium |
87 | 262.48 | 45.98 | 15.78 | 324.24 | 52.68 | 9 | High |
88 | 158.05 | 53.95 | 65.35 | 277.35 | 47.05 | 8 | High |
89 | 46.02 | 19.42 | 36.92 | 102.36 | 35.92 | 6 | Low |
90 | 110.7 | 8 | 29.7 | 148.4 | 36.7 | 6 | High |
91 | 34.08 | 8.78 | 32.78 | 75.64 | 33.98 | 5 | Low |
92 | 275.11 | 34.01 | 11.41 | 320.53 | 47.21 | 7 | Low |
93 | 249.6 | 19.4 | 66.5 | 335.5 | 44.9 | 9 | High |
94 | 267.13 | 50.83 | 34.83 | 352.79 | 54.93 | 11 | Low |
95 | 200.45 | 27.55 | 13.35 | 241.35 | 46.55 | 6 | Low |
96 | 234.48 | 32.88 | 21.78 | 289.14 | 48.48 | 10 | Low |
97 | 100.69 | 56.69 | 43.19 | 200.57 | 44.79 | 10 | High |
98 | 97.27 | 44.57 | 61.07 | 202.91 | 42.47 | 8 | High |
99 | 164.18 | 26.68 | 36.78 | 227.64 | 44.88 | 8 | High |
100 | 99.93 | 11.73 | 24.53 | 136.19 | 40.63 | 8 | High |
101 | 43.28 | 27.28 | 32.38 | 102.94 | 38.18 | 5 | Medium |
102 | 163.07 | 35.97 | 54.17 | 253.21 | 44.97 | 10 | Low |
103 | 245.19 | 10.99 | 22.99 | 279.17 | 40.49 | 7 | Low |
104 | 149.35 | 48.25 | 24.85 | 222.45 | 49.15 | 10 | Medium |
105 | 253.23 | 43.43 | 84.13 | 380.79 | 51.13 | 10 | Medium |
106 | 107.99 | 19.99 | 32.89 | 160.87 | 39.09 | 5 | Medium |
107 | 28.01 | 46.51 | 57.01 | 131.53 | 34.51 | 6 | Low |
108 | 102.85 | 10.05 | 18.05 | 130.95 | 39.15 | 5 | High |
109 | 246.07 | 62.17 | 15.17 | 323.41 | 58.17 | 10 | Low |
110 | 83.09 | 22.89 | 52.79 | 158.77 | 40.89 | 10 | Low |
111 | 26.83 | 53.13 | 21.03 | 100.99 | 35.43 | 4 | Medium |
112 | 289.62 | 14.72 | 53.62 | 357.96 | 44.82 | 6 | Low |
113 | 30.2 | 36.4 | 10.1 | 76.7 | 35.2 | 3 | Low |
114 | 59.22 | 48.32 | 74.12 | 181.66 | 40.82 | 7 | Low |
115 | 68.64 | 54.74 | 15.04 | 138.42 | 39.64 | 6 | Medium |
116 | 44.99 | 45.79 | 14.89 | 105.67 | 36.59 | 4 | High |
117 | 298.01 | 40.61 | 70.21 | 408.83 | 52.81 | 12 | High |
118 | 63.02 | 33.32 | 26.72 | 123.06 | 37.32 | 7 | High |
119 | 207.32 | 53.72 | 10.32 | 271.36 | 50.82 | 12 | Medium |
120 | 97.18 | 28.18 | 22.88 | 148.24 | 42.38 | 8 | Low |
121 | 219.94 | 49.04 | 88.04 | 357.02 | 53.14 | 10 | High |
122 | 243.27 | 43.37 | 46.87 | 333.51 | 50.57 | 12 | Medium |
123 | 127.29 | 15.79 | 43.29 | 186.37 | 40.79 | 6 | Medium |
124 | 116.71 | 22.71 | 45.61 | 185.03 | 39.61 | 6 | Medium |
125 | 164.88 | 13.88 | 19.78 | 198.54 | 42.58 | 6 | Low |
126 | 262.58 | 17.18 | 17.38 | 297.14 | 43.38 | 8 | Medium |
127 | 266.08 | 59.28 | 53.38 | 378.74 | 55.98 | 12 | High |
128 | 65.45 | 33.95 | 27.55 | 126.95 | 38.55 | 3 | High |
129 | 301.6 | 22.2 | 44.1 | 367.9 | 44.7 | 6 | High |
130 | 144.47 | 19.27 | 58.37 | 222.11 | 42.77 | 6 | High |
131 | 194.62 | 50.42 | 47.22 | 292.26 | 50.02 | 10 | Medium |
132 | 207.64 | 28.34 | 15.54 | 251.52 | 43.14 | 6 | Low |
133 | 23.65 | 18.55 | 11.45 | 53.65 | 30.45 | 3 | Low |
134 | 117.7 | 54.7 | 60.5 | 232.9 | 46.8 | 10 | Low |
135 | 175.6 | 14.5 | 36.3 | 226.4 | 43.6 | 7 | High |
136 | 35.19 | 47.79 | 54.89 | 137.87 | 38.49 | 5 | Medium |
137 | 154.93 | 29.03 | 44.03 | 227.99 | 43.83 | 7 | Low |
138 | 195.48 | 28.48 | 39.88 | 263.84 | 45.08 | 10 | Low |
139 | 111.9 | 49.4 | 61.7 | 223 | 47.2 | 7 | High |
140 | 186.35 | 47.05 | 16.45 | 249.85 | 48.55 | 8 | Low |
141 | 141.28 | 26.18 | 15.68 | 183.14 | 43.68 | 7 | Low |
142 | 258.01 | 14.31 | 94.51 | 366.83 | 43.11 | 9 | Low |
143 | 38.87 | 45.97 | 28.77 | 113.61 | 36.67 | 4 | Medium |
144 | 227.91 | 13.71 | 26.71 | 268.33 | 41.01 | 10 | Low |
145 | 239.81 | 35.41 | 68.21 | 343.43 | 49.21 | 10 | Low |
146 | 306.77 | 20.47 | 15.07 | 342.31 | 45.17 | 10 | Medium |
147 | 185.03 | 28.83 | 54.13 | 267.99 | 42.73 | 7 | Low |
148 | 44.13 | 32.03 | 27.73 | 103.89 | 39.83 | 4 | Medium |
149 | 188.43 | 14.53 | 19.03 | 221.99 | 39.43 | 7 | Low |
150 | 247.75 | 16.15 | 24.65 | 288.55 | 44.55 | 7 | Low |
151 | 300.5 | 59.9 | 51.6 | 412 | 58.3 | 9 | High |
152 | 193.4 | 18.4 | 44.6 | 256.4 | 42.6 | 9 | Low |
153 | 298.3 | 11.3 | 31.5 | 341.1 | 41.1 | 10 | High |
154 | 188.07 | 19.87 | 26.27 | 234.21 | 42.77 | 6 | High |
155 | 179.4 | 12.8 | 17.3 | 209.5 | 41 | 9 | Low |
156 | 239.07 | 13.37 | 34.17 | 286.61 | 40.47 | 10 | Medium |
157 | 79.21 | 16.11 | 38.91 | 134.23 | 39.41 | 4 | Low |
158 | 307.43 | 50.23 | 77.83 | 435.49 | 53.73 | 11 | Low |
159 | 275.04 | 29.94 | 37.44 | 342.42 | 46.54 | 9 | Low |
160 | 225.79 | 53.29 | 26.59 | 305.67 | 51.09 | 10 | Medium |
161 | 160.79 | 10.79 | 34.09 | 205.67 | 39.29 | 4 | Medium |
162 | 216.67 | 41.67 | 29.97 | 288.31 | 50.57 | 12 | Medium |
163 | 308.42 | 23.72 | 12.32 | 344.46 | 46.02 | 9 | Low |
164 | 43.46 | 24.26 | 34.36 | 102.08 | 39.16 | 6 | Medium |
165 | 64.96 | 53.96 | 17.46 | 136.38 | 43.96 | 9 | High |
166 | 97.69 | 20.39 | 14.39 | 132.47 | 39.79 | 7 | High |
167 | 36.95 | 11.25 | 37.55 | 85.75 | 33.35 | 7 | Low |
168 | 188.45 | 51.05 | 11.45 | 250.95 | 48.95 | 9 | Low |
169 | 174.07 | 47.37 | 16.57 | 238.01 | 49.37 | 12 | High |
170 | 59.86 | 12.76 | 21.66 | 94.28 | 36.96 | 4 | Low |
171 | 117.91 | 16.01 | 18.01 | 151.93 | 41.11 | 7 | Low |
172 | 308.78 | 54.58 | 77.58 | 440.94 | 58.38 | 10 | Medium |
3) For this assignment, you need to use Smarket dataset which is available with ISLR library.
Support Vector Machine:
a. Using tune() function try different values for gamma and cost. Use the training data from part (1) (Consider that Direction shows the classes), (You can consider the code below).
tune.out=tune(svm, y~., data=dat, kernel="radial", ranges=list(cost=c(0.1,1,10,100,1000),gamma=c(0.5,1,2,3,4)))
b. After obtaining the optimized parameters gamma and cost from part .a, fit a support vector machine with radial kernel for the training data from part (1).
c. Find the support vectors and find their corresponding alpha values. (Make sure that they should be non-zero).
Need help with the R code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started