The two data frames come first in every call, so the estimators compose with the native pipe. Examples below use it where it reads better.
Suppose you want to know how a biomarker relates to a symptom score. The biomarker is drawn at clinic visits; the symptom score is collected by a questionnaire on its own schedule. Neither is measured often, and the two schedules have nothing to do with each other. You have, per subject,
and \(\{T_{ij}\}\) and \(\{S_{ik}\}\) never coincide. The model you want is the ordinary one,
\[E\{Y_i(t) \mid X_i(t)\} = g\{X_i(t)^\top \beta\},\]
but \(Y_i\) and \(X_i\) are never observed at the same \(t\), so not one row of the data set is a complete observation of the regression you are trying to fit.
This is the setting of Cao, Zeng and Fine (2015). The package
implements their two estimators: kee_async() for a constant
\(\beta\), and
kee_async_td() for a coefficient curve \(\beta(t)\).
Carrying the last covariate value forward, or smoothing the covariate first and substituting it, both attenuate \(\hat\beta\) when sampling is sparse. The kernel-weighted equation instead evaluates the link at each covariate vector you actually have, and lets a weight express how much that observation says about a given response occasion.
\[U_n(\beta) = n^{-1} \sum_i \sum_j \sum_k W_h(T_{ij} - S_{ik})\, X_i(S_{ik}) \left[ Y_i(T_{ij}) - g\{X_i(S_{ik})^\top \beta\} \right] = 0,\]
with \(W_h(u) = W(u/h)/h\) and \(W\) the Epanechnikov kernel. Every (response, covariate) pair within a subject contributes, weighted by how far apart in time the two occasions are. Pairs more than \(h\) apart drop out; pairs close together dominate.
The equation sees times only through \((T -
S)/h\), so any units work as long as h is in the
same ones. The package warns when it looks wrong.
sim_async_data() generates the design from Section 4 of
the paper: response and covariate observation times are independent
Poisson streams, and the covariate is a Gaussian process.
set.seed(2024)
d <- sim_async_data(n = 400, beta = c(0.5, 1.5))
str(d$y)
#> tibble [1,985 × 3] (S3: tbl_df/tbl/data.frame)
#> $ id : int [1:1985] 1 1 1 1 1 1 1 2 2 2 ...
#> $ time: num [1:1985] 0.303 0.416 0.457 0.68 0.698 ...
#> $ y : num [1:1985] 1.03 2.36 2.85 2.65 2.24 ...
str(d$x)
#> tibble [1,987 × 3] (S3: tbl_df/tbl/data.frame)
#> $ id : int [1:1987] 1 1 1 1 2 2 3 3 4 4 ...
#> $ time: num [1:1987] 0.119 0.704 0.902 0.953 0.192 ...
#> $ x : num [1:1987] 0.5317 0.0115 -0.292 0.3258 -0.9327 ...Two tables, sharing only id. There is no way to merge
them without inventing rows, which is precisely the point.
c(
subjects = length(unique(d$y$id)),
response_occasions = nrow(d$y),
covariate_occasions = nrow(d$x),
median_per_subject_y = median(table(d$y$id)),
median_per_subject_x = median(table(d$x$id))
)
#> subjects response_occasions covariate_occasions
#> 400 1985 1987
#> median_per_subject_y median_per_subject_x
#> 5 5The formula spans both tables, taking its left-hand side from
data_y and its right-hand side from data_x.
id and time name columns present in each:
fit <- d$y |>
kee_async(d$x, y ~ x, id = id, time = time, h = 0.25)
summary(fit)
#> Call:
#> kee_async(data_y = d$y, data_x = d$x, formula = y ~ x, id = id,
#> time = time, h = 0.25)
#>
#> Asynchronous longitudinal regression (identity link, full kernel)
#> n= 400 subjects bandwidth h = 0.25
#>
#> Estimate Std. Error z value Pr(>|z|)
#> (Intercept) 0.441986 0.057943 7.6279 2.386e-14 ***
#> x 1.408638 0.044451 31.6897 < 2.2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Optimization status: 0The truth is \(\beta = (0.5, 1.5)\).
npair records how many weighted pairs actually
contributed:
Standard generics work as usual:
tidy(), glance() and augment()
return tibbles, so a fit goes straight into the rest of a tidy workflow
without reshaping.
tidy(fit, conf.int = TRUE)
#> # A tibble: 2 × 7
#> term estimate std.error statistic p.value conf.low conf.high
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 0.442 0.0579 7.63 2.39e- 14 0.328 0.556
#> 2 x 1.41 0.0445 31.7 2.16e-220 1.32 1.50
glance(fit)
#> # A tibble: 1 × 7
#> nobs nterms h npair link one_sided convergence
#> <int> <int> <dbl> <int> <chr> <lgl> <int>
#> 1 400 2 0.25 4324 identity FALSE 0augment() attaches the fitted mean to the
covariate table, because that is where a fitted value
lives here: the estimating equation evaluates the link at each observed
covariate vector, and the kernel weight is what ties that vector to a
response occasion.
augment(fit, d$x)
#> # A tibble: 1,987 × 4
#> id time x .fitted
#> <int> <dbl> <dbl> <dbl>
#> 1 1 0.119 0.532 1.19
#> 2 1 0.704 0.0115 0.458
#> 3 1 0.902 -0.292 0.0307
#> 4 1 0.953 0.326 0.901
#> 5 2 0.192 -0.933 -0.872
#> 6 2 0.456 -0.523 -0.294
#> 7 3 0.0395 -0.480 -0.234
#> 8 3 0.748 -0.366 -0.0731
#> 9 4 0.0143 -1.84 -2.15
#> 10 4 0.0156 -1.86 -2.18
#> # ℹ 1,977 more rowsThe fit does not retain its data, so augment() needs
data_x handed back to it.
h is in the units of time. Small
h admits few pairs; large h admits pairs far
apart in time. Fit at several values and compare:
hs <- c(0.05, 0.10, 0.15, 0.25, 0.40, 0.60)
sweep <- t(sapply(hs, function(h) {
f <- d$y |> kee_async(d$x, y ~ x, id = id, time = time, h = h)
c(h = h, est = coef(f)[2], se = sqrt(vcov(f)[2, 2]))
}))
round(sweep, 3)
#> h est.x se
#> [1,] 0.05 1.524 0.054
#> [2,] 0.10 1.478 0.049
#> [3,] 0.15 1.456 0.046
#> [4,] 0.25 1.409 0.044
#> [5,] 0.40 1.355 0.043
#> [6,] 0.60 1.286 0.043
plot(sweep[, "h"], sweep[, 2],
type = "b", pch = 19, ylim = range(sweep[, 2] + c(-2, 2) * sweep[, 3]),
xlab = "bandwidth h", ylab = expression(hat(beta)[1]),
main = "Estimate against bandwidth"
)
arrows(sweep[, "h"], sweep[, 2] - 1.96 * sweep[, 3],
sweep[, "h"], sweep[, 2] + 1.96 * sweep[, 3],
angle = 90, code = 3, length = 0.04, col = "grey50"
)
abline(h = 1.5, lty = 2, col = "firebrick")Choose h from this plot, weighing the width of the
interval against how far the estimate moves.
By default the kernel is two-sided: a response at \(T_{ij}\) is informed by covariate
observations on either side of it. If the covariate must be already
known at the response time, whether for a causal reading or because
later covariate values could not have influenced the response, set
one_sided = TRUE.
full <- kee_async(d$y, d$x, y ~ x, id = id, time = time, h = 0.25)
half <- kee_async(d$y, d$x,
y ~ x, id = id, time = time,
h = 0.25, one_sided = TRUE
)
rbind(
full = c(coef(full), pairs = full$npair),
half = c(coef(half), pairs = half$npair)
)
#> (Intercept) x pairs
#> full 0.4419863 1.408638 4324
#> half 0.4571807 1.385280 2184The half kernel discards roughly half the pairs, so at a fixed
h it is noisier. Compare bandwidths under the same setting
you intend to fit with.
If the association itself changes over follow-up,
kee_async_td() estimates \(\beta(t)\) pointwise. At a target time
\(t\) the response and covariate
occasions are weighted by their separate distances to
\(t\); the lag between them never
enters.
\[U_n\{\beta(t)\} = n^{-1} \sum_i \sum_j \sum_k W_{h_1}(t - T_{ij})\, W_{h_2}(t - S_{ik})\, X_i(S_{ik}) \left[ Y_i(T_{ij}) - g\{X_i(S_{ik})^\top \beta(t)\} \right] = 0.\]
Both time arguments are smoothed, so the curve needs more data than the time-invariant fit.
set.seed(2025)
dt <- sim_async_data(
n = 800, beta = function(tt) cbind(0.5, 1 + tt),
lambda_y = 8, lambda_x = 8,
x_cov = function(s, t) exp(-4 * (s - t)^2)
)
fit_td <- kee_async_td(dt$y, dt$x,
y ~ x, id = id, time = time,
times = seq(0.2, 0.8, by = 0.05), h = 0.2
)
plot(fit_td)The truth is \(\beta_1(t) = 1 + t\). Compare the fitted curve against it:
tidy(fit_td, conf.int = TRUE) |>
subset(term == "x", select = c(time, estimate, conf.low, conf.high)) |>
head(4)
#> # A tibble: 4 × 4
#> time estimate conf.low conf.high
#> <dbl> <dbl> <dbl> <dbl>
#> 1 0.2 1.12 1.03 1.22
#> 2 0.25 1.16 1.06 1.25
#> 3 0.3 1.19 1.10 1.27
#> 4 0.35 1.20 1.12 1.28The bands are pointwise. Keep times inside the observed
time range.
link = "log" and link = "logistic" fit the
corresponding generalised linear models by Newton-Raphson on the same
estimating equation. Everything above applies unchanged.
set.seed(11)
db <- sim_async_data(n = 400, beta = c(0.3, 1.0), link = "logistic")
fit_b <- kee_async(db$y, db$x,
y ~ x,
id = id, time = time, h = 0.3, link = "logistic"
)
summary(fit_b)
#> Call:
#> kee_async(data_y = db$y, data_x = db$x, formula = y ~ x, id = id,
#> time = time, h = 0.3, link = "logistic")
#>
#> Asynchronous longitudinal regression (logistic link, full kernel)
#> n= 400 subjects bandwidth h = 0.3
#>
#> Estimate Std. Error z value Pr(>|z|)
#> (Intercept) 0.285310 0.059651 4.783 1.727e-06 ***
#> x 0.899442 0.068209 13.187 < 2.2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Optimization status: 0h must be on the same scale as the observation times.
With times in days and h chosen as though they were on the
unit interval, the package warns that almost nothing is
contributing.
d_days <- d
d_days$y$time <- d$y$time * 365
d_days$x$time <- d$x$time * 365
fit_days <- kee_async(d_days$y, d_days$x,
y ~ x,
id = id, time = time, h = 1
)
#> Warning: only 62 of 1985 response occasions have a covariate observation within
#> h = 1. Check that 'h' is on the same scale as the observation times.Rescaling h by the same factor recovers the original fit
exactly, which is the scale-equivariance of the estimating equation:
fit_ok <- kee_async(d_days$y, d_days$x,
y ~ x,
id = id, time = time, h = 0.25 * 365
)
all.equal(coef(fit_ok), coef(fit))
#> [1] TRUEThe survival estimators behave the same way. Their spline basis and cumulative-hazard quadrature are built on the observed follow-up rather than a fixed interval, so rescaling time and the bandwidth together leaves the coefficients unchanged.
Cao, H., Zeng, D. and Fine, J. P. (2015). Regression analysis of sparse asynchronous longitudinal data. Journal of the Royal Statistical Society, Series B 77, 755–776.