hpower <- c(200, 230, 200, 148, 291, 300, 295, 140, 166, 138, 306, 300, 212, 158, 150)
mpg <- c(32, 30, 30, 32, 22, 20, 21, 40, 34, 36, 28, 18, 25, 34, 30)
df <- cbind(hpower, mpg)
df <- as.data.frame(df)
Making the model;
model <- lm(mpg ~ hpower, data = df)yhat <- predict(model)
e <- model$residuals
plot(yhat, e, pch = 18, xlab = "predicted values", ylab = "errors")
abline(h=0)
-> The error terms are distributed randomly around the zero which implies that the linearity assumption is met and variances are constant.
qqnorm(e)
qqline(e)
-> This plot implies that residuals are normally distributed since they are mostly on the diagonal line.
comment : From these conclusions, we can say that the assumptions for linear regression model are met hence, we can check the model now. These process is important for us to be ensure the validity and reliability of the results.
Fitting the model;
model
##
## Call:
## lm(formula = mpg ~ hpower, data = df)
##
## Coefficients:
## (Intercept) hpower
## 46.8680 -0.0838We can get the fitted model from this result.
\[ \begin{align} \hat{y} = \hat{\beta_0}+\hat{\beta_1} x. \end{align} \]
Put 46.8680 for:
\[ \hat{\beta_0} \]
and -0.0838 for:
\[ \hat{\beta_1} \]
the equation is :
\[ \hat{y} = 46.8680 - 0.0838 x \]
Draw the fitted regression line graph ;
ggplot(df, aes(x=hpower, y=mpg)) +
geom_point() +
geom_smooth(method = "lm", se = F) +
labs(x="horse power", y="mpg", title = "scatter plot for the data with regression line")
## `geom_smooth()` using formula = 'y ~ x'
To interpret the model, we can look at the anova table and summary.
anova(model)
## Analysis of Variance Table
##
## Response: mpg
## Df Sum Sq Mean Sq F value Pr(>F)
## hpower 1 431.94 431.94 39.977 2.647e-05 ***
## Residuals 13 140.46 10.80
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model)
##
## Call:
## lm(formula = mpg ~ hpower, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.2975 -2.0961 -0.1073 1.4680 6.7758
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 46.86798 2.98100 15.722 7.72e-10 ***
## hpower -0.08380 0.01325 -6.323 2.65e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.287 on 13 degrees of freedom
## Multiple R-squared: 0.7546, Adjusted R-squared: 0.7357
## F-statistic: 39.98 on 1 and 13 DF, p-value: 2.647e-05
-> Significance of the coefficients:
confint(model, level = 0.95)
## 2.5 % 97.5 %
## (Intercept) 40.4279240 53.30802637
## hpower -0.1124374 -0.05516906
for :
\[ \hat{\beta_0} \]
The confidence interval does not include zero so, we can
reject the H0 so it is significant.
for :
\[ \hat{\beta_1} \]
The confidence interval does not include zero and from anova table we
can see the Fvalue is 39.977 and the
Fcritical which F1,13,0.05 is 4.667. So, we can
reject the H0
which means it is significant.
-> The significance of the model:
Since the beta_hat is significant, we can interpret that the model is also significant.
-> The coefficient of determination:
From the summary of the model we can see the R2 value is
0.7546. This means that, the 75.46% of the mpg values can
be explained by the horse power.
anova(model)
## Analysis of Variance Table
##
## Response: mpg
## Df Sum Sq Mean Sq F value Pr(>F)
## hpower 1 431.94 431.94 39.977 2.647e-05 ***
## Residuals 13 140.46 10.80
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
From the table we see that the Fvalue is
39.977 and Fcritical is 4.667. So we reject the
H0 . The model is significant.
predict(model, data.frame(hpower = 182))
## 1
## 31.61579
If the horse power is 182, the predicted mpg value is 31.61579.