-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmediate.R
464 lines (408 loc) · 16.8 KB
/
mediate.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#' Perform mediation analysis
#'
#' \code{getMediation} and \code{addMediation} provide a wrapper of
#' \code{\link[mediation:mediate]{mediate}} for
#' \code{\link[SummarizedExperiment:SummarizedExperiment-class]{SummarizedExperiment}}.
#'
#' @param x a \code{\link[SummarizedExperiment:SummarizedExperiment-class]{SummarizedExperiment}}.
#'
#' @param outcome \code{Character scalar}. Indicates the colData variable used
#' as outcome in the model.
#'
#' @param treatment \code{Character scalar}. Indicates the colData variable
#' used as treatment in the model.
#'
#' @param mediator \code{Character scalar}. Indicates the colData variable used
#' as mediator in the model. (Default: \code{NULL})
#'
#' @param assay.type \code{Character scalar}. Specifies the assay used for
#' feature-wise mediation analysis. (Default: \code{"counts"})
#'
#' @param dimred \code{Character scalar}. Indicates the reduced dimension
#' result in \code{reducedDims(object)} for component-wise mediation analysis.
#' (Default: \code{NULL})
#'
#' @param family \code{Character scalar}. A specification for the outcome model link function.
#' (Default: \code{gaussian("identity")})
#'
#' @param covariates \code{Character scalar} or \code{character vector}. Indicates the colData
#' variables used as covariates in the model.
#' (Default: \code{NULL})
#'
#' @param p.adj.method \code{Character scalar}. Selects adjustment method
#' of p-values. Passed to `p.adjust` function.
#' (Default: \code{"holm"})
#'
#' @param add.metadata \code{Logical scalar}. Should the model metadata be returned.
#' (Default: \code{FALSE})
#'
#' @param verbose \code{Logical scalar}. Should execution messages be printed.
#' (Default: \code{TRUE})
#'
#' @param name \code{Character scalar}. A name for the column of the
#' \code{colData} where results will be stored. (Default: \code{"mediation"})
#'
#' @param ... additional parameters that can be passed to
#' \code{\link[mediation:mediate]{mediate}}.
#'
#' @details
#' This wrapper of \code{\link[mediation:mediate]{mediate}} for
#' \code{\link[SummarizedExperiment:SummarizedExperiment-class]{SummarizedExperiment}}
#' provides a simple method to analyse the effect of a treatment variable on an
#' outcome variable found in \code{colData(se)} through the mediation of either
#' another variable in colData (argument \code{mediator}) or an assay
#' (argument \code{assay.type}) or a reducedDim (argument \code{dimred}).
#' Importantly, those three arguments are mutually exclusive.
#'
#' @return
#' \code{getMediation} returns a \code{data.frame} of adjusted p-values and
#' effect sizes for the ACMEs and ADEs of every mediator given as input, whereas
#' \code{addMediation} returns an updated
#' \code{\link[SummarizedExperiment:SummarizedExperiment-class]{SummarizedExperiment}}
#' instance with the same \code{data.frame} stored in the metadata as
#' "mediation". Its columns include:
#'
#' \describe{
#' \item{Mediator}{the mediator variable}
#' \item{ACME_estimate}{the Average Causal Mediation Effect (ACME) estimate}
#' \item{ADE_estimate}{the Average Direct Effect (ADE) estimate}
#' \item{ACME_pval}{the adjusted p-value for the ACME estimate}
#' \item{ADE_pval}{the adjusted p-value for the ADE estimate}
#' }
#'
#' @name getMediation
#'
#' @examples
#' \dontrun{
#' # Import libraries
#' library(mia)
#' library(scater)
#'
#' # Load dataset
#' data(hitchip1006, package = "miaTime")
#' tse <- hitchip1006
#'
#' # Agglomerate features by family (merely to speed up execution)
#' tse <- agglomerateByRank(tse, rank = "Phylum")
#' # Convert BMI variable to numeric
#' tse$bmi_group <- as.numeric(tse$bmi_group)
#'
#' # Analyse mediated effect of nationality on BMI via alpha diversity
#' # 100 permutations were done to speed up execution, but ~1000 are recommended
#' med_df <- getMediation(tse,
#' outcome = "bmi_group",
#' treatment = "nationality",
#' mediator = "diversity",
#' covariates = c("sex", "age"),
#' treat.value = "Scandinavia",
#' control.value = "CentralEurope",
#' boot = TRUE, sims = 100,
#' add.metadata = TRUE)
#'
#' # Visualise model statistics for 1st mediator
#' plot(attr(med_df, "metadata")[[1]])
#'
#' # Apply clr transformation to counts assay
#' tse <- transformAssay(tse,
#' method = "clr",
#' pseudocount = 1)
#'
#' # Analyse mediated effect of nationality on BMI via clr-transformed features
#' # 100 permutations were done to speed up execution, but ~1000 are recommended
#' tse <- addMediation(tse, name = "assay_mediation",
#' outcome = "bmi_group",
#' treatment = "nationality",
#' assay.type = "clr",
#' covariates = c("sex", "age"),
#' treat.value = "Scandinavia",
#' control.value = "CentralEurope",
#' boot = TRUE, sims = 100,
#' p.adj.method = "fdr")
#'
#' # Show results for first 5 mediators
#' head(metadata(tse)$assay_mediation, 5)
#'
#' # Perform ordination
#' tse <- runMDS(tse, name = "MDS",
#' method = "euclidean",
#' assay.type = "clr",
#' ncomponents = 3)
#'
#' # Analyse mediated effect of nationality on BMI via NMDS components
#' # 100 permutations were done to speed up execution, but ~1000 are recommended
#' tse <- addMediation(tse, name = "reddim_mediation",
#' outcome = "bmi_group",
#' treatment = "nationality",
#' dimred = "MDS",
#' covariates = c("sex", "age"),
#' treat.value = "Scandinavia",
#' control.value = "CentralEurope",
#' boot = TRUE, sims = 100,
#' p.adj.method = "fdr")
#'
#' # Show results for first 5 mediators
#' head(metadata(tse)$reddim_mediation, 5)
#' }
#'
NULL
#' @rdname getMediation
#' @export
setGeneric("addMediation", signature = c("x"),
function(x, ...) standardGeneric("addMediation"))
#' @rdname getMediation
#' @export
#' @importFrom stats gaussian
setMethod("addMediation", signature = c(x = "SummarizedExperiment"),
function(x, outcome, treatment, name = "mediation",
mediator = NULL, assay.type = NULL, dimred = NULL,
family = gaussian(), covariates = NULL, p.adj.method = "holm",
add.metadata = FALSE, verbose = TRUE, ...) {
med_df <- getMediation(
x, outcome, treatment,
mediator, assay.type, dimred,
family, covariates, p.adj.method,
add.metadata, verbose, ...
)
x <- .add_values_to_metadata(x, name, med_df)
return(x)
}
)
#' @rdname getMediation
#' @export
setGeneric("getMediation", signature = c("x"),
function(x, ...) standardGeneric("getMediation"))
#' @rdname getMediation
#' @export
#' @importFrom stats gaussian
#' @importFrom SingleCellExperiment reducedDim reducedDimNames
setMethod("getMediation", signature = c(x = "SummarizedExperiment"),
function(x, outcome, treatment,
mediator = NULL, assay.type = "counts", dimred = NULL,
family = gaussian(), covariates = NULL, p.adj.method = "holm",
add.metadata = FALSE, verbose = TRUE, ...) {
###################### Input check ########################
if( !outcome %in% names(colData(x)) ){
stop(outcome, " not found in colData(x).", call. = FALSE)
}
if( !treatment %in% names(colData(x)) ){
stop(treatment, " not found in colData(x).", call. = FALSE)
}
if( !is.null(covariates) && !all(covariates %in% names(colData(x))) ){
stop("covariates not found in colData(x).", call. = FALSE)
}
if( !.is_a_bool(add.metadata) ){
stop("add.metadata must be TRUE or FALSE.", call. = FALSE)
}
if( !.is_a_bool(verbose) ){
stop("verbose must be TRUE or FALSE.", call. = FALSE)
}
# Check that arguments can be passed to mediate and filter samples
x <- .check.mediate.args(
x, outcome, treatment, mediator, covariates, verbose, ...
)
# Check which mediator was provided (colData, assay or reducedDim)
med_opts <- unlist(lapply(
list(mediator = mediator, assay.type = assay.type, dimred = dimred),
function(x) !is.null(x)
))
if( sum(med_opts) != 1 ){
# Throw error if none or multiple mediation options are specified
stop(
"The arguments mediator, assay.type and dimred are mutually ",
"exclusive, but ", sum(med_opts), " were provided.",
call. = FALSE
)
}
if ( med_opts[[1]] ){
# Check that mediator is in colData
if( !mediator %in% names(colData(x)) ) {
stop(mediator, " not found in colData(x).", call. = FALSE)
}
# Use mediator for analysis
mediators <- mediator
mat <- NULL
} else if( med_opts[[2]] ){
# Check that assay is in assays
.check_assay_present(assay.type, x)
# Define matrix for analysis
mat <- assay(x, assay.type)
# Use assay for analysis
mediators <- rownames(mat)
} else if( med_opts[[3]] ){
# Check that reducedDim is in reducedDims
if(!dimred %in% reducedDimNames(x)){
stop(dimred, " not found in reducedDims(x).", call. = FALSE)
}
# Define matrix for analysis
mat <- t(reducedDim(x, dimred))
# Give component names to matrix rows
rownames(mat) <- paste0(dimred, seq(1, nrow(mat)))
# Use reducedDim for analysis
mediators <- rownames(mat)
}
# Create template list of results
results <- list(
Mediator = c(), ACME_estimate = c(), ADE_estimate = c(),
ACME_pval = c(), ADE_pval = c(), Model = list()
)
# Set initial index
i <- 0
for( mediator in mediators ){
# Update index
i <- i + 1
if( verbose ){
message("\rMediator ", i, " out of ",
length(mediators), ": ", mediator
)
}
# Run mediation analysis for current mediator
med_out <- .run.mediate(
x, outcome, treatment, mediator,
family = family, mat = mat,
covariates = covariates, ...
)
# Update list of results
results <- .update.results(results, med_out, mediator)
}
# Combine results into dataframe
med_df <- .make.output(results, p.adj.method, add.metadata)
return(med_df)
}
)
# Check that arguments can be passed to mediate and remove unused samples
#' @importFrom stats na.omit
.check.mediate.args <- function(x, outcome, treatment, mediator,
covariates, verbose = TRUE, ...) {
# Create dataframe from selected columns of colData
df <- as.data.frame(colData(x)[ , names(colData(x)) %in% c(outcome,
treatment,
mediator,
covariates)])
# Store kwargs into variable
kwargs <- list(...)
# Remove missing data from df
df <- na.omit(df)
diff <- ncol(x) - nrow(df)
if( diff != 0 ){
# Remove missing data from se
x <- x[ , rownames(df)]
if( verbose ){
message(diff, " samples removed because of missing data.")
}
}
# If boot is TRUE and treatment variable is discrete and has 3+ levels
if( !is.null(kwargs[["boot"]]) && !is.numeric(df[[treatment]]) &&
length(unique((df[[treatment]]))) > 2 ) {
## if control and treat value are not specified
if( any(sapply(kwargs[c("control.value", "treat.value")], is.null)) ){
stop(
"Too many treatment levels. Consider specifing a treat.value ",
"and a control.value", call. = FALSE
)
}
## but if they are specified
# if they appear in the treatment variable
if( !all(kwargs[c("control.value", "treat.value")] %in% unique(df[[treatment]])) ){
stop(
"treat.value and/or control.value not found in the levels of ",
"the treatment variable.", call. = FALSE
)
}
# Find indices of samples that belong to either control or treatment
keep <- df[[treatment]] %in% kwargs[c("control.value", "treat.value")]
# Remove samples different from control and treatment from df
df <- df[keep, ]
diff <- ncol(x) - nrow(df)
# Remove samples different from control and treatment from se
x <- x[ , rownames(df)]
if( verbose ){
message(
diff, " samples removed because different ",
"from control and treatment."
)
}
}
return(x)
}
# Run mediation analysis
#' @importFrom mediation mediate
#' @importFrom stats lm formula glm
.run.mediate <- function(x, outcome, treatment, mediator = NULL, mat = NULL,
family = gaussian(), covariates = NULL, ...) {
# Create initial dataframe with outcome and treatment variables
df <- data.frame(Outcome = colData(x)[[outcome]], Treatment = colData(x)[[treatment]])
if( is.null(mat) ){
# If matrix not given, fetch mediator from colData
df[["Mediator"]] <- colData(x)[[mediator]]
} else {
# If matrix given, use it as mediators
df[["Mediator"]] <- mat[mediator, ]
}
# Define basic formula mediation model
relation_m <- "Mediator ~ Treatment"
# Define basic formula outcome model
relation_dv <- "Outcome ~ Treatment + Mediator"
if( !is.null(covariates) ){
# Fetch covariates from colData and store them in dataframe
df <- cbind(df, colData(x)[covariates])
# Add covariate to formula of mediation model
relation_m <- paste(
relation_m, "+",
paste(covariates,collapse = " + ")
)
# Add covariate to formula of outcome model
relation_dv <- paste(
relation_dv, "+",
paste(covariates, collapse = " + ")
)
}
# Fit mediation model
fit_m <- do.call(lm, list(formula = formula(relation_m), data = df))
# Fit outcome model
fit_dv <- do.call(
glm,
list(formula = formula(relation_dv), family = family, data = df)
)
# Run mediation analysis
med_out <- mediate(
fit_m, fit_dv,
treat = "Treatment", mediator = "Mediator",
covariates = covariates, ...
)
return(med_out)
}
# Update list of results
.update.results <- function(results, med_out, mediator) {
# Update model variables
results[["Mediator"]] <- c(results[["Mediator"]], mediator)
# Update stats of ACME (average causal mediation effect)
results[["ACME_estimate"]] <- c(results[["ACME_estimate"]], med_out$d.avg)
results[["ACME_pval"]] <- c(results[["ACME_pval"]], med_out$d.avg.p)
# Update stats of ADE (average direct effect)
results[["ADE_estimate"]] <- c(results[["ADE_estimate"]], med_out$z.avg)
results[["ADE_pval"]] <- c(results[["ADE_pval"]], med_out$z.avg.p)
# Add current model to metadata
results[["Model"]][[length(results[["Model"]]) + 1]] <- med_out
return(results)
}
# Combine results into output dataframe
.make.output <- function(results, p.adj.method, add.metadata) {
# Create dataframe with model variables, effect sizes and p-values
med_df <- do.call(data.frame, results[seq_len(length(results) - 1)])
# Compute adjusted p-values and add them to dataframe
med_df[["ACME_pval"]] <- p.adjust(
med_df[["ACME_pval"]],
method = p.adj.method
)
med_df[["ADE_pval"]] <- p.adjust(
med_df[["ADE_pval"]],
method = p.adj.method
)
if( add.metadata ){
# models for every mediator are saved into the metadata attribute
attr(med_df, "metadata") <- results[["Model"]]
}
# Order output dataframe by ACME p-values
med_df <- med_df[order(med_df[["ACME_pval"]]), ]
return(med_df)
}