#We have seen that we can fit an SVM with a non-linear kernel in order to perform classification using a non-linear decision boundary. We will now see that we can also obtain a non-linear decision boundary by performing logistic regression using non-linear transformations of the features.# (a) Generate a data set with n = 500 and p = 2, such that the observations belong to two classes with a quadratic decision boundary between them. For instance, you can do this as follows:

library(e1071)
library(ISLR)
set.seed(8)
x1 <- runif (500) - 0.5
x2 <- runif (500) - 0.5
y <- 1 * (x1^2 - x2^2 > 0)
data <- data.frame(x1 = x1, x2 = x2, y = y)
data
##               x1            x2 y
## 1   -0.033704759  0.3496110768 0
## 2   -0.292176683 -0.3276057940 0
## 3    0.299657958 -0.1714835337 1
## 4    0.151871318 -0.0484592929 1
## 5   -0.178490821 -0.4102651116 0
## 6    0.218927504 -0.1245771123 1
## 7   -0.209126613 -0.0648902298 1
## 8    0.432269813 -0.1156270974 1
## 9    0.269146954 -0.0983964619 1
## 10   0.144491137  0.3413480935 0
## 11  -0.042955111  0.2461652106 0
## 12  -0.410698989 -0.3076886791 1
## 13  -0.067608627  0.2402028048 0
## 14   0.044962116 -0.1856529445 0
## 15  -0.361775654 -0.3028729577 1
## 16   0.427812252 -0.3160181022 1
## 17  -0.498698279 -0.2656897318 1
## 18  -0.235541136 -0.2792671863 0
## 19  -0.223467753 -0.0548859420 1
## 20   0.021107042  0.4767564062 0
## 21  -0.276311076 -0.1352186513 1
## 22  -0.092075249 -0.1408523093 0
## 23   0.114875749  0.1583197801 0
## 24  -0.285026240  0.1457872002 1
## 25   0.163267976 -0.2062228571 0
## 26   0.477502698 -0.2830034406 1
## 27  -0.402251729 -0.4872578140 0
## 28   0.111932659 -0.3294314349 0
## 29   0.027618650  0.4061845783 0
## 30   0.317365612 -0.1528149035 1
## 31  -0.291902003  0.1634451263 1
## 32   0.123149050  0.3779978063 0
## 33   0.434587061 -0.2711135258 1
## 34  -0.317070804  0.2258987043 1
## 35  -0.107038612  0.0973711251 1
## 36  -0.157000408 -0.4395158442 0
## 37   0.440413465 -0.2035941209 1
## 38   0.448384064 -0.0953030258 1
## 39  -0.093807312 -0.4155185651 0
## 40   0.243738999  0.0912973539 1
## 41   0.400275486 -0.1593844797 1
## 42  -0.211283883 -0.1249640137 1
## 43  -0.003785761  0.3581645342 0
## 44   0.375936153  0.1614589647 1
## 45  -0.155452225  0.3274645989 0
## 46  -0.236427950  0.2547324619 0
## 47   0.008770293 -0.2455639350 0
## 48  -0.323493572  0.4781293569 0
## 49   0.459312873  0.1783985279 1
## 50  -0.395544968 -0.2715255506 1
## 51  -0.365890677 -0.2977178167 1
## 52  -0.432869141 -0.0688199860 1
## 53  -0.355539015 -0.4511501081 0
## 54  -0.154792103 -0.1770957622 0
## 55   0.474481072 -0.0959767804 1
## 56  -0.219168865  0.2220016026 0
## 57   0.226648105 -0.2687782750 0
## 58  -0.144380187  0.2325976230 0
## 59  -0.478339898  0.1133457937 1
## 60   0.361738985  0.3606056347 1
## 61   0.434053075 -0.4941300105 0
## 62   0.497600239  0.1138232714 1
## 63   0.332404600  0.2120012729 1
## 64   0.148552976  0.0947758714 1
## 65  -0.439894839 -0.1445974836 1
## 66  -0.447468330 -0.0305191348 1
## 67  -0.280428844 -0.0972945909 1
## 68  -0.368067365 -0.3412816341 1
## 69   0.396305571 -0.3573600519 1
## 70  -0.185795124 -0.0030072515 1
## 71   0.165840341  0.0748985829 1
## 72  -0.003405425  0.2898504820 0
## 73   0.300828293 -0.2580679008 1
## 74   0.171971040  0.4764609099 0
## 75  -0.257771026 -0.0218797275 1
## 76   0.114041755 -0.4294062892 0
## 77  -0.025858179  0.0018846274 1
## 78   0.420169606 -0.3986617532 1
## 79   0.181173794  0.1962846101 0
## 80   0.370288668 -0.0813195864 1
## 81   0.141884527 -0.0917631474 1
## 82   0.177956100  0.0904020965 1
## 83  -0.230575986 -0.1928002296 1
## 84   0.314632795  0.4634852859 0
## 85   0.090545838  0.3241112411 0
## 86  -0.221175104  0.1659420861 1
## 87  -0.103361186  0.3069421060 0
## 88  -0.200380313 -0.2674795848 0
## 89   0.229915018 -0.4468529569 0
## 90  -0.244502602  0.3203237781 0
## 91  -0.474862252 -0.1608758823 1
## 92   0.215213873  0.1225695950 1
## 93  -0.246497200  0.1163389194 1
## 94   0.085201133 -0.0753929080 1
## 95   0.111713393 -0.1919600810 0
## 96  -0.281607979  0.1765860578 1
## 97   0.105926640  0.0668642642 1
## 98  -0.030459523  0.3840332110 0
## 99  -0.238947748 -0.4311625178 0
## 100 -0.125311820  0.4910956952 0
## 101  0.359918064 -0.3251482961 1
## 102 -0.362240815 -0.0004782074 1
## 103 -0.142498916 -0.4063022707 0
## 104 -0.075511323 -0.3299994951 0
## 105  0.222191907 -0.3005318998 0
## 106  0.251640060  0.1116656624 1
## 107 -0.038277817 -0.0715167741 0
## 108  0.152123173  0.2516582580 0
## 109  0.334352253 -0.4296941853 0
## 110  0.309392158  0.3290803796 0
## 111  0.218526216  0.1167114356 1
## 112  0.375862740  0.2110257992 1
## 113 -0.281471790  0.1856673537 1
## 114 -0.148605122  0.4043546629 0
## 115 -0.226027424  0.0272928469 1
## 116  0.054568195 -0.4685676023 0
## 117 -0.433104610  0.1136450854 1
## 118  0.398254090 -0.4608165103 0
## 119  0.274624449  0.4057083060 0
## 120  0.441403540 -0.4699748177 0
## 121  0.211336823 -0.1162217825 1
## 122 -0.162587036 -0.1208664074 1
## 123  0.037793794 -0.0257451746 1
## 124 -0.178911280 -0.0876848588 1
## 125 -0.462579813  0.2812088111 1
## 126 -0.468981805 -0.3472137789 1
## 127 -0.366240125 -0.3598421952 1
## 128 -0.377997977 -0.3429137175 1
## 129 -0.105437798  0.2231246661 0
## 130  0.472427536 -0.4477729048 1
## 131 -0.359182364  0.1280762998 1
## 132  0.152157268  0.4795650435 0
## 133 -0.188340433 -0.4891579477 0
## 134  0.348261682  0.4973006360 0
## 135  0.449413983 -0.1564996631 1
## 136  0.326355824 -0.4000704277 0
## 137  0.154234048  0.3698968031 0
## 138  0.046071119  0.3005869563 0
## 139 -0.444986673  0.3561300531 1
## 140 -0.393633881 -0.1587791364 1
## 141 -0.428905795 -0.3762398474 1
## 142 -0.454695113 -0.2709730736 1
## 143  0.350862076 -0.3301279538 1
## 144  0.480078327 -0.1113595511 1
## 145  0.283136817 -0.2396792595 1
## 146 -0.207435262  0.3442518620 0
## 147 -0.183353274 -0.0515947533 1
## 148  0.410727002  0.4179117680 0
## 149  0.037455202  0.1163197258 0
## 150  0.419608684  0.0775173681 1
## 151  0.225406670  0.2952856806 0
## 152  0.183747987 -0.4199080523 0
## 153 -0.498118043 -0.3746872155 1
## 154 -0.052246455 -0.1521693687 0
## 155  0.220287807  0.4435820004 0
## 156 -0.018653753  0.2904416646 0
## 157  0.491251252 -0.0528424515 1
## 158 -0.449035127 -0.0426366183 1
## 159 -0.240945175 -0.0473226043 1
## 160 -0.212725751  0.2946868299 0
## 161  0.096024017  0.0475313421 1
## 162  0.160544582  0.4287238512 0
## 163 -0.285088103  0.0767139820 1
## 164  0.236212345  0.1222699168 1
## 165 -0.362956586  0.4291476586 0
## 166 -0.138235219 -0.3719853708 0
## 167  0.479553212 -0.4846290515 0
## 168 -0.388322027 -0.4888483449 0
## 169 -0.453650943  0.4895473670 0
## 170  0.117866409  0.2915992269 0
## 171  0.387870944  0.3375864066 1
## 172 -0.403216552 -0.0942456138 1
## 173 -0.038129925  0.4345769295 0
## 174  0.467750097  0.3938310286 1
## 175 -0.329297922 -0.0177501631 1
## 176  0.426503518 -0.2175721722 1
## 177  0.226168413 -0.3756434929 0
## 178  0.310754160 -0.2303404417 1
## 179 -0.498713104  0.4891088638 1
## 180  0.360794513 -0.4469647030 0
## 181  0.279197161  0.2049431931 1
## 182 -0.105403917  0.3481158058 0
## 183  0.441669271 -0.0581803978 1
## 184 -0.187725691 -0.1699039869 1
## 185 -0.192418295 -0.3362640175 0
## 186  0.346405937  0.4899270949 0
## 187 -0.231675486 -0.4664626217 0
## 188 -0.102699908 -0.1923886365 0
## 189 -0.411721452  0.4692605906 0
## 190 -0.405954580 -0.2084584828 1
## 191  0.279079436 -0.4335831089 0
## 192  0.468376542 -0.0446381189 1
## 193  0.129099435  0.4897839902 0
## 194  0.160883251  0.1564952037 1
## 195 -0.017605855 -0.3954931675 0
## 196  0.262902434  0.3856119404 0
## 197 -0.238014847 -0.3260071464 0
## 198 -0.076836687  0.3508631564 0
## 199 -0.027617718 -0.2008268277 0
## 200  0.239727438  0.0840846812 1
## 201  0.116709977 -0.4584768671 0
## 202 -0.144021869 -0.3516595566 0
## 203 -0.471321061 -0.3596455867 1
## 204  0.162024961  0.2084479372 0
## 205 -0.450258543 -0.1442117291 1
## 206 -0.129766868  0.0227720791 1
## 207 -0.462331352  0.4219956892 1
## 208 -0.432659538 -0.0104482884 1
## 209  0.013937565 -0.3316020565 0
## 210 -0.161460311  0.2618308989 0
## 211 -0.189539837  0.1716856675 1
## 212 -0.374211809  0.4287190835 0
## 213  0.228255333  0.3212740845 0
## 214 -0.495413257 -0.0705458820 1
## 215 -0.142207211  0.0980937108 1
## 216 -0.006435806 -0.1824004566 0
## 217  0.066682536 -0.1018419962 0
## 218 -0.288203695  0.4679741000 0
## 219  0.214514713  0.4892932458 0
## 220  0.058362357  0.3695580994 0
## 221 -0.061857593  0.0249316976 1
## 222 -0.321413127  0.0099491272 1
## 223 -0.298980179  0.0913868675 1
## 224  0.073177419  0.1074039931 0
## 225  0.207815603  0.4354434421 0
## 226 -0.236595299 -0.4823721433 0
## 227 -0.361710978 -0.1910197402 1
## 228 -0.276679653  0.1600889119 1
## 229  0.426722380  0.0016966350 1
## 230  0.297455504  0.1527027406 1
## 231  0.417275391  0.1403955566 1
## 232  0.123492674  0.3946963658 0
## 233  0.102602064  0.4813728365 0
## 234 -0.208179070 -0.4530930845 0
## 235  0.425577890 -0.4204395078 1
## 236 -0.247003664 -0.4124051693 0
## 237 -0.383220564  0.3883595997 0
## 238  0.450832641  0.2080056891 1
## 239 -0.117296999  0.2647885461 0
## 240  0.444590895  0.1992149891 1
## 241 -0.373773065 -0.0799034662 1
## 242  0.233882315  0.3815440284 0
## 243 -0.457423135  0.0379888548 1
## 244 -0.482244316  0.3846276160 1
## 245 -0.144191591 -0.4789548996 0
## 246 -0.341298276 -0.4428657868 0
## 247  0.465778236 -0.4621147094 1
## 248  0.025390452 -0.4237165619 0
## 249  0.183528906  0.0263213953 1
## 250  0.188073800 -0.1538034510 1
## 251  0.055886693 -0.3895434965 0
## 252  0.376641637 -0.0021996335 1
## 253 -0.457828498  0.4427693887 1
## 254  0.325826848  0.2062316353 1
## 255  0.108930121 -0.0810451729 1
## 256  0.474932415 -0.4555364980 1
## 257 -0.413520869  0.1603158989 1
## 258  0.045214118  0.2648212397 0
## 259  0.298260435 -0.1389090978 1
## 260  0.425947235  0.1790545930 1
## 261  0.486980285  0.4427390110 1
## 262  0.013361711 -0.4452008209 0
## 263  0.068386172 -0.3627790883 0
## 264  0.086922015  0.0251289294 1
## 265 -0.433269539 -0.2782099510 1
## 266  0.153197566  0.0475520908 1
## 267 -0.410948250 -0.4926728171 0
## 268  0.115846789  0.3342606598 0
## 269 -0.491826675 -0.2478932999 1
## 270  0.290077019  0.0171158088 1
## 271  0.286262659 -0.3099718159 0
## 272 -0.003973736  0.1167730205 0
## 273  0.171439419 -0.0985081145 1
## 274  0.443012726 -0.0216471977 1
## 275  0.192808205 -0.3197844119 0
## 276 -0.141471996 -0.0690426487 1
## 277  0.423366252  0.0322296389 1
## 278  0.087053570  0.0950364056 0
## 279 -0.466530763 -0.0330282981 1
## 280  0.107550110 -0.2245562284 0
## 281  0.410328844 -0.1577862152 1
## 282 -0.437185715  0.2260509944 1
## 283  0.492634882  0.2577262216 1
## 284  0.027677000 -0.1901308710 0
## 285  0.250776759  0.1816960073 1
## 286 -0.236523666  0.4392059611 0
## 287 -0.475522346  0.3369390029 1
## 288  0.138173619 -0.4841885460 0
## 289  0.032033056 -0.4258397748 0
## 290 -0.440654259  0.3712578746 1
## 291 -0.259523070  0.0065840075 1
## 292 -0.339003073  0.3411155962 0
## 293  0.224830531 -0.0758005916 1
## 294 -0.092765663 -0.2623502621 0
## 295  0.286919932  0.2277926065 1
## 296  0.196911894 -0.0870991379 1
## 297 -0.348691148  0.3666279619 0
## 298 -0.473900522  0.1118411554 1
## 299 -0.162584425 -0.0116114845 1
## 300  0.431677472  0.2169843228 1
## 301  0.063595359 -0.1149859771 0
## 302 -0.124596965  0.0807928671 1
## 303 -0.213786357  0.4303886644 0
## 304  0.184338134  0.2370470830 0
## 305  0.089985061  0.2678452288 0
## 306 -0.348694000  0.3763707499 0
## 307  0.101441060 -0.3847340397 0
## 308  0.226443280 -0.1223976249 1
## 309 -0.132205665  0.4352114392 0
## 310 -0.367755378  0.1264254851 1
## 311  0.119654100 -0.1436423410 0
## 312  0.253824517 -0.2789610536 0
## 313  0.265601278 -0.2295765851 1
## 314 -0.018050388  0.2520748742 0
## 315 -0.253382717 -0.3399061905 0
## 316  0.004241528  0.4588642407 0
## 317  0.245849619  0.0885610557 1
## 318  0.477267595  0.0593896615 1
## 319  0.457774062  0.3933617307 1
## 320 -0.095686801  0.0828341870 1
## 321  0.126025785 -0.4840900202 0
## 322  0.325851830 -0.3326075608 0
## 323  0.217799158  0.2007575221 1
## 324 -0.317119280 -0.3217924035 0
## 325 -0.420729003 -0.0527423015 1
## 326 -0.452563847 -0.3778377336 1
## 327 -0.340253932 -0.1418122943 1
## 328  0.065111218 -0.0021087006 1
## 329  0.440655379 -0.3133764551 1
## 330 -0.389419211  0.0567672208 1
## 331  0.080664611 -0.2068359384 0
## 332 -0.128984195 -0.4664990343 0
## 333 -0.150856844 -0.1774347082 0
## 334 -0.141043143  0.1773042330 0
## 335  0.321112961 -0.0785183450 1
## 336 -0.081286293  0.2269066656 0
## 337 -0.147930384 -0.0215273639 1
## 338 -0.224770646 -0.3950182216 0
## 339  0.271635942  0.0267634154 1
## 340  0.436056844 -0.1930899762 1
## 341 -0.381631507  0.4147534550 0
## 342 -0.288067179  0.1553240083 1
## 343  0.108886892 -0.4566083502 0
## 344 -0.210369875 -0.3764088652 0
## 345 -0.139161238 -0.4383138365 0
## 346  0.179096422  0.0876577394 1
## 347  0.338356053  0.4234341797 0
## 348 -0.471409096 -0.0774260873 1
## 349 -0.105355362 -0.3842099509 0
## 350 -0.224141281 -0.3985369350 0
## 351  0.184396000  0.3106525133 0
## 352  0.455438610 -0.3432056529 1
## 353  0.278584892 -0.4537235785 0
## 354  0.045944248  0.4652169975 0
## 355  0.326248089  0.4553295239 0
## 356  0.357923134 -0.0842630521 1
## 357 -0.353878218 -0.0615742099 1
## 358  0.434672984  0.1339829171 1
## 359  0.344617413 -0.3986684638 0
## 360 -0.231268377  0.1018699454 1
## 361 -0.395110479 -0.2196982980 1
## 362 -0.044870056  0.2119901013 0
## 363 -0.148243888 -0.1666424458 0
## 364 -0.051090344  0.3348021575 0
## 365  0.082810552  0.1196063096 0
## 366 -0.270817054  0.3788460405 0
## 367 -0.049641712  0.1312861578 0
## 368  0.092978765  0.4296527442 0
## 369  0.485606020 -0.4888218227 0
## 370 -0.251098524  0.4083000310 0
## 371  0.032685881 -0.3102867766 0
## 372  0.428963695  0.1720413535 1
## 373  0.482661151  0.3367182303 1
## 374 -0.386014435 -0.0264154328 1
## 375  0.349512005  0.2711730537 1
## 376  0.381632553  0.2712232426 1
## 377 -0.078545227  0.0400531364 1
## 378 -0.169401091  0.3279072992 0
## 379 -0.350651013  0.0671096244 1
## 380  0.433289026  0.1441461053 1
## 381 -0.053165905  0.1343160737 0
## 382 -0.198913245 -0.2139617689 0
## 383 -0.219728848  0.3489443385 0
## 384 -0.282768733  0.0226060464 1
## 385 -0.316740034  0.4469320413 0
## 386  0.101719273  0.1072730082 0
## 387 -0.288674855 -0.4802737248 0
## 388  0.417800694  0.1787101144 1
## 389  0.406719419  0.2616007673 1
## 390 -0.340595233 -0.0599536342 1
## 391  0.350639408 -0.0782857032 1
## 392 -0.425460927 -0.4863395805 0
## 393  0.211615718 -0.4406180223 0
## 394  0.308449949  0.2401432577 1
## 395 -0.408024394  0.1524834004 1
## 396  0.066272588 -0.2925195210 0
## 397 -0.472857586 -0.0415867425 1
## 398 -0.245312543  0.3637690742 0
## 399  0.009053381 -0.1194307031 0
## 400 -0.360156767 -0.2059686596 1
## 401 -0.219544340 -0.1448540990 1
## 402  0.359981164 -0.3115215902 1
## 403 -0.088680337  0.4012677420 0
## 404 -0.114313024  0.2876132329 0
## 405  0.384625981  0.0668358665 1
## 406  0.399334974 -0.4288403606 0
## 407 -0.412859935  0.2862607965 1
## 408 -0.320513611 -0.0275398672 1
## 409 -0.008006784 -0.4245918086 0
## 410 -0.400400472 -0.2257335430 1
## 411  0.313302875 -0.4975167855 0
## 412  0.112511371  0.3561897562 0
## 413 -0.011112655  0.0969851657 0
## 414 -0.053866106 -0.2160471014 0
## 415 -0.310391161 -0.1655113271 1
## 416  0.379059866 -0.2911617088 1
## 417  0.277960095  0.1284959419 1
## 418  0.485398941 -0.4330274188 1
## 419 -0.335861067  0.0965201873 1
## 420 -0.037691597 -0.0034589542 1
## 421  0.202049741  0.3264536953 0
## 422 -0.268685003  0.2441806842 1
## 423  0.325081623 -0.0544550437 1
## 424  0.149404624 -0.4518515153 0
## 425  0.135399388 -0.4681478334 0
## 426  0.041621972  0.3882756657 0
## 427 -0.268397877  0.4498848787 0
## 428 -0.239232691 -0.0254873380 1
## 429  0.261285332 -0.0568641936 1
## 430 -0.033151256 -0.0811561765 0
## 431 -0.126541820 -0.1999273810 0
## 432 -0.115162086 -0.0357807667 1
## 433 -0.140815168  0.2850369613 0
## 434  0.354193199 -0.2324939801 1
## 435  0.061649762  0.3318347540 0
## 436  0.066047145  0.3291932950 0
## 437 -0.417873057  0.4020824444 1
## 438  0.409451549  0.1975513282 1
## 439  0.415153974 -0.4124577253 1
## 440  0.409802644 -0.1157153745 1
## 441 -0.306521680 -0.3028903825 1
## 442 -0.159150328 -0.2239751809 0
## 443 -0.403628754 -0.3649672538 1
## 444  0.398646731  0.3729930262 1
## 445  0.314184055  0.3003619418 1
## 446 -0.402566171  0.4788770652 0
## 447 -0.310672351 -0.1572150926 1
## 448  0.223649827 -0.3216092244 0
## 449 -0.161521608  0.1348138538 1
## 450  0.201654185 -0.3895918711 0
## 451 -0.016204235  0.0461312223 0
## 452  0.401095262  0.0926274043 1
## 453  0.057087749  0.0090459597 1
## 454 -0.093061704  0.0075139054 1
## 455  0.038839849  0.4648821519 0
## 456  0.307502320 -0.0875928211 1
## 457  0.066090598  0.0733621439 0
## 458 -0.174251040  0.2294245812 0
## 459  0.430069797 -0.1656383101 1
## 460  0.289203532 -0.0342325594 1
## 461 -0.438948869  0.3449065837 1
## 462 -0.176089345 -0.4272832279 0
## 463  0.414801148 -0.2973450653 1
## 464  0.258737853  0.1612566723 1
## 465 -0.120531487 -0.4295955012 0
## 466  0.313370918 -0.4164402024 0
## 467 -0.070697266  0.1749896391 0
## 468 -0.479996197 -0.4199294411 1
## 469  0.299788375  0.2463754225 1
## 470 -0.434111533 -0.2057720956 1
## 471 -0.259238360 -0.2628157954 0
## 472  0.321279196  0.0566088257 1
## 473 -0.148156645  0.4783479143 0
## 474  0.369180038  0.3490372549 1
## 475 -0.230011390  0.4727280580 0
## 476 -0.303686797 -0.2028644213 1
## 477 -0.301672424 -0.2046816668 1
## 478 -0.075080926  0.4253927066 0
## 479  0.426645907 -0.3688690623 1
## 480  0.319320859  0.0023829141 1
## 481 -0.350931958 -0.2739630877 1
## 482 -0.088997766  0.2708580047 0
## 483  0.009836574 -0.1574788499 0
## 484 -0.062040512 -0.1034960831 0
## 485  0.305711353 -0.0691992852 1
## 486  0.149546849  0.3847518975 0
## 487  0.260616415  0.3406900356 0
## 488 -0.156805567  0.2828158096 0
## 489 -0.237796891  0.1941373623 1
## 490 -0.424690074 -0.4167236870 1
## 491  0.318259471 -0.3338592902 0
## 492  0.057349530 -0.1851916104 0
## 493 -0.219963288  0.0845710067 1
## 494 -0.158954368  0.1133581407 1
## 495  0.032317894 -0.2336242755 0
## 496 -0.490528402 -0.3249852953 1
## 497 -0.017406506  0.1294060689 0
## 498 -0.046683781  0.0292718133 1
## 499  0.108490199 -0.4033513698 0
## 500  0.311776650 -0.0062817857 1

(b) Plot the observations, colored according to their class labels.Your plot should display X1 on the x-axis, and X2 on the yaxis.

library(ggplot2)

# Plot the dataset

ggplot(data, aes(x=x1, y=x2, color=y)) + geom_point() + theme_minimal() + 
  labs(title="Quadratic Decision Boundary Dataset", x="x1", y="x2", color="Class")

(c) Fit a logistic regression model to the data, using X1 and X2 as predictors.

# Fit a logistic regression model
lm.fit <- glm(y ~ x1 + x2, data=data, family=binomial())

# Print the model summary
summary(lm.fit)
## 
## Call:
## glm(formula = y ~ x1 + x2, family = binomial(), data = data)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -1.320  -1.209   1.066   1.135   1.233  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.10187    0.08969   1.136    0.256
## x1           0.19002    0.30712   0.619    0.536
## x2          -0.27844    0.30994  -0.898    0.369
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 691.79  on 499  degrees of freedom
## Residual deviance: 690.58  on 497  degrees of freedom
## AIC: 696.58
## 
## Number of Fisher Scoring iterations: 3

(d) Apply this model to the training data in order to obtain a predicted class label for each training observation. Plot the observations, colored according to the predicted class labels. The decision boundary should be linear.

# Obtain predicted class labels for each training observation
lm.prob <- predict(lm.fit, type="response")>= 0.5
lm.pred = ifelse(lm.prob > 0.52, 1, 0)
data.pos = data[lm.pred == 1, ]
data.neg = data[lm.pred == 0, ]
# Plot the dataset with predicted class labels
ggplot(data, aes(x=x1, y=x2, color=factor(lm.prob))) + geom_point() + 
  theme_minimal() + labs(title="Logistic Regression Decision Boundary Dataset",
                          x="X1", y="X2", color="Predicted Class")

(e) Now fit a logistic regression model to the data using non-linear functions of X1 and X2 as predictors (e.g. X1xX1 , X1×X2, log(X2), and so forth).

# Create non-linear predictors
data$X1_X1 <- data$x1^2
data$X1_X2 <- data$x1*data$x2
data$X2_log <- log(data$x2)

# Fit logistic regression model with non-linear predictors
fit2 <- glm(y ~ x1 + x2 + X1_X1 + X1_X2 + X2_log, data=data, family=binomial)

# Print the summary of the new model
summary(fit2)
## 
## Call:
## glm(formula = y ~ x1 + x2 + X1_X1 + X1_X2 + X2_log, family = binomial, 
##     data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.59981  -0.00206   0.00000   0.01558   1.86034  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  12.0882     3.6491   3.313 0.000924 ***
## x1           -8.0211     5.9626  -1.345 0.178548    
## x2          -90.0431    21.2797  -4.231 2.32e-05 ***
## X1_X1       169.4443    38.2887   4.425 9.62e-06 ***
## X1_X2        30.2407    18.5383   1.631 0.102837    
## X2_log        1.8272     0.8037   2.273 0.022997 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 336.865  on 242  degrees of freedom
## Residual deviance:  37.341  on 237  degrees of freedom
##   (257 observations deleted due to missingness)
## AIC: 49.341
## 
## Number of Fisher Scoring iterations: 10
#option#2:lm.fit2 = glm(y ~ poly(x1, 2) + poly(x2, 2) + I(x1 * x2), data = data, family = binomial)
#lm.fit2#

(f) Apply this model to the training data in order to obtain a predicted class label for each training observation. Plot the observations, colored according to the predicted class labels. The decision boundary should be obviously non-linear. If it is not, then repeat (a)-(e) until you come up with an example in which the predicted class labels are obviously non-linear.

# Create new variables for non-linear terms
data$x1_sq <- data$x1^2
data$x2_sq <- data$x2^2

# Fit logistic regression model with non-linear terms
lm.fit2 <- glm(y ~ x1 + x2 + x1_sq + x2_sq, data=data, family=binomial)

# Obtain predicted class labels for each training observation
lm.prob2 <- predict(lm.fit2, type="response") >= 0.5
lm.pred2 <- ifelse(lm.prob2, 1, 0)


# Plot the dataset with predicted class labels
data$pred_class <- factor(lm.pred2)
ggplot(data, aes(x=x1, y=x2, color=pred_class)) +
  geom_point() +
  theme_minimal() +
  labs(title="Logistic Regression with Non-Linear Terms",
       x="X1", y="X2", color="Predicted Class")

(g) Fit a support vector classifier to the data with X1 and X2 as predictors. Obtain a class prediction for each training observation. Plot the observations, colored according to the predicted class labels.

# Fit an SVM model with a linear basis function kernel
svm.fit <- svm(as.factor(y) ~ x1 + x2, data, kernel = "linear",cost = 0.1)

# Obtain predicted class labels for each training observation
svm.pred <- predict(svm.fit, data[,1:2])

data.pos <- data[svm.pred == 1, ]
data.neg <- data[svm.pred == 0, ]

# Convert predicted class labels to factor variable and add to data dataframe
data$pred_class <- factor(ifelse(svm.pred == 1, "pink","blue"))

# Plot the dataset with predicted class labels
ggplot(data, aes(x = x1, y = x2,color = pred_class)) +
  geom_point() +
  theme_minimal() +
  labs(title = "Support Vector Classifier",
       x = "X1", y = "X2",color = "Predicted Class")

(h) Fit a SVM using a non-linear kernel to the data. Obtain a class prediction for each training observation. Plot the observations, colored according to the predicted class labels.

# Fit an SVM model with a radial basis function kernel
svm.fit <- svm(as.factor(y) ~ x1 + x2, data, kernel = "radial",cost = 0.1)

# Obtain predicted class labels for each training observation
svm.pred <- predict(svm.fit, data[,1:2])

data.pos <- data[svm.pred == 1, ]
data.neg <- data[svm.pred == 0, ]

# Convert predicted class labels to factor variable and add to data dataframe
data$pred_class <- factor(ifelse(svm.pred == 1, "blue","red"))

# Plot the dataset with predicted class labels
ggplot(data, aes(x = x1, y = x2,color = pred_class)) +
  geom_point() +
  theme_minimal() +
  labs(title = "Support Vector Classifier",
       x = "X1", y = "X2",color = "Predicted Class")

(i) Comment on your results. The logistic regression model with non-linear functions of X1 and X2 as predictors was able to fit a non-linear decision boundary and classify the data points accurately. Similarly, the support vector classifier with a radial basis function kernel was also able to fit a non-linear decision boundary and classify the data points accurately. However, the linear SVM model was not able to fit a non-linear decision boundary.This demonstrates the importance of choosing an appropriate model for the data at hand. In cases where the decision boundary is non-linear, linear models such as logistic regression and linear SVM may not be appropriate and non-linear models such as SVM with a radial basis function kernel should be used.

7.In this problem, you will use support vector approaches in order to predict whether a given car gets high or low gas mileage based on the Auto data set.

(a) Create a binary variable that takes on a 1 for cars with gas mileage above the median, and a 0 for cars with gas mileage below the median.

data("Auto")
set.seed(4)
mpg_median <- median(Auto$mpg)
# Create a binary variable for mpg
Auto$mpg_binary <- ifelse(Auto$mpg > mpg_median, 1, 0)

(b) Fit a support vector classifier to the data with various values of cost, in order to predict whether a car gets high or low gas mileage. Report the cross-validation errors associated with different values of this parameter. Comment on your results. Note you will need to fit the classifier without the gas mileage variable to produce sensible results.

tune.out = tune(svm, mpg~ ., data = Auto, kernel = "linear", ranges = list(cost = c(0.01, 
    0.1, 1, 5, 10)))
summary(tune.out)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##   0.1
## 
## - best performance: 8.997146 
## 
## - Detailed performance results:
##    cost     error dispersion
## 1  0.01  9.599019   3.497692
## 2  0.10  8.997146   2.856999
## 3  1.00  9.536679   2.583884
## 4  5.00  9.976757   2.956660
## 5 10.00 10.197712   3.088822

We can conclude that cost = 0.1 results in the lowest error rate.

(c) Now repeat (b), this time using SVMs with radial and polynomial basis kernels, with different values of gamma and degree and cost. Comment on your results.

tune_out1 = tune(svm, mpg ~ ., data = Auto, kernel = "polynomial", ranges = list(cost = c(0.1, 
    1, 5, 10), degree = c(2, 3, 4)))
summary(tune_out1)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost degree
##    10      3
## 
## - best performance: 58.161 
## 
## - Detailed performance results:
##    cost degree    error dispersion
## 1   0.1      2 62.53708   18.19422
## 2   1.0      2 61.71996   19.10574
## 3   5.0      2 59.32593   22.73034
## 4  10.0      2 58.25041   25.13693
## 5   0.1      3 62.59339   18.10228
## 6   1.0      3 62.17187   18.13417
## 7   5.0      3 60.34725   18.27489
## 8  10.0      3 58.16100   18.42266
## 9   0.1      4 62.63976   18.09917
## 10  1.0      4 62.63377   18.10367
## 11  5.0      4 62.60721   18.12365
## 12 10.0      4 62.57409   18.14860
tune.out2 = tune(svm, mpg ~ ., data = Auto, kernel = "radial", ranges = list(cost = c(0.1, 
    1, 5, 10), gamma = c(0.01, 0.1, 1, 5, 10)))
summary(tune.out2)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost gamma
##     5   0.1
## 
## - best performance: 6.269838 
## 
## - Detailed performance results:
##    cost gamma     error dispersion
## 1   0.1  0.01 12.237900   4.451380
## 2   1.0  0.01  7.469449   3.062163
## 3   5.0  0.01  6.805091   2.633398
## 4  10.0  0.01  6.772744   2.638448
## 5   0.1  0.10  9.383910   4.165271
## 6   1.0  0.10  6.445695   2.651680
## 7   5.0  0.10  6.269838   2.091180
## 8  10.0  0.10  6.380076   2.169053
## 9   0.1  1.00 46.866015  11.418499
## 10  1.0  1.00 21.193368   6.599244
## 11  5.0  1.00 19.996053   5.900998
## 12 10.0  1.00 19.991186   5.909671
## 13  0.1  5.00 61.473608  13.366909
## 14  1.0  5.00 58.802509  13.238653
## 15  5.0  5.00 58.350697  12.765941
## 16 10.0  5.00 58.350697  12.765941
## 17  0.1 10.00 61.672451  13.346337
## 18  1.0 10.00 59.930780  13.125662
## 19  5.0 10.00 59.501530  12.679424
## 20 10.0 10.00 59.501530  12.679424

(d) Make some plots to back up your assertions in (b) and (c). >Hint: In the lab, we used the plot() function for svm objects only in cases with p = 2. When p > 2, you can use the plot() function to create plots displaying pairs of variables at a time. Essentially, instead of typing > plot (svmfit , dat) where svmfit contains your fitted model and dat is a data frame containing your data, you can type > plot (svmfit , dat , x1 ∼ x4) in order to plot just the first and fourth variables. However, you must replace x1 and x4 with the correct variable names. To find out more, type ?plot.svm.

Auto$mpglevel = as.factor(Auto$mpg_binary)

svm_lin = svm(mpglevel ~ ., data = Auto, kernel = "linear", ranges = list(cost = c(0.01, 
    0.1, 1, 5, 10)))



svm_poly = svm(mpglevel ~ ., data = Auto, kernel = "polynomial",ranges = list(cost = c(0.1, 
    1, 5, 10), degree = c(2, 3, 4)))




svm_radial = svm(mpglevel ~ ., data = Auto, kernel = "radial",  ranges = list(cost = c(0.1, 
    1, 5, 10), gamma = c(0.01, 0.1, 1, 5, 10)))
plotpairs = function(fit) {
    for (name in names(Auto)[!(names(Auto) %in% c("mpg", "mpglevel", "name"))]) {
        plot(fit, Auto, as.formula(paste("mpg~", name, sep = "")))
    }
}
plotpairs(svm_lin)

8.This problem involves the OJ data set which is part of the ISLR2 package.

(a) Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.

train <- sample(nrow(OJ), 800)
OJ.train <- OJ[train, ]
OJ.test <- OJ[-train, ]

(b) Fit a support vector classifier to the training data using cost = 0.01, with Purchase as the response and the other variables as predictors. Use the summary() function to produce summary statistics, and describe the results obtained.

svm.fit1 <- svm(Purchase ~ ., data = OJ.train, kernel = "linear", cost = 0.01)
summary(svm.fit1)
## 
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "linear", cost = 0.01)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  linear 
##        cost:  0.01 
## 
## Number of Support Vectors:  437
## 
##  ( 218 219 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM

(c) What are the training and test error rates?

train.pred <- predict(svm.fit1, OJ.train)
mean(train.pred != OJ.train$Purchase) # Training error rate
## [1] 0.16625
test.pred <- predict(svm.fit1, OJ.test)
mean(test.pred != OJ.test$Purchase) # Test error rate
## [1] 0.1814815

(d) Use the tune() function to select an optimal cost. Consider values in the range 0.01 to 10.

set.seed(4)
tune.out <- tune(svm, Purchase ~ ., data = OJ.train, kernel = "linear", ranges = list(cost = c(0.01, 0.1, 1, 10)))
summary(tune.out)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##     1
## 
## - best performance: 0.165 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1  0.01 0.17000 0.05627314
## 2  0.10 0.16625 0.05775006
## 3  1.00 0.16500 0.06061032
## 4 10.00 0.16500 0.05296750

(e) Compute the training and test error rates using this new value for cost.

set.seed(4)
svm.fit2 <- svm(Purchase ~ ., data = OJ.train, kernel = "linear", cost = 0.1)
train.pred2 <- predict(svm.fit2, OJ.train)
mean(train.pred2 != OJ.train$Purchase) # Training error rate
## [1] 0.16625
test.pred2 <- predict(svm.fit2, OJ.test)
mean(test.pred2 != OJ.test$Purchase) # Test error rate
## [1] 0.1740741

(f) Repeat parts (b) through (e) using a support vector machine with a radial kernel. Use the default value for gamma.

set.seed(4)
svm.fit3 <- svm(Purchase ~ ., data = OJ.train, kernel = "radial", cost = 0.01)
summary(svm.fit3)
## 
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "radial", cost = 0.01)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  0.01 
## 
## Number of Support Vectors:  629
## 
##  ( 316 313 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
tune.out <- tune(svm, Purchase ~ ., data = OJ.train, kernel = "radial", ranges = list(cost = c(0.01, 0.1, 1, 10)))
summary(tune.out)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##     1
## 
## - best performance: 0.1775 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1  0.01 0.39125 0.03821086
## 2  0.10 0.17875 0.04966904
## 3  1.00 0.17750 0.05230785
## 4 10.00 0.18875 0.06493854
svm.fit4 <- svm(Purchase ~ ., data = OJ.train, kernel = "radial", cost = 1)
train.pred <- predict(svm.fit4, OJ.train)
mean(train.pred != OJ.train$Purchase)
## [1] 0.15875
test.pred <- predict(svm.fit4, OJ.test)
mean(test.pred != OJ.test$Purchase)
## [1] 0.1666667

(g) Repeat parts (b) through (e) using a support vector machine with a polynomial kernel. Set degree = 2.

set.seed(4)
svm.fit <- svm(Purchase ~ ., data = OJ.train, kernel = "polynomial", cost = 0.01)
summary(svm.fit)
## 
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "polynomial", 
##     cost = 0.01)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  polynomial 
##        cost:  0.01 
##      degree:  3 
##      coef.0:  0 
## 
## Number of Support Vectors:  617
## 
##  ( 310 307 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
tune.out <- tune(svm, Purchase ~ ., data = OJ.train, kernel = "polynomial", ranges = list(cost = c(0.01, 0.1, 1, 10)))
summary(tune.out)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##    10
## 
## - best performance: 0.185 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1  0.01 0.37000 0.04495368
## 2  0.10 0.29875 0.05846711
## 3  1.00 0.19000 0.05296750
## 4 10.00 0.18500 0.05096295
svm.fit <- svm(Purchase ~ ., data = OJ.train, kernel = "polynomial", cost = 1)
train.pred <- predict(svm.fit, OJ.train)
mean(train.pred != OJ.train$Purchase)
## [1] 0.15625
test.pred <- predict(svm.fit, OJ.test)
mean(test.pred != OJ.test$Purchase)
## [1] 0.1666667

(h) Overall, which approach seems to give the best results on this data?

The linear basis approach seems to give the best results on this data.