---
title: "Estimating from Period-Level Summary Data"
description: "Estimate effects from period-level counts, means, and variances when respondent records are unavailable."
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Estimating from Period-Level Summary Data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.2)
```

`eventsurvey_summary()` is a primary estimation interface for data frames or
tibbles stored at the survey-period level. Use it when respondent records
cannot be shared, are unnecessarily large for the task, or have already been
reduced to a count, mean, and sample variance for each observed survey period.

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

## Choose the function that matches the data

| Data available | Function | Required input |
|:--|:--|:--|
| One row per respondent | `eventsurvey()` | Outcome and interview period |
| One row per observed period | `eventsurvey_summary()` | Period mean, count, sample variance, and period |

With a time-only formula, both functions use the same linear extrapolation, rolling forecast
diagnostics, bias bound, and interval construction. They return the same
`eventsurvey` object class, so the same `summary()` and `plot()` methods apply.

## Required period-level structure

The summary data must have exactly one row for every observed survey period
and four quantities:

1. The period or interview date.
2. The outcome mean in that period.
3. The number of non-missing outcome observations, `n`.
4. The ordinary within-period sample variance, using denominator `n - 1`.

Do not replace an unobserved date with a fabricated zero-count row or an
interpolated outcome. A date without respondents remains absent. The
`schedule` argument determines whether the analysis treats the observed
periods as the schedule or requires consecutive calendar periods.

## From respondent records to period summaries

The following example first estimates from respondent records and then from
their period-level sufficient statistics. Both main fits use the default
estimand-specific calibration.

```{r}
respondents <- sample_data

daily <- respondents |>
  group_by(day) |>
  summarise(
    mean = mean(y),
    n = n(),
    variance = var(y),
    .groups = "drop"
  )

head(daily)
```

The respondent-level call is:

```{r}
fit_respondents <- eventsurvey(
  y ~ day,
  data = respondents
)
```

With the conventional column names `n` and `variance`, the corresponding
period-level call is equally concise:

```{r}
fit_daily <- eventsurvey_summary(
  mean ~ day,
  data = daily
)
```

Use `n = another_count_column` or `variance = another_variance_column` when
the summary columns have different names. Likewise, supply `event_time`
explicitly when the time variable is not coded relative to an event at zero.

For this time-only model, correctly constructed summaries preserve all
information used by the estimator:

```{r}
all.equal(fit_respondents$estimate, fit_daily$estimate)
all.equal(fit_respondents$days, fit_daily$days)
```

This equivalence applies only to the time-only model. Adding period averages
of respondent covariates does not recover their within-period relationship
with the outcome or the uncertainty in overlapping adjusted fits.
For covariate adjustment, use respondent-level `eventsurvey()`; see
[Covariate Adjustment](covariate-adjustment.html).

## Inspect and plot the result

The result has the same structure and methods as a respondent-level fit.

```{r}
fit_daily
summary(fit_daily)
plot(fit_daily, type = "counterfactual")
plot(fit_daily, type = "effects")
```

## Sparse periods and single responses

The default `schedule = "observed"` supports irregular survey schedules.
The package keeps the actual time values in estimation, reports missing dates,
and reports the elapsed span of the rolling windows. Use
`schedule = "consecutive"` when every calendar period must be observed.

When a period has one response, its sample variance is undefined and may be
recorded as `NA`. The package retains its mean and count and uses the pooled
within-period variance for the sampling-uncertainty calculation. Such a period
should be treated as diagnostically fragile rather than as strong standalone
population evidence.

## Published summary-data example

The bundled `published_examples` object contains daily sufficient statistics
for the Epifanio, Giani, and Ivandic application.

```{r}
data(published_examples)
privacy <- published_examples |>
  filter(grepl("privacy", outcome))

fit_privacy <- eventsurvey_summary(
  mean ~ day,
  data = privacy,
  n = n,
  variance = variance,
  event_time = 0,
  window = 5,
  event_day = "include",
  report_event_day = FALSE,
  schedule = "observed"
)

fit_privacy$estimate
```


## Also see

- [Getting Started](getting-started.html) for the respondent-level interface
  and basic reporting workflow.
- [Examples](published-applications.html) for a complete report estimated from
  bundled period-level summaries.
- [Preparing Survey Fieldwork Data](data-preparation.html) for time-variable,
  event-timing, and schedule requirements.
