---
title: "Basic topics for the qgcomp package: g-computation on exposure quantiles"
author: "Alexander Keil"
date: "`r Sys.Date()`"
#output: rmarkdown::pdf_document 
output: rmarkdown::html_vignette  
vignette: >
  %\VignetteIndexEntry{The qgcomp package: basic usage}
  %\VignetteEngine{knitr::knitr}
  \usepackage[utf8]{inputenc}
---

```{r invisibles, echo=FALSE, results='markup', message=FALSE}
library("knitr")
#library("gWQS")
```

# Table of Contents
1. [Introduction](#intro)
2. [How to use the `qgcomp` package](#howto)
3. [Example 1: linear model](#ex-linear)
4. [Example 2: conditional odds ratio, marginal odds ratio in a logistic model](#ex-logistic)
5. [Example 3: adjusting for covariates, plotting estimates](#ex-logistic)

6. [Example 4: non-linearity (and non-homogeneity)](#ex-nonlin)
7. [Example 5: comparing model fits and further exploring non-linearity](#ex-nonlin2)
8. [Example 6: miscellaneous other ways to allow non-linearity](#ex-nonlin3)
9. [FAQ](#faq)
10. [Acknowledgments](#ack)


## Introduction<a name="intro"></a>
`qgcomp` is a package to implement g-computation for analyzing the effects of exposure
mixtures. Quantile g-computation yields estimates of the effect of increasing
all exposures by one quantile, simultaneously. This, it estimates a "mixture
effect" useful in the study of exposure mixtures such as air pollution, diet,
and water contamination.

Using terminology from methods developed for causal effect estimation, quantile 
g-computation estimates the parameters of a marginal structural model that 
characterizes the change in the expected potential outcome given a joint intervention
on all exposures, possibly conditional on confounders. Under the assumptions of
exchangeability, causal consistency, positivity, no interference, and correct
model specification, this model yields a causal effect for an intervention
on the mixture as a whole. While these assumptions may not be met exactly, they
provide a useful road map for how to interpret the results of a qgcomp fit, and
where efforts should be spent in terms of ensuring accurate model specification
and selection of exposures that are sufficient to control co-pollutant confounding.

### The model
Say we have an outcome $Y$, some exposures $\mathbb{X}$ and possibly some other covariates (e.g. potential confounders) denoted by $\mathbb{Z}$.

The basic model of quantile g-computation is a joint marginal structural model given by

$$
\mathbb{E}(Y^{\mathbf{X}_q} | \mathbf{Z,\psi,\eta}) = g(\psi_0 + \psi_1 S_q +  \mathbf{\eta Z})
$$

where $g(\cdot)$ is a link function in a generalized linear model (e.g. the inverse logit function in the case of a logistic model for the probability that $Y=1$), $\psi_0$ is the model intercept, $\mathbf{\eta}$ is a set of model coefficients for the covariates and $S_q$ is an "index" that represents a joint value of exposures. Quantile g-computation (by default) transforms all exposures $\mathbf{X}$ into $\mathbf{X}_q$, which are "scores" taking on discrete values 0,1,2,etc. representing a categorical "bin" of exposure. By default, there are four bins with evenly spaced quantile cutpoints for each exposure, so ${X}_q=0$ means that $X$ was below the observed 25th percentile for that exposure. The index $S_q$ represents all exposures being set to the same value (again, by default, discrete values 0,1,2,3). Thus, *the parameter $\psi_1$ quantifies the expected change in the outcome, given a one quantile increase in all exposures simultaneously,* possibly adjusted for $\mathbf{Z}$. 

There are nuances to this particular model form that are available in the `qgcomp` package which will be explored below. There exists one special case of quantile g-computation that leads to fast fitting: linear/additive exposure effects. Here we simulate "pre-quantized" data where the exposures $X_1, X_2, X_3$ can only take on values of 0,1,2,3 in equal proportions. The model underlying the outcomes is given by the linear regression:

$$
\mathbb{E}(Y | \mathbf{X,\beta}) = \beta_0 + \beta_1 X_1 + \beta_2 X_2  + \beta_3 X_3 
$$

with the true values of $\beta_0=0, \beta_1 =0.25, \beta_2 =-0.1, \beta_3=0.05$, and $X_1$ is strongly positively correlated with $X_2$ ($\rho=0.95$) and negatively correlated with $X_3$ ($\rho=-0.3$). In this simple setting, the parameter $\psi_1$ will equal the sum of the $\beta$ coefficients (0.2). Here we see that qgcomp estimates a value very close to 0.2 (as we increase sample size, the estimated value will be expected to become increasingly close to 0.2).
```{r first step, echo=TRUE, results='markup', message=FALSE}
library("qgcomp")
set.seed(543210)
qdat = simdata_quantized(n=5000, outcomtype="continuous", cor=c(.95, -0.3), b0=0, coef=c(0.25, -0.1, 0.05), q=4)
head(qdat)
cor(qdat[,c("x1", "x2", "x3")])
qgcomp(y~x1+x2+x3, expnms=c("x1", "x2", "x3"), data=qdat)
```


## How to use the `qgcomp` package<a name="howto"></a>
Here we use a running example from the `metals` dataset (part of the  
`qgcomp` package) to demonstrate some features of the package and method. 

Namely, the examples below demonstrate use of the package for:
1. Fast estimation of exposure effects under a linear model for quantized exposures for continuous (normal) outcomes
2. Estimating conditional and marginal odds/risk ratios of a mixture effect for binary outcomes
3. Adjusting for non-exposure covariates when estimating effects of the mixture
4. Allowing non-linear and non-homogeneous effects of individual exposures and the mixture as a whole by including product terms
5. Using qgcomp to fit a time-to-event model to estimate conditional and marginal hazard ratios for the exposure mixture

For analogous approaches to estimating exposure mixture effects, illustrative examples can be seen in the `gQWS` package help files, which implements
weighted quantile sum (WQS) regression, and at https://jenfb.github.io/bkmr/overview.html, which describes Bayesian kernel machine regression.

The `metals` dataset from the package `qgcomp`, comprises a set of simulated well water exposures and two health outcomes (one continuous, one binary/time-to-event). The exposures are transformed to have mean = 0.0, standard deviation = 1.0. The data are used throughout to demonstrate usage and features of the `qgcomp` package.


```{r metals data, echo=TRUE, results='markup', message=FALSE}
library("ggplot2")
data("metals", package="qgcomp")
head(metals)
```


### Example 1: linear model<a name="ex-linear"></a>
```{r linear model and runtime, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
# we save the names of the mixture variables in the variable "Xnm"
Xnm <- c(
    'arsenic','barium','cadmium','calcium','chromium','copper',
    'iron','lead','magnesium','manganese','mercury','selenium','silver',
    'sodium','zinc'
)
covars = c('nitrate','nitrite','sulfate','ph', 'total_alkalinity','total_hardness')



# Example 1: linear model
# Run the model and save the results "qc.fit"
system.time(qc.fit <- qgcomp.glm.noboot(y~.,dat=metals[,c(Xnm, 'y')], family=gaussian()))
#   user  system elapsed 
#  0.011   0.002   0.018 

# contrasting other methods with computational speed
# WQS regression (v3.0.1 of gWQS package)
#system.time(wqs.fit <- gWQS::gwqs(y~wqs,mix_name=Xnm, data=metals[,c(Xnm, 'y')], family="gaussian"))
#   user  system elapsed 
# 35.775   0.124  36.114 

# Bayesian kernel machine regression (note that the number of iterations here would 
#  need to be >5,000, at minimum, so this underestimates the run time by a factor
#  of 50+
#system.time(bkmr.fit <- kmbayes(y=metals$y, Z=metals[,Xnm], family="gaussian", iter=100))
#   user  system elapsed 
# 81.644   4.194  86.520 
```

First note that qgcomp can be very fast relative to competing methods (with their example times given from single runs from a laptop). 

One advantage of quantile g-computation over other methods that estimate 
"mixture effects" (the effect of changing all exposures at once), is that it 
is very computationally efficient. Contrasting methods such as WQS (`gWQS` 
package) and Bayesian Kernel Machine regression (`bkmr` package), 
quantile g-computation can provide results many orders of magnitude faster.
For example, the example above ran 3000X faster for quantile g-computation
versus WQS regression, and we estimate the speedup would be several
hundred thousand times versus Bayesian kernel machine regression. 

The speed relies on an efficient method to fit qgcomp when exposures are added additively to the model. When exposures are added using non-linear terms or non-additive terms (see below for examples), then qgcomp will be slower but often still faster than competetive approaches.

Quantile g-computation yields fixed weights in the estimation procedure, similar
to WQS regression. However, note that the weights from `qgcomp.glm.noboot` 
can be negative or positive. When all effects are linear and in the same 
direction ("directional homogeneity"), quantile g-computation is equivalent to 
weighted quantile sum regression in large samples.

The overall mixture effect from quantile g-computation (psi1) is interpreted as 
the effect on the outcome of increasing every exposure by one quantile, possibly
conditional on covariates. Given the overall exposure effect, the weights are
considered fixed and so do not have confidence intervals or p-values.

```{r linear model and runtimeb, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
# View results: scaled coefficients/weights and statistical inference about
# mixture effect
qc.fit
```

Now let's take a brief look under the hood. `qgcomp` works in steps. First, the exposure variables are "quantized" or turned into score variables based on the total number of quantiles from the parameter `q`. You can access these via the `qx` object from the `qgcomp` fit object.
```{r linear model and runtime c, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
# quantized data
head(qc.fit$qx)
```

You can re-fit a linear model using these quantized exposures. This is the "underlying model" of a qgcomp fit.
```{r linear model and runtime d, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
# regression with quantized data
qc.fit$qx$y = qc.fit$fit$data$y # first bring outcome back into the quantized data
newfit <- lm(y ~ arsenic_q + barium_q + cadmium_q + calcium_q + chromium_q + copper_q + 
    iron_q + lead_q + magnesium_q + manganese_q + mercury_q + selenium_q + 
    silver_q + sodium_q + zinc_q, data=qc.fit$qx)
newfit
```
Here you can see that, *for a GLM in which all quantized exposures enter linearly and additively into the underlying model*, the overall effect from `qgcomp` is simply the sum of the adjusted coefficients from the underlying model. 
```{r linear model and runtime e, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
sum(newfit$coefficients[-1]) # sum of all coefficients excluding intercept and confounders, if any
coef(qc.fit) # overall effect and intercept from qgcomp fit
```
This equality is why we can fit qgcomp so efficiently under such a model. This is a specific case, and `qgcomp` also allows deviations from linear/additive approaches via Monte-Carlo (here generally referred to as bootstrapping methods) and estimating-equation-based methods, which require a 2-stage approach. `qgcomp` can allow for non-linearity and non-additivity in the underlying model, as well as non-linearity in the overall model. These extensions are described in some of the following examples.

### Example 2: conditional odds ratio, marginal odds ratio in a logistic model<a name="ex-logistic"></a>

This example introduces the use of a binary outcome in `qgcomp` via the 
`qgcomp.glm.noboot` function, which yields a conditional odds ratio or the
`qgcomp.glm.boot`, which yields a marginal odds ratio or risk/prevalence ratio. These
will not equal each other when there are non-exposure covariates (e.g. 
confounders) included in the model because the odds ratio is not collapsible (both
are still valid). Marginal parameters will yield estimates of the population
average exposure effect, which is often of more interest due to better 
interpretability over conditional odds ratios. Further, odds ratios are not
generally of interest when risk ratios can be validly estimated, so `qgcomp.glm.boot`
will estimate the risk ratio by default for binary data (set rr=FALSE to 
allow estimation of ORs when using `qgcomp.glm.boot`).

```{r logistic qgcomp, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}

qc.fit2 <- qgcomp.glm.noboot(disease_state~., expnms=Xnm, 
          data = metals[,c(Xnm, 'disease_state')], family=binomial(), 
          q=4)
qcboot.fit2 <- qgcomp.glm.boot(disease_state~., expnms=Xnm, 
          data = metals[,c(Xnm, 'disease_state')], family=binomial(), 
          q=4, B=10,# B should be 200-500+ in practice
          seed=125, rr=FALSE)
qcboot.fit2b <- qgcomp.glm.boot(disease_state~., expnms=Xnm, 
          data = metals[,c(Xnm, 'disease_state')], family=binomial(), 
          q=4, B=10,# B should be 200-500+ in practice
          seed=125, rr=TRUE)
```

Compare a qgcomp.glm.noboot fit:
```{r logistic qgcompb, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
qc.fit2
```

with a qgcomp.glm.boot fit:
```{r logistic qgcompc, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
qcboot.fit2
```

with a qgcomp.glm.boot fit, where the risk/prevalence ratio is estimated,  rather than the odds ratio:
```{r logistic qgcompd, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
qcboot.fit2b
```

### Example 3: adjusting for covariates, plotting estimates

In the following code we run a maternal age-adjusted linear model with 
`qgcomp` (`family = "gaussian"`). Further, we plot both the weights, as well as the mixture slope
which yields overall model confidence bounds, representing the bounds that, for each value of the
joint exposure are expected to contain the true regression line over 95% of trials (so-called 95% 'pointwise' bounds for the regression line). The pointwise comparison bounds, denoted by error bars on the plot, represent comparisons of the expected difference in outcomes at each quantile, with reference 
to a specific quantile (which can be specified by the user, as below). These pointwise bounds are similar to the bounds
created in the bkmr package when plotting the overall effect of all exposures. The pointwise bounds can be obtained via the pointwisebound.boot function. To avoid confusion between "pointwise regression" and "pointwise comparison" bounds, the pointwise regression bounds are denoted as the "model confidence band" in the plots, since they yield estimates of the same type of bounds as the `predict` function in R when applied to linear model fits.

Note that the underlying regression model is on the exposure quantile 'scores', which take on integer values 0, 1, ..., q-1. For plotting purposes (when plotting regression line results from qgcomp.glm.boot), 
the quantile score is translated into a quantile (range = [0-1]). This is not a perfect correspondence, 
because the quantile g-computation model treats the  quantile score as a continuous variable, but the each quantile category spans a range of quantiles.
For visualization, we fix the ends of the plot at the mid-points of the first and last quantile cut-point,
so the range of the plot will change slightly if 'q' is changed.

```{r adj4cov-a, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}

qc.fit3 <- qgcomp.glm.noboot(y ~ mage35 + arsenic + barium + cadmium + calcium + chloride + 
                           chromium + copper + iron + lead + magnesium + manganese + 
                           mercury + selenium + silver + sodium + zinc,
                         expnms=Xnm,
                         metals, family=gaussian(), q=4)
qc.fit3

plot(qc.fit3)

```

From the first plot we see weights from `qgcomp.glm.noboot` function, which include both
positive and negative effect directions. When the weights are all on a single side of the null,
these plots are easy to in interpret since the weight corresponds to the proportion of the
overall effect from each exposure. WQS uses a constraint in the model to force
all of the weights to be in the same direction - unfortunately such constraints
lead to biased effect estimates. The `qgcomp` package takes a different approach
and allows that "weights" might go in either direction, indicating that some exposures
may beneficial, and some harmful, or there may be sampling variation due to using
small or moderate sample sizes (or, more often, systematic bias such as unmeasured
confounding). The "weights" in `qgcomp` correspond to the proportion of the overall effect
when all of the exposures have effects in the same direction, but otherwise they
correspond to the proportion of the effect *in a particular direction*, which
may be small (or large) compared to the overall "mixture" effect. NOTE: the left 
and right sides of the  plot should not be compared with each other because the 
length of the bars corresponds to the effect size only relative to other effects 
in the same direction. The darkness of the bars corresponds to the overall effect 
size - in this case the bars on the right (positive) side of the plot are darker 
because the overall "mixture" effect is positive. Thus, the shading allows one
to make informal comparisons across the left and right sides: a large, darkly
shaded bar indicates a larger independent effect than a large, lightly shaded bar.

```{r adj4cov-b, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
qcboot.fit3 <- qgcomp.glm.boot(y ~ mage35 + arsenic + barium + cadmium + calcium + chloride + 
                           chromium + copper + iron + lead + magnesium + manganese + 
                           mercury + selenium + silver + sodium + zinc,
                         expnms=Xnm,
                         metals, family=gaussian(), q=4, B=10,# B should be 200-500+ in practice
                         seed=125)
qcboot.fit3
qcee.fit3 <- qgcomp.glm.ee(y ~ mage35 + arsenic + barium + cadmium + calcium + chloride + 
                           chromium + copper + iron + lead + magnesium + manganese + 
                           mercury + selenium + silver + sodium + zinc,
                         expnms=Xnm,
                         metals, family=gaussian(), q=4)
qcee.fit3
```

We can change the referent category for pointwise comparisons via the `pointwiseref` parameter:
```{r adj4cov-c, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
qgcomp:::modelbound.ee(qcee.fit3)
plot(qcee.fit3, pointwiseref = 3, flexfit = FALSE, modelbound=TRUE)
plot(qcboot.fit3, pointwiseref = 3, flexfit = FALSE)
```


Using `qgcomp.glm.boot` also allows us to assess
linearity of the total exposure effect (the second plot). Similar output is available
for WQS (`gWQS` package), though WQS results will generally be less interpretable
when exposure effects are non-linear (see below how to do this with `qgcomp.glm.boot` and `qgcomp.glm.ee`). 

The plot for the `qcboot.fit3` object (using g-computation with bootstrap variance) 
gives predictions at the joint intervention levels of exposure. It also displays
a smoothed (graphical) fit. 

Note that the uncertainty intervals given in the plot are directly accessible via the `pointwisebound` (pointwise comparison confidence intervals) and `modelbound` functions (confidence interval for the regression line):

```{r adj4cov-d, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
pointwisebound.boot(qcboot.fit3, pointwiseref=3)
qgcomp:::modelbound.boot(qcboot.fit3)
```

Because qgcomp estimates a joint effect of multiple exposures, we cannot, in general, assess model fit by overlaying predictions from the plots above with the data. Hence, it is useful to explore non-linearity by fitting models that
allow for non-linear effects, as in the next example.


### Example 4: non-linearity (and non-homogeneity)<a name="ex-nonlin"></a>

`qgcomp` (specifically `qgcomp.*.boot` and `qgcomp.*.ee` methods) addresses non-linearity in a way similar to standard parametric regression models, which lends itself to being able to leverage R language features for n-lin parametric models (or, more precisely, parametric models that deviate from a purely additive, linear function on the link function basis via the use of basis function representation of non-linear functions). 
Here is an example where we use a feature of the R language for fitting models
with interaction terms. We use `y~. + .^2` as the model formula, which fits a model
that allows for quadratic term for every predictor in the model. 



#### Aside: some details on qgcomp methods for non-linearity

Note that both `qgcomp.*.boot` (bootstrap) and `qgcomp.*.ee` (estimating equations) use standard methods for g-computation, whereas the `qgcomp.*.noboot` methods use a fast algorithm that works under the assumption of linearity and additivity of exposures (as described in the original paper on quantile-based g-computation). The "standard" method of g-computation with time-fixed exposures involves first fitting conditional models for the outcome, making predictions from those models under set exposure values, and then summarizing the predicted outcome distribution, possibly by fitting a second (marginal structural) model. `qgcomp.*.boot` follows this three-step process, while `qgcomp.*.ee` leverages estimating equations (sometimes: M-estimation) to estimate the parameters of the conditional and marginal structural model simultaneously. `qgcomp.*.ee` uses a sandwich variance estimator, which is similar to GEE (generalized estimating equation) approaches, and thus, when used correctly, can yield inference for longitudinal data in the same way that GEE does. The bootstrapping approach can also do this, but it takes longer. The extension to longitudinal data is representative of the broader concept that `qgcomp.*.boot` and `qgcomp.*.ee` can be used in a broader number of settings than `qgcomp.*.noboot` algorithms, but if one assumes linearity and additivity with no clustering of observations, and conditional parameters are of interest, then they are just a slower way to get equivalent results to `qgcomp.*.noboot`.


Below, we demonstrate a non-linear conditional fit (with a linear MSM) using the bootstrap approach. Similar approaches could be used to include interaction terms between exposures, as well as between exposures and covariates. Note this example is purposefully done incorrectly, as explained below.

```{r n-lin-non-hom-intro, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}

qcboot.fit4 <- qgcomp(y~. + .^2,
                         expnms=Xnm,
                         metals[,c(Xnm, 'y')], family=gaussian(), q=4, B=10, seed=125)
plot(qcboot.fit4)
```

Note that allowing for a non-linear effect of all exposures induces an apparent 
non-linear trend in the overall exposure effect. The smoothed regression line is 
still well within the confidence bands of the marginal linear model 
(by default, the overall effect of joint exposure is assumed linear, 
though this assumption can be relaxed via the 'degree' parameter in qgcomp.glm.boot or qgcomp.glm.ee, 
as follows:

```{r ovrl-n-lin, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}

qcboot.fit5 <- qgcomp(y~. + .^2,
                         expnms=Xnm,
                         metals[,c(Xnm, 'y')], family=gaussian(), q=4, degree=2, 
                      B=10, rr=FALSE, seed=125)
plot(qcboot.fit5)
qcee.fit5b <- qgcomp.glm.ee(y~. + .^2,
                         expnms=Xnm,
                         metals[,c(Xnm, 'y')], family=gaussian(), q=4, degree=2, 
                         rr=FALSE)
plot(qcee.fit5b)
```

Note that some features are not availble to ` qgcomp.* .ee` methods, which use estimating equations, rather than maximum likelihood methods. Briefly, these allow assessment of uncertainty under n-lin (and other) scenarios where the ` qgcomp.* .noboot` functions cannot, since they rely on the additivity and linearity assumptions to achieve speed. The ` qgcomp.* .ee` methods will generally be faster than a bootstrapped version, but they are not used extensively here because they are the newest additions to the qgcomp package, and the bootstrapped versions can be made fast (but not accurate) by reducing the number of bootstraps. Where available, the ` qgcomp.* .ee` will be preferred to the ` qgcomp.* .boot` versions for more stable and faster analyses when bootstrapping would otherwise be necessary.

Once again, we can access numerical estimates of uncertainty (answers differ between the `qgcomp.*.boot` and `qgcomp.*.ee` fits due to the small number of bootstrap samples):
```{r ovrl-n-linb, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
modelbound.boot(qcboot.fit5)
pointwisebound.boot(qcboot.fit5)
pointwisebound.noboot(qcee.fit5b)
```

Ideally, the smooth fit will look very similar to the model prediction regression 
line.

#### Interpretation of model parameters

As the output below shows, setting "degree=2" yields a second parameter in the model fit ($\psi_2$). The output of qgcomp now corresponds to estimates of the marginal structural model given by 
$$
\mathbb{E}\left(Y^{\mathbf{X}_q}\right) = g(\psi_0 + \psi_1 S_q + \psi_2 S_q^2)
$$

```{r ovrl-n-lin-psi interp, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
qcboot.fit5
```


so that $\psi_2$ can be interpreted similar to quadratic terms that might appear in a generalized linear model. $\psi_2$ estimates the change in the outcome for an additional unit of squared joint exposure, over-and-above the linear effect given by $\psi_1$. Informally, this is a way of assessing specific types of non-linearity in the joint exposure-response curves, and there are many other (slightly incorrect but intuitively useful) ways of interpreting parameters for squared terms in regressions (beyond the scope of this document). Intuition from generalized linear models (i.e. regarding interpretation of coefficients) applies directly to the models fit by quantile g-computation.


### Example 5: comparing model fits and further exploring non-linearity<a name="ex-nonlin2"></a>
Exploring a non-linear fit in settings with multiple exposures is challenging. One way to explore non-linearity, as demonstrated above, is to to include all 2-way interaction terms (including quadratic terms, or "self-interactions"). Sometimes this approach is not desired, either because the number of terms in the model can become very large, or because some sort of model selection procedure is required, which risks inducing over-fit (biased estimates and standard errors that are too small). Short of having a set of a priori non-linear terms to include, we find it best to take a default approach (e.g. taking all second order terms) that doesn't rely on statistical significance, or to simply be honest that the search for a non-linear model is exploratory and shouldn't be relied upon for robust inference. Methods such as kernel machine regression may be good alternatives, or supplementary approaches to exploring non-linearity.

NOTE: qgcomp necessarily fits a regression model with exposures that have a small number of possible values, based on the quantile chosen. By package default, this is `q=4`, but it is difficult to fully examine non-linear fits using only four points, so we recommend exploring larger values of `q`, which will change effect estimates (i.e. the model coefficient implies a smaller change in exposures, so the expected change in the outcome will also decrease).

Here, we examine a one strategy for default and exploratory approaches to mixtures that can be implemented in qgcomp using a smaller subset of exposures (iron, lead, cadmium), which we choose via the correlation matrix. High correlations between exposures may result from a common source, so small subsets of the mixture may be useful for examining hypotheses that relate to interventions on a common environmental source or set of behaviors. We can still adjust for the measured exposures, even though only 3 our exposures of interest are considered as the mixture of interest. This next example will require a new R package to help in exploring non-linearity: `splines`. Note that `qgcomp.glm.boot` must be used in order to produce the graphics below, as `qgcomp.glm.noboot` does not calculate the necessary quantities.

#### Graphical approach to explore non-linearity in a correlated subset of exposures using splines
```{r graf-n-lin-1, results='markup', fig.show='hold', fig.height=3, fig.width=7.5, cache=FALSE}
library(splines)
# find all correlations > 0.6 (this is an arbitrary choice)
cormat = cor(metals[,Xnm])
idx = which(cormat>0.6 & cormat <1.0, arr.ind = TRUE)
newXnm = unique(rownames(idx)) # iron, lead, and cadmium


qc.fit6lin <- qgcomp.glm.boot(y ~ iron + lead + cadmium + 
                         mage35 + arsenic + magnesium + manganese + mercury + 
                         selenium + silver + sodium + zinc,
                         expnms=newXnm,
                         metals, family=gaussian(), q=8, B=10)

qc.fit6nonlin <- qgcomp.glm.boot(y ~ bs(iron) + bs(cadmium) + bs(lead) +
                         mage35 + arsenic + magnesium + manganese + mercury + 
                         selenium + silver + sodium + zinc,
                         expnms=newXnm,
                         metals, family=gaussian(), q=8, B=10, degree=2)

qc.fit6nonhom <- qgcomp.glm.boot(y ~ bs(iron)*bs(lead) + bs(iron)*bs(cadmium) + bs(lead)*bs(cadmium) +
                         mage35 + arsenic + magnesium + manganese + mercury + 
                         selenium + silver + sodium + zinc,
                         expnms=newXnm,
                         metals, family=gaussian(), q=8, B=10, degree=3)
```


It helps to place the plots on a common y-axis, which is easy due to dependence of the qgcomp plotting functions on ggplot. Here's the linear fit :
```{r graf-n-lin-1b, results='markup', fig.show='hold', fig.height=3, fig.width=7.5, cache=FALSE}
pl_fit6lin <- plot(qc.fit6lin, suppressprint = TRUE, pointwiseref = 4)
pl_fit6lin + coord_cartesian(ylim=c(-0.75, .75)) + 
  ggtitle("Linear fit: mixture of iron, lead, and cadmium")
```

Here's the non-linear fit :
```{r graf-n-lin-2, results='markup', fig.show='hold', fig.height=3, fig.width=7.5, cache=FALSE}
pl_fit6nonlin <- plot(qc.fit6nonlin, suppressprint = TRUE, pointwiseref = 4)
pl_fit6nonlin + coord_cartesian(ylim=c(-0.75, .75)) + 
  ggtitle("Non-linear fit: mixture of iron, lead, and cadmium")
```

And here's the non-linear fit with statistical interaction between exposures (recalling that this will lead to non-linearity in the overall effect):
```{r graf-n-lin 3, results='markup', fig.show='hold', fig.height=3, fig.width=7.5, cache=FALSE}
pl_fit6nonhom <- plot(qc.fit6nonhom, suppressprint = TRUE, pointwiseref = 4)
pl_fit6nonhom + coord_cartesian(ylim=c(-0.75, .75)) + 
  ggtitle("Non-linear, non-homogeneous fit: mixture of iron, lead, and cadmium")
```

#### Caution about graphical approaches
The underlying conditional model fit can be made extremely flexible, and the graphical representation of this (via the 
smooth conditional fit) can look extremely flexible. Simply matching the overall (MSM) fit to this line is not
a viable strategy for identifying parsimonious models because that would ignore potential for overfit. Thus,
caution should be used when judging the accuracy of a fit when comparing the "smooth conditional fit" to the 
"MSM fit." 
```{r grafwarn, results='markup', fig.show='hold', fig.height=3, fig.width=7.5, cache=FALSE}
qc.overfit <- qgcomp.glm.boot(y ~ bs(iron) + bs(cadmium) + bs(lead) +
                         mage35 + bs(arsenic) + bs(magnesium) + bs(manganese) + bs(mercury) + 
                         bs(selenium) + bs(silver) + bs(sodium) + bs(zinc),
                         expnms=Xnm,
                         metals, family=gaussian(), q=8, B=10, degree=1)
qc.overfit
plot(qc.overfit, pointwiseref = 5)
```

Here, there is little statistical evidence for even a linear trend, which makes the 
smoothed conditional fit appear to be overfit. The smooth conditional fit can be turned off, as below.
```{r grafwarn-2, results='markup', fig.show='hold', fig.height=3, fig.width=7.5, cache=FALSE}
plot(qc.overfit, flexfit = FALSE, pointwiseref = 5)
```

### Example 6: miscellaneous other ways to allow non-linearity<a name="ex-nonlin3"></a>
Note that these are included as examples of *how* to include non-linearities, and are not intended as 
a demonstration of appropriate model selection. In fact, qc.fit7b is generally a bad idea in small
to moderate sample sizes due to large numbers of parameters. 

#### using indicator terms for each quantile
```{r n-lin-exs, results='markup', fig.show='hold', fig.height=3, fig.width=7.5, cache=FALSE}
qc.fit7a <- qgcomp.glm.boot(y ~ factor(iron) + lead + cadmium + 
                         mage35 + arsenic + magnesium + manganese + mercury + 
                         selenium + silver + sodium + zinc,
                         expnms=newXnm,
                         metals, family=gaussian(), q=8, B=20, deg=2)
# underlying fit
summary(qc.fit7a$fit)$coefficients
plot(qc.fit7a)
```

#### interactions between indicator terms
```{r n-lin-exs-2, results='markup', fig.show='hold', fig.height=3, fig.width=7.5, cache=FALSE}
qc.fit7b <- qgcomp.glm.boot(y ~ factor(iron)*factor(lead) + cadmium + 
                         mage35 + arsenic + magnesium + manganese + mercury + 
                         selenium + silver + sodium + zinc,
                         expnms=newXnm,
                         metals, family=gaussian(), q=8, B=10, deg=3)
# underlying fit
#summary(qc.fit7b$fit)$coefficients
plot(qc.fit7b)
```

#### breaks at specific quantiles (these breaks act on the quantized basis)
```{r n-lin-exs-3, results='markup', fig.show='hold', fig.height=3, fig.width=7.5, cache=FALSE}
qc.fit7c <- qgcomp.glm.boot(y ~ I(iron>4)*I(lead>4) + cadmium + 
                         mage35 + arsenic + magnesium + manganese + mercury + 
                         selenium + silver + sodium + zinc,
                         expnms=newXnm,
                         metals, family=gaussian(), q=8, B=10, deg=2)
# underlying fit
summary(qc.fit7c$fit)$coefficients
plot(qc.fit7c)
```

Note one restriction on exploring non-linearity: while we can use flexible functions such as splines for individual exposures, the overall fit is limited via the `degree` parameter to polynomial functions (here a quadratic polynomial fits the non-linear model well, and a cubic polynomial fits the non-linear/non-homogeneous model well - though this is an informal argument and does not account for the wide confidence intervals). We note here that only 10 bootstrap iterations are used to calculate confidence intervals (to increase computational speed for the example), which is far too low.

#### Statistical approach explore non-linearity in a correlated subset of exposures using splines

The graphical approaches don't give a clear picture of which model might be preferred, but we can compare the model fits using AIC, or BIC (information criterion that weigh model fit with over-parameterization). Both of these criterion suggest the linear model fits best (lowest AIC and BIC), which suggests that the apparently non-linear fits observed in the graphical approaches don't improve prediction of the health outcome, relative to the linear fit, due to the increase in variance associated with including more parameters.
```{r splines, results='markup', fig.show='hold', fig.height=5, fig.width=7.5, cache=FALSE}
AIC(qc.fit6lin$fit)
AIC(qc.fit6nonlin$fit)
AIC(qc.fit6nonhom$fit)

BIC(qc.fit6lin$fit)
BIC(qc.fit6nonlin$fit)
BIC(qc.fit6nonhom$fit)
```

More examples on advanced topics can be viewed in the other package vignette.



## FAQ<a name="faq"></a>
### Why don't I get weights/scaled effects from the `boot` or `ee` functions? (and other questions about the weights/scaled effect sizes)
Users often use the `qgcomp.*.boot` or `qgcomp.*.ee` functions because they want to marginalize over confounders or fit a non-linear joint exposure function. In both cases, the overall exposure response will no longer correspond to a simple weighted average of model coefficients, so none of the `qgcomp.*.boot` or `qgcomp.*.ee` functions will calculate weights. In most use cases, the weights would vary according to which level of joint exposure you're at, so it is not a straightforward proposition to calculate them (and you may not wish to report 4 sets of weights if you use the default `q=4`). That is, the contribution of each exposure to the overall effect will change across levels of exposure if there is any non-linearity, which makes the weights not useful as simple inferential tools (and, at best, an approximation). If you wish to have an approximation, then use a "noboot" method and report the weights from that along with a caveat that they are not directly applicable to the results in which non-linearity/non-additivity/marginalization-over-covariates is performed (using boot methods). If you fit an otherwise linear model, you can get weights from a `qgcomp.*.noboot` which will be very close to the weights you might get from a linear model fit via `qgcomp.*.boot` functions, but be explicit that the weights come from a different model than the inference about joint exposure effects. 

It should be emphasized here that the weights are not a stable or entirely useful quantity for many research goals. Qgcomp addresses the mixtures problem of variance inflation by focusing on a parameter that is less susceptible to variance inflation than independent effects (the psi parameters, or overall effects of a mixture). The weights are a form of independent effect and will always be sensitive to this issue, regardless of the statistical method that is used. Some statistical approaches to improving estimation of independent effects (e.g. setting `bayes=TRUE`) is accessible in many qgcomp functions, but these approaches universally introduce bias in exchange for reducing variance and shouldn't be used without a good understanding of what shrinkage and penalization methods actually accomplish. Principled and rigorous integration of these statistical approaches with qgcomp is in progress, but that work is inherently more bespoke and likely will not be available in this R package. The shrinkage and penalization literature is large and outside the scope of this software and documentation, so no other guidance is given here. In any case, the calculated weights are only interpretable as proportional effect sizes in a setting in which there is linearity, additivity, and collapsibility, and so the package makes no efforts to try to introduce weights into other settings in which those assumptions may not be met. Outside of those narrow settings, the weights would have a dubious interpretation and the programming underlying the qgcomp package errs on the side of preventing the reporting of results that are mutually inconsistent. If you are using the `boot` versions of a qgcomp function in a setting in which you know that the weights are valid, it is very likely that you do not actually need to be using the `boot` versions of the functions.

### Do I need to model non-linearity and non-additivity of exposures?
Maybe. The inferential object of qgcomp is the set of $\psi$ parameters that correspond to a joint exposure response. As it turns out, with correlated exposures non-linearity can disguise itself as non-additivity (Belzak and Bauer [2019] Addictive Behaviors). If we were inferring independent effects, this distinction would be crucial, but for joint effects it may turn out that it doesn't matter much if you model non-linearity in the joint response function through non-additivity or non-linearity of individual exposures in a given study. Models fit in qgcomp still make the crucial assumption that you are able to model the joint exposure response via parametric models, so that assumption should not be forgotten in an effort to try to disentagle non-linearity (e.g. quadratic terms of exposures) from non-additivity (e.g. product terms between exposures). The important part to note about parametric modeling is that we have to explicitly tell the model to be non-linear, and no adaptation to non-linear settings will happen automatically. Exploring non-linearity is not a trivial endeavor.

### Do I have to use quantiles?
No. You can turn off "quantization" by setting `q=NULL` or you can supply your own categorization cutpoints via the "breaks" argument. It is up to the user to interpret the results if either of these options is taken. Frequently, `q=NULL` is used in concert with standardizing exposure variables by dividing them by their interquartile ranges (IQR). The joint exposure response can then be interpreted as the effect of an IQR change in all exposures. Using IQR/2 (with or without a log transformation before hand) will yield results that are most (roughly) compatible with the package defaults (`q=4`) but that does not require quantization. Quantized variables have nice properties: they prevent extrapolation and reduce influence of outliers, but the choice of how to include exposures in the model should be a deliberate and well-informed one. There are examples of setting `q=NULL` in the help files for qgcomp.glm.boot and qgcomp.glm.ee, but this approach is available for any of the qgcomp methods (and is accomplished nearly 100% outside of the package functions, aside from setting `q=NULL`).

### Can I cite this document?
Probably not in a scientific manuscript. If you find an idea here that is not published anywhere else and wish to develop it into a full manuscript, feel free! (But probably check with alex.keil@nih.gov to ask if a paper is already in development or is, perhaps, already published.)

### Where else can I get help?
The vignettes of the package and the help files of the functions give many, many examples of usage. Additionally, some edge case or interesting applicationsare available in the form of github gists at <https://gist.github.com/alexpkeil1>. If you come up with an interesting problem that you think could be solved in this package, but is currently not, feel free to submit an issue on the R package github page <https://github.com/alexpkeil1/qgcomp/issues>. Several additions to the package have already come about through that avenue (though not always quickly).



## References
Alexander P. Keil, Jessie P. Buckley, Katie M. O'Brien, Kelly K. Ferguson, Shanshan Zhao, Alexandra J. White. A quantile-based g-computation approach to addressing the effects of exposure mixtures. <https://doi.org/10.1289/EHP5838> 

## Acknowledgments<a name="ack"></a>

The development of this package was supported by NIH Grant RO1ES02953101. Invaluable code testing has been performed by Nicole Niehoff, Michiel van den Dries, Emily Werder, Jessie Buckley, Barrett Welch, Che-Jung (Rong) Chang, Jordan Kuiper, various github users, and Katie O'Brien. Thank you to Hanna Jardel for contributing the "qgcomp.tobit.noboot" function.


