Standards for controlling time-varying confounders in environmental time series studies. Use when adjusting for trends, seasonality, temperature, or calendar effects.
The most critical confounders in environmental time series:
# Combined trend + seasonality (standard approach)
ns(date, df = 7 * nyears)
# Separate trend and seasonality
ns(as.numeric(date), df = 1 * nyears) + # trend
ns(yday(date), df = 5) # seasonality
# Natural spline with 3-6 df
ns(tmean, df = 4)
# Or as cross-basis if lagged effects expected
cb_temp <- crossbasis(tmean, lag = 21, argvar = list(fun = "ns", df = 4),
arglag = list(fun = "ns", knots = logknots(21, 3)))
ns(humidity, df = 3) # Usually 3 df sufficient
| Confounder | Implementation | Notes |
|---|---|---|
| Day of week | factor(dow) or factor(wday(date)) | Always include |
| Public holidays | Binary indicator holiday | Country-specific |
| Influenza | ns(flu_count, df = 3) or binary epidemic flag | Especially for respiratory outcomes |
| Day after holiday | Binary indicator | Sometimes significant |
acf(residuals(model, type = "deviance")) — no significant spikes.spec.pgram() — no remaining seasonal peaks.pacf() — no remaining AR structure.ns(date) df or include AR terms.dow: Day-of-week effects are strong in health data.