2026-09-14

Linear Regression

Linear regression is a statistical method used to descibe the relationship between a response variable and one or more predictor variables.

For simple linear regression, we use one predictor variable.

The Regression Equation

\[ \hat{y} = b_0 + b_1x \] where:
\(b_0\) is the intercept
\(b_1\) is the slope
\(x\) is the predictor
\(\hat{y}\) is the predicted response

Example: Studying and Exam Scores

Suppose we want to investigate whether the number of hours a student studies is related to their exam score.

We can create a data frame for hours studied and score achieved.

study_data = data.frame(
  hours = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 
  score = c(52, 55, 61, 65, 68, 72, 78, 82, 85, 91))

Data

study_data
##    hours score
## 1      1    52
## 2      2    55
## 3      3    61
## 4      4    65
## 5      5    68
## 6      6    72
## 7      7    78
## 8      8    82
## 9      9    85
## 10    10    91

Interpreting the Regression Equation

We use R to obtain our regression equation:

model = lm(score ~ hours, data = study_data)

\[ \hat{y} = 46.93 + 4.20x \]

Intercept: 46.93

When a student studies for 0 hours, the model predicts an exam score of approximately 46.93.

Slope: 4.20

For each additional hour studied, the predicted exam score increases by approximately 4.20 points.

Visualizing the Data

Residuals: Measuring Prediction Error

A residual is the difference between an observed value and the value predicted by the regression model.

\[ e_i = y_i = \hat{y}_i \] A positive residual means the actual score is higher than predicted.

A negative residual means teh actual score is lower than predicted.

A residual of 0 means the prediction was exact.

Residuals Plot

Regression Plot