---
title: "Preparing Survey Fieldwork Data"
description: "Prepare respondent-level data, time variables, event timing, and survey schedules."
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Preparing Survey Fieldwork Data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
```

`eventsurvey()` accepts a data frame or tibble with one respondent per row.
Prepare a numeric outcome and one equally spaced time variable before fitting.

All estimation examples below use estimand-specific calibration.
## Formula requirements

Use one outcome column, put time first, and optionally add baseline covariates:

```{r, eval = FALSE}
fit <- eventsurvey(outcome ~ time, data = survey_data)
adjusted <- eventsurvey(outcome ~ time + age + education, data = survey_data)
```

Both sides of the formula must be untransformed column names. The following
specifications are intentionally rejected:

```{r, eval = FALSE}
eventsurvey(log(outcome) ~ time, survey_data) # transformed in formula
eventsurvey(outcome ~ I(time^2), survey_data) # nonlinear time term
eventsurvey(outcome ~ time * group, survey_data) # interaction
```

This restriction keeps the fitted model additive and linear in time.
Numeric covariates enter linearly; factors, character, and logical columns
are categorical. Ordered factors are treated as unordered. To analyze a substantively justified
transformation of the outcome, create an explicit numeric column first and use
its name in the formula. The package does not currently support nonlinear time
trends or interactions. Covariates must represent characteristics unaffected
by the event. See [Covariate Adjustment](covariate-adjustment.html).

Period-level input remains time-only: use
`eventsurvey_summary(period_mean ~ time, data = period_data)` with simple
column names. Counts, means, and outcome variances are insufficient for
respondent-level covariate adjustment.

## Dates

```{r}
library(eventsurvey)
library(dplyr)

dat <- sample_data |>
  mutate(interview_date = as.Date("2026-06-15") + day)
fit <- eventsurvey(y ~ interview_date, dat,
  event_time = as.Date("2026-06-15"), window = 6
)
```

For timestamps, the package converts `POSIXt` values to calendar dates.
Normalize time zones and decide how interviews spanning midnight should be
classified before analysis.

## Event-period exposure

The default `event_day = "exclude"` omits relative period 0 from the forecast
window and begins estimation with the first observed period after the event.
This is the safer choice when a period combines respondents interviewed before
and after the event or when exact within-period interview timing is unknown.

Use `event_day = "include"` only when period-0 respondents can defensibly be
classified as exposed. If period 0 belongs in the window-average estimand but
should not appear as a day-specific result, also set
`report_event_day = FALSE`. Record the rule and its substantive justification
before comparing results.

## Outcomes and missingness

Binary outcomes coded 0/1, ordered survey responses coded with numeric values
such as 1--5, and continuous outcomes can be analyzed when their period means
are substantively meaningful. The estimator reports differences in those
original coded units: for example, changes in proportions on a 0--1 outcome
(multiply by 100 for percentage points) or scale-point changes on a 1--5
outcome. A factor or character response must first be recoded deliberately;
for an ordinal item, interpreting means assumes that the chosen numerical
spacing is meaningful. Recode special missing values to `NA`. Rows with
missing outcome, time, or included covariates are omitted with a warning; report that decision and
the resulting daily counts.
Adjusted models require at least two complete responses in each pre-event and
forecast period and identifiable coefficients in every fitting window.
Use the same complete-case sample when comparing adjusted and time-only fits.

## Sparse and irregular fieldwork

By default, rolling windows contain consecutive *observed survey periods*.
The package detects unobserved dates before and after the event, retains the
actual time values in every trend, and reports both the gaps and each window's
elapsed span. It does not interpolate outcomes for dates with no respondents.

Inspect these diagnostics directly:

```{r}
fit$settings$missing_pre_periods
fit$settings$missing_forecast_periods
fit$settings$fit_span
fit$settings$forecast_span
head(fit$windows)
```

When the design requires uninterrupted calendar days, request strict checking:

```{r}
strict_fit <- eventsurvey(y ~ day, dat,
  event_time = 0, window = 6,
  schedule = "consecutive"
)
```

Strict mode stops and lists missing pre-event periods. Do not remove a date
merely because its outcome is unusual. Restrictions should follow fieldwork
operations or a prespecified design rule.

See [Sparse Survey Schedules](sparse-survey-schedules.html) for a complete
simulation comparing uninterrupted and irregular fieldwork, including fitted
counterfactuals and gap diagnostics.

## Also see

- [Getting Started](getting-started.html) to fit a model after preparing the
  required outcome and time variables.
- [Methodology and Assumptions](methodology.html) for the estimand and the role
  of observed-period versus consecutive-calendar windows.
- [Estimating from Period-Level Summary Data](summary-data.html) when only
  period counts, means, and variances are available.
