## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)

## ----setup--------------------------------------------------------------------
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

## ----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)

## ----diagnostics--------------------------------------------------------------
adjusted$model_diagnostics
plot(adjusted, type = "diagnostics")

## ----counterfactual-----------------------------------------------------------
plot(adjusted, type = "counterfactual")

## ----effects------------------------------------------------------------------
plot(adjusted, type = "effects", inner_level = 0.90)

## -----------------------------------------------------------------------------
coef(adjusted)
confint(adjusted)

## ----eval = FALSE-------------------------------------------------------------
# eventsurvey_report(adjusted, "adjusted-report.html")

