OmVitalis Academy ▶  Watch Episode 11All guidesCourseEnroll free
Academy
Multi-omics, Demystified · Episode 11

Covariates, Models and Adjusting

What fitting a model means, how categorical and numerical variables enter it, what adjusting for a covariate does to your result and why, what collinearity does, fixed versus random effects, and a practical recipe. Every section has a live lab: change the model and watch the result move.

What is a model, and what is fitting?

A model is a recipe for the expected value of a feature, plus noise. Fitting finds the coefficients that leave the least error.

Live lab

Fit the line yourself

24 people, one feature, and their ages. Lift and tilt the line to make the orange residual bars as short as you can. Then let least squares do it.

Squared error0
Explained by age0%

The numbers from the episode

Recipe

feature = baseline + slope x age + noise

The baseline and the slope are coefficients. Each answers one question.

Residual

What the recipe misses

The gap between a measured value and the value the recipe predicts for that sample.

Fitting

Least squares

Choose the coefficients that make the sum of squared residuals as small as possible.

In the video

10.2 down to 2.1

24 people. Overall mean only: squared residuals 10.2. Baseline 5.75 plus 0.048 per year: 2.1. Age explains 79 percent of the variation.

Variable types and the design matrix

The type of a variable decides how it enters the model.

Live lab

Build the design matrix

Six samples. Add variables to the model and watch each one become columns of numbers. Change a reference level and see which column flips.

Reference level for treatment
Reference level for batch

The numbers from the episode

TypeExamplesHow it entersWhat its coefficient means
CategoricalTreatment, sex, batchDummy coding: one 0 and 1 column per non-reference levelDifference from the reference level
Numerical, continuousAge, BMI, doseAs it is, one columnChange in the feature per extra unit
Numerical, discreteNumber of visitsAs it is, one columnChange per extra count

Put the columns side by side and you have the design matrix: one row per sample, one column per coefficient. The reference level of every categorical variable is the baseline, all zeros.

What adjusting does

Adjusting for a covariate means comparing like with like. It can remove a false difference or sharpen a real one.

Live lab

Adjust the model, watch the volcano move

A simulated study with 30 cases, 30 controls and 500 features. The cases are older than the controls and most were run in the second batch. Only 40 features truly differ with disease.

abundance ~ group + age + batch
Called significant0
True discoveries of 400
False positives0

Every feature: the volcano plot

Each dot is one feature. Dots above the dashed line pass FDR 0.05. Click any dot to inspect it.

True disease signalDriven by age onlyNo real effect

One feature

Batch 1Batch 2More solid means older

  1. Turn on "Adjust for age"Watch the orange dots fall below the line. They were never about disease.
  2. Add "Adjust for batch"Noise drops, so more true signals rise above the line.
  3. Slide the age gap to 0Now adjusting for age changes little. A covariate only biases the result when it differs between groups.

This is simulated data built to show the mechanism, so we know which features are truly different. In real data you never see the colours, which is why the design and the model matter.

The numbers from the episode

Story 1: confounding

+1.20 becomes -0.22

15 per group, treated about 56 years old, controls about 34, and the feature rises with age. Without age: difference +1.20, p < 0.001. With age in the model: -0.22, p = 0.52. The difference was age.

Story 2: precision

p = 0.21 becomes p = 0.001

12 per group, same ages, but age adds noise. Without age: +0.49, standard error 0.38, p = 0.21. With age: +0.68, standard error 0.18, p = 0.001.

On a volcano plot

ModelUpDownReal callsFalse calls
Treatment only20019897301
Treatment + age1920363

Simulation: 1000 features, 20 vs 20, treated older, BH at 5 percent. Features that only respond to age slide back to the centre once age is in the model. Features that truly respond to treatment keep their direction and size but lose some certainty, because age and treatment overlap, so fewer cross the line. The volcano changes from a map of age into a map of treatment.

A confounder is linked to both the group and the feature. A covariate that only explains noise does not change the estimate much, but it shrinks the standard error.

What adjusting costs and buys

Our simulation of 1000 features, 20 samples per group, treated older. 100 features respond to treatment, 300 respond only to age. Ordinary linear model per feature, BH at 5 percent.

Live lab

Spend your degrees of freedom

Every sample gives you one degree of freedom. Every coefficient spends one. Add covariates, then decide how much noise they really explain.

Baseline and groupSpent on covariatesLeft to estimate noise
Degrees of freedom left0
Power, 1 SD effect0%

Power as covariates are added

The numbers from the episode

ModelCalled changedRealFalse
Without age39897301 (76 percent)
With age39363

The price: power with 24 samples, one feature, effect of about 1 SD

Extra covariatesResidual degrees of freedomPowerFalse positive rate
02262 %4.4 %
22054 %5.9 %
41850 %5.0 %
81441 %4.5 %
121028 %3.8 %
16619 %4.4 %

The extra covariates here are pure noise. The false positive rate stays near 5 percent, but power drops because every covariate uses a degree of freedom. Adjust for what matters, not for everything.

Collinearity

Two covariates that carry almost the same information. The model cannot tell whose credit is whose.

Live lab

Make two covariates overlap

40 patients. The feature truly depends only on age. Slide the correlation between age and disease duration and watch the model lose its certainty.

The two covariates

What the model reports

Variance inflation1
Standard errors×1
Age + duration0

The numbers from the episode

Example

Age and disease duration, r = 0.97

40 patients. The feature truly depends only on age. Age alone: 0.49, p < 0.001. Duration alone: 0.47, p < 0.001, only because it travels with age.

Both in the model

Neither is significant

Age 0.44, p = 0.39. Duration 0.06, p = 0.91. The variance inflation factor is about 18, so standard errors are about 4 times larger.

Resampling

Coefficients swing, the sum does not

Over 200 bootstrap resamples the two coefficients moved in opposite directions (correlation -0.98) while their sum stayed steady.

Remove one

0.44 ± 1.03 becomes 0.49 ± 0.24

Drop duration and the age estimate (95 percent interval) tightens about 4 times, the variance inflation factor falls from 18 to 1, and the resampled coefficients collapse from an elongated cloud onto a single stable value. Duration carried almost nothing that age did not already say.

Extreme case

Perfect overlap

All treated samples run in one batch: the treatment and batch columns are identical, and no model can separate them. Cross treatment and batch when you design the study.

What to do: check the correlation between covariates, keep one of a correlated pair or combine them, and read an unstable coefficient as a data limit, not a modelling error. The variance inflation factor is 1 / (1 - r squared) for two covariates.

Fixed and random effects

Live lab

The same people, twice

8 subjects measured before and after treatment. Tell the model that the measurements come in pairs and watch what it can suddenly see.

Treatment effect0
p value0

The numbers from the episode

Fixed effect

Levels you chose and care about, and would reuse: treatment, sex, genotype. One coefficient per level.

Random effect

Levels are a sample from a larger population: subjects, litters, labs, plates. You estimate how much they vary, not each one.

Partial pooling

Each level estimate is pulled toward the overall mean, more when its data are noisy or few.

In the video: 8 subjects, before and after

ModelTreatment effectp
Subject ignored1.30.31
Subject in the model1.30.007

Estimated shrink factor 0.89 for subject baselines. With complete, balanced data, a random subject term gives the same answer as the paired test. Random effects pay off with many subjects, missing visits, and when conclusions should extend beyond these subjects. In limma, duplicateCorrelation handles repeated subjects. In lme4, the term is (1 | subject).

Putting it into practice

Live lab

Write the model for your study

Describe your design and get the formula for each tool, with the reason for every term.

The numbers from the episode

RoleHandling
Variable of interest (treatment)The term you test; report its adjusted fold change and adjusted p value
Covariates measured before treatment (age, sex, batch)Adjust for them
Categorical covariateDummy columns
Numeric covariateA slope
Repeated subjects, litters, labsA block term or a random effect
limma

~ 0 + group + age + sex + batch

Then contrasts between group columns.

DESeq2

~ batch + sex + age + condition

Covariates first, the variable of interest last.

Look first

PCA coloured by each variable

Shows which variables drive the data before you choose the model.

Caution

Never adjust for what the treatment causes

That removes part of the effect you want to measure. Keep covariates few compared with samples.

Checklist

Tick as you go. Your ticks stay in this browser.

0 of 0

Before

During

After

Shape the next lesson

This series is built from the questions researchers actually ask. Tell us where you got stuck and what you want next. It takes under a minute.

Turn on JavaScript to send your questions, or write to wael@omvitalis.com.

References

DOIs were checked against Crossref. Check the details against the publisher page before you cite.

  1. Random effectsLaird NM, Ware JH. Random-effects models for longitudinal data. Biometrics 1982;38:963-974.doi:10.2307/2529876
  2. Mixed models in RBates D, Maechler M, Bolker B, Walker S. Fitting linear mixed-effects models using lme4. J Stat Softw 2015;67(1).doi:10.18637/jss.v067.i01
  3. Repeated measures in limmaSmyth GK, Michaud J, Scott HS. Use of within-array replicate spots for assessing differential expression in microarray experiments. Bioinformatics 2005;21:2067-2075.doi:10.1093/bioinformatics/bti270
  4. limmaRitchie ME, et al. limma powers differential expression analyses for RNA-sequencing and microarray studies. Nucleic Acids Res 2015;43:e47.doi:10.1093/nar/gkv007
  5. DESeq2Love MI, Huber W, Anders S. Moderated estimation of fold change and dispersion for RNA-seq data with DESeq2. Genome Biol 2014;15:550.doi:10.1186/s13059-014-0550-8
  6. What to adjust forGreenland S, Pearl J, Robins JM. Causal diagrams for epidemiologic research. Epidemiology 1999;10:37-48.doi:10.1097/00001648-199901000-00008
  7. Batch effectsLeek JT, et al. Tackling the widespread and critical impact of batch effects in high-throughput data. Nat Rev Genet 2010;11:733-739.doi:10.1038/nrg2825