Sitemap

How to Model Random Effects: A Statistical and Predictive Perspective

5 min readJun 8, 2025

--

When you first learn statistics, you usually start with the basics of regression:

“If x increases by 1, how much does y increase on average?”

This is called a fixed effect.

But as you go deeper, you’ll encounter something called a random effect.

So what exactly is a random effect?
Why should we care?
And how should we deal with it — not just from a statistical lens, but also from a prediction perspective?

Let’s break it down.

1. What Is a Random Effect?

Suppose x is study time, and y is exam score.

At first glance, there doesn’t appear to be a strong relationship between study time and scores. The points are scattered, and the overall trend (red line) is flat. This suggests no clear association between studying and performance.

But is that the whole story?

Now imagine the data comes from five different schools, labeled G1 to G5. Each school gives exams with different difficulty levels.

  • That means the average score (intercept) varies by school.
  • But the relationship between study time and score (slope) is similar across schools.

Once we account for these group-level differences, we can see clear upward trends in each group. What initially looked like “no relationship” turns out to be quite meaningful once random effects are considered.

So in this case:

  • The slope (study effect) is a fixed effect.
  • The different baselines (intercepts by school) are random effects.

This example illustrates why it’s so important to consider group structure when analyzing or modeling your data.

2. The Statistical Perspective

In statistical terms:

  • The group (e.g. school, hospital, doctor) is called a factor level
  • If each group has a different intercept, that’s modeled as a random effect
  • If the relationship (slope) is consistent across groups, that’s a fixed effect

Why use a random effect model?

Because it helps explain hidden variation. Without accounting for group-level differences, a model might show no effect at all. But once you model the random effects, the underlying relationships can emerge clearly — increasing statistical power.

How do we know if random effects are significant?

You can use the Intraclass Correlation Coefficient (ICC):

Press enter or click to view image in full size
  • Low ICC → values within the same group are very different → random effects likely unimportant
  • High ICC → values cluster tightly within groups → random effects likely important

Random effect models are especially useful when dealing with repeated measurements — such as blood samples taken multiple times from the same patient. These samples tend to resemble each other more than samples from other patients.

3. The Predictive Perspective

Now let’s switch gears.
What happens when you’re building a predictive model, not just doing statistical inference?

3.1 How to Measure Prediction Performance Properly

In prediction tasks, your goal is to perform well on unseen data.

Get Donghyeok Lee’s stories in your inbox

Join Medium for free to get updates from this writer.

But if your data includes groups (like patients or batches), using a standard random train-test split can break the independence assumption. For example, if some samples from a patient go into training and others into testing, your model might seem overly accurate.

Solution:

Split your data by group, so that each group appears either in training or in testing — not both.

Press enter or click to view image in full size

For instance, all samples from the same individual (e.g., blood tests) must go entirely into either the training set or the test set — not both.

3.2 How Random Effects Hurt Prediction

Go back to the school example.

You trained a model to predict test scores using study hours. But now, you receive data from a new school not seen during training.

If schools differ in baseline scores (intercepts), and your model hasn’t seen this school, it can’t adjust properly. Without knowing the intercept for the new group, the prediction will be inaccurate.

In a fixed effect model, you simply calculate:

Predicted score = slope × study hours + intercept

But in a random effect model, the intercept varies by group. Since you don’t know the new group’s intercept, accurate prediction becomes impossible.

3.3 How to Handle or Remove Random Effects

If random effects are minor, you can ignore them and use a simple fixed effect model.

But if they’re important, what can you do?

If the new group resembles a known group in your data, you might be able to estimate its effect. This isn’t easy and depends on how well you can define similarity between groups.

Interestingly, this is one reason why complex deep learning models may excel only in tasks like image and text classification — these domains have little to no random effect. For example, a cat is a cat regardless of the camera model.

3.4 Bonus: Random Effects Caused by Technical Noise

Sometimes, what looks like a random effect is actually technical noise — such as differences between labs, instruments, or measurement batches.

This is often referred to as a batch effect, especially in biomedical research.

Press enter or click to view image in full size
Red batch and Green batch

Batch effect correction algorithms can be used to remove this type of variation.

One practical approach:

  1. Split your data into:
  • Reference set: used for correcting distributional shifts across ‘groups’
  • Training set: used to build the predictive model
  • Test set: used to evaluate performance

2. Use the reference data as a baseline to align both training and test distributions to it.

3. Train your model on the corrected training data.

4. When new data arrives, ‘correct’ it first using the same reference data before making predictions.

4. Final Thoughts

Random effects are widely studied in statistical modeling, but often ignored in machine learning.

Before you start fine-tuning models or optimizing hyperparameters, ask yourself:

  • Is there a random effect in my dataset?
  • Is it interfering with my predictions?
  • How can I properly model or remove it?

Often, understanding the structure of your data leads to better performance than any complex model.

--

--