Causal inference in ecology with R

Causal inference in ecology with R: confounding, colliders, BACI, instrumental variables, propensity scores, panel designs, and the assumption no diagnostic can test.

Ecologists almost never get to randomise. The protected area was placed where the land was cheap, the fire burned where the fuel was, the restoration went to the sites someone cared about. Every one of those is a treatment assigned by a process that also affects the outcome, which is the definition of the problem, and no amount of model fit repairs it. A regression on observational data returns an association. Turning that into a causal claim takes an argument, and the argument has to be made before the model runs.

This page collects every causal inference tutorial on this site. All of them are built by hand in base R on simulated data where the true effect is known, which is the only way to show that a method recovers it.

Adjusting for everything is the wrong instinct

The reflex is to put every measured variable on the right-hand side. It is wrong, and it is wrong in a way that no diagnostic reports. Adjusting for a confounder removes bias. Adjusting for a mediator removes part of the effect you are trying to measure. Adjusting for a collider, a variable that both the treatment and the outcome cause, MANUFACTURES an association out of nothing: the model fits well, the coefficient is significant, and the correlation it reports does not exist in the world. Selection works the same way. If your sites got into the data set because of something both variables influence, you have conditioned on a collider without writing a single line of adjustment.

So the covariate set is a decision, and it is a decision about causal structure, which no dataset contains. That is why this family starts with a diagram rather than a data frame.

Two ways out, and they are different in kind

Designs that identify. Some situations carry their own argument. A threshold rule that decides who gets treated makes the units either side of it comparable by construction. A variable that shifts treatment without touching the outcome any other way is an instrument. A before-after contrast at both an impacted and a control site differences out the trend and the site difference at once. These do not need you to have measured the confounders. They need a structural claim to be true, and that claim is usually the thing worth arguing about in the paper.

Adjustment for what you measured. Propensity scores, weighting, standardisation and matching all assume the confounders are in your data, and then differ in how they use them. Doubly robust estimation is the neat one: combine an outcome model and a treatment model, and consistency survives either being wrong. It does not survive both being wrong, and it does not survive a confounder you never measured.

And the rare case where you do randomise. Assigning the treatment yourself settles the confounding argument and leaves two others open, because a field experiment happens outdoors. The treatment named in the assignment sheet is not always the treatment a plot received, and a treatment applied to one plot does not always stay on it. Neither failure shows up as a bad residual plot or an implausible interval: the estimate quietly answers a different question while the output still looks healthy, so the check has to be made on the design rather than on the fit.

The assumption you cannot test

No unmeasured confounding is not testable. Not with a residual plot, not with cross-validation, not with a bigger model. What you can do is ask how strong a hidden confounder would have to be to overturn the result, and answer in units someone can argue with: stronger than elevation? Stronger than anything you did measure? That reframing is the honest end of this literature, and it is where an ecological causal claim should finish.

The tutorials

The two mistakes that decide everything

Designs that identify

When the field does not do what the design says

  • Non-compliance in field experiments - assignment and receipt come apart, and dropping the plots that failed turns the comparison into a selection problem whenever compliance tracks the ground.
  • Interference between experimental plots - treatment that reaches the neighbours pulls the contrast towards zero without widening the interval, and a buffer buys the repair with plots you no longer measure.

Adjusting for what you measured

The population the answer is about

Every estimator above returns an effect for the units that were studied. A management decision is usually about somewhere else.

  • Transporting an effect to a new region - standardising to the covariate distribution of the place the decision will be made, what it costs in precision, and how to tell when the target sits outside the study’s support.

Panels: units and time

The assumption you cannot test

Where this connects

  • Path analysis, piecewise SEM and d-separation and checking a structural equation model fit a whole diagram rather than one effect, and hit the limit that Markov-equivalent graphs cannot be told apart.
  • Pseudoreplication is what happens to the standard error when the experimental unit is not what you think it is.
  • Non-response and site substitution is the selection problem arriving through the field season rather than through the model: the units that came back are a subset chosen by terrain and access, and the propensity weighting used to repair it is the same machinery as above pointed at a survey mean.
  • Split-plot designs in ecology is the designed version of the same worry, where randomisation happens at two scales at once and a single residual line misreads both treatments, in opposite directions.
  • Collinearity and VIF explains why throwing correlated covariates at the problem does not help either.
  • Ecological time series in R covers convergent cross mapping, which asks the causal question from dynamics rather than from a design.
  • Quantitative genetics in R meets the same problem in a regression of fitness on traits, where a selection gradient is causal only if nothing unmeasured drives both.

Newsletter

Get new tutorials by email

New R and QGIS tutorials for ecologists, straight to your inbox. No spam; unsubscribe anytime.

By subscribing you agree to receive these emails and confirm your address once. See the privacy policy.