---
title: "Exposure-Phenotype Workflow"
author: "Jason Laird"
header-includes:
- \usepackage{amsmath}
- \usepackage{amsfonts}
output:
BiocStyle::html_document:
toc_float: true
toc: true
vignette: >
%\VignetteIndexEntry{tidyexposomics: Exposure-Phenotype Workflow}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
`````{r setup,include=FALSE}
knitr::opts_chunk$set(echo = TRUE,warning = FALSE,message = FALSE)
`````
Objectives
* Create an `exposomicset` object from a codebook and exposure metadata.
* Identify and visualize missing values in exposure data.
* Impute missing exposure values.
* Assess variable normality and apply transformations to improve it.
* Understand which exposures/samples are correlated with one another.
* Determine if exposures/samples cluster into groups.
* Run an exposome-wide association study (ExWAS) to test many exposures against a single outcome.
* Fit interaction models to test whether an exposure-outcome relationship depends on another variable.
* Stratify models or subset samples to examine specific subgroups.
* Compare effect estimates across models.
* Summarize multiple exposures into a single exposome score.
* Test exposome scores against an outcome and vizualize.
# Exposure Preprocessing
## Getting the data in order
To get started with an exposure-outcome-only workflow, we need to create our exposomicset which stores this data:
`````{r load-data}
# Load Libraries
library(tidyexposomics)
# Load example data
data("tidyexposomics_example")
# Create exposomic set object
expom <- create_exposomicset(
codebook = tidyexposomics_example$annotated_cb,
exposure = tidyexposomics_example$meta
)
`````
You'll notice it took two inputs: (1) a codebook with information about the exposures ([see more about codebooks here](exposure_annotation.html)), and (2) the meta data which contain the exposures and covariate information.
For the sake of simplicity we will focus on a short list of exposures:
`````{r exposure-priority}
# Grab exposure variables
exp_vars <- tidyexposomics_example$annotated_cb |>
dplyr::filter(
category %in% c(
"aerosol",
"main group molecular entity",
"polyatomic entity"
)
) |>
dplyr::pull(variable) |>
as.character()
`````
# Quality Control
## Missingness
Oftentimes when collecting data, there are missing values. Let’s use the `plot_missing` function to determine where our missing values are:
`````{r missing-bar,fig.height=2,fig.width=5}
# Plot the missingness summary
plot_missing(
exposomicset = expom,
plot_type = "summary",
threshold = 0
)
`````
Here we see that there are 4 variables in the exposure data that are missing data. Let’s take a look at them:
```{r missing-bar-lollipop,fig.height=2,fig.width=4}
# Plot missing variables withing exposure group
plot_missing(
exposomicset = expom,
plot_type = "lollipop",
threshold = 0,
layers = "Exposure"
)
```
Here we see that one variable, `h_parity_None`, has about 4% missing values. We can apply a missingness filter using the `filter_missing` function. However, given that this level of missingness is quite low, we will not be applying a missingness filter and instead impute the missing data.
## Imputation
The `run_impute_missing` function is used to impute missing values. Here we can specify the imputation method for exposure and omics data separately.
The `exposure_impute_method` argument is used to set the imputation method for exposure data, and the `omics_impute_method` argument is used to set the imputation method for omics data. The `omics_to_impute` argument is used to specify which omics data to impute. Here we will impute the exposure data given using the `missforest` method, but other options for imputation methods include:
- `median`: Imputes missing values with the median of the variable.
- `mean`: Imputes missing values with the mean of the variable.
- `knn`: Uses k-nearest neighbors to impute missing values.
- `mice`: Uses the Multivariate Imputation by Chained Equations (MICE) method to impute missing values.
- `missforest`: Uses the MissForest method to impute missing values.
- `lod_sqrt2`: Imputes missing values using the square root of the lower limit of detection (LOD) for each variable. This is useful for variables that have a lower limit of detection, such as chemical exposures.
```{r impute-missing}
# Impute missing values
expom <- run_impute_missing(
exposomicset = expom,
exposure_impute_method = "missforest",
exposure_cols = exp_vars
)
```
## Normality Check
When determining variable associations, it is important to check the normality of the data. The `run_normality_check` function is used to check the normality of the data.
```{r normality-check}
# Check variable normality
expom <- run_normality_check(
exposomicset = expom,
action = "add"
)
```
The `transform_exposure` function is used to transform the data to make it more normal. Here the transform_method is set to `boxcox_best` as it will automatically select the best transformation method based on the data. The `transform_method` can be manually set to `log2`, `sqrt`, or `x_1_3` as well. We specify the exposure_cols argument to set the columns to transform.
```{r trasform-vars}
# Transform variables
expom <- transform_exposure(
exposomicset = expom,
transform_method = "boxcox_best",
exposure_cols = exp_vars
)
```
To check the normality of the exposure data, we can use the `plot_normality_summary` function. This function plots the normality of the data before and after transformation. The `transformed` argument is set to `TRUE` to plot the normality status of the transformed data.
```{r norm-plot,fig.width= 4,fig.height= 4}
# Examine normality summary
plot_normality_summary(
exposomicset = expom,
transformed = TRUE
)
```
## Exposure Summary
We can summarize the exposure data using the `run_summarize_exposures` function. This function calculates summary statistics for each exposure variable, including the number of values, number of missing values, minimum, maximum, range, sum, median, mean, standard error, and confidence intervals. The `exposure_cols` argument determines which variables to include in the summary.
```{r exp-sum, fig.width=6, fig.height=4}
# Summarize exposure data
run_summarize_exposures(
exposomicset = expom,
action = "get",
exposure_cols = exp_vars
) |>
head()
```
## Exposure Visualization
To visualize our exposure data, we can use the `plot_exposures` function. This function allows us to plot the exposure data in a variety of ways. Here we will plot the exposure data using a boxplot. The `exposure_cat` argument is used to set the exposure category to plot. Additionally, we could specify `exposure_cols` to only plot certain exposures. The `plot_type` argument is used to set the type of plot to create. Here we use a boxplot, but we could also use a ridge plot.
```{r plot-aerosol, fig.width=3, fig.height=5}
# Plot aerosol exposure distributions by sex
plot_exposures(
exposomicset = expom,
group_by = "e3_sex_None",
exposure_cols = c(
"h_pm25_ratio_preg_None",
"h_pm10_ratio_preg_None"
),
plot_type = "boxplot",
ylab = "Values",
title = "Aerosol Exposure by Sex"
) +
ggpubr::stat_compare_means(label = "p.signif")
```
Here we see that levels of PM2.5 were higher in females compared to males.
## Coverting the Exposomicset back to dataframes
If you need to grab your codebook, feel free to do so using the `extract_results` function:
```{r extract-codebook}
extract_results(
exposomicset = expom,
result = "codebook"
) |>
head()
```
If you want to grab the exposure+metadata do so using the `pivot_sample` function:
```{r grab-exposure-data}
expom |>
pivot_sample() |>
head()
```
# Exposure/Sample Clustering
## Co-exposure
Exposures rarely occur in isolation, and at any given time, we are exposured to many different exposures. Here we provide code to see which exposures co-occur together.
## Exposure Correlations
The `run_correlation` function identifies correlations between exposure variables. We set `feature_type` to `exposures` to focus on exposure variables and use a correlation cutoff of `0.3` to filter for meaningful associations. This cutoff can be adjusted based on your data and analysis needs.
```{r correlate-exposures}
# Run correlation analysis
expom <- run_correlation(
exposomicset = expom,
feature_type = "exposures",
action = "add",
exposure_cols = exp_vars,
correlation_cutoff = 0
)
```
To visualize the exposure correlations, we can use the `plot_circos_correlation` function. Here we will plot the circos plot. This function creates a circular plot of the exposure correlations. The `correlation_cutoff` argument is used to set the minimum correlation score for the association. Here we use a cutoff of `0.3`.
```{r exposure-circos-corr, fig.height=10,fig.width=10,fig.align='center'}
# Plot exposure correlation circos plot
plot_circos_correlation(
exposomicset = expom,
feature_type = "exposures",
corr_threshold = 0,
exposure_cols = exp_vars
)
```
## Exposure Clusters
We can also visualize these these co-occurence patterns using a heatmap as well. We can either specify the number of clusters or use a method like `diana` through the `clustering_approach` argument that will decide this for you. Here we specify two exposure groups:
```{r cluster-exposures,fig.width=12,fig.height=3.5}
# Cluster exposures and plot
expom |>
run_cluster_exposures(
user_k = 2,
exposure_cols = exp_vars
) |>
plot_clusters(
exposure_cols = exp_vars,
cluster_type = "exposure"
)
```
Here we note that there are two groups of exposures, mainly driven by PM2.5 levels during pregnancy.
## Sample Clustering
We can also see this pattern by clustering samples as well:
```{r cluster-samples,fig.width=12,fig.height=3.5}
# Cluster samples and plot
expom |>
run_cluster_samples(
user_k = 2,
exposure_cols = exp_vars
) |>
plot_clusters(
exposure_cols = exp_vars,
cluster_type = "sample"
)
```
# ExWAS
## Exposome-wide association study (ExWAS)
Inspired by the concept of genome-wide association studies, or GWAS, the ExWAS is a high-throughput way of identifying many exposures with an association to an outcome. The `run_association` allows us to see which exposure variables are associated with our outcome, `hs_asthma`, which is a 0 or 1, so we set the family to `binomial`. If the variable was continuous, we'd usually set it to `gaussian`. Just one more note: since we are running this model on many different exposures, we use `.feature` as a placeholder:
```{r assoc-exposures}
# Perform ExWAS
expom <- run_association(
exposomicset = expom,
formula = hs_asthma ~ .feature,
source = "exposures",
feature_set = exp_vars,
action = "add",
family = "binomial"
)
```
We can visualize these associations using the `plot_association` function and set our p-value filter to color associations below that threshold, here we set it to 0.3 just to demonstrate the coloring, but we suggest using the `p_adjust` column for `filter_col` and a cutoff of 0.05:
```{r plot-exwas,fig.width=6}
# Plot the association forest plot
plot_association(
exposomicset = expom,
effect = "main",
source = "exposures",
terms = exp_vars,
filter_thresh = 0.3,
up_col = "#8E0152",
down_col = "#006666",
filter_col = "p.value",
r2_col = "r2"
)
```
## Interaction Modeling
Sometimes, the relationship between an exposure an outcome *depends* on another variable. For this folks tend to leverage interaction modeling. Let's look at how we would see if the the relationship between asthma and an exposure depends on age:
```{r int-models,fig.width=6}
run_association(
exposomicset = expom,
overwrite = TRUE,
formula = hs_asthma ~ .feature * h_age_None,
source = "exposures",
feature_set = exp_vars,
action = "add",
family = "binomial"
) |>
plot_association(
effect = "h_age_None",
source = "exposures",
terms = exp_vars,
filter_thresh = 0.3,
up_col = "#8E0152",
down_col = "#006666",
filter_col = "p.value",
r2_col = "r2"
)
```
Notice, how we switched `effect = "main"` to `effect = "h_age_None"` so that we could grab the interaction results.
## Stratifying Models
We may also want to run a model on a portion of our data, like say just male patients:
```{r stratify-m,fig.width=7.5,fig.height=3.5}
expom |>
run_association(
formula = hs_asthma ~ .feature + h_age_None,
source = "exposures",
overwrite = TRUE,
feature_set = exp_vars,
family = "binomial",
strata = "e3_sex_None",
name = "by_sex"
) |>
plot_association(
effect = "main",
source = "exposures",
model = "by_sex_male",
terms = exp_vars,
filter_thresh = 0.3,
up_col = "#8E0152",
down_col = "#006666",
filter_col = "p.value",
r2_col = "r2"
)
```
We can also filter our object with the `subset` argument. Let's examine the association in children with a higher BMI z-score:
```{r stratify-f,fig.width=7.5,fig.height=3.5}
expom |>
run_association(
formula = hs_asthma ~ .feature + h_age_None,
source = "exposures",
overwrite = TRUE,
feature_set = exp_vars,
family = "binomial",
subset = hs_zbmi_who > 0,
name = "high_bmi"
) |>
plot_association(
effect = "main",
source = "exposures",
model = "high_bmi",
terms = exp_vars,
filter_thresh = 0.3,
up_col = "#8E0152",
down_col = "#006666",
filter_col = "p.value",
r2_col = "r2"
)
```
## Comparing models
Now sometimes we want to compare the results of different models. Here we demonstrate how to make many different models using two approaches: (1) specifying a list of formulas, and (2) using the `strata` or `subset` functionality. This will create results for four different models:
```{r compare-models}
expom <- expom |>
run_association(
formula = list(
crude = hs_asthma ~ .feature,
adj = hs_asthma ~ .feature + h_age_None
),
source = "exposures",
overwrite = TRUE,
feature_set = exp_vars,
family = "binomial"
) |>
run_association(
formula = hs_asthma ~ .feature + h_age_None,
source = "exposures",
feature_set = exp_vars,
family = "binomial",
strata = "e3_sex_None",
name = "by_sex"
)
```
To see what model results are available you can use the following:
```{r av-models}
association_models(expom)
```
Now let's demonstrate how to compare two models, here we will compare effect estimates for males and females:
```{r compare-sex,fig.width=6,fig.height=3.5}
expom |>
compare_association(
"exposures",
models = c(
"by_sex_female",
"by_sex_male"
)
) |>
plot_association_comparison(type = "forest")
```
We could also choose to visualize it using a scatter plot:
```{r compare-scatter,fig.width=4,fig.height=3.7}
expom |>
compare_association(
"exposures",
models = c(
"by_sex_female",
"by_sex_male"
)
) |>
plot_association_comparison(type = "scatter")
```
Here we see that the effect estimates are not correlated with one another, suggesting that the relationship between asthma and exposure differs by sex.
# Exposome Scores
## Exposome Scores
We can calculate exposome scores, which are a summary measure of exposure. The `run_exposome_score` function is used to calculate the exposome score. The `exposure_cols` argument is used to set the columns to use for the exposome score. The `score_type` argument is used to set the type of score to calculate. Here we could use:
- `median`: Calculates the median of the exposure variables.
- `mean`: Calculates the mean of the exposure variables.
- `sum`: Calculates the sum of the exposure variables.
- `pca`: Calculates the first principal component of the exposure variables.
- `irt`: Uses Item Response Theory to calculate the exposome score.
- `quantile`: Calculates the quantile of the exposure variables.
- `var`: Calculates the variance of the exposure variables.
The `score_column_name` argument is used to set the name of the column to store the exposome score in. Here we will define a score for aerosols using a variety of different methods and demonstrate their use in association with asthma status.
```{r calc-exposome, results='hide'}
# determine which aerosol variables to use
aerosols <- c("h_pm25_ratio_preg_None", "h_pm10_ratio_preg_None")
# Create exposome scores
expom <- expom |>
run_exposome_score(
exposure_cols = aerosols,
score_type = "median",
score_column_name = "exposome_median_score"
) |>
run_exposome_score(
exposure_cols = aerosols,
score_type = "pca",
score_column_name = "exposome_pca_score"
) |>
run_exposome_score(
exposure_cols = aerosols,
score_type = "quantile",
score_column_name = "exposome_quantile_score"
) |>
run_exposome_score(
exposure_cols = aerosols,
score_type = "var",
score_column_name = "exposome_var_score"
)
```
We can then associate these exposome scores with asthma status using the `run_association` function, just like we did before. However, this time we specify our `feature_set` to be the exposome scores we just calculated.
```{r associate-exposome-score}
# Associate exposome scores with outcome
expom <- run_association(
exposomicset = expom,
formula = hs_asthma ~ .feature,
source = "exposures",
feature_set = c(
"exposome_median_score",
"exposome_pca_score",
"exposome_quantile_score",
"exposome_var_score"
),
action = "add",
family = "binomial",
name = "exposome_scores"
)
```
```{r plot-exposome-scores, fig.height=2.5, fig.width=5}
# Plot the association forest plot
plot_association(
exposomicset = expom,
source = "exposures",
terms = c(
"exposome_median_score",
"exposome_pca_score",
"exposome_quantile_score",
"exposome_var_score"
),
model = "exposome_scores",
filter_col = "p.value",
filter_thresh = 0.05,
r2_col = "r2"
)
```
Session Info
```{r si}
sessionInfo()
```