tests/testthat/test-linked-vs-reliability.R

test_that("linked and reliability give reasonable variances under a simple model", {
  set.seed(1)
  
  N0 <- 200
  N1 <- 180
  nS <- 150
  
  rho   <- 0.6
  sigma <- 1
  
  ids_S <- paste0("s", seq_len(nS))
  ids_L <- paste0("l", seq_len(N0 - nS))
  ids_J <- paste0("j", seq_len(N1 - nS))
  
  e0 <- rnorm(nS, 0, sigma)
  e1 <- rnorm(nS, 0, sigma)
  T  <- rnorm(nS, 0, sigma * sqrt(rho / (1 - rho)))
  
  y0S <- T + e0
  y1S <- T + e1
  
  y0L <- rnorm(N0 - nS, 0, sigma)
  y1J <- rnorm(N1 - nS, 0, sigma)
  
  dat <- rbind(
    data.frame(eeid = c(ids_S, ids_L),
               wave = 0,
               metric = c(y0S, y0L)),
    data.frame(eeid = c(ids_S, ids_J),
               wave = 1,
               metric = c(y1S, y1J))
  )
  
  res_linked <- compare_waves(
    data       = dat,
    wave_0     = 0,
    wave_1     = 1,
    id_col     = "eeid",
    wave_col   = "wave",
    metric_col = "metric",
    method     = "linked"
  )
  
  res_rel <- compare_waves(
    data       = dat,
    wave_0     = 0,
    wave_1     = 1,
    id_col     = "eeid",
    wave_col   = "wave",
    metric_col = "metric",
    method     = "reliability",
    reliability = rho
  )
  
  tidy_linked <- tidy(res_linked)
  tidy_rel    <- tidy(res_rel)
  
  v_linked <- tidy_linked$change_variance
  v_rel    <- tidy_rel$change_variance
  
  # basic sanity checks
  expect_true(is.finite(v_linked) && v_linked > 0)
  expect_true(is.finite(v_rel)    && v_rel    > 0)
  
  # they should be in the same ballpark (within a factor of 2, say)
  ratio <- max(v_linked, v_rel) / min(v_linked, v_rel)
  expect_lt(ratio, 2)
})

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.