2

KNN classifiers is useful when you have classification data. Given a point KNN can find the k’th nearest neighbor and see which class those neighbors belong too. Then we determine that point falls into the class with the most neighbors in a single class. KNN regression is usefull in a linear or sometimes non-linear regression technique. Example we have a plot of linear data points and want to predict where a point will fall at a given x value. We measure the k’th closest points to a vertical line drawn at that x value and average those points to give an estimate what the value will be at that x.

9

Auto = read.csv("~/Auto.csv")
pairs(Auto)

Auto$cylinders = as.double(Auto$cylinders)
Auto$origin = as.double(Auto$origin)
Auto$horsepower = as.double(Auto$horsepower)
Auto$weight = as.double(Auto$weight)
Auto$year = as.double(Auto$year)
Auto
##      mpg cylinders displacement horsepower weight acceleration year origin
## 1   18.0         8        307.0         17   3504         12.0   70      1
## 2   15.0         8        350.0         35   3693         11.5   70      1
## 3   18.0         8        318.0         29   3436         11.0   70      1
## 4   16.0         8        304.0         29   3433         12.0   70      1
## 5   17.0         8        302.0         24   3449         10.5   70      1
## 6   15.0         8        429.0         42   4341         10.0   70      1
## 7   14.0         8        454.0         47   4354          9.0   70      1
## 8   14.0         8        440.0         46   4312          8.5   70      1
## 9   14.0         8        455.0         48   4425         10.0   70      1
## 10  15.0         8        390.0         40   3850          8.5   70      1
## 11  15.0         8        383.0         37   3563         10.0   70      1
## 12  14.0         8        340.0         34   3609          8.0   70      1
## 13  15.0         8        400.0         29   3761          9.5   70      1
## 14  14.0         8        455.0         48   3086         10.0   70      1
## 15  24.0         4        113.0         91   2372         15.0   70      3
## 16  22.0         6        198.0         91   2833         15.5   70      1
## 17  18.0         6        199.0         93   2774         15.5   70      1
## 18  21.0         6        200.0         81   2587         16.0   70      1
## 19  27.0         4         97.0         84   2130         14.5   70      3
## 20  26.0         4         97.0         50   1835         20.5   70      2
## 21  25.0         4        110.0         83   2672         17.5   70      2
## 22  24.0         4        107.0         86   2430         14.5   70      2
## 23  25.0         4        104.0         91   2375         17.5   70      2
## 24  26.0         4        121.0         10   2234         12.5   70      2
## 25  21.0         6        199.0         86   2648         15.0   70      1
## 26  10.0         8        360.0         46   4615         14.0   70      1
## 27  10.0         8        307.0         43   4376         15.0   70      1
## 28  11.0         8        318.0         45   4382         13.5   70      1
## 29   9.0         8        304.0         41   4732         18.5   70      1
## 30  27.0         4         97.0         84   2130         14.5   71      3
## 31  28.0         4        140.0         86   2264         15.5   71      1
## 32  25.0         4        113.0         91   2228         14.0   71      3
## 33  25.0         4         98.0          1   2046         19.0   71      1
## 34  19.0         6        232.0          2   2634         13.0   71      1
## 35  16.0         6        225.0          5   3439         15.5   71      1
## 36  17.0         6        250.0          2   3329         15.5   71      1
## 37  19.0         6        250.0         84   3302         15.5   71      1
## 38  18.0         6        232.0          2   3288         15.5   71      1
## 39  14.0         8        350.0         35   4209         12.0   71      1
## 40  14.0         8        400.0         38   4464         11.5   71      1
## 41  14.0         8        351.0         31   4154         13.5   71      1
## 42  14.0         8        318.0         29   4096         13.0   71      1
## 43  12.0         8        383.0         39   4955         11.5   71      1
## 44  13.0         8        400.0         37   4746         12.0   71      1
## 45  13.0         8        400.0         38   5140         12.0   71      1
## 46  18.0         6        258.0          8   2962         13.5   71      1
## 47  22.0         4        140.0         69   2408         19.0   71      1
## 48  19.0         6        250.0          2   3282         15.0   71      1
## 49  18.0         6        250.0         84   3139         14.5   71      1
## 50  23.0         4        122.0         82   2220         14.0   71      1
## 51  28.0         4        116.0         86   2123         14.0   71      2
## 52  30.0         4         79.0         67   2074         19.5   71      2
## 53  30.0         4         88.0         72   2065         14.5   71      2
## 54  31.0         4         71.0         62   1773         19.0   71      3
## 55  35.0         4         72.0         66   1613         18.0   71      3
## 56  27.0         4         97.0         57   1834         19.0   71      2
## 57  26.0         4         91.0         67   1955         20.5   71      1
## 58  24.0         4        113.0         91   2278         15.5   72      3
## 59  25.0         4         97.5         76   2126         17.0   72      1
## 60  23.0         4         97.0         55   2254         23.5   72      2
## 61  20.0         4        140.0         86   2408         19.5   72      1
## 62  21.0         4        122.0         82   2226         16.5   72      1
## 63  13.0         8        350.0         35   4274         12.0   72      1
## 64  14.0         8        400.0         38   4385         12.0   72      1
## 65  15.0         8        318.0         29   4135         13.5   72      1
## 66  14.0         8        351.0         31   4129         13.0   72      1
## 67  17.0         8        304.0         29   3672         11.5   72      1
## 68  11.0         8        429.0         44   4633         11.0   72      1
## 69  13.0         8        350.0         32   4502         13.5   72      1
## 70  12.0         8        350.0         34   4456         13.5   72      1
## 71  13.0         8        400.0         40   4422         12.5   72      1
## 72  19.0         3         70.0         93   2330         13.5   72      3
## 73  15.0         8        304.0         29   3892         12.5   72      1
## 74  13.0         8        307.0         17   4098         14.0   72      1
## 75  13.0         8        302.0         24   4294         16.0   72      1
## 76  14.0         8        318.0         29   4077         14.0   72      1
## 77  18.0         4        121.0          9   2933         14.5   72      2
## 78  22.0         4        121.0         72   2511         18.0   72      2
## 79  21.0         4        120.0         83   2979         19.5   72      2
## 80  26.0         4         96.0         66   2189         18.0   72      2
## 81  22.0         4        122.0         82   2395         16.0   72      1
## 82  28.0         4         97.0         88   2288         17.0   72      3
## 83  23.0         4        120.0         93   2506         14.5   72      3
## 84  28.0         4         98.0         76   2164         15.0   72      1
## 85  27.0         4         97.0         84   2100         16.5   72      3
## 86  13.0         8        350.0         38   4100         13.0   73      1
## 87  14.0         8        304.0         29   3672         11.5   73      1
## 88  13.0         8        350.0         26   3988         13.0   73      1
## 89  14.0         8        302.0         21   4042         14.5   73      1
## 90  15.0         8        318.0         29   3777         12.5   73      1
## 91  12.0         8        429.0         42   4952         11.5   73      1
## 92  13.0         8        400.0         29   4464         12.0   73      1
## 93  13.0         8        351.0         33   4363         13.0   73      1
## 94  14.0         8        318.0         29   4237         14.5   73      1
## 95  13.0         8        440.0         46   4735         11.0   73      1
## 96  12.0         8        455.0         48   4951         11.0   73      1
## 97  13.0         8        360.0         38   3821         11.0   73      1
## 98  18.0         6        225.0          5   3121         16.5   73      1
## 99  16.0         6        250.0          2   3278         18.0   73      1
## 100 18.0         6        232.0          2   2945         16.0   73      1
## 101 18.0         6        250.0         84   3021         16.5   73      1
## 102 23.0         6        198.0         91   2904         16.0   73      1
## 103 26.0         4         97.0         50   1950         21.0   73      2
## 104 11.0         8        400.0         29   4997         14.0   73      1
## 105 12.0         8        400.0         36   4906         12.5   73      1
## 106 13.0         8        360.0         37   4654         13.0   73      1
## 107 12.0         8        350.0         39   4499         12.5   73      1
## 108 18.0         6        232.0          2   2789         15.0   73      1
## 109 20.0         4         97.0         84   2279         19.0   73      3
## 110 21.0         4        140.0         69   2401         19.5   73      1
## 111 22.0         4        108.0         90   2379         16.5   73      3
## 112 18.0         3         70.0         86   2124         13.5   73      3
## 113 19.0         4        122.0         81   2310         18.5   73      1
## 114 21.0         6        155.0          6   2472         14.0   73      1
## 115 26.0         4         98.0         86   2265         15.5   73      2
## 116 15.0         8        350.0         26   4082         13.0   73      1
## 117 16.0         8        400.0         49   4278          9.5   73      1
## 118 29.0         4         68.0         52   1867         19.5   73      2
## 119 24.0         4        116.0         71   2158         15.5   73      2
## 120 20.0         4        114.0         87   2582         14.0   73      2
## 121 19.0         4        121.0          9   2868         15.5   73      2
## 122 15.0         8        318.0         29   3399         11.0   73      1
## 123 24.0         4        121.0          8   2660         14.0   73      2
## 124 20.0         6        156.0         14   2807         13.5   73      3
## 125 11.0         8        350.0         39   3664         11.0   73      1
## 126 20.0         6        198.0         91   3102         16.5   74      1
## 127 21.0         6        200.0          1   2875         17.0   74      1
## 128 19.0         6        232.0          2   2901         16.0   74      1
## 129 15.0         6        250.0          2   3336         17.0   74      1
## 130 31.0         4         79.0         64   1950         19.0   74      3
## 131 26.0         4        122.0         76   2451         16.5   74      1
## 132 32.0         4         71.0         62   1836         21.0   74      3
## 133 25.0         4        140.0         71   2542         17.0   74      1
## 134 16.0         6        250.0          2   3781         17.0   74      1
## 135 16.0         6        258.0          8   3632         18.0   74      1
## 136 18.0         6        225.0          5   3613         16.5   74      1
## 137 16.0         8        302.0         24   4141         14.0   74      1
## 138 13.0         8        350.0         29   4699         14.5   74      1
## 139 14.0         8        318.0         29   4457         13.5   74      1
## 140 14.0         8        302.0         24   4638         16.0   74      1
## 141 14.0         8        304.0         29   4257         15.5   74      1
## 142 29.0         4         98.0         79   2219         16.5   74      2
## 143 26.0         4         79.0         64   1963         15.5   74      2
## 144 26.0         4         97.0         74   2300         14.5   74      2
## 145 31.0         4         76.0         53   1649         16.5   74      3
## 146 32.0         4         83.0         58   2003         19.0   74      3
## 147 28.0         4         90.0         71   2125         14.5   74      1
## 148 24.0         4         90.0         71   2108         15.5   74      2
## 149 26.0         4        116.0         71   2246         14.0   74      2
## 150 24.0         4        120.0         93   2489         15.0   74      3
## 151 26.0         4        108.0         89   2391         15.5   74      3
## 152 31.0         4         79.0         64   2000         16.0   74      2
## 153 19.0         6        225.0         91   3264         16.0   75      1
## 154 18.0         6        250.0          5   3459         16.0   75      1
## 155 15.0         6        250.0         69   3432         21.0   75      1
## 156 15.0         6        250.0         69   3158         19.5   75      1
## 157 16.0         8        400.0         37   4668         11.5   75      1
## 158 15.0         8        350.0         26   4440         14.0   75      1
## 159 16.0         8        318.0         29   4498         14.5   75      1
## 160 14.0         8        351.0         27   4657         13.5   75      1
## 161 17.0         6        231.0          8   3907         21.0   75      1
## 162 16.0         6        250.0          5   3897         18.5   75      1
## 163 15.0         6        258.0          8   3730         19.0   75      1
## 164 18.0         6        225.0         91   3785         19.0   75      1
## 165 21.0         6        231.0          8   3039         15.0   75      1
## 166 20.0         8        262.0          8   3221         13.5   75      1
## 167 13.0         8        302.0         16   3169         12.0   75      1
## 168 29.0         4         97.0         71   2171         16.0   75      3
## 169 23.0         4        140.0         79   2639         17.0   75      1
## 170 20.0         6        232.0          2   2914         16.0   75      1
## 171 23.0         4        140.0         74   2592         18.5   75      1
## 172 24.0         4        134.0         92   2702         13.5   75      3
## 173 25.0         4         90.0         68   2223         16.5   75      2
## 174 24.0         4        119.0         93   2545         17.0   75      3
## 175 18.0         6        171.0         93   2984         14.5   75      1
## 176 29.0         4         90.0         67   1937         14.0   75      2
## 177 19.0         6        232.0         86   3211         17.0   75      1
## 178 23.0         4        115.0         91   2694         15.0   75      2
## 179 23.0         4        120.0         84   2957         17.0   75      2
## 180 22.0         4        121.0         94   2945         14.5   75      2
## 181 25.0         4        121.0         11   2671         13.5   75      2
## 182 33.0         4         91.0         54   1795         17.5   75      3
## 183 28.0         4        107.0         82   2464         15.5   76      2
## 184 25.0         4        116.0         77   2220         16.9   76      2
## 185 25.0         4        140.0         88   2572         14.9   76      1
## 186 26.0         4         98.0         75   2255         17.7   76      1
## 187 27.0         4        101.0         79   2202         15.3   76      2
## 188 17.5         8        305.0         24   4215         13.0   76      1
## 189 16.0         8        318.0         29   4190         13.0   76      1
## 190 15.5         8        304.0         13   3962         13.9   76      1
## 191 14.5         8        351.0         30   4215         12.8   76      1
## 192 22.0         6        225.0          2   3233         15.4   76      1
## 193 22.0         6        250.0          5   3353         14.5   76      1
## 194 24.0         6        200.0         77   3012         17.6   76      1
## 195 22.5         6        232.0         86   3085         17.6   76      1
## 196 29.0         4         85.0         53   2035         22.2   76      1
## 197 24.5         4         98.0         57   2164         22.1   76      1
## 198 29.0         4         90.0         67   1937         14.2   76      2
## 199 33.0         4         91.0         54   1795         17.4   76      3
## 200 20.0         6        225.0          2   3651         17.7   76      1
## 201 18.0         6        250.0         74   3574         21.0   76      1
## 202 18.5         6        250.0          8   3645         16.2   76      1
## 203 17.5         6        258.0         91   3193         17.8   76      1
## 204 29.5         4         97.0         68   1825         12.2   76      2
## 205 32.0         4         85.0         67   1990         17.0   76      3
## 206 28.0         4         97.0         71   2155         16.4   76      3
## 207 26.5         4        140.0         69   2565         13.6   76      1
## 208 20.0         4        130.0          3   3150         15.7   76      2
## 209 13.0         8        318.0         29   3940         13.2   76      1
## 210 19.0         4        120.0         84   3270         21.9   76      2
## 211 19.0         6        156.0          7   2930         15.5   76      3
## 212 16.5         6        168.0         13   3820         16.7   76      2
## 213 16.5         8        350.0         39   4380         12.1   76      1
## 214 13.0         8        350.0         26   4055         12.0   76      1
## 215 13.0         8        302.0         17   3870         15.0   76      1
## 216 13.0         8        318.0         29   3755         14.0   76      1
## 217 31.5         4         98.0         65   2045         18.5   77      3
## 218 30.0         4        111.0         76   2155         14.8   77      1
## 219 36.0         4         79.0         56   1825         18.6   77      2
## 220 25.5         4        122.0         92   2300         15.5   77      1
## 221 33.5         4         85.0         67   1945         16.8   77      3
## 222 17.5         8        305.0         26   3880         12.5   77      1
## 223 17.0         8        260.0          8   4060         19.0   77      1
## 224 15.5         8        318.0         26   4140         13.7   77      1
## 225 15.0         8        302.0         17   4295         14.9   77      1
## 226 17.5         6        250.0          8   3520         16.4   77      1
## 227 20.5         6        231.0          5   3425         16.9   77      1
## 228 19.0         6        225.0          2   3630         17.7   77      1
## 229 18.5         6        250.0         94   3525         19.0   77      1
## 230 16.0         8        400.0         39   4220         11.1   77      1
## 231 15.5         8        350.0         37   4165         11.4   77      1
## 232 15.5         8        400.0         40   4325         12.2   77      1
## 233 16.0         8        351.0         28   4335         14.5   77      1
## 234 29.0         4         97.0         74   1940         14.5   77      2
## 235 24.5         4        151.0         84   2740         16.0   77      1
## 236 26.0         4         97.0         71   2265         18.2   77      3
## 237 25.5         4        140.0         85   2755         15.8   77      1
## 238 30.5         4         98.0         60   2051         17.0   77      1
## 239 33.5         4         98.0         79   2075         15.9   77      1
## 240 30.0         4         97.0         64   1985         16.4   77      3
## 241 30.5         4         97.0         74   2190         14.1   77      2
## 242 22.0         6        146.0         93   2815         14.5   77      3
## 243 21.5         4        121.0          8   2600         12.8   77      2
## 244 21.5         3         80.0          8   2720         13.5   77      3
## 245 43.1         4         90.0         51   1985         21.5   78      2
## 246 36.1         4         98.0         63   1800         14.4   78      1
## 247 32.8         4         78.0         53   1985         19.4   78      3
## 248 39.4         4         85.0         67   2070         18.6   78      3
## 249 36.1         4         91.0         57   1800         16.4   78      3
## 250 19.9         8        260.0          8   3365         15.5   78      1
## 251 19.4         8        318.0         24   3735         13.2   78      1
## 252 20.2         8        302.0         23   3570         12.8   78      1
## 253 19.2         6        231.0          5   3535         19.2   78      1
## 254 20.5         6        200.0         91   3155         18.2   78      1
## 255 20.2         6        200.0         81   2965         15.8   78      1
## 256 25.1         4        140.0         84   2720         15.4   78      1
## 257 20.5         6        225.0          2   3430         17.2   78      1
## 258 19.4         6        232.0         86   3210         17.2   78      1
## 259 20.6         6        231.0          5   3380         15.8   78      1
## 260 20.8         6        200.0         81   3070         16.7   78      1
## 261 18.6         6        225.0          8   3620         18.7   78      1
## 262 18.1         6        258.0         13   3410         15.1   78      1
## 263 19.2         8        305.0         26   3425         13.2   78      1
## 264 17.7         6        231.0         35   3445         13.4   78      1
## 265 18.1         8        302.0         23   3205         11.2   78      1
## 266 17.5         8        318.0         24   4080         13.7   78      1
## 267 30.0         4         98.0         65   2155         16.5   78      1
## 268 27.5         4        134.0         91   2560         14.2   78      3
## 269 27.2         4        119.0         93   2300         14.7   78      3
## 270 30.9         4        105.0         71   2230         14.5   78      1
## 271 21.1         4        134.0         91   2515         14.8   78      3
## 272 23.2         4        156.0          5   2745         16.7   78      1
## 273 23.8         4        151.0         81   2855         17.6   78      1
## 274 23.9         4        119.0         93   2405         14.9   78      3
## 275 20.3         5        131.0          4   2830         15.9   78      2
## 276 17.0         6        163.0         15   3140         13.6   78      2
## 277 21.6         4        121.0         11   2795         15.7   78      2
## 278 16.2         6        163.0         19   3410         15.8   78      2
## 279 31.5         4         89.0         68   1990         14.9   78      2
## 280 29.5         4         98.0         65   2135         16.6   78      3
## 281 21.5         6        231.0         11   3245         15.4   79      1
## 282 19.8         6        200.0         81   2990         18.2   79      1
## 283 22.3         4        140.0         84   2890         17.3   79      1
## 284 20.2         6        232.0         86   3265         18.2   79      1
## 285 20.6         6        225.0          8   3360         16.6   79      1
## 286 17.0         8        305.0         17   3840         15.4   79      1
## 287 17.6         8        302.0         16   3725         13.4   79      1
## 288 16.5         8        351.0         22   3955         13.2   79      1
## 289 18.2         8        318.0         20   3830         15.2   79      1
## 290 16.9         8        350.0         32   4360         14.9   79      1
## 291 15.5         8        351.0         25   4054         14.3   79      1
## 292 19.2         8        267.0         15   3605         15.0   79      1
## 293 18.5         8        360.0         29   3940         13.0   79      1
## 294 31.9         4         89.0         68   1925         14.0   79      2
## 295 34.1         4         86.0         62   1975         15.2   79      3
## 296 35.7         4         98.0         76   1915         14.4   79      1
## 297 27.4         4        121.0         76   2670         15.0   79      1
## 298 25.4         5        183.0         73   3530         20.1   79      2
## 299 23.0         8        350.0         15   3900         17.4   79      1
## 300 27.2         4        141.0         68   3190         24.8   79      2
## 301 23.9         8        260.0         86   3420         22.2   79      1
## 302 34.2         4        105.0         67   2200         13.2   79      1
## 303 34.5         4        105.0         67   2150         14.9   79      1
## 304 31.8         4         85.0         62   2020         19.2   79      3
## 305 37.3         4         91.0         66   2130         14.7   79      2
## 306 28.4         4        151.0         86   2670         16.0   79      1
## 307 28.8         6        173.0         11   2595         11.3   79      1
## 308 26.8         6        173.0         11   2700         12.9   79      1
## 309 33.5         4        151.0         86   2556         13.2   79      1
## 310 41.5         4         98.0         72   2144         14.7   80      2
## 311 38.1         4         89.0         57   1968         18.8   80      3
## 312 32.1         4         98.0         67   2120         15.5   80      1
## 313 37.2         4         86.0         62   2019         16.4   80      3
## 314 28.0         4        151.0         86   2678         16.5   80      1
## 315 26.4         4        140.0         84   2870         18.1   80      1
## 316 24.3         4        151.0         86   3003         20.1   80      1
## 317 19.1         6        225.0         86   3381         18.7   80      1
## 318 34.3         4         97.0         74   2188         15.8   80      2
## 319 29.8         4        134.0         86   2711         15.5   80      3
## 320 31.3         4        120.0         71   2542         17.5   80      3
## 321 37.0         4        119.0         88   2434         15.0   80      3
## 322 32.2         4        108.0         71   2265         15.2   80      3
## 323 46.6         4         86.0         62   2110         17.9   80      3
## 324 27.9         4        156.0          5   2800         14.4   80      1
## 325 40.8         4         85.0         62   2110         19.2   80      3
## 326 44.3         4         90.0         51   2085         21.7   80      2
## 327 43.4         4         90.0         51   2335         23.7   80      2
## 328 36.4         5        121.0         64   2950         19.9   80      2
## 329 30.0         4        146.0         64   3250         21.8   80      2
## 330 44.6         4         91.0         64   1850         13.8   80      3
## 331 40.9         4         85.0          1   1835         17.3   80      2
## 332 33.8         4         97.0         64   2145         18.0   80      3
## 333 29.8         4         89.0         59   1845         15.3   80      2
## 334 32.7         6        168.0         18   2910         11.4   80      3
## 335 23.7         3         70.0          2   2420         12.5   80      3
## 336 35.0         4        122.0         84   2500         15.1   80      2
## 337 23.6         4        140.0          1   2905         14.3   80      1
## 338 32.4         4        107.0         69   2290         17.0   80      3
## 339 27.2         4        135.0         80   2490         15.7   81      1
## 340 26.6         4        151.0         80   2635         16.4   81      1
## 341 25.8         4        156.0         88   2620         14.4   81      1
## 342 23.5         6        173.0          8   2725         12.6   81      1
## 343 30.0         4        135.0         80   2385         12.9   81      1
## 344 39.1         4         79.0         56   1755         16.9   81      3
## 345 39.0         4         86.0         61   1875         16.4   81      1
## 346 35.1         4         81.0         57   1760         16.1   81      3
## 347 32.3         4         97.0         64   2065         17.8   81      3
## 348 37.0         4         85.0         62   1975         19.4   81      3
## 349 37.7         4         89.0         59   2050         17.3   81      3
## 350 34.1         4         91.0         65   1985         16.0   81      3
## 351 34.7         4        105.0         60   2215         14.9   81      1
## 352 34.4         4         98.0         62   2045         16.2   81      1
## 353 29.9         4         98.0         62   2380         20.7   81      1
## 354 33.0         4        105.0         70   2190         14.2   81      2
## 355 34.5         4        100.0          1   2320         15.8   81      2
## 356 33.7         4        107.0         71   2210         14.4   81      3
## 357 32.4         4        108.0         71   2350         16.8   81      3
## 358 32.9         4        119.0          2   2615         14.8   81      3
## 359 31.6         4        120.0         70   2635         18.3   81      3
## 360 28.1         4        141.0         76   3230         20.4   81      2
## 361 30.7         6        145.0         72   3160         19.6   81      2
## 362 25.4         6        168.0         12   2900         12.6   81      3
## 363 24.2         6        146.0         13   2930         13.8   81      3
## 364 22.4         6        231.0          8   3415         15.8   81      1
## 365 26.6         8        350.0          5   3725         19.0   81      1
## 366 20.2         6        200.0         84   3060         17.1   81      1
## 367 17.6         6        225.0         81   3465         16.6   81      1
## 368 28.0         4        112.0         84   2605         19.6   82      1
## 369 27.0         4        112.0         84   2640         18.6   82      1
## 370 34.0         4        112.0         84   2395         18.0   82      1
## 371 31.0         4        112.0         81   2575         16.2   82      1
## 372 29.0         4        135.0         80   2525         16.0   82      1
## 373 27.0         4        151.0         86   2735         18.0   82      1
## 374 24.0         4        140.0         88   2865         16.4   82      1
## 375 36.0         4        105.0         70   1980         15.3   82      2
## 376 37.0         4         91.0         65   2025         18.2   82      3
## 377 31.0         4         91.0         65   1970         17.6   82      3
## 378 38.0         4        105.0         60   2125         14.7   82      1
## 379 36.0         4         98.0         67   2125         17.3   82      1
## 380 36.0         4        120.0         84   2160         14.5   82      3
## 381 36.0         4        107.0         71   2205         14.5   82      3
## 382 34.0         4        108.0         67   2245         16.9   82      3
## 383 38.0         4         91.0         64   1965         15.0   82      3
## 384 32.0         4         91.0         64   1965         15.7   82      3
## 385 38.0         4         91.0         64   1995         16.2   82      3
## 386 25.0         6        181.0          8   2945         16.4   82      1
## 387 38.0         6        262.0         81   3015         17.0   82      1
## 388 26.0         4        156.0         88   2585         14.5   82      1
## 389 22.0         6        232.0          9   2835         14.7   82      1
## 390 32.0         4        144.0         92   2665         13.9   82      3
## 391 36.0         4        135.0         80   2370         13.0   82      1
## 392 27.0         4        151.0         86   2950         17.3   82      1
## 393 27.0         4        140.0         82   2790         15.6   82      1
## 394 44.0         4         97.0         53   2130         24.6   82      2
## 395 32.0         4        135.0         80   2295         11.6   82      1
## 396 28.0         4        120.0         75   2625         18.6   82      1
## 397 31.0         4        119.0         78   2720         19.4   82      1
##                                     name
## 1              chevrolet chevelle malibu
## 2                      buick skylark 320
## 3                     plymouth satellite
## 4                          amc rebel sst
## 5                            ford torino
## 6                       ford galaxie 500
## 7                       chevrolet impala
## 8                      plymouth fury iii
## 9                       pontiac catalina
## 10                    amc ambassador dpl
## 11                   dodge challenger se
## 12                    plymouth 'cuda 340
## 13                 chevrolet monte carlo
## 14               buick estate wagon (sw)
## 15                 toyota corona mark ii
## 16                       plymouth duster
## 17                            amc hornet
## 18                         ford maverick
## 19                          datsun pl510
## 20          volkswagen 1131 deluxe sedan
## 21                           peugeot 504
## 22                           audi 100 ls
## 23                              saab 99e
## 24                              bmw 2002
## 25                           amc gremlin
## 26                             ford f250
## 27                             chevy c20
## 28                            dodge d200
## 29                              hi 1200d
## 30                          datsun pl510
## 31                   chevrolet vega 2300
## 32                         toyota corona
## 33                            ford pinto
## 34                           amc gremlin
## 35             plymouth satellite custom
## 36             chevrolet chevelle malibu
## 37                       ford torino 500
## 38                           amc matador
## 39                      chevrolet impala
## 40             pontiac catalina brougham
## 41                      ford galaxie 500
## 42                     plymouth fury iii
## 43                     dodge monaco (sw)
## 44              ford country squire (sw)
## 45                   pontiac safari (sw)
## 46            amc hornet sportabout (sw)
## 47                   chevrolet vega (sw)
## 48                      pontiac firebird
## 49                          ford mustang
## 50                    mercury capri 2000
## 51                             opel 1900
## 52                           peugeot 304
## 53                             fiat 124b
## 54                   toyota corolla 1200
## 55                           datsun 1200
## 56                  volkswagen model 111
## 57                      plymouth cricket
## 58                 toyota corona hardtop
## 59                    dodge colt hardtop
## 60                     volkswagen type 3
## 61                        chevrolet vega
## 62                   ford pinto runabout
## 63                      chevrolet impala
## 64                      pontiac catalina
## 65                     plymouth fury iii
## 66                      ford galaxie 500
## 67                    amc ambassador sst
## 68                       mercury marquis
## 69                  buick lesabre custom
## 70            oldsmobile delta 88 royale
## 71                chrysler newport royal
## 72                       mazda rx2 coupe
## 73                      amc matador (sw)
## 74      chevrolet chevelle concours (sw)
## 75                 ford gran torino (sw)
## 76        plymouth satellite custom (sw)
## 77                       volvo 145e (sw)
## 78                   volkswagen 411 (sw)
## 79                      peugeot 504 (sw)
## 80                       renault 12 (sw)
## 81                       ford pinto (sw)
## 82                       datsun 510 (sw)
## 83           toyouta corona mark ii (sw)
## 84                       dodge colt (sw)
## 85              toyota corolla 1600 (sw)
## 86                     buick century 350
## 87                           amc matador
## 88                      chevrolet malibu
## 89                      ford gran torino
## 90                  dodge coronet custom
## 91              mercury marquis brougham
## 92             chevrolet caprice classic
## 93                              ford ltd
## 94              plymouth fury gran sedan
## 95          chrysler new yorker brougham
## 96              buick electra 225 custom
## 97               amc ambassador brougham
## 98                      plymouth valiant
## 99                 chevrolet nova custom
## 100                           amc hornet
## 101                        ford maverick
## 102                      plymouth duster
## 103              volkswagen super beetle
## 104                     chevrolet impala
## 105                         ford country
## 106               plymouth custom suburb
## 107             oldsmobile vista cruiser
## 108                          amc gremlin
## 109                        toyota carina
## 110                       chevrolet vega
## 111                           datsun 610
## 112                            maxda rx3
## 113                           ford pinto
## 114                     mercury capri v6
## 115                 fiat 124 sport coupe
## 116              chevrolet monte carlo s
## 117                   pontiac grand prix
## 118                             fiat 128
## 119                           opel manta
## 120                           audi 100ls
## 121                          volvo 144ea
## 122                    dodge dart custom
## 123                            saab 99le
## 124                       toyota mark ii
## 125                     oldsmobile omega
## 126                      plymouth duster
## 127                        ford maverick
## 128                           amc hornet
## 129                       chevrolet nova
## 130                          datsun b210
## 131                           ford pinto
## 132                  toyota corolla 1200
## 133                       chevrolet vega
## 134    chevrolet chevelle malibu classic
## 135                          amc matador
## 136           plymouth satellite sebring
## 137                     ford gran torino
## 138             buick century luxus (sw)
## 139            dodge coronet custom (sw)
## 140                ford gran torino (sw)
## 141                     amc matador (sw)
## 142                             audi fox
## 143                    volkswagen dasher
## 144                           opel manta
## 145                        toyota corona
## 146                           datsun 710
## 147                           dodge colt
## 148                             fiat 128
## 149                          fiat 124 tc
## 150                          honda civic
## 151                               subaru
## 152                            fiat x1.9
## 153              plymouth valiant custom
## 154                       chevrolet nova
## 155                      mercury monarch
## 156                        ford maverick
## 157                     pontiac catalina
## 158                    chevrolet bel air
## 159                  plymouth grand fury
## 160                             ford ltd
## 161                        buick century
## 162            chevroelt chevelle malibu
## 163                          amc matador
## 164                        plymouth fury
## 165                        buick skyhawk
## 166                  chevrolet monza 2+2
## 167                      ford mustang ii
## 168                       toyota corolla
## 169                           ford pinto
## 170                          amc gremlin
## 171                        pontiac astro
## 172                        toyota corona
## 173                    volkswagen dasher
## 174                           datsun 710
## 175                           ford pinto
## 176                    volkswagen rabbit
## 177                            amc pacer
## 178                           audi 100ls
## 179                          peugeot 504
## 180                          volvo 244dl
## 181                            saab 99le
## 182                     honda civic cvcc
## 183                             fiat 131
## 184                            opel 1900
## 185                             capri ii
## 186                           dodge colt
## 187                         renault 12tl
## 188    chevrolet chevelle malibu classic
## 189               dodge coronet brougham
## 190                          amc matador
## 191                     ford gran torino
## 192                     plymouth valiant
## 193                       chevrolet nova
## 194                        ford maverick
## 195                           amc hornet
## 196                   chevrolet chevette
## 197                      chevrolet woody
## 198                            vw rabbit
## 199                          honda civic
## 200                       dodge aspen se
## 201                    ford granada ghia
## 202                   pontiac ventura sj
## 203                        amc pacer d/l
## 204                    volkswagen rabbit
## 205                         datsun b-210
## 206                       toyota corolla
## 207                           ford pinto
## 208                            volvo 245
## 209           plymouth volare premier v8
## 210                          peugeot 504
## 211                       toyota mark ii
## 212                   mercedes-benz 280s
## 213                     cadillac seville
## 214                            chevy c10
## 215                            ford f108
## 216                           dodge d100
## 217                    honda accord cvcc
## 218              buick opel isuzu deluxe
## 219                        renault 5 gtl
## 220                    plymouth arrow gs
## 221                datsun f-10 hatchback
## 222            chevrolet caprice classic
## 223           oldsmobile cutlass supreme
## 224                dodge monaco brougham
## 225              mercury cougar brougham
## 226                   chevrolet concours
## 227                        buick skylark
## 228               plymouth volare custom
## 229                         ford granada
## 230                pontiac grand prix lj
## 231         chevrolet monte carlo landau
## 232                     chrysler cordoba
## 233                     ford thunderbird
## 234             volkswagen rabbit custom
## 235                pontiac sunbird coupe
## 236              toyota corolla liftback
## 237                  ford mustang ii 2+2
## 238                   chevrolet chevette
## 239                       dodge colt m/m
## 240                            subaru dl
## 241                    volkswagen dasher
## 242                           datsun 810
## 243                             bmw 320i
## 244                           mazda rx-4
## 245      volkswagen rabbit custom diesel
## 246                          ford fiesta
## 247                     mazda glc deluxe
## 248                       datsun b210 gx
## 249                     honda civic cvcc
## 250    oldsmobile cutlass salon brougham
## 251                       dodge diplomat
## 252                 mercury monarch ghia
## 253                   pontiac phoenix lj
## 254                     chevrolet malibu
## 255                 ford fairmont (auto)
## 256                  ford fairmont (man)
## 257                      plymouth volare
## 258                          amc concord
## 259                buick century special
## 260                       mercury zephyr
## 261                          dodge aspen
## 262                      amc concord d/l
## 263         chevrolet monte carlo landau
## 264      buick regal sport coupe (turbo)
## 265                          ford futura
## 266                      dodge magnum xe
## 267                   chevrolet chevette
## 268                        toyota corona
## 269                           datsun 510
## 270                           dodge omni
## 271            toyota celica gt liftback
## 272                     plymouth sapporo
## 273               oldsmobile starfire sx
## 274                        datsun 200-sx
## 275                            audi 5000
## 276                          volvo 264gl
## 277                           saab 99gle
## 278                        peugeot 604sl
## 279                  volkswagen scirocco
## 280                      honda accord lx
## 281                    pontiac lemans v6
## 282                     mercury zephyr 6
## 283                      ford fairmont 4
## 284                     amc concord dl 6
## 285                        dodge aspen 6
## 286            chevrolet caprice classic
## 287                      ford ltd landau
## 288                mercury grand marquis
## 289                      dodge st. regis
## 290              buick estate wagon (sw)
## 291             ford country squire (sw)
## 292        chevrolet malibu classic (sw)
## 293 chrysler lebaron town @ country (sw)
## 294                     vw rabbit custom
## 295                     maxda glc deluxe
## 296          dodge colt hatchback custom
## 297                        amc spirit dl
## 298                   mercedes benz 300d
## 299                    cadillac eldorado
## 300                          peugeot 504
## 301    oldsmobile cutlass salon brougham
## 302                     plymouth horizon
## 303                 plymouth horizon tc3
## 304                           datsun 210
## 305                   fiat strada custom
## 306                buick skylark limited
## 307                   chevrolet citation
## 308            oldsmobile omega brougham
## 309                      pontiac phoenix
## 310                            vw rabbit
## 311                toyota corolla tercel
## 312                   chevrolet chevette
## 313                           datsun 310
## 314                   chevrolet citation
## 315                        ford fairmont
## 316                          amc concord
## 317                          dodge aspen
## 318                            audi 4000
## 319               toyota corona liftback
## 320                            mazda 626
## 321                 datsun 510 hatchback
## 322                       toyota corolla
## 323                            mazda glc
## 324                           dodge colt
## 325                           datsun 210
## 326                 vw rabbit c (diesel)
## 327                   vw dasher (diesel)
## 328                  audi 5000s (diesel)
## 329                   mercedes-benz 240d
## 330                  honda civic 1500 gl
## 331                 renault lecar deluxe
## 332                            subaru dl
## 333                     vokswagen rabbit
## 334                        datsun 280-zx
## 335                        mazda rx-7 gs
## 336                    triumph tr7 coupe
## 337                   ford mustang cobra
## 338                         honda accord
## 339                     plymouth reliant
## 340                        buick skylark
## 341               dodge aries wagon (sw)
## 342                   chevrolet citation
## 343                     plymouth reliant
## 344                       toyota starlet
## 345                       plymouth champ
## 346                     honda civic 1300
## 347                               subaru
## 348                       datsun 210 mpg
## 349                        toyota tercel
## 350                          mazda glc 4
## 351                   plymouth horizon 4
## 352                       ford escort 4w
## 353                       ford escort 2h
## 354                     volkswagen jetta
## 355                          renault 18i
## 356                        honda prelude
## 357                       toyota corolla
## 358                         datsun 200sx
## 359                            mazda 626
## 360            peugeot 505s turbo diesel
## 361                         volvo diesel
## 362                      toyota cressida
## 363                    datsun 810 maxima
## 364                        buick century
## 365                oldsmobile cutlass ls
## 366                      ford granada gl
## 367               chrysler lebaron salon
## 368                   chevrolet cavalier
## 369             chevrolet cavalier wagon
## 370            chevrolet cavalier 2-door
## 371           pontiac j2000 se hatchback
## 372                       dodge aries se
## 373                      pontiac phoenix
## 374                 ford fairmont futura
## 375                  volkswagen rabbit l
## 376                   mazda glc custom l
## 377                     mazda glc custom
## 378               plymouth horizon miser
## 379                       mercury lynx l
## 380                     nissan stanza xe
## 381                         honda accord
## 382                       toyota corolla
## 383                          honda civic
## 384                   honda civic (auto)
## 385                        datsun 310 gx
## 386                buick century limited
## 387    oldsmobile cutlass ciera (diesel)
## 388           chrysler lebaron medallion
## 389                       ford granada l
## 390                     toyota celica gt
## 391                    dodge charger 2.2
## 392                     chevrolet camaro
## 393                      ford mustang gl
## 394                            vw pickup
## 395                        dodge rampage
## 396                          ford ranger
## 397                           chevy s-10
auto = Auto[,-9]
cor(auto)
##                     mpg  cylinders displacement horsepower     weight
## mpg           1.0000000 -0.7762599   -0.8044430  0.4228227 -0.8317389
## cylinders    -0.7762599  1.0000000    0.9509199 -0.5466585  0.8970169
## displacement -0.8044430  0.9509199    1.0000000 -0.4820705  0.9331044
## horsepower    0.4228227 -0.5466585   -0.4820705  1.0000000 -0.4821507
## weight       -0.8317389  0.8970169    0.9331044 -0.4821507  1.0000000
## acceleration  0.4222974 -0.5040606   -0.5441618  0.2662877 -0.4195023
## year          0.5814695 -0.3467172   -0.3698041  0.1274167 -0.3079004
## origin        0.5636979 -0.5649716   -0.6106643  0.2973734 -0.5812652
##              acceleration       year     origin
## mpg             0.4222974  0.5814695  0.5636979
## cylinders      -0.5040606 -0.3467172 -0.5649716
## displacement   -0.5441618 -0.3698041 -0.6106643
## horsepower      0.2662877  0.1274167  0.2973734
## weight         -0.4195023 -0.3079004 -0.5812652
## acceleration    1.0000000  0.2829009  0.2100836
## year            0.2829009  1.0000000  0.1843141
## origin          0.2100836  0.1843141  1.0000000
auto$origin = factor(auto$origin, labels = c("American", "European", "Japanese"))

mpg_lm = lm(mpg ~ . , data = auto)
summary(mpg_lm)
## 
## Call:
## lm(formula = mpg ~ ., data = auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.9699 -2.0621  0.0693  1.9776 13.3807 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    -2.278e+01  4.284e+00  -5.318 1.77e-07 ***
## cylinders      -2.569e-01  3.353e-01  -0.766  0.44415    
## displacement    2.018e-02  7.361e-03   2.741  0.00641 ** 
## horsepower      9.850e-03  6.780e-03   1.453  0.14710    
## weight         -7.152e-03  5.831e-04 -12.267  < 2e-16 ***
## acceleration    1.579e-01  7.681e-02   2.056  0.04048 *  
## year            8.036e-01  5.006e-02  16.053  < 2e-16 ***
## originEuropean  2.728e+00  5.514e-01   4.947 1.13e-06 ***
## originJapanese  2.665e+00  5.333e-01   4.997 8.82e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.301 on 388 degrees of freedom
## Multiple R-squared:  0.8257, Adjusted R-squared:  0.8221 
## F-statistic: 229.8 on 8 and 388 DF,  p-value: < 2.2e-16
  1. There is a relationship between predictors and response. The p-vlaue is very small 2.2e-16. When looking at each predictor we see cylinders and horsepower however do not have significant p-values.

  2. Displacement, Weight, Acceleration, Year, and Origin do have a significant relationship to mpg

  3. Year coef = 0.8036. We interpret this as a 1 year increase in the car will result in an increase of 0.8036 in mpg. As cars become more modern their mpg goes up!

par(mfrow = c(2,2))
plot(mpg_lm)

The residual plot has a slight curve to it. This might suggest there is non-linearity among the relationships. Observation 323 and 327 appear in the residual plot and the qq plot. Observation 14 shows up with a very high leverage compared to other points.

summary(lm(formula = mpg ~ . * ., data = auto))
## 
## Call:
## lm(formula = mpg ~ . * ., data = auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.8783 -1.3913 -0.0053  1.2860 12.2457 
## 
## Coefficients:
##                               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                  1.016e+02  4.285e+01   2.370 0.018296 *  
## cylinders                    3.141e+00  7.716e+00   0.407 0.684219    
## displacement                -2.563e-01  1.743e-01  -1.471 0.142193    
## horsepower                   6.841e-02  1.432e-01   0.478 0.633173    
## weight                       1.026e-03  1.544e-02   0.066 0.947081    
## acceleration                -8.780e+00  1.636e+00  -5.366 1.44e-07 ***
## year                        -2.623e-01  5.289e-01  -0.496 0.620219    
## originEuropean              -4.071e+01  1.148e+01  -3.546 0.000442 ***
## originJapanese              -3.126e+01  1.396e+01  -2.239 0.025782 *  
## cylinders:displacement      -2.737e-03  5.918e-03  -0.462 0.644018    
## cylinders:horsepower         4.230e-03  1.108e-02   0.382 0.702852    
## cylinders:weight             6.569e-04  7.700e-04   0.853 0.394141    
## cylinders:acceleration       2.857e-01  1.339e-01   2.134 0.033476 *  
## cylinders:year              -1.235e-01  1.019e-01  -1.212 0.226276    
## cylinders:originEuropean    -6.225e-01  1.119e+00  -0.556 0.578395    
## cylinders:originJapanese     1.049e+00  1.261e+00   0.833 0.405656    
## displacement:horsepower      1.374e-04  3.020e-04   0.455 0.649468    
## displacement:weight          1.466e-05  7.742e-06   1.893 0.059145 .  
## displacement:acceleration   -1.435e-03  2.714e-03  -0.529 0.597347    
## displacement:year            3.155e-03  2.307e-03   1.368 0.172201    
## displacement:originEuropean -3.426e-02  4.151e-02  -0.825 0.409762    
## displacement:originJapanese  7.092e-02  4.782e-02   1.483 0.138945    
## horsepower:weight           -2.426e-05  2.318e-05  -1.047 0.295924    
## horsepower:acceleration     -6.044e-04  3.431e-03  -0.176 0.860246    
## horsepower:year             -5.648e-04  1.791e-03  -0.315 0.752652    
## horsepower:originEuropean    2.916e-02  2.124e-02   1.373 0.170702    
## horsepower:originJapanese    1.375e-03  2.663e-02   0.052 0.958846    
## weight:acceleration         -4.947e-05  2.003e-04  -0.247 0.805034    
## weight:year                 -1.691e-04  1.796e-04  -0.942 0.347028    
## weight:originEuropean        2.306e-03  2.341e-03   0.985 0.325219    
## weight:originJapanese       -4.380e-03  2.829e-03  -1.548 0.122513    
## acceleration:year            9.784e-02  1.939e-02   5.046 7.18e-07 ***
## acceleration:originEuropean  1.043e+00  1.930e-01   5.403 1.19e-07 ***
## acceleration:originJapanese  7.133e-01  2.655e-01   2.686 0.007558 ** 
## year:originEuropean          3.081e-01  1.391e-01   2.214 0.027422 *  
## year:originJapanese          2.353e-01  1.412e-01   1.666 0.096518 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.649 on 361 degrees of freedom
## Multiple R-squared:  0.8955, Adjusted R-squared:  0.8854 
## F-statistic: 88.41 on 35 and 361 DF,  p-value: < 2.2e-16

I used the * to test all interactions since we have a relatively low number of predictors. Some of the interactions do appear statistically significant. Cylinders:Acceleration acceleration:year acceleration:origionEuropean acceleration:originJapanese year:originEurope all have some significance.

lm.fit2 = lm(formula = log(mpg) ~ ., data = auto)
lm.fit3 = lm(formula = mpg^2 ~., data = auto)
summary(lm.fit2)
## 
## Call:
## lm(formula = log(mpg) ~ ., data = auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.39718 -0.06971  0.00706  0.06908  0.34177 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     1.4171502  0.1542685   9.186  < 2e-16 ***
## cylinders      -0.0170756  0.0120760  -1.414 0.158161    
## displacement    0.0004969  0.0002651   1.875 0.061609 .  
## horsepower      0.0003565  0.0002442   1.460 0.145112    
## weight         -0.0002967  0.0000210 -14.132  < 2e-16 ***
## acceleration    0.0052528  0.0027659   1.899 0.058290 .  
## year            0.0320410  0.0018025  17.775  < 2e-16 ***
## originEuropean  0.0810993  0.0198565   4.084 5.37e-05 ***
## originJapanese  0.0662720  0.0192057   3.451 0.000621 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1189 on 388 degrees of freedom
## Multiple R-squared:  0.8803, Adjusted R-squared:  0.8778 
## F-statistic: 356.7 on 8 and 388 DF,  p-value: < 2.2e-16
summary(lm.fit3)
## 
## Call:
## lm(formula = mpg^2 ~ ., data = auto)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -473.56 -138.76  -22.94  103.34 1061.09 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    -2.093e+03  2.708e+02  -7.727 9.51e-14 ***
## cylinders      -4.248e+00  2.120e+01  -0.200  0.84131    
## displacement    1.490e+00  4.654e-01   3.202  0.00148 ** 
## horsepower      4.435e-01  4.287e-01   1.035  0.30154    
## weight         -3.816e-01  3.686e-02 -10.351  < 2e-16 ***
## acceleration    1.111e+01  4.856e+00   2.287  0.02274 *  
## year            4.357e+01  3.165e+00  13.766  < 2e-16 ***
## originEuropean  1.798e+02  3.486e+01   5.158 4.00e-07 ***
## originJapanese  1.857e+02  3.372e+01   5.507 6.66e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 208.7 on 388 degrees of freedom
## Multiple R-squared:  0.7357, Adjusted R-squared:  0.7302 
## F-statistic:   135 on 8 and 388 DF,  p-value: < 2.2e-16

When doing some transformations I found no significant changes to which predictors are significant to mpg for squaring or taking the log.

10

library(ISLR)
## Warning: package 'ISLR' was built under R version 3.6.3
## 
## Attaching package: 'ISLR'
## The following object is masked _by_ '.GlobalEnv':
## 
##     Auto
sales_lm = lm(Sales ~ Price + Urban + US, data = Carseats)
summary(sales_lm)
## 
## Call:
## lm(formula = Sales ~ Price + Urban + US, data = Carseats)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.9206 -1.6220 -0.0564  1.5786  7.0581 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 13.043469   0.651012  20.036  < 2e-16 ***
## Price       -0.054459   0.005242 -10.389  < 2e-16 ***
## UrbanYes    -0.021916   0.271650  -0.081    0.936    
## USYes        1.200573   0.259042   4.635 4.86e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.472 on 396 degrees of freedom
## Multiple R-squared:  0.2393, Adjusted R-squared:  0.2335 
## F-statistic: 41.52 on 3 and 396 DF,  p-value: < 2.2e-16
  1. Urban is not significant to the model. Price and US are significant predictors to Sales. A 1 unit increase in price results in a -0.054 drop in sales. Being built in the US increases the likelyhood/number of sales.

  2. Sales = 13.04 - 0.054 * Price + 1.20 * USYes if we want to include Urban Sales = 13.04 - 0.054 * Price - 0.022 * UrbanYes + 1.20 * USYES

  3. We can reject the null for Price and US predictors as they have a very low p-value. We should keep them in the model and remove UrbanYes

sales2 = lm(Sales ~ Price + US, data = Carseats)
summary(sales2)
## 
## Call:
## lm(formula = Sales ~ Price + US, data = Carseats)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.9269 -1.6286 -0.0574  1.5766  7.0515 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 13.03079    0.63098  20.652  < 2e-16 ***
## Price       -0.05448    0.00523 -10.416  < 2e-16 ***
## USYes        1.19964    0.25846   4.641 4.71e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.469 on 397 degrees of freedom
## Multiple R-squared:  0.2393, Adjusted R-squared:  0.2354 
## F-statistic: 62.43 on 2 and 397 DF,  p-value: < 2.2e-16
  1. The model in a) Multiple R-squared: 0.2393, Adjusted R-squared: 0.2335 The model in e) Multiple R-squared: 0.2393, Adjusted R-squared: 0.2354 Both models with and without the UrbanYes explain about 23-24% of the variance.

confint(sales2, level = 0.95)
##                   2.5 %      97.5 %
## (Intercept) 11.79032020 14.27126531
## Price       -0.06475984 -0.04419543
## USYes        0.69151957  1.70776632
par(mfrow = c(2,2))
plot(sales2)

  1. Residual graph is a very nice looking straight line with no evident urvature. QQ plot is virtually a straight line. The leverage plot does seem to have one point farther than the others.

12

  1. This will happen if the sum of the x^2 and y^2 values are the same.

set.seed(1)
x = 1:100
sum(x^2)
## [1] 338350
y = 2 * x + rnorm(100, sd = 0.1)
sum(y^2)
## [1] 1353606
regX = lm(x ~ y)
regY = lm(y ~ x)
summary(regX)
## 
## Call:
## lm(formula = x ~ y)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.114850 -0.029264 -0.000719  0.030278  0.116917 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -6.462e-03  9.096e-03   -0.71    0.479    
## y            5.000e-01  7.818e-05 6395.53   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04513 on 98 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 4.09e+07 on 1 and 98 DF,  p-value: < 2.2e-16
summary(regY)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.234005 -0.060584  0.001551  0.058514  0.229747 
## 
## Coefficients:
##              Estimate Std. Error  t value Pr(>|t|)    
## (Intercept) 0.0131666  0.0181897    0.724    0.471    
## x           1.9999549  0.0003127 6395.532   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09027 on 98 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 4.09e+07 on 1 and 98 DF,  p-value: < 2.2e-16
x = 1:100
sum(x^2)
## [1] 338350
y = 100:1
sum(y^2)
## [1] 338350
xval = lm(x ~ y)
yval = lm(y ~ x)
summary(xval)
## Warning in summary.lm(xval): essentially perfect fit: summary may be
## unreliable
## 
## Call:
## lm(formula = x ~ y)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -2.680e-13 -4.300e-16  2.850e-15  5.302e-15  3.575e-14 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)  1.010e+02  5.598e-15  1.804e+16   <2e-16 ***
## y           -1.000e+00  9.624e-17 -1.039e+16   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.778e-14 on 98 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 1.08e+32 on 1 and 98 DF,  p-value: < 2.2e-16
summary(yval)
## Warning in summary.lm(yval): essentially perfect fit: summary may be
## unreliable
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -3.575e-14 -5.302e-15 -2.850e-15  4.300e-16  2.680e-13 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)  1.010e+02  5.598e-15  1.804e+16   <2e-16 ***
## x           -1.000e+00  9.624e-17 -1.039e+16   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.778e-14 on 98 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 1.08e+32 on 1 and 98 DF,  p-value: < 2.2e-16