2025-10-18

Hypothesis Testing using p-values

Hypothesis testing is a method that is used to test an assumption about a data set based on a sample data.

Steps to do the hypothesis testing -

Step 1:-
We make 2 statements - 1) Null Hypothesis (H\(_0\)): There is no effect or relationship.
2) Alternative Hypothesis (H\(_1\)): There is an effect or relationship.

Step 2:-
Assume a \(\alpha\) value to compare the p-value to

Step 3:-
We calculate the p-value to decide whether to reject the null hypothesis.
If pval <= \(\alpha\), Reject H\(_0\) -> statistic significance
If pval > \(\alpha\), Fail to reject H\(_0\) -> Not significant

Mathematical representation

The mathematical representation of the hypothesis is as follows:-

H\(_0\) : \(\beta\)\(_1 = 0\)
H\(_1\) : \(\beta\)\(_1\) \(\neq\) 0

Here,
\(\beta\)\(_1\) represents the slope (relationship b/w X and Y)

If \(\beta\)\(_1 = 0\), X and Y have no relationship (flat line, slope = 0)

If \(\beta\)\(_1\) \(\neq\) 0, there is a significant relationship (slant line, slope is either +ve or -ve)

Introducing the Dataset

Example:- Testing the relationship on mtcars

I will be testing whether “Car Weight” has a significant effect on “Miles per gallon” using the mtcars dataset.

Step 1:-
1) Null Hypothesis (H\(_0\)): Weight has no effect on mpg.
2) Alternative Hypothesis (H\(_1\)): Weight affects mpg.

Step 2:-
Assume a \(\alpha = 0.05\).

Step 3:-
Fit a simple regression model to find p-value and decide if the relationship is significant.

Why fit a simple regression model

  1. Regression will draw a line that shows how mpg changes with weight.

  2. The line will have a slope that will make it clear if the weight makes a difference.

  3. If slope = 0 (flat line) -> no effect of weight

  4. If slope \(\neq\) 0 (slant line) -> weight affects mpg

  5. p-value will test whether slope is really 0 or jut a coincidence

Regression Line


The green line shows that as car weight increases, fuel efficiency decreases, which shows a significant negative relationship between weight and mpg.

Calculating the p-value

We fit the model and find the p-value for the slope.

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

Residuals:
    Min      1Q  Median      3Q     Max 
-4.5432 -2.3647 -0.1252  1.4096  6.8727 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
wt           -5.3445     0.5591  -9.559 1.29e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared:  0.7528,    Adjusted R-squared:  0.7446 
F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

Understanding the p-value

  1. From the summary from the prev. slide the p-value = 1.29e-10
  2. Comparing this value to the significance value \(\alpha\) = 0.05
  3. Since pval < 0.05, we reject the null hypothesis H\(_0\)
  4. This means car weight has a significant effect on mpg
  5. The -ve slope (-5.34) shows that as weight increases, mpg decreases

Calculating 95% confidence interval on the slope

A confidence interval tells the range of possible values for the actual slope
We calculate it to see how precise our estimate of the slope is -
if interval does not have 0 -> supports that relationship b/w weight and mpg is statistically significant.

Formula:-
\[ CI = b_1 \pm t_{\alpha/2, n-2} \times SE(b_1) \]
where,
\(b_1\): estimated slope
\(SE(b_1)\): standard error of slope
\(t_{\alpha/2, n-2}\): critical t value for 95% confidence level

Confidence Interval on our data

                2.5 %    97.5 %
(Intercept) 33.450500 41.119753
wt          -6.486308 -4.202635

The 95% confidence interval for the slope of weight is [-6.49, -4.20]. Since 0 does not lie within this range, we reject H\(_0\). This confirms a significant negative relationship between weight and fuel efficiency - as car weight increases, miles per gallon decrease.

Visual Interpretation of Weight vs. MPG Relationship

This plot shows that heavier cars tend to have lower miles per gallon. Bubble size represents horsepower. Larger points (higher horsepower) are generally heavier and less fuel-efficient.

plot_ly(mtcars, x = ~wt, y = ~mpg, size = ~hp, type = 'scatter')

Conclusion

Through hypothesis testing and simple linear regression on the mtcars dataset, I found that car weight (wt) has a statistically significant negative effect on miles per gallon (mpg).
- The p-value (1.29e−10) is far below 0.05, so we reject the null hypothesis.
- The slope is negative (−5.34) - meaning heavier cars tend to have lower fuel efficiency.
- The 95% confidence interval [−6.49, −4.20] does not include 0, confirming the conclusion.

Overall, as car weight increases, fuel efficiency decreases,showing a strong negative linear relationship between these weight and mpg.