--- title: "Getting Started" description: "Fit a first event-survey model, inspect the results, and create a shareable report." output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Getting Started} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.2) ``` An event-during-survey design arises when an event occurs while a survey is already collecting responses. The observed post-event outcome is available, but its no-event counterpart is not. The central task is therefore to extrapolate the pre-event path and compare it with what was observed after the event. ## Install the current version These examples use version 0.4.1, with daily calibration followed by aggregation for period averages. ```{r installation, eval = FALSE} install.packages("remotes") remotes::install_github("yhoriuchi/eventsurvey", ref = "v0.4.1") packageVersion("eventsurvey") ``` The installation is pinned to the `v0.4.1` release for reproducibility. ## 1. Prepare respondent-level data Each row of the data frame or tibble should be a respondent. The outcome must be numeric, and time must be an integer-like numeric variable, a `Date`, or a `POSIXt`. The package includes the ready-to-use `sample_data` dataset so the full workflow can be run without private research data or a separate simulation step. ```{r} library(eventsurvey) head(sample_data) ``` In this dataset, `y` is a simulated five-category survey response coded from 1 (lowest) to 5 (highest), and `day` is the interview day relative to the event: negative values are before the event, 0 is the event day, and positive values are after the event. Only these observed variables are included. The no-event counterfactual and event effects are quantities to be estimated, not columns supplied to `eventsurvey()`. ## 2. Estimate the effect The formula contains the outcome and time variables. By default, `event_time = 0`, `event_day = "exclude"`, and `window = 6`. The model always uses estimand-specific calibration. It uses the final six observed pre-event periods, skips the event period, and forecasts the first six observed post-event periods. The rolling pre-event exercises fit six observed periods and match the actual elapsed forecast horizons of the final analysis. With daily observations, this preserves the omitted Day 0. On sparse schedules, only complete windows with those forecast offsets are used. Use **untransformed column names**, with time first: `outcome ~ time` or `outcome ~ time + covariate1 + covariate2`. For example, `y ~ day + group` adds a baseline group covariate when the data contain it. See [Covariate Adjustment](covariate-adjustment.html) for a complete example. Formulas such as `log(y) ~ day`, `y ~ I(day^2)`, and `y ~ day * group` are rejected because formula transformations, nonlinear time terms, and interactions are not supported. If a substantively meaningful transformed outcome is needed, create it as a clearly named numeric column before fitting; the resulting effect is measured in that column's units. ```{r} fit <- eventsurvey(y ~ day, sample_data) fit ``` The fitted object automatically checks calendar coverage, responses per period, variance information, the number of rolling reference windows, and elapsed fitting and forecasting spans. `summary(fit)` and the HTML report show each check as `OK` or `Review`. A `Review` flag is descriptive: it does not change the estimate or automatically invalidate the analysis. See [Understanding Automatic Diagnostics](automatic-diagnostics.html) for examples of every check and guidance on responding to a `Review` status. For a one-command workflow, pipe the fitted analysis to `eventsurvey_report()`. Without a filename, the package creates a temporary self-contained HTML report. The default `open = interactive()` opens that report automatically in an interactive session such as the RStudio console, but not in `Rscript`, tests, continuous integration, or other batch runs. The HTML file is created either way. Set `open = TRUE` or `open = FALSE` to override this behavior. ```{r report-example, eval=FALSE} eventsurvey(y ~ day, sample_data) |> eventsurvey_report() ``` To keep the report, provide an output filename: ```{r report-save-example, eval=FALSE} eventsurvey(y ~ day, sample_data) |> eventsurvey_report("eventsurvey-report.html") ``` The printed interval is the honest bounded-misspecification-error interval. It is generally wider than the conventional interval because it allows the post-event extrapolation to be wrong by as much as the worst comparable pre-event forecast. ### Estimate the average effect over five days The package uses estimand-specific calibration for each daily effect, matching reference errors by elapsed forecast horizon. For a period average, it first calibrates each day, then aggregates with equal day weights: ```{r five-day-average} fit_five <- eventsurvey( y ~ day, sample_data, window = 5, event_day = "exclude", schedule = "consecutive" ) fit_five$estimate fit_five$days fit_five$average_components ``` This targets Days 1–5 and fits Days -5 through -1. Consecutive mode ensures that five observed periods really are five calendar days. The standard error includes covariance from the shared pre-event regression. The average honest interval uses daily calibration at significance level `alpha / 5` before averaging endpoints (Bonferroni); the displayed daily intervals use `alpha`. The same procedure works with covariates and `eventsurvey_summary()`. See [Methodology and Assumptions](methodology.html) for the derivation. ## 3. See what was extrapolated ```{r counterfactual} plot(fit, type = "counterfactual") ``` Dark points are observed daily means. The gray line is the fitted pre-event trend and its continuation. Open points mark the forecast no-event means; blue vertical gaps are the day-specific estimates. ```{r effects} plot(fit, type = "effects") ``` ## 4. Inspect the forecasting diagnostic ```{r diagnostics} plot(fit, type = "diagnostics") ``` Every line is one rolling fit. Its points show the errors for the six forecast target dates. With daily observations and an excluded Day 0, these are elapsed horizons two through seven after the last fitting day. These are not placebo treatment effects; they are direct checks of the same extrapolation task used after the event. ## Event-day conventions By default, relative period 0 is excluded because respondents interviewed during the event period often have ambiguous exposure. Forecasting therefore begins with the first observed period after the event. Include period 0 only when the event timing and fieldwork make its exposure status defensible. ```{r} fit_including_event <- eventsurvey( y ~ day, sample_data, event_time = 0, window = 6, event_day = "include" ) fit_hidden <- eventsurvey( y ~ day, sample_data, event_time = 0, window = 6, event_day = "include", report_event_day = FALSE ) ``` The second alternative retains the event period in the window-average estimate but suppresses its day-specific display; use it only when that estimand is substantively intended. These choices should follow the survey's fieldwork and event timing, not whichever result is most favorable. Under the default exclusion rule with daily observations, the diagnostic also skips one period between each rolling fit and its forecasts. The interface requires at least `2 * window + 1` observed pre-event periods. Sparse schedules may need more history to supply a complete matching window. ## Also see - [Preparing Survey Fieldwork Data](data-preparation.html) for variable, timing, and data-format requirements. - [Reading an HTML Report](reading-html-reports.html) for guidance on every estimate, diagnostic, and figure in the report. - [Understanding Automatic Diagnostics](automatic-diagnostics.html) for the meaning of each `OK` and `Review` check. - [Complete Example Workflow](example-workflow.html) for a longer analysis with plots and sensitivity checks.