Add baseline predictors
Use outcome ~ time + covariates, with time first. The
package fits respondent-level OLS separately in every rolling and final
pre-event window. Numeric covariates enter linearly. Factors, character,
and logical columns are categorical indicators; ordered factors are
treated as unordered. Formula transformations, interactions, and
nonlinear time trends remain unsupported.
This simulation deliberately adds a strong baseline predictor and changing survey composition, so the role of adjustment can be checked against a known effect. These are synthetic observations, not an empirical application. Both main models use estimand-specific calibration.
library(eventsurvey)
set.seed(926)
survey <- data.frame(day = rep(-24:6, each = 100))
survey$baseline <- rnorm(nrow(survey), mean = 0.7 * sin(survey$day / 3))
survey$group <- factor(rep(c("A", "B"), length.out = nrow(survey)))
survey$y <- 10 + 0.1 * survey$day + 3 * survey$baseline +
0.8 * (survey$group == "B") + 2 * (survey$day >= 0) +
rnorm(nrow(survey))
time_only <- eventsurvey(y ~ day, survey)
adjusted <- eventsurvey(y ~ day + baseline + group, survey)
adjusted
#> Event-during-survey estimate
#> Formula: y ~ day + baseline + group
#> Covariates: baseline, group
#> Window: 6 periods | average-calibration references: 72
#> Period schedule: observed | missing pre/forecast periods: 0 / 0
#> Calibration: estimand
#> Average effect: 2.174
#> Honest 95% CI: [1.090, 3.256]
#> Diagnostics: 0 of 5 checks need reviewThe true event effect is two outcome units in every post-event period. The baseline variables describe characteristics that the event cannot change. In real data, measurement after the event is acceptable only if the variable still records an unaffected baseline characteristic; a contemporaneous attitude or behavior changed by the event is not an appropriate control.
Compare predictive performance
Compare models on the same observations and with the same windows. If any candidate covariate is missing, construct a common complete-case sample before fitting both models. Automatic omission otherwise changes the comparison.
comparison <- data.frame(
model = c("Time only", "Baseline-adjusted"),
effect = c(coef(time_only), coef(adjusted)),
standard_error = c(time_only$estimate$std_error, adjusted$estimate$std_error),
reference_rmse = c(
sqrt(mean(time_only$reference$error^2)),
sqrt(mean(adjusted$reference$error^2))
),
bias_bound = c(time_only$estimate$bias_bound, adjusted$estimate$bias_bound)
)
knitr::kable(comparison, digits = 3, row.names = FALSE)| model | effect | standard_error | reference_rmse | bias_bound |
|---|---|---|---|---|
| Time only | 3.402 | 0.577 | 3.274 | 5.116 |
| Baseline-adjusted | 2.174 | 0.181 | 0.149 | 0.335 |
The rolling RMSE describes the same pre-event forecasting exercises for both models. Their errors overlap and are not independent validation observations. Lower in-sample RMSE alone is insufficient: adding weak predictors can make forecasts less stable, and adjusted intervals need not be narrower. Choose covariates from substantive knowledge before examining post-event results. Repeatedly tuning a specification on the rolling errors introduces selection uncertainty that the reported intervals do not include.
adjusted$model_diagnostics
#> $parameters
#> [1] 4
#>
#> $final_residual_df
#> [1] 596
#>
#> $final_fit_rmse
#> [1] 1.014465
#>
#> $rolling_fit_rmse
#> [1] 0.9688660 0.9706014 0.9501803 0.9568607 0.9513864 0.9497634 0.9617967
#> [8] 0.9785635 0.9974882 1.0007058 1.0170210 1.0068408
plot(adjusted, type = "diagnostics")
What is predicted and averaged?
For each forecast date, the fitted pre-event model predicts the outcome for each respondent actually observed on that date, using that respondent’s covariates. The counterfactual mean averages those predictions. The period effect subtracts that mean from the observed outcome mean; the window effect averages the period effects with equal weight.
plot(adjusted, type = "counterfactual")
The adjusted path can bend because covariate composition changes, even though time enters the regression linearly. A causal interpretation requires a credible no-event conditional outcome model and a defensible relation between interview timing, baseline covariates, and potential outcomes. The target is the average effect for each forecast date’s respondent population. A common population ATE needs additional representativeness or standardization assumptions. Covariates do not remove an unrelated contemporaneous shock or automatically repair event-induced nonresponse.
Uncertainty and practical requirements
The adjusted covariance calculation combines respondent influence
functions for every effect and rolling reference error. It includes
fitted coefficients, each forecast period’s estimated covariate mean,
and all shared-observation covariances needed for the intervals. Scores
are centered within interview period, with covariance correction
n / (n - 1), conditional on period counts. Respondents must
be independent within and across periods; outcome variances may differ.
These are asymptotic intervals, not finite-sample guarantees.
The honest interval additionally assumes that the population error of
the adjusted post-event forecast is bounded by the largest absolute
corresponding population reference error. Its empirical estimate is the
displayed bias bound. The procedure does not verify this assumption from
data. Each day uses matching elapsed horizons. Period averages are
computed after daily calibration: their bias bound is the mean daily
bound, and their honest interval aggregates daily intervals at
significance level alpha / window (Bonferroni). The average
sampling variance includes cross-day covariance from shared fitted
coefficients. See Methodology.
Every fitting window must identify all coefficients and retain positive residual degrees of freedom. Constant covariates, collinear variables, or factor levels missing from a fitting window cause a clear error. Consider fewer substantively justified covariates or a longer defensible window; do not silently discard problematic reference windows. Also inspect whether forecast covariate distributions extend beyond those in the fitting windows.
Each pre-event and forecast period must have at least two complete responses. The adjusted method does not borrow a variance for singleton periods. Small samples and many predictors can still make uncertainty unreliable despite satisfying the formal minimums. Missing outcome, time, or included covariates cause omission with a warning; inspect the resulting period counts.
plot(adjusted, type = "effects", inner_level = 0.90)
The report and extraction methods work as usual:
coef(adjusted)
#> window_average_effect
#> 2.173917
confint(adjusted)
#> 2.5 % 97.5 %
#> window_average_effect 1.09047 3.255535
eventsurvey_report(adjusted, "adjusted-report.html")eventsurvey_summary() remains time-only. Counts, outcome
means, and outcome variances, even with mean covariates, do not recover
the joint respondent information used by the adjusted estimator and its
covariance calculation.
See Methodology, Data Preparation, and Automatic Diagnostics for the shared design and data requirements.