2025-04-10

Introduction

In this presentation, we will look at different hypothesis testing using the ‘mtcars’ dataset. We will be comparing the fuel efficiency in the different transmission types. We will then build a regression model to further understand what predicts mpg.

mtcars Introduction

data(mtcars)
head(mtcars)
##                    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
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

A p-value represents the probability of obtaining results either as extreme or more extreme than the results, under the assumption that the null hypothesis is true. The formula for a p-value is \[ \text{p-value} = P(\text{observed or more extreme} \mid H_0 \text{ true}) \] If the p-value is less than 0.05, we can reject the null hypothesis

This t-test compares average mpg between the different transmissions

t_test = t.test(mpg ~ am, data = mtcars)
t_test 
## 
##  Welch Two Sample t-test
## 
## data:  mpg by am
## t = -3.7671, df = 18.332, p-value = 0.001374
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -11.280194  -3.209684
## sample estimates:
## mean in group 0 mean in group 1 
##        17.14737        24.39231

I want to test weather there is a difference in average MPG between automatic and manual transmissions \[ H_0: \mu_{\text{auto}} = \mu_{\text{manual}} \\ H_a: \mu_{\text{auto}} \ne \mu_{\text{manual}} \] this is a two-sample t-test comparing average mpg between the two transmissions

MPG by Transmission

Code for previous MPG plot

ggplot(mtcars, aes(x = am, y= mpg, fill = am)) +
  geom_boxplot() + 
  labs(title = "MPG by Tranmission Type",
       x= "Transmission",
       y = "MPG")+
  theme_minimal()

Code for Linear Regression

model = lm(mpg ~ wt, data = mtcars)
summary(model)

## `geom_smooth()` using formula = 'y ~ x'

MPG vs Weight and Horsepower