Title: | Distributed Lag Interaction Model |
---|---|
Description: | Collection of functions for fitting and interpreting distributed lag interaction models (DLIM). A DLIM regresses a scalar outcome on repeated measures of exposure and allows for modification by a continuous variable. Includes a dlim() function for fitting, predict() function for inference, and plotting functions for visualization. Details on methodology are described in Demateis et al. (2024) <doi:10.1002/env.2843>. |
Authors: | Danielle Demateis [aut, cre] |
Maintainer: | Danielle Demateis <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.2.0 |
Built: | 2025-02-25 04:31:40 UTC |
Source: | https://github.com/ddemateis/dlim |
The package dlim contains functions to fit, perform inference and estimation on, and visualize a distributed lag interaction model (DLIM).
A distributed lag interaction model (DLIM) is an extension of a distributed lag model. A DLIM regresses an outcome onto repeated measures of an exposure and allows for associations to vary by a single continuous modifier. More details on methodology are provided in the reference listed below.
To fit a DLIM using this package, use the dlim
function, which calls the cross_basis
function to create the cross-basis and estimates regression coefficients using gam
from mgcv package.
The predict.dlim
S3 function provides point-wise or cumulative effect estimates and uncertainty measures.
The plot_DLF
and plot_cumulative
functions provide plots of the modified distributed lag functions and the cumulative effect estimate curve.
Additonal details on the package dlim are available in the vignette, available by typing:
vignette("dlimOverview")
The dlim package is available on the Comprehensive R Archive Network (CRAN). A development website is available on GitHub (github.com/ddemateis/dlim).
Please use citation("dlim")
to cite this package.
Danielle Demateis, Kayleigh Keller, and Ander Wilson
Maintainer: Danielle Demateis <[email protected]>
Demateis et al. (2024) <doi:10.1002/env.2843>, avaibable at (arxiv.org/abs/2401.02939).
Type 'vignette(dlimOverview)'
for a detailed description.
Creates cross-basis using natural splines for regression in DLIM
cross_basis( x, M, L = NULL, argmod = list(), arglag = list(), model_type = "standard" )
cross_basis( x, M, L = NULL, argmod = list(), arglag = list(), model_type = "standard" )
x |
a numeric time series vector of length n or matrix of lagged exposures (columns) for n individuals (rows) |
M |
vector of length n containing modifier values |
L |
a numeric vector of length 1 containing the number of lag terms. This is required if |
argmod |
a list: $fun is the spline function for the modifier ("ps" or "cr" to penalize), $arg is a list of arguments for the spline function (must be named by argument), $df is the degrees of freedom, $sp is optional smoothing parameter |
arglag |
a list: $fun is the spline function for the lag ("ps" or "cr" to penalize), $arg is a list of arguments for the spline function (must be named by argument), $df is the degrees of freedom, $sp is optional smoothing parameter |
model_type |
"linear" for a DLIM with linear interaction, "quadratic" for a DLIM with quadratic interaction, "standard" for a DLIM with splines |
This function returns a list of 5 or 6 elements:
cb |
cross-basis (matrix) |
B_lag |
lag basis (basis matrix) |
B_mod |
modifier basis (basis matrix) |
df_l |
lag degrees of freedom (numeric) |
df_m |
modifier degrees of freedom (numeric) |
L |
number of lags (numeric) |
Slist |
lag and modifier penalty matrices, if penalizing (list) |
Type vignette('dlimOverview')
for a detailed description.
Fit distributed lag interaction model
dlim( y, x, modifiers, z = NULL, df_m = NULL, df_l, penalize = TRUE, pen_fn = "ps", mod_args = NULL, lag_args = NULL, fit_fn = "gam", model_type = "standard", ID = NULL, ... )
dlim( y, x, modifiers, z = NULL, df_m = NULL, df_l, penalize = TRUE, pen_fn = "ps", mod_args = NULL, lag_args = NULL, fit_fn = "gam", model_type = "standard", ID = NULL, ... )
y |
vector of response values (class " |
x |
matrix of exposure history (columns) for individuals (rows) (class " |
modifiers |
vector of modifying values (class " |
z |
matrix of covariates, not including the modifier (class " |
df_m |
degrees of freedom for modifier basis. Cannot specify for linear modification (model_type = "linear") (class " |
df_l |
degrees of freedom for exposure time basis (class " |
penalize |
|
pen_fn |
if penalizing, can specify "ps" for penalized B-splines or "cr" for cubic regression splines with penalties on second derivatives |
mod_args |
a list of additional arguments for the spline function (must be named by argument) |
lag_args |
a list of additional arguments for the spline function (must be named by argument) |
fit_fn |
specify "gam" to use the |
model_type |
"linear" for a DLIM with linear interaction, "quadratic" for a DLIM with quadratic interaction, "standard" for a DLIM with splines (class " |
ID |
group identifier for random intercept, only supported for penalized models |
... |
Other arguments to pass to model fitting function |
This function returns a list that is an object of class "dlim
" with the following components
cb |
cross-basis (class " |
fit |
model object (class " |
modifiers |
modifying values (class " |
call |
model call |
Type vignette('dlimOverview')
for a detailed description.
library(dlim) data("ex_data") dlim_fit <- dlim(y = ex_data$y, x = ex_data$exposure, modifier = ex_data$modifier, z = ex_data$z, df_m = 10, df_l = 10, method = "REML") dlim_pred <- predict(dlim_fit, newdata = 0.5, type="CE")
library(dlim) data("ex_data") dlim_fit <- dlim(y = ex_data$y, x = ex_data$exposure, modifier = ex_data$modifier, z = ex_data$z, df_m = 10, df_l = 10, method = "REML") dlim_pred <- predict(dlim_fit, newdata = 0.5, type="CE")
Data set for examples
ex_data
ex_data
List of response, exposure, modifiers, covariates
Simulated
data(ex_data) # lazy load
data(ex_data) # lazy load
Data set of PM 2.5 exposure history for 1000 individuals over 37 weeks
exposure
exposure
A data frame of 1000 rows and 37 columns
Data source??
data(exposure) # lazy load
data(exposure) # lazy load
Compare models to test for interaction
model_comparison(fit, null = "DLM", x, B, conf.level = 0.95)
model_comparison(fit, null = "DLM", x, B, conf.level = 0.95)
fit |
dlim object (must be fit with REML) |
null |
"DLM", "linear" to specify the null model |
x |
exposure |
B |
number of bootstrap samples |
conf.level |
The confidence level (class " |
The function returns a decision to either reject or fail to reject the null model
Type vignette('dlimOverview')
for a detailed description.
Plot estimated distributed lag function values from a DLIM object, can also compare those of a DLM
plot_cumulative( new_modifiers, mod_fit, dlm_fit = NULL, mod_name = NULL, mod_trans = NULL, link_trans = NULL )
plot_cumulative( new_modifiers, mod_fit, dlm_fit = NULL, mod_name = NULL, mod_trans = NULL, link_trans = NULL )
new_modifiers |
a vector of new modifier values for prediction (class " |
mod_fit |
DLIM model object (class " |
dlm_fit |
a list containing a |
mod_name |
modifier name (character) |
mod_trans |
if modifiers are transformed, specify back transformation function (class " |
link_trans |
if family for |
This function returns a ggplot for cumulative effects, including for a DLM if specified
Type vignette('dlimOverview')
for a detailed description.
Plot estimated cumulative effects from a DLIM object, can also compare estimated cumulative effects between a DLM and DLIM
plot_DLF( new_modifiers, mod_fit, mod_name, dlm_fit = NULL, plot_by, time_pts = NULL, mod_trans = NULL, link_trans = NULL )
plot_DLF( new_modifiers, mod_fit, mod_name, dlm_fit = NULL, plot_by, time_pts = NULL, mod_trans = NULL, link_trans = NULL )
new_modifiers |
a vector of new modifier values for prediction (class " |
mod_fit |
DLIM model object (class " |
mod_name |
modifier name that follows variable name nomenclature (class " |
dlm_fit |
a list containing a |
plot_by |
choose to create plots for particular modifier values, "modifier", or particular time points, "time", (class " |
time_pts |
a set of time points if plotting by time (class " |
mod_trans |
if modifiers are transformed, specify back transformation function (class " |
link_trans |
if family for |
This function returns a ggplot for point-wise effects isolated by either time points or modifier, including a DLM if specified
Type vignette('dlimOverview')
for a detailed description.
Predicted values based on a dlim
object.
## S3 method for class 'dlim' predict( object, newdata = NULL, type = c("DLF", "CE", "response"), conf.level = 0.95, ... )
## S3 method for class 'dlim' predict( object, newdata = NULL, type = c("DLF", "CE", "response"), conf.level = 0.95, ... )
object |
an object of class " |
newdata |
a vector of new modifier values for prediction (class " |
type |
Type of prediction. "DLF" for the estimated distributed lag functions, "CE" for cumulative effects, "response" for fitted values, or any combination of these in a vector (class " |
conf.level |
The confidence level (class " |
... |
additional arguments affecting the predictions produced |
This function returns a list of 3 elements:
est_dlim |
cumulative and/or point-wise estimates, standard errors, and confidence intervals (class " |
cb |
cross-basis object (class " |
model |
model object (class " |
Type vignette('dlimOverview')
for a detailed description.
This function estimates cumulative and non-cumulative lag/modifier coefficients from a model in which the response is regressed on a cross-basis generated by the cross_basis()
function.
## S3 method for class 'sim_dlim' predict(object, newdata = NULL, type = c("DLF", "CE", "response"), ...)
## S3 method for class 'sim_dlim' predict(object, newdata = NULL, type = c("DLF", "CE", "response"), ...)
object |
an object of class " |
newdata |
vector of modifiers for inference (class " |
type |
Type of prediction. "response" for predicted responses, "DLF" for the estimated distributed lag functions, "CE" for cumulative effects (class " |
... |
additional arguments affecting the predictions produced |
This function returns a list of 4 or 7 elements:
est_dlim |
|
cb |
cross-bais from |
fit |
|
true_betas |
|
cb_dlm |
|
model_dlm |
|
est_dlm |
cumulative and/or point-wise estimates, standard errors, and confidence intervals for the DLM (class " |
Type vignette('dlimOverview')
for a detailed description.
prints information about an object of class dlim
## S3 method for class 'dlim' print(x, ...)
## S3 method for class 'dlim' print(x, ...)
x |
a |
... |
further arguments passed to or from other methods |
This function returns information about an object of class dlim
Type vignette('dlimOverview')
for a detailed description.
Simulate data to use with the dlim package. There are different effect modification scenarios to choose for simulation.
sim_data( x, L = NULL, modifiers, noise = 1, type = 2, SNR, ncovariates = 0, gamma = 1 )
sim_data( x, L = NULL, modifiers, noise = 1, type = 2, SNR, ncovariates = 0, gamma = 1 )
x |
a time series vector of length |
L |
a vector of length 1 containing the number of lag terms. This is required if |
modifiers |
vector of length |
noise |
a vector of length 1 containing the standard deviation for a normal distribution with mean 0 used to add noise to the simulated response values. Must proivde if |
type |
a vector containing the number 1, 2, 3, or 4 for simulation modification type: none, linear, non-linear shift, non-linear shift with linear scale (class " |
SNR |
The signal-to-noise ratio. If |
ncovariates |
number of covariates to add to the model, numeric vector of length 1. |
gamma |
True coefficient for the main effect of the modifier (class " |
This returns a list of 8 items:
x |
a lagged exposure matrix. If |
L |
a numeric vector of length 1 containing the number of lag terms (class " |
modifiers |
the |
y |
a numeric vector of length |
betas |
a matrix containing true coefficients for each lag/modifier combination, with each row representing a lag and each column a modifier (class " |
betas_cumul |
a numeric vector of length |
Z |
covariates (class " |
gammas |
true coefficients for the covariates (class " |
Type vignette('dlimOverview')
for a detailed description.
generate true distributed lag function values for a given type of simulation
sim_dlf(L, modifiers, type)
sim_dlf(L, modifiers, type)
L |
Number of lags minus 1 |
modifiers |
Vector of modifiers |
type |
Effect modification simulation type: 1 is no modification, 2 is linear scale modification, 3 is non-linear shift modification, 4 is types 2 and 3 combined |
This function returns the true distributed lag function values (class "numeric
")
Type vignette('dlimOverview')
for a detailed description.
Fit DLIM for simulation
sim_dlim( data, df_m, df_l, penalize = TRUE, pen_fn = "ps", mod_args = NULL, lag_args = NULL, fit_dlm = FALSE, model_type = "standard", ... )
sim_dlim( data, df_m, df_l, penalize = TRUE, pen_fn = "ps", mod_args = NULL, lag_args = NULL, fit_dlm = FALSE, model_type = "standard", ... )
data |
output from |
df_m |
degrees of freedom for modifiers |
df_l |
degrees of freedom for lags |
penalize |
True to penalize model |
pen_fn |
if penalizing, can specify "ps" for penalized B-splines or "cr" for cubic regression splines with penalties on second derivatives |
mod_args |
a list of additional arguments for the spline function (must be named by argument) |
lag_args |
a list of additional arguments for the spline function (must be named by argument) |
fit_dlm |
True to additionally fit dlm for comparison |
model_type |
"linear" for a DLIM with linear interaction, "quadratic" for a DLIM with quadratic interaction, "standard" for a DLIM with splines |
... |
arguments to pass to model fitting function |
This function returns an object of class "sim_dlim
"
cb |
DLIM cross-basis (class " |
fit |
DLIM model fit (class " |
cb_dlm |
DLM cross-basis (class " |
model_dlm |
DLM model fit (class " |
true_betas |
true linear effect of the exposure on the response for each individual and time point (class " |
modifiers |
|
data |
|
Type vignette('dlimOverview')
for a detailed description.
prints summary of object of class dlim
## S3 method for class 'dlim' summary(object, ...)
## S3 method for class 'dlim' summary(object, ...)
object |
a |
... |
additional arguments affecting the summary produced |
This function returns a summary for an object of class dlim
Type vignette('dlimOverview')
for a detailed description.