Causal inference in ecology with R
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
- Confounding and backdoor adjustment - which covariates to adjust for, and why a mediator is not one of them.
- Collider bias and selection - how conditioning on a common effect invents a correlation, and how sampling does it silently.
Designs that identify
- Before-after-control-impact - the interaction that differences out both a trend and a site difference.
- Instrumental variables and 2SLS - recovering an effect when the confounder was never measured.
- Regression discontinuity - a threshold rule as a local experiment.
- Front-door adjustment - a measured mechanism standing in for the instrument you do not have, and what it charges for the substitution.
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
- Propensity scores and IPW - weighting by the probability of treatment, with balance and overlap checked.
- G-computation and standardisation - the same adjustment written as an outcome model and averaged.
- Doubly robust estimation with AIPW - two chances to be right, and what still kills it.
- Propensity score matching - pairing rather than weighting, and what that trades away.
- Dichotomising a continuous predictor - having the confounder is not the same as using it: a median split hands part of it back to the treatment coefficient, and the adjusted model reports an effect that is not there.
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
- Event-study difference-in-differences - one number becomes a path, and the pre-treatment periods become a check.
- Staggered adoption and two-way fixed effects - why the standard regression is biased when timing varies.
- The synthetic control method - a weighted comparison unit when only one unit was treated.
- Placebo tests for panel designs - inference from permutation when the usual standard errors cannot be trusted.
- Baseline selection and the return to the mean - when the units were chosen because their baseline was bad, the luck in that baseline reverts and the reversion is read as the effect, and the leads that would expose a differential trend sit flat while it happens.
The assumption you cannot test
- Sensitivity to unmeasured confounding - how far a hidden confounder would have to reach, in units you can argue about.
- Checking causal assumptions - negative controls, and honest bounds on an untestable premise.
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.