Package {lwqs}


Type: Package
Title: Lagged Weighted Quantile Sum Regression
Version: 0.5.1
Description: Wrapper functions for the implementation of lagged weighted quantile sum regression, as per 'Gennings et al' (2020) <doi:10.1016/j.envres.2020.109529>.
License: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]
Encoding: UTF-8
LazyData: true
Depends: R (≥ 3.5.0)
Imports: data.table, ggplot2, plyr, gridExtra, gWQS, gamm4, stats
Suggests: knitr, rmarkdown
VignetteBuilder: knitr
NeedsCompilation: no
Config/roxygen2/version: 8.1.1
Packaged: 2026-10-14 22:53:48 UTC; etm2132
Author: Paul Curtin [aut, cre], Stefano Renzetti [aut], Chris Gennings [aut]
Maintainer: Paul Curtin <paul.c.p.curtin@gmail.com>
Repository: CRAN
Date/Publication: 2026-10-16 09:00:02 UTC

Function to extract time-varying mixture (wqs) index from lWQS object.

Description

Function to extract time-varying mixture (wqs) index from lWQS object.

Usage

extract_mixture(lobj)

Arguments

lobj

An object returned from the lwqs function.

Value

Data frame containing the time index, wqs index estimated at each repeated measure, subject ID, and the outcome variable.

Examples

## Not run: 
# identify predictor variables used in mixture
mixvars=names(lwqs_data)[5:9]

# run model. Note for example run-time only a small number of bootstraps
# (b=5) is used; set b to be >50 in practice.
model=lwqs(data=lwqs_data,
           timevar="time",
           wqs_parms=list(formula=out ~ wqs,
              data = lwqs_data,
              mix_name=mixvars,
              b_constr = TRUE,
              b1_pos=FALSE,
              b = 5,
              q = 5,
              validation = 0,
              family = "gaussian",
              seed = 1),
              outcome="out",
              ID="ID")

# use extract_mixture to access time-varying wqs index
mixtime=extract_mixture(model)
## End(Not run)


Function to extract time-varying weights from lWQS object.

Description

Function to extract time-varying weights from lWQS object.

Usage

extract_weights(lobj)

Arguments

lobj

An object returned from the lwqs function.

Value

A (long-form) data frame containing the time index and corresponding variable weights estimated in an lWQS.

Examples

## Not run: 
# identify predictor variables used in mixture
mixvars=names(lwqs_data)[5:9]

# run model
model=lwqs(data=lwqs_data,
           timevar="time",
           wqs_parms=list(formula=out ~ wqs,
              data = lwqs_data,
              mix_name=mixvars,
              b_constr = TRUE,
              b1_pos=TRUE,
              b = 5,
              q = 5,
              validation = 0,
              family = "gaussian",
              seed = 1),
              outcome="out",
              ID="ID")

# use extract_weights to access time-varying predictor weights
timeweights=extract_weights(model)
## End(Not run)


Wrapper function for the implementation of lagged WQS.

Description

Wrapper function for the implementation of lagged WQS.

Usage

lwqs(
  data,
  timevar,
  wqs_parms,
  outcome,
  ID,
  rDLM_parms = list(formula = wqs ~ s(time, by = y, bs = "cr"), random = ~(1 | id)),
  ci_level = 0.95
)

Arguments

data

Data frame containing observations in long format.

timevar

Enquoted variable name identifying the repeated measure / time variable.

wqs_parms

A list containing parameters to be passed to the WQS algorithm. See gWQS package for details.

outcome

An enquoted variable name identifying the outcome measure.

ID

An enquoted variable name identifying the subject identifier.

rDLM_parms

(optional). A list containing parameters to be passed to the GAM algorithm. See gamm4 package for details. Parameters wqs, time, by, and id are created by the lwqs function and passed to the gamm4 function automatically.

ci_level

(optional). Confidence level for the time-varying effect band. Defaults to 0.95.

Value

The lwqs function returns a list containing final model output and time-specific model parameters.

parameters

This list contains several objects summarizing different stages of the lagged ensemble model. The first object, res, contains output from the gWQS algorithm applied to each discrete repeated measure in the overall model; see package gWQS for details. The second output, wqstime, provides the mixture index, identified as "wqs", estimated for each subject at each discrete time point. The third item, weightstime, provides the weights estimated for each predictor at each discrete time point.

plot

A combined plot (as a grob) summarizing the output of the lwqs algorithm.

Examples

## Not run:  # identify predictor variables used in mixture
mixvars=names(lwqs_data)[5:9]

# run model.
model=lwqs(data=lwqs_data,
           timevar="time",
           wqs_parms=list(formula=out ~ wqs,
              data = lwqs_data,
              mix_name=mixvars,
              b_constr = TRUE,
              b1_pos=TRUE,
              b = 5,
              q = 5,
              validation = 0,
              family = "gaussian",
              seed = 1),
              outcome="out",
              ID="ID")
## End(Not run)


Simulated dataset for accompanying vignette.

Description

Simulated dataset for accompanying vignette.

Usage

data(lwqs_data)

Value

A data frame containing simulated data to explore the lwqs algorithm. Variables included are as follows:

ID

Variable identifying each simulated subject. Data reflect 30 successive measures per subject.

Sex

A simulated binary covariate, either 1 or 0.

time

Variable identifying the successive timing of each repeated measure.

out

Simulated outcome on standardized scale.

pred1

First simulated time-varying predictor. This has a large positive association with "out" from times 11-20.

pred2

Second simulated time-varying predictor. This has a moderate positive association with "out" from times 11-20.

pred3

Third simulated time-varying predictor. This has a moderate negative association with "out" from times 1-10.

pred4

Fourth simulated time-varying predictor. This has a strong negative association with "out" from times 1-10.

pred5

Fifth simulated time-varying predictor. This has no significant association with "out".