1. Load the mpg excel file into R Markdown, and convert the dataset into a data frame.

library(readxl)
#mpg data file
mpgdata <- read_excel("G:/Other computers/My Laptop/Documents/Richard 621/Week 11/mpg.xlsx")
# made data set a data frame
mpgdata <- as.data.frame(mpgdata)

2. Using all rows of the dataset as your training set, create a linear regression model to predict mpg based on acceleration. What is the adjusted R-squared for your model?

#linear regression model
mpgmodel1 <- lm(mpg~., data = mpgdata)
summary(mpgmodel1)
## 
## Call:
## lm(formula = mpg ~ ., data = mpgdata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -18.007  -5.636  -1.242   4.758  23.192 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    4.9698     2.0432   2.432   0.0154 *  
## acceleration   1.1912     0.1292   9.217   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.101 on 396 degrees of freedom
## Multiple R-squared:  0.1766, Adjusted R-squared:  0.1746 
## F-statistic: 84.96 on 1 and 396 DF,  p-value: < 2.2e-16
# Adjusted R-squared:  0.1746 for this model

3. Determine if a Box-Cox transformation would be beneficial. If so, perform the transformation. What is your adjusted R-squared value for your model now? Did it improve after applying a Box-Cox transformation?

#Based on the plot below a box cox transformation would be beneficial since the data does not stay linear and 
#instead travels away from the horizontal line
plot(mpgmodel1$fitted.values, mpgmodel1$residuals)
abline(h = 0)

# The center line below is closest to zero which means a log of the data should improve the model
library(MASS)
boxcox(mpgmodel1)

mpgmodel2 <- lm(I(log(mpg)) ~ ., data = mpgdata)
summary(mpgmodel2)
## 
## Call:
## lm(formula = I(log(mpg)) ~ ., data = mpgdata)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.06515 -0.23641 -0.00943  0.23576  0.79343 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.24656    0.08759  25.648   <2e-16 ***
## acceleration  0.05491    0.00554   9.911   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3044 on 396 degrees of freedom
## Multiple R-squared:  0.1987, Adjusted R-squared:  0.1967 
## F-statistic: 98.23 on 1 and 396 DF,  p-value: < 2.2e-16
# Initial adjusted Adjusted R-squared:  0.1746 
# After transformation Adjusted R-squared:  0.1967, the model was improved after applying the box-cox 
#transformation

4. Create a scatter plot comparing mpg and acceleration. Does the relationship between these two variables appear to be perfectly linear? Or is there perhaps some slight nonlinearity in the relationship?

#  The plots below do not show perfect linearity and that there is some non-linearity in the relationship
pairs(mpgdata)

5. Create a new regression model to predict the Box-Cox transformation of mpg using both acceleration and a transformation of acceleration. Look at the “Common Transformation on Covariates” document in the “Week 11” folder of our Google Drive to determine some appropriate transformations. Which of these 4 “common transformations” yields the highest adjusted R-squared?

mpgmodel 3 has the highest R-Squared at 0.2234

mpgmodel3 <- lm(I(log(mpg)) ~ acceleration + I(acceleration^2), data = mpgdata)
summary(mpgmodel3)
## 
## Call:
## lm(formula = I(log(mpg)) ~ acceleration + I(acceleration^2), 
##     data = mpgdata)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.07126 -0.22527 -0.00066  0.21838  0.77803 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        1.023320   0.331575   3.086 0.002170 ** 
## acceleration       0.213095   0.041764   5.102 5.22e-07 ***
## I(acceleration^2) -0.004959   0.001298  -3.820 0.000155 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2993 on 395 degrees of freedom
## Multiple R-squared:  0.2273, Adjusted R-squared:  0.2234 
## F-statistic:  58.1 on 2 and 395 DF,  p-value: < 2.2e-16
#Adjusted R-squared:  0.2234
mpgmodel4 <- lm(I(log(mpg)) ~ acceleration + I(1/acceleration), data = mpgdata)
summary(mpgmodel4)
## 
## Call:
## lm(formula = I(log(mpg)) ~ acceleration + I(1/acceleration), 
##     data = mpgdata)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.05749 -0.22920  0.00108  0.22127  0.76895 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         4.26294    0.58605   7.274 1.89e-12 ***
## acceleration       -0.01068    0.01963  -0.544  0.58682    
## I(1/acceleration) -14.99800    4.31148  -3.479  0.00056 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3002 on 395 degrees of freedom
## Multiple R-squared:  0.2226, Adjusted R-squared:  0.2186 
## F-statistic: 56.54 on 2 and 395 DF,  p-value: < 2.2e-16
#Adjusted R-squared:  0.2186
mpgmodel5 <- lm(I(log(mpg)) ~ acceleration + (I(log(acceleration))), data = mpgdata)
summary(mpgmodel5)
## 
## Call:
## lm(formula = I(log(mpg)) ~ acceleration + (I(log(acceleration))), 
##     data = mpgdata)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.06111 -0.22515  0.00151  0.21794  0.77069 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          -1.59298    1.04365  -1.526 0.127724    
## acceleration         -0.09011    0.03966  -2.272 0.023624 *  
## I(log(acceleration))  2.23400    0.60516   3.692 0.000254 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2997 on 395 degrees of freedom
## Multiple R-squared:  0.2255, Adjusted R-squared:  0.2215 
## F-statistic: 57.49 on 2 and 395 DF,  p-value: < 2.2e-16
#Adjusted R-squared:  0.2215 
mpgmodel6 <- lm(I(log(mpg)) ~ acceleration + I(sqrt(acceleration)), data = mpgdata)
summary(mpgmodel6)
## 
## Call:
## lm(formula = I(log(mpg)) ~ acceleration + I(sqrt(acceleration)), 
##     data = mpgdata)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.06339 -0.22731  0.00077  0.21655  0.77214 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -2.38791    1.23506  -1.933 0.053897 .  
## acceleration          -0.24561    0.08008  -3.067 0.002310 ** 
## I(sqrt(acceleration))  2.36968    0.62997   3.762 0.000194 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2995 on 395 degrees of freedom
## Multiple R-squared:  0.2265, Adjusted R-squared:  0.2225 
## F-statistic: 57.82 on 2 and 395 DF,  p-value: < 2.2e-16
#Adjusted R-squared:  0.2225

6. Using the “data.frame” function, create a new data frame with the following three variables:

• The Box-Cox transformation of mpg

• acceleration

• The transformation of acceleration that yielded the best adjusted R-squared in the preceding question

#data frame
mpg_data1 <- data.frame(log_mpg=log(mpgdata$mpg), acceleration = mpgdata$acceleration, acceleration_sq = (mpgdata$acceleration)^2)
mpg_data1
##      log_mpg acceleration acceleration_sq
## 1   2.890372         12.0          144.00
## 2   2.708050         11.5          132.25
## 3   2.890372         11.0          121.00
## 4   2.772589         12.0          144.00
## 5   2.833213         10.5          110.25
## 6   2.708050         10.0          100.00
## 7   2.639057          9.0           81.00
## 8   2.639057          8.5           72.25
## 9   2.639057         10.0          100.00
## 10  2.708050          8.5           72.25
## 11  2.708050         10.0          100.00
## 12  2.639057          8.0           64.00
## 13  2.708050          9.5           90.25
## 14  2.639057         10.0          100.00
## 15  3.178054         15.0          225.00
## 16  3.091042         15.5          240.25
## 17  2.890372         15.5          240.25
## 18  3.044522         16.0          256.00
## 19  3.295837         14.5          210.25
## 20  3.258097         20.5          420.25
## 21  3.218876         17.5          306.25
## 22  3.178054         14.5          210.25
## 23  3.218876         17.5          306.25
## 24  3.258097         12.5          156.25
## 25  3.044522         15.0          225.00
## 26  2.302585         14.0          196.00
## 27  2.302585         15.0          225.00
## 28  2.397895         13.5          182.25
## 29  2.197225         18.5          342.25
## 30  3.295837         14.5          210.25
## 31  3.332205         15.5          240.25
## 32  3.218876         14.0          196.00
## 33  3.218876         19.0          361.00
## 34  2.944439         13.0          169.00
## 35  2.772589         15.5          240.25
## 36  2.833213         15.5          240.25
## 37  2.944439         15.5          240.25
## 38  2.890372         15.5          240.25
## 39  2.639057         12.0          144.00
## 40  2.639057         11.5          132.25
## 41  2.639057         13.5          182.25
## 42  2.639057         13.0          169.00
## 43  2.484907         11.5          132.25
## 44  2.564949         12.0          144.00
## 45  2.564949         12.0          144.00
## 46  2.890372         13.5          182.25
## 47  3.091042         19.0          361.00
## 48  2.944439         15.0          225.00
## 49  2.890372         14.5          210.25
## 50  3.135494         14.0          196.00
## 51  3.332205         14.0          196.00
## 52  3.401197         19.5          380.25
## 53  3.401197         14.5          210.25
## 54  3.433987         19.0          361.00
## 55  3.555348         18.0          324.00
## 56  3.295837         19.0          361.00
## 57  3.258097         20.5          420.25
## 58  3.178054         15.5          240.25
## 59  3.218876         17.0          289.00
## 60  3.135494         23.5          552.25
## 61  2.995732         19.5          380.25
## 62  3.044522         16.5          272.25
## 63  2.564949         12.0          144.00
## 64  2.639057         12.0          144.00
## 65  2.708050         13.5          182.25
## 66  2.639057         13.0          169.00
## 67  2.833213         11.5          132.25
## 68  2.397895         11.0          121.00
## 69  2.564949         13.5          182.25
## 70  2.484907         13.5          182.25
## 71  2.564949         12.5          156.25
## 72  2.944439         13.5          182.25
## 73  2.708050         12.5          156.25
## 74  2.564949         14.0          196.00
## 75  2.564949         16.0          256.00
## 76  2.639057         14.0          196.00
## 77  2.890372         14.5          210.25
## 78  3.091042         18.0          324.00
## 79  3.044522         19.5          380.25
## 80  3.258097         18.0          324.00
## 81  3.091042         16.0          256.00
## 82  3.332205         17.0          289.00
## 83  3.135494         14.5          210.25
## 84  3.332205         15.0          225.00
## 85  3.295837         16.5          272.25
## 86  2.564949         13.0          169.00
## 87  2.639057         11.5          132.25
## 88  2.564949         13.0          169.00
## 89  2.639057         14.5          210.25
## 90  2.708050         12.5          156.25
## 91  2.484907         11.5          132.25
## 92  2.564949         12.0          144.00
## 93  2.564949         13.0          169.00
## 94  2.639057         14.5          210.25
## 95  2.564949         11.0          121.00
## 96  2.484907         11.0          121.00
## 97  2.564949         11.0          121.00
## 98  2.890372         16.5          272.25
## 99  2.772589         18.0          324.00
## 100 2.890372         16.0          256.00
## 101 2.890372         16.5          272.25
## 102 3.135494         16.0          256.00
## 103 3.258097         21.0          441.00
## 104 2.397895         14.0          196.00
## 105 2.484907         12.5          156.25
## 106 2.564949         13.0          169.00
## 107 2.484907         12.5          156.25
## 108 2.890372         15.0          225.00
## 109 2.995732         19.0          361.00
## 110 3.044522         19.5          380.25
## 111 3.091042         16.5          272.25
## 112 2.890372         13.5          182.25
## 113 2.944439         18.5          342.25
## 114 3.044522         14.0          196.00
## 115 3.258097         15.5          240.25
## 116 2.708050         13.0          169.00
## 117 2.772589          9.5           90.25
## 118 3.367296         19.5          380.25
## 119 3.178054         15.5          240.25
## 120 2.995732         14.0          196.00
## 121 2.944439         15.5          240.25
## 122 2.708050         11.0          121.00
## 123 3.178054         14.0          196.00
## 124 2.995732         13.5          182.25
## 125 2.397895         11.0          121.00
## 126 2.995732         16.5          272.25
## 127 3.044522         17.0          289.00
## 128 2.944439         16.0          256.00
## 129 2.708050         17.0          289.00
## 130 3.433987         19.0          361.00
## 131 3.258097         16.5          272.25
## 132 3.465736         21.0          441.00
## 133 3.218876         17.0          289.00
## 134 2.772589         17.0          289.00
## 135 2.772589         18.0          324.00
## 136 2.890372         16.5          272.25
## 137 2.772589         14.0          196.00
## 138 2.564949         14.5          210.25
## 139 2.639057         13.5          182.25
## 140 2.639057         16.0          256.00
## 141 2.639057         15.5          240.25
## 142 3.367296         16.5          272.25
## 143 3.258097         15.5          240.25
## 144 3.258097         14.5          210.25
## 145 3.433987         16.5          272.25
## 146 3.465736         19.0          361.00
## 147 3.332205         14.5          210.25
## 148 3.178054         15.5          240.25
## 149 3.258097         14.0          196.00
## 150 3.178054         15.0          225.00
## 151 3.258097         15.5          240.25
## 152 3.433987         16.0          256.00
## 153 2.944439         16.0          256.00
## 154 2.890372         16.0          256.00
## 155 2.708050         21.0          441.00
## 156 2.708050         19.5          380.25
## 157 2.772589         11.5          132.25
## 158 2.708050         14.0          196.00
## 159 2.772589         14.5          210.25
## 160 2.639057         13.5          182.25
## 161 2.833213         21.0          441.00
## 162 2.772589         18.5          342.25
## 163 2.708050         19.0          361.00
## 164 2.890372         19.0          361.00
## 165 3.044522         15.0          225.00
## 166 2.995732         13.5          182.25
## 167 2.564949         12.0          144.00
## 168 3.367296         16.0          256.00
## 169 3.135494         17.0          289.00
## 170 2.995732         16.0          256.00
## 171 3.135494         18.5          342.25
## 172 3.178054         13.5          182.25
## 173 3.218876         16.5          272.25
## 174 3.178054         17.0          289.00
## 175 2.890372         14.5          210.25
## 176 3.367296         14.0          196.00
## 177 2.944439         17.0          289.00
## 178 3.135494         15.0          225.00
## 179 3.135494         17.0          289.00
## 180 3.091042         14.5          210.25
## 181 3.218876         13.5          182.25
## 182 3.496508         17.5          306.25
## 183 3.332205         15.5          240.25
## 184 3.218876         16.9          285.61
## 185 3.218876         14.9          222.01
## 186 3.258097         17.7          313.29
## 187 3.295837         15.3          234.09
## 188 2.862201         13.0          169.00
## 189 2.772589         13.0          169.00
## 190 2.740840         13.9          193.21
## 191 2.674149         12.8          163.84
## 192 3.091042         15.4          237.16
## 193 3.091042         14.5          210.25
## 194 3.178054         17.6          309.76
## 195 3.113515         17.6          309.76
## 196 3.367296         22.2          492.84
## 197 3.198673         22.1          488.41
## 198 3.367296         14.2          201.64
## 199 3.496508         17.4          302.76
## 200 2.995732         17.7          313.29
## 201 2.890372         21.0          441.00
## 202 2.917771         16.2          262.44
## 203 2.862201         17.8          316.84
## 204 3.384390         12.2          148.84
## 205 3.465736         17.0          289.00
## 206 3.332205         16.4          268.96
## 207 3.277145         13.6          184.96
## 208 2.995732         15.7          246.49
## 209 2.564949         13.2          174.24
## 210 2.944439         21.9          479.61
## 211 2.944439         15.5          240.25
## 212 2.803360         16.7          278.89
## 213 2.803360         12.1          146.41
## 214 2.564949         12.0          144.00
## 215 2.564949         15.0          225.00
## 216 2.564949         14.0          196.00
## 217 3.449988         18.5          342.25
## 218 3.401197         14.8          219.04
## 219 3.583519         18.6          345.96
## 220 3.238678         15.5          240.25
## 221 3.511545         16.8          282.24
## 222 2.862201         12.5          156.25
## 223 2.833213         19.0          361.00
## 224 2.740840         13.7          187.69
## 225 2.708050         14.9          222.01
## 226 2.862201         16.4          268.96
## 227 3.020425         16.9          285.61
## 228 2.944439         17.7          313.29
## 229 2.917771         19.0          361.00
## 230 2.772589         11.1          123.21
## 231 2.740840         11.4          129.96
## 232 2.740840         12.2          148.84
## 233 2.772589         14.5          210.25
## 234 3.367296         14.5          210.25
## 235 3.198673         16.0          256.00
## 236 3.258097         18.2          331.24
## 237 3.238678         15.8          249.64
## 238 3.417727         17.0          289.00
## 239 3.511545         15.9          252.81
## 240 3.401197         16.4          268.96
## 241 3.417727         14.1          198.81
## 242 3.091042         14.5          210.25
## 243 3.068053         12.8          163.84
## 244 3.068053         13.5          182.25
## 245 3.763523         21.5          462.25
## 246 3.586293         14.4          207.36
## 247 3.490429         19.4          376.36
## 248 3.673766         18.6          345.96
## 249 3.586293         16.4          268.96
## 250 2.990720         15.5          240.25
## 251 2.965273         13.2          174.24
## 252 3.005683         12.8          163.84
## 253 2.954910         19.2          368.64
## 254 3.020425         18.2          331.24
## 255 3.005683         15.8          249.64
## 256 3.222868         15.4          237.16
## 257 3.020425         17.2          295.84
## 258 2.965273         17.2          295.84
## 259 3.025291         15.8          249.64
## 260 3.034953         16.7          278.89
## 261 2.923162         18.7          349.69
## 262 2.895912         15.1          228.01
## 263 2.954910         13.2          174.24
## 264 2.873565         13.4          179.56
## 265 2.895912         11.2          125.44
## 266 2.862201         13.7          187.69
## 267 3.401197         16.5          272.25
## 268 3.314186         14.2          201.64
## 269 3.303217         14.7          216.09
## 270 3.430756         14.5          210.25
## 271 3.049273         14.8          219.04
## 272 3.144152         16.7          278.89
## 273 3.169686         17.6          309.76
## 274 3.173878         14.9          222.01
## 275 3.010621         15.9          252.81
## 276 2.833213         13.6          184.96
## 277 3.072693         15.7          246.49
## 278 2.785011         15.8          249.64
## 279 3.449988         14.9          222.01
## 280 3.384390         16.6          275.56
## 281 3.068053         15.4          237.16
## 282 2.985682         18.2          331.24
## 283 3.104587         17.3          299.29
## 284 3.005683         18.2          331.24
## 285 3.025291         16.6          275.56
## 286 2.833213         15.4          237.16
## 287 2.867899         13.4          179.56
## 288 2.803360         13.2          174.24
## 289 2.901422         15.2          231.04
## 290 2.827314         14.9          222.01
## 291 2.740840         14.3          204.49
## 292 2.954910         15.0          225.00
## 293 2.917771         13.0          169.00
## 294 3.462606         14.0          196.00
## 295 3.529297         15.2          231.04
## 296 3.575151         14.4          207.36
## 297 3.310543         15.0          225.00
## 298 3.234749         20.1          404.01
## 299 3.135494         17.4          302.76
## 300 3.303217         24.8          615.04
## 301 3.173878         22.2          492.84
## 302 3.532226         13.2          174.24
## 303 3.540959         14.9          222.01
## 304 3.459466         19.2          368.64
## 305 3.618993         14.7          216.09
## 306 3.346389         16.0          256.00
## 307 3.360375         11.3          127.69
## 308 3.288402         12.9          166.41
## 309 3.511545         13.2          174.24
## 310 3.725693         14.7          216.09
## 311 3.640214         18.8          353.44
## 312 3.468856         15.5          240.25
## 313 3.616309         16.4          268.96
## 314 3.332205         16.5          272.25
## 315 3.273364         18.1          327.61
## 316 3.190476         20.1          404.01
## 317 2.949688         18.7          349.69
## 318 3.535145         15.8          249.64
## 319 3.394508         15.5          240.25
## 320 3.443618         17.5          306.25
## 321 3.610918         15.0          225.00
## 322 3.471966         15.2          231.04
## 323 3.841601         17.9          320.41
## 324 3.328627         14.4          207.36
## 325 3.708682         19.2          368.64
## 326 3.790985         21.7          470.89
## 327 3.770459         23.7          561.69
## 328 3.594569         19.9          396.01
## 329 3.401197         21.8          475.24
## 330 3.797734         13.8          190.44
## 331 3.711130         17.3          299.29
## 332 3.520461         18.0          324.00
## 333 3.394508         15.3          234.09
## 334 3.487375         11.4          129.96
## 335 3.165475         12.5          156.25
## 336 3.555348         15.1          228.01
## 337 3.161247         14.3          204.49
## 338 3.478158         17.0          289.00
## 339 3.303217         15.7          246.49
## 340 3.280911         16.4          268.96
## 341 3.250374         14.4          207.36
## 342 3.157000         12.6          158.76
## 343 3.401197         12.9          166.41
## 344 3.666122         16.9          285.61
## 345 3.663562         16.4          268.96
## 346 3.558201         16.1          259.21
## 347 3.475067         17.8          316.84
## 348 3.610918         19.4          376.36
## 349 3.629660         17.3          299.29
## 350 3.529297         16.0          256.00
## 351 3.546740         14.9          222.01
## 352 3.538057         16.2          262.44
## 353 3.397858         20.7          428.49
## 354 3.496508         14.2          201.64
## 355 3.540959         15.8          249.64
## 356 3.517498         14.4          207.36
## 357 3.478158         16.8          282.24
## 358 3.493473         14.8          219.04
## 359 3.453157         18.3          334.89
## 360 3.335770         20.4          416.16
## 361 3.424263         19.6          384.16
## 362 3.234749         12.6          158.76
## 363 3.186353         13.8          190.44
## 364 3.109061         15.8          249.64
## 365 3.280911         19.0          361.00
## 366 3.005683         17.1          292.41
## 367 2.867899         16.6          275.56
## 368 3.332205         19.6          384.16
## 369 3.295837         18.6          345.96
## 370 3.526361         18.0          324.00
## 371 3.433987         16.2          262.44
## 372 3.367296         16.0          256.00
## 373 3.295837         18.0          324.00
## 374 3.178054         16.4          268.96
## 375 3.135494         20.5          420.25
## 376 3.583519         15.3          234.09
## 377 3.610918         18.2          331.24
## 378 3.433987         17.6          309.76
## 379 3.637586         14.7          216.09
## 380 3.583519         17.3          299.29
## 381 3.583519         14.5          210.25
## 382 3.583519         14.5          210.25
## 383 3.526361         16.9          285.61
## 384 3.637586         15.0          225.00
## 385 3.465736         15.7          246.49
## 386 3.637586         16.2          262.44
## 387 3.218876         16.4          268.96
## 388 3.637586         17.0          289.00
## 389 3.258097         14.5          210.25
## 390 3.091042         14.7          216.09
## 391 3.465736         13.9          193.21
## 392 3.583519         13.0          169.00
## 393 3.295837         17.3          299.29
## 394 3.295837         15.6          243.36
## 395 3.784190         24.6          605.16
## 396 3.465736         11.6          134.56
## 397 3.332205         18.6          345.96
## 398 3.433987         19.4          376.36

7. Apply unit normal scaling to the new data frame that you created in Question 6. Is acceleration or its transformation more influential in predicting the Box-Cox transformation of mpg?

#After performing unit normal scaling acceleration without it's transformation is more influential as 
#it's estimate (1.730) is larger than the absolute value of the transformation of accelration (1.295)

mpgdata1_unit_normal = as.data.frame(apply(mpg_data1, 2, function(x){(x - mean(x))/sd(x)}))
modelmpg_unit_normal <- lm(log_mpg~., data = mpgdata1_unit_normal)
summary(modelmpg_unit_normal)
## 
## Call:
## lm(formula = log_mpg ~ ., data = mpgdata1_unit_normal)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3.15395 -0.66322 -0.00194  0.64295  2.29063 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      7.683e-16  4.417e-02   0.000 1.000000    
## acceleration     1.730e+00  3.391e-01   5.102 5.22e-07 ***
## acceleration_sq -1.295e+00  3.391e-01  -3.820 0.000155 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8813 on 395 degrees of freedom
## Multiple R-squared:  0.2273, Adjusted R-squared:  0.2234 
## F-statistic:  58.1 on 2 and 395 DF,  p-value: < 2.2e-16