Why does statistics matter in machine learning?

  • A predictive model looks for patterns in past observations.
  • Statistics helps us describe relationships and judge how reliable our predictions are.
  • Our example asks: Can study habits help predict an exam score?

We will use plots, a regression model, and a measure of prediction error to explore this question.

Our example dataset

We simulate data for 120 students, recording three values for each student:

  • Study hours: time spent reviewing before the exam.
  • Practice problems: number of problems completed.
  • Exam score: result on a scale from 0 to 100.

The data is simulated for this lesson. It does not prove that one study habit causes a higher score.

First look: study hours and scores

Distribution of prediction errors

Interactive view: study time and exam scores

Scatterplot of study hours and exam scores

The plot shows the relationship between study time and exam score. When the interactive version is available, hover over a point to see that student’s practice-problem count.

Linear regression: a statistical model

A multiple linear regression uses both study hours and practice problems:

\[ \widehat{y}_i = \widehat{\beta}_0 + \widehat{\beta}_1 x_{i1} + \widehat{\beta}_2 x_{i2} \]

Here, \(\widehat{y}_i\) is the predicted exam score, \(x_{i1}\) is study hours, and \(x_{i2}\) is the number of practice problems.

The model chooses coefficients that minimize squared prediction errors:

\[ \min_{\beta_0,\beta_1,\beta_2} \sum_{i=1}^{n}(y_i - \beta_0 - \beta_1x_{i1} - \beta_2x_{i2})^2 \]

Interpreting our fitted model

  • Intercept: 31.04 points (the model’s baseline).
  • Study hours: 5.24 additional points per hour, holding practice problems constant.
  • Practice problems: 0.26 additional points per problem, holding study hours constant.
  • Explained variance: \(R^2 = 0.733\) in these simulated data.

These are estimated relationships in our simulated data, not proof of cause and effect.

Measuring accuracy with RMSE

A residual is the difference between a student’s observed score and their predicted score:

\[e_i = y_i - \widehat{y}_i\]

The root mean squared error (RMSE) summarizes prediction errors in points:

\[ \mathrm{RMSE} = \sqrt{\frac{1}{n}\sum_{i=1}^{n}(y_i-\widehat{y}_i)^2} \]

Training RMSE: 7.75 score points.

The error should also be tested on new data before treating the model as reliable.

R code: fitting and checking a model

This is the main R code used to fit the model and calculate its error:

model <- lm(exam_score ~ study_hours + practice_problems,
            data = students)

predicted <- predict(model, newdata = students)
errors <- students$exam_score - predicted
rmse <- sqrt(mean(errors^2))

summary(model)
rmse

What did we learn?

  • Scatterplots can reveal patterns between two measurements.
  • Regression gives us a way to make numerical predictions.
  • Residuals and RMSE show how far predictions are from observed scores.
  • Results from one dataset should be checked on new data.

Statistics helps us understand both what a model predicts and where it may be wrong.