tests/testthat/test-summarize-panel.R

test_that("summarize_panel computes core quantities correctly", {
  dat <- data.frame(
    eeid   = c("a", "b", "c",  "a",  "b",  "d"),
    wave   = c(0,   0,   0,    1,    1,    1),
    metric = c(1,   2,   3,    2,    3,    4)
  )
  
  s <- summarize_panel(
    data       = dat,
    wave_0     = 0,
    wave_1     = 1,
    id_col     = "eeid",
    wave_col   = "wave",
    metric_col = "metric"
  )
  
  # Basic counts
  expect_equal(s$N0, 3L)
  expect_equal(s$N1, 3L)
  expect_equal(s$nS, 2L)  # a and b
  
  # Means
  expect_equal(s$ybar0, mean(c(1,2,3)))
  expect_equal(s$ybar1, mean(c(2,3,4)))
  
  # Individual-level variances
  expect_equal(s$V0_ind, var(c(1,2,3)))
  expect_equal(s$V1_ind, var(c(2,3,4)))
  
  # Stayer metrics: a:(1,2), b:(2,3)
  y0S <- c(1,2)
  y1S <- c(2,3)
  
  expect_equal(s$C01_ind, cov(y0S, y1S))
  expect_equal(s$V0S_ind, var(y0S))
  expect_equal(s$V1S_ind, var(y1S))
})

test_that("summarize_panel rejects missing values", {
  dat_na <- data.frame(
    eeid   = c("a", "b", NA),
    wave   = c(0,    0,   1),
    metric = c(1,    2,   3)
  )
  
  expect_error(
    summarize_panel(
      data       = dat_na,
      wave_0     = 0,
      wave_1     = 1,
      id_col     = "eeid",
      wave_col   = "wave",
      metric_col = "metric"
    ),
    "does not handle missing values"
  )
})

test_that("summarize_panel rejects duplicate id–wave combinations", {
  dat_dup <- data.frame(
    eeid   = c("a", "a", "b", "b"),
    wave   = c(0,   0,   1,   1),
    metric = c(1,   2,   3,   4)
  )
  
  expect_error(
    summarize_panel(
      data       = dat_dup,
      wave_0     = 0,
      wave_1     = 1,
      id_col     = "eeid",
      wave_col   = "wave",
      metric_col = "metric"
    ),
    "Duplicate id-wave combinations detected; aggregate upstream for v0.1."
  )
})

Try the wavecov package in your browser

Any scripts or data that you put into this service are public.

wavecov documentation built on Nov. 3, 2026, 5:07 p.m.