The data

head(mtcars,3)
##                mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

A data frame with 32 observations on 11 variables.

Variables Description
mpg Miles/(US) gallon
cyl Number of cylinders
disp Displacement (cu.in.)
hp Gross horsepower
drat Rear axle ratio
wt Weight (lb/1000)
qsec 1/4 mile time
vs V/S
am Transmission (0 = automatic, 1 = manual
gear Number of forward gears
carb Number of carburetors

Exploratory Analysis

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000
pairs(mtcars[,c(1,3:7)], main = "Motor Trend Car Road Tests", col = 4 + (mtcars$am == 0)*5, pch = 19,cex = 2)

Analysis

model1<-step(lm(mpg~.,data=mtcars),trace = 0)
summary(model1)
## 
## Call:
## lm(formula = mpg ~ wt + qsec + am, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4811 -1.5555 -0.7257  1.4110  4.6610 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   9.6178     6.9596   1.382 0.177915    
## wt           -3.9165     0.7112  -5.507 6.95e-06 ***
## qsec          1.2259     0.2887   4.247 0.000216 ***
## am            2.9358     1.4109   2.081 0.046716 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.459 on 28 degrees of freedom
## Multiple R-squared:  0.8497, Adjusted R-squared:  0.8336 
## F-statistic: 52.75 on 3 and 28 DF,  p-value: 1.21e-11

We can see that a model has 3 variables; wt, qsec and am. This model captured 0.85 of total variance. - Optimize the model, placing as the variable to am.

modelF<- lm(mpg ~ factor(am):wt + factor(am):qsec, data = mtcars)
summary(modelF)
## 
## Call:
## lm(formula = mpg ~ factor(am):wt + factor(am):qsec, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.9361 -1.4017 -0.1551  1.2695  3.8862 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       13.9692     5.7756   2.419  0.02259 *  
## factor(am)0:wt    -3.1759     0.6362  -4.992 3.11e-05 ***
## factor(am)1:wt    -6.0992     0.9685  -6.297 9.70e-07 ***
## factor(am)0:qsec   0.8338     0.2602   3.205  0.00346 ** 
## factor(am)1:qsec   1.4464     0.2692   5.373 1.12e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.097 on 27 degrees of freedom
## Multiple R-squared:  0.8946, Adjusted R-squared:  0.879 
## F-statistic: 57.28 on 4 and 27 DF,  p-value: 8.424e-13
par(mfrow=c(1,4))
plot(modelF)

This final model captured 0.89 of total variance. Which predicts better mpg

par(mfrow=c(1,2))
plot(mtcars$mpg~mtcars$wt,col=4+(mtcars$am==0)*5,pch=19,xlab="Weight (lb/1000)",ylab="Miles/(US) gallon",frame=F)
abline(lm(mtcars$mpg[mtcars$am==1]~mtcars$wt[mtcars$am==1]),col=4,lwd = 2)
abline(lm(mtcars$mpg[mtcars$am==0]~mtcars$wt[mtcars$am==0]),col=1,lwd = 2)
legend("topright",pch=21,pt.bg=c(4,1),legend=c("Manual","Automatic"))

plot(mtcars$mpg~mtcars$qsec,col=4+(mtcars$am==0)*5,pch=19,xlab="1/4 mile time",ylab="Miles/(US) gallon",frame=F)
abline(lm(mtcars$mpg[mtcars$am==1]~mtcars$qsec[mtcars$am==1]),col=4,lwd = 2)
abline(lm(mtcars$mpg[mtcars$am==0]~mtcars$qsec[mtcars$am==0]),col=1,lwd = 2)
legend("bottomright",pch=21,pt.bg=c(4,1),legend=c("Manual","Automatic"))

-Relationship Miles/(US) gallon and transmission

mean(mtcars$mpg[mtcars$am==1])
## [1] 24.39231
mean(mtcars$mpg[mtcars$am==0])
## [1] 17.14737
mean(mtcars$mpg[mtcars$am==1]) - mean(mtcars$mpg[mtcars$am==0])
## [1] 7.244939

Conclusion

The manual transmission 24.39 better than the automatic transmission 17.15 for Miles/(US) gallon. Although manual cars have (on average) 7.24 better MPG (Miles/(US) gallon) than automatic cars.
This model captured 89.5% of total variance and adjusted variance is 0.879. When the weight increased 1000 lbs, the mpg decreased, -3.176 miles for automatic cars and -6.099 miles for manual cars and when the weight decreases 1000 lbs, the mpg increased, 3.176 miles for automatic cars and 6.099 miles for manual cars. And when the accelaration speed droped and 1/4 mile time increased 1 secs, the mpg increased 0.834 miles for automatic cars and 1.446 miles for manual cars.
In conclusion, the mpg is largely determined by the interplay between weight, accelaration and tramsmission.