Suggested citation
Kayleigh P. Keller, Meredith Pedde, Emma Nichols, and Sara D. Adar. 2026. Time-Varying Environmental Exposures in Time-to-Event Analysis. GECC Guidance Series. Version 1.0 (November 2026). Funded by the US National Institute on Aging, grant U24 AG088894. Los Angeles: University of Southern California, November. https://doi.org/10.25553/gecc.102
Download the PDF Download citation: BibTeX · RIS · EndNote Version 1.0 · November 2026
Illustrative example of exposure with seasonality and a time drift – exposure should be calculated from the window just before each event, not from one fixed date.
This guidance provides best practices for incorporating time-varying environmental exposures into time-to-event analyses.
Time-varying, moving-average exposures require particular care to ensure they are appropriately included in statistical models. Due to temporal trends in the exposure, exposure data cannot be linked to observation times in the same manner as non-environmental time-varying covariates.
Use the interactive guidance tool below to learn how to tackle this issue.
First, choose a topic:
Time-to-event analyses are commonly used to model disease incidence and mortality, with the Cox proportional hazards regression (PH regression) model being the most common.
Here we provide a brief overview of the PH regression model, with a focus on the aspects most relevant to dealing with time-varying environmental exposures. A comprehensive overview of the PH regression model and its applications is available in Hosmer, Lemeshow, and May (2008) and many other sources.
The basic version of the PH regression model takes the form:
Here, λi(t) is the The instantaneous risk of the event at time t for someone who has not yet had the event. for individual i, Xi is the exposure of interest with corresponding coefficient βX, and Zi are adjustment variables. The hazard λi(t) is the instantaneous risk of disease onset (or other event) occurring at time t. The advantage of the proportional hazards model is that the hazard function is decomposed into two components: the baseline hazard, λ0(t), and the covariate term, exp(βXXi + βzTZi). The baseline hazard is a time-varying baseline risk of disease and is not directly estimated in the model. Thus, the model can account for variation in risk over time without having to parametrically specify its form. The covariate term allows for the baseline hazard to be scaled by effects of different predictor variables. Here, we consider a linear exposure-response relationship (βXXi), but the principles apply to non-linear functions of exposure and other more complex settings.
Further, it is common to have the proportional hazards model stratified by additional categorical variable(s). Each stratum has its own baseline hazard. Letting m index the discrete levels of the stratifying variable, the model becomes:
Including the strata allows for non-parametric adjustment for the stratifying variable(s). In studies related to aging, stratifying by age (e.g., in two-year ranges) is a useful way to account for the complex relationship age has with disease risk (Zhang et al. 2023).
In models (1) and (2), the value βX is the log hazard ratio for the exposure of interest. It is more commonly interpreted on an exponentiated scale, where exp(βX) is the hazard ratio (HR) for a one-unit difference in exposure. As a ratio, the HR quantifies the relative (multiplicative) difference in risk of incident disease, conditional on the other variables adjusted for in the model. The HR can take any value between 0 and positive infinity. An HR greater than 1 indicates increased risk of disease (or other event) and an HR less than 1 indicates lower risk of disease.
A key attribute of the PH regression model is that the information for estimating the model parameters comes from comparing the exposure status of those individuals who have an event with those who have not yet had an event, at every time point when someone has an event. The set of individuals who have not yet had an event up to that point is called the The individuals who have not yet had the event and have not been censored at a given time, so are still at risk of the outcome., as it encompasses all individuals currently at risk for the outcome.

How to read Figure 1. Each horizontal line is one individual’s follow-up over study time.
At time 4, a risk set is formed because person 002 experiences the event. At this time, all people contribute information to the risk set (001, 002, 003, 004, and 005).
At time 13, in contrast, only persons 001, 003, and 004 are informing the risk of an event. Person 003 has the event; person 002 had theirs at time 4, and person 005 was censored at time 6.
Risk sets change when individuals have an event or because an individual is censored (through death, drop out, or some other mechanism). When there are no time-varying predictor variables, most software for fitting PH regression models will calculate risk sets automatically based on the event times.
When there are time-varying predictors in a PH regression model, the calculation of risk sets and required data structures becomes more complicated. The form of the model appears the same:
In this model, the log hazard ratio βX has the same interpretation as before, but the exposure of interest and the adjustment variables change over time. The information about time-varying predictor variables consists of both the predictor variable values and the time points or time ranges for each of those values.
The most important part of including time-varying predictors is that the predictors—including the exposure—must be updated at the time of every event (i.e., for each risk set). The exposure and predictor values cannot just be calculated from the time an individual has an event or is censored, but rather must be calculated at each time point throughout the study.
The problem. If there is a temporal trend in overall exposure levels and exposure values are only calculated from each person’s final time point, large bias can be introduced.
The setup. Take the five individuals from Figure 1, with air pollution as the exposure of interest and a decreasing trend over time. For simplicity, we show a single city-wide trend; in practice, the trend would vary across space and be different for each individual.
The correct approach. For the risk of an event at time t=4, calculate everyone’s exposure over the period right before t=4 (the blue box in Figure E1). All five individuals have the same average exposure, about 10.8 units.

The incorrect approach. The wrong thing to do is to calculate each person’s exposure over the period before their own event or censoring date (Figure E2). The individual censored at time 20 then has a much lower exposure than the others, only because pollution is decreasing.

Why it matters. People with a longer time until event or censoring appear to have lower exposures because of the time trend. This leads to Bias that makes an association look stronger, or more statistically significant, than it really is. in the analysis, even though everyone had the same exposure at each time point in the study.
When the time-varying predictors are constant over a time interval, handling the creation of risk sets is relatively straightforward in most statistical packages. For example, marital status may change over time, but in a piecewise constant manner. Someone who is married retains that status until separation or widowhood, and so time-varying marital status is fully determined by the date of marriage (if any) and date of separation (if any). Given two pieces of information (marital status at study baseline and time at which marital status changes), statistical programs can transform data into a format where current marital status can be filled in for any time point.
However, when considering time-varying Exposures averaged over a window of time just before each date, such as the prior 12 months., the required data structure becomes more complicated. For example, suppose we are interested in the impact of prior 12-months average air pollution exposure on risk of dementia and we have data on individuals from surveys that take place every two years. It is insufficient to calculate the 12-month average pollution exposure only at the times when we have survey responses from that individual. Instead, we need to have the exposure value for each person for each risk set – which is determined by the times at which any individual has an event. Thus, we need to have values of the prior 12-month average exposure calculated for any date at which any person had a dementia diagnosis. While this requires more technical setup, this approach is important to ensure that the exposures precede the outcome (event). Allowing future exposure information to inform current risk breaks temporal ordering assumptions.
The R Walkthrough provides example code for how to prepare a dataset for including time-varying environmental data.
It is important to note that when fitting models with time-varying environmental exposure and a time-varying outcome, there is potential for Confounding that arises when exposure and outcome both change over calendar time or with age, so the time trend itself can create or mask an association.. This can be accounted for by adjusting for or stratifying by calendar time and age (Adar et al. 2018; Zhang et al. 2023).
Besides the additional technical detail for accommodating time-varying environmental exposure data, there are additional decisions that typically must be made when dealing with longitudinal data for PH regression models.
Covariates (here referring to confounders and other adjustment variables besides the exposure(s) of interest) are typically “The most recent recorded value is carried forward and used until a new value is recorded.”. This means that the values are used as-is until a new value is recorded. With this approach, in a study like the Health and Retirement Study (HRS) someone’s marital status at Wave 4 is used for all time points up until the variable is measured again at Wave 5. This choice assumes that the covariate status is constant over the interval between waves. That assumption may not always hold but is typically not far from the truth—with the important exception of covariates that are known to be highly time-varying (e.g., weather data). For those cases, time-varying values could be calculated based on residential history in a manner similar to time-varying exposures.
When outcome data are only available at certain intervals, this presents a challenge for time-to-event analyses—even those that do not use time-varying exposures. For example, in a study with biennial follow-up, dementia status may only be recorded at survey waves but the condition itself manifests between waves1.
In this type of setting, a common strategy is to use the midpoint between the two survey waves as the time of diagnosis. For example, suppose an individual has no dementia diagnosis on January 1, 2012 at their Wave 2 visit but does have a dementia diagnosis by January 1, 2014 at their Wave 3 visit. Using the date of the Wave 3 assessment is going to be “too late” which is important for a model that is looking for time to event (as opposed to a binomial or logistic model that is looking simply at if a person was ever diagnosed or not). Instead, the dementia onset could be assigned to January 1, 2013.
1 With dementia in particular, it can be difficult to establish a specific date of onset, as can be done for conditions such as a myocardial infarction. ↩
For analyses of incident disease, we need to arrange the data in “A data format with one row per time interval (tstart to tstop) for each person.” form. This format means that we have a row in the data for each time interval for each person. The time intervals are determined by the event times for the outcome of interest across all individuals in the analysis. The exposure values need to be calculated for each row of the expanded dataset (see Example 1 in the Proportional Hazards Regression Model topic for why this matters).
This walkthrough uses functions from the survival package in R. In particular, it makes use of tmerge(), a function designed for creating datasets with time-varying variables. While most of what follows applies to general datasets for PH regression, the step Create the Structure for Adding in Time-Varying Exposure is critical for preparing to add time-varying moving-average environmental exposure data. Additional help and examples for using tmerge() are available in the survival package vignette (Therneau, Crowson, and Atkinson 2026). The outcome for this example is dementia, but the approach can be applied to other time-to-event outcomes.
survival, dplyr, tidyr and zoo, which can take a minute. Chrome, Edge and Firefox are recommended.Figure 2 shows simulated data for five individuals, in “long” format. In the long format, each individual has many rows of information mapping to different time periods over the follow-up.
walkthrough_example_data_small.csv · 21 rows × 8 columnsid, baseline_age, sex, bmi, visit, visit_date, prob, dementia. The prob column is not used in this walkthrough.walkthrough_exposure_data_small.csv · 118 rows × 6 columnsdate (the first of each month, 2009-05-01 to 2019-02-01) and one column of monthly exposure values for each of the five individuals (1001, 1008, 1010, 1024, 1029). It is used in the last step of the walkthrough.baseline_date column is each person’s visit t1 date and is created in the setup step below.In the data in Figure 2, each individual appears in multiple rows, each corresponding to a single time point—the study visit where data were updated. To prepare these data for the Cox model, we need to transform the data so each row represents a time interval and that each individual has a line of data that ends at the time of an event for anyone else in the dataset if they were still at risk at the time of that event. In other words, each person will have a new line of data for each time interval where they contribute person-time (i.e., are still at risk for the event at the start of the interval). The end points of the time intervals will correspond to when (i) an individual had a change in their variable values or (ii) when anyone in the cohort had an event. For these data, transforming to a counting process format yields the following:
As an example, consider individual 1024. They had visits at 2010-12-01, 2012-11-01, 2014-11-01, and 2016-11-01 (rows 16 through 19 of the data in Figure 2). Since they were diagnosed at their third visit (2014-11-01), the fourth observation is not used in the PH regression model. The first interval in their data is 2010-12-01 to 2012-11-01. But since individual 1010 had a dementia diagnosis at 2012-05-01, the first interval for individual 1024 gets split into two parts: 2010-12-01 to 2012-05-01 and 2012-05-01 to 2012-11-01, which are shown in rows 12 and 13 of Figure 3.
The next section details each step of how to transform the data in this manner.
This step is not part of the printed guidance document. It loads the packages, reads the downloaded CSV, and creates the baseline_date column (each person’s first visit, t1), giving the long-format object data2_full that the remaining steps start from. On this page the CSV is already placed in R’s working directory; if you run the script locally, keep the CSV next to it.
The first step is to extract the baseline dates. This is the foundation data object on which the rest of the analytical dataset will be built.
Figure 4: Extracting the baseline dates for all individuals.
The next step to preparing the data is to identify all of the event times, which are the times of either dementia diagnosis or censoring.
Figures 5 and 6: Code for identifying the risk set times, and the resulting data frame of event times for each individual.
This generates a data frame like the one in Figure 6. Note that there is one row for each individual and the event_dementia column indicates that that individual had dementia. Those that did not have dementia were censored at their last visit and identified as event_censored.
Using tmerge(), the initial counting process dataset is created, by adding columns to the baseline date data frame.
Figure 7: Data frame showing the initial counting process dataset.
This step takes all time-varying and time-constant variables from the longitudinal dataset and adds them to the model dataset, by carrying them forward from one time point to the next. Note how each individual has a new row every time their covariate data changes. So far, this is only when they had a visit. But a similar structure will be used in the next step to add a row to capture exposure changes at each event time.
Figure 8: Data frame showing the counting process dataset after predictor variables have been added.
This step has two parts. First, we create a data frame that has a row for every combination of person and endpoint (i.e., all timepoints where there was a dementia diagnosis or censoring event).
Figure 9: Data frame with all combinations of ID and event time.
We then use that data frame to add these time points to the counting process dataset. This creates the dataset in Figure 10. In this code, the utility variable created by the tdc(event_date) code is what is used to split the dataset rows on event dates. However, once the split is done, we no longer need that variable; this is why utility is removed in the next line of code.
Figure 10: Counting process dataset that is ready for merging with exposure data.
From here, exposure data can be merged into the dataset. For example, for individual 1001, we would need to calculate seven exposure averages; one each for the periods ending 2012-05-01, 2013-01-01, 2014-04-01, 2014-11-01, 2015-02-01, 2017-03-01, and 2019-02-01. This includes not just the four follow-up visit times for this person (2013-01-01, 2015-02-01, 2017-03-01, and 2019-02-01), but also the three time points when someone else in the cohort had an event while this participant was under observation (2012-05-01, 2014-04-01, and 2014-11-01). If we used 1-year average exposures, this would mean calculating average exposure between 2011-05-02 and 2012-05-01, 2012-01-02 and 2013-01-01, 2013-04-02 and 2014-04-01, 2013-11-02 and 2014-11-01, 2014-02-02 and 2015-02-01, 2016-03-02 and 2017-03-01, and 2018-02-02 and 2019-02-01.
The exposure data are then merged with the counting process data. First, if the exposure data have not already been averaged to the chosen scale, construct those averages. Figure 11 shows an example of how this can be done for a 12-month An average over a window that moves forward one step at a time (here, the most recent 12 months).. The data (exposure_values) have columns of monthly values for each person that have previously been linked by spatial location. The rollmean() function is then used to construct the 12-month rolling mean for all times and individuals.
exposure_values is read from walkthrough_exposure_data_small.csv in the setup step: monthly values for the five individuals, May 2009 to February 2019. These are example values for illustration.Figure 11: Code for constructing 12-month rolling mean exposure averages.
Once the exposure averages are constructed, then they are merged with the model dataset. Note that the merge is based upon the end point for each time interval, so the exposure average corresponding to tstop is selected into the analysis dataset. The other rolling mean exposures are not used.
Figure 12: Code for merging exposure information into model dataset. Note that only the average exposures corresponding to the end of each time interval (tstop) are selected.
After merging in the exposure data, the PH regression model can now be fit.
The Gateway Exposome Coordinating Center is funded by the US National Institute on Aging, U24 AG088894. Feedback or questions: contact@gatewayexposome.org.
Current Version: 1.0 (November 2026)
Version History: 1.0 (November 2026): First version.