Loading the Data

carData <- cars

Visualising the Data

plot(carData[,"dist"], carData[,"speed"], main="Stopping Distance as a Function of Speed", xlab = "Stopping Distance", ylab="Speed")

The independent variable, Stopping Distance, is plotted on the x-axis The dependent variable, Speed, is plotted on the y-axis.

The plot shows a tendency for speed to increase as stopping distance increases and thus exhibits a linear relationship.

The LInear Model Function

lfunction <- lm(carData$speed ~ carData$dist)
lfunction
## 
## Call:
## lm(formula = carData$speed ~ carData$dist)
## 
## Coefficients:
##  (Intercept)  carData$dist  
##       8.2839        0.1656

The model is: speed = 8.2839 + 0.1656 * stopping distance

Evaluating quality of the model

summary(lfunction)
## 
## Call:
## lm(formula = carData$speed ~ carData$dist)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.5293 -2.1550  0.3615  2.4377  6.4179 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   8.28391    0.87438   9.474 1.44e-12 ***
## carData$dist  0.16557    0.01749   9.464 1.49e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.156 on 48 degrees of freedom
## Multiple R-squared:  0.6511, Adjusted R-squared:  0.6438 
## F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12

The model performs well.

Residual Analysis

plot(fitted(lfunction), resid(lfunction))

The graph show that for the most part the reisduals increase as the input values increase.

qqnorm(resid(lfunction))
qqline(resid(lfunction))

The residuals are mostly normal. Indicates that stoppping distance is a sufficient predictor of speed.