-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_diff.R
422 lines (374 loc) · 16.2 KB
/
test_diff.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
#' Identify differentially abundant proteins
#'
#' The `test_diff()` function is used to test coefficients of a 'proDAFit'
#' object. It provides a Wald test to test individual
#' coefficients and a likelihood ratio F-test to compare the
#' original model with a reduced model. The \code{result_names}
#' method provides a quick overview which coefficients are
#' available for testing.
#'
#' To test if coefficient is different from zero with a Wald
#' test use the \code{contrast} function argument. To test if two
#' models differ with an F-test use the \code{reduced_model}
#' argument. Depending on the test that is conducted, the functions
#' returns slightly different data.frames.
#'
#' The function is designed to follow the principles of the
#' base R test functions (ie. \code{\link[stats]{t.test}} and
#' \code{\link[stats]{wilcox.test}}) and the functions designed
#' for collecting the results of high-throughput testing
#' (ie. \code{limma::topTable} and \code{DESeq2::results}).
#'
#' @param fit an object of class 'proDAFit'. Usually, this is
#' produced by calling \code{proDA()}
#' @param contrast an expression or a string specifying which
#' contrast is tested. It can be a single coefficient (to see
#' the available options use \code{result_names(fit)}) or any
#' linear combination of them. The contrast is always compared
#' against zero. Thus, to find out if two coefficients differ
#' use \code{coef1 - coef2}. Remember if the coefficient is not
#' a valid identifier in R, to escape it using back ticks. For
#' example if you test the interaction of A and B use
#' \code{`A:B`}.
#' @param reduced_model If you don't want to test an individual
#' coefficient, you can can specify a reduced model and compare
#' it with the original model using an F-test. This is useful
#' to find out how a set of parameters affect the goodness of
#' the fit. If neither a \code{contrast}, nor
#' a \code{reduced_model} is specified, by default a comparison
#' with an intercept model (ie. just the average across conditions)
#' is done. Default: \code{~ 1}.
#' @param alternative a string that decides how the
#' hypothesis test is done. This parameter is only relevant for
#' the Wald-test specified using the `contrast` argument.
#' Default: \code{"two.sided"}
#' @param pval_adjust_method a string the indicates the method
#' that is used to adjust the p-value for the multiple testing.
#' It must match the options in \code{\link[stats]{p.adjust}}.
#' Default: \code{"BH"}
#' @param sort_by a string that specifies the column that is used
#' to sort the resulting data.frame. Default: \code{NULL} which
#' means the result is sorted by the order of the input matrix.
#' @param decreasing a boolean to indicate if the order is reversed.
#' Default: \code{FALSE}
#' @param n_max the maximum number of rows returned by the method.
#' Default: \code{Inf}
#' @param verbose boolean that signals if the method prints informative
#' messages. Default: \code{FALSE}.
#'
#' @return
#' The `result_names()` function returns a character vector.
#'
#' The `test_diff()` function returns a \code{data.frame} with one row per protein
#' with the key parameters of the statistical test. Depending what kind of test
#' (Wald or F test) the content of the `data.frame` differs.
#'
#' The Wald test, which can considered equivalent to a t-test, returns
#' a `data.frame` with the following columns:
#' \describe{
#' \item{name}{the name of the protein, extracted from the rowname of
#' the input matrix}
#' \item{pval}{the p-value of the statistical test}
#' \item{adj_pval}{the multiple testing adjusted p-value}
#' \item{diff}{the difference that particular coefficient makes. In
#' differential expression analysis this value is also called
#' log fold change, which is equivalent to the difference on the
#' log scale.}
#' \item{t_statistic}{the \code{diff} divided by the standard
#' error \code{se}}
#' \item{se}{the standard error associated with the \code{diff}}
#' \item{df}{the degrees of freedom, which describe the amount
#' of available information for estimating the \code{se}. They
#' are the sum of the number of samples the protein was observed
#' in, the amount of information contained in the missing values,
#' and the degrees of freedom of the variance prior.}
#' \item{avg_abundance}{the estimate of the average abundance of
#' the protein across all samples.}
#' \item{n_approx}{the approximated information available for estimating
#' the protein features, expressed as multiple of the information
#' contained in one observed value.}
#' \item{n_obs}{the number of samples a protein was observed in}
#' }
#'
#'
#' The F-test returns a `data.frame` with the following columns
#' \describe{
#' \item{name}{the name of the protein, extracted from the rowname of
#' the input matrix}
#' \item{pval}{the p-value of the statistical test}
#' \item{adj_pval}{the multiple testing adjusted p-value}
#' \item{f_statistic}{the ratio of difference of normalized deviances
#' from original model and the reduced model, divided by the
#' standard deviation.}
#' \item{df1}{the difference of the number of coefficients in the
#' original model and the number of coefficients in the reduced
#' model}
#' \item{df2}{the degrees of freedom, which describe the amount
#' of available information for estimating the \code{se}. They
#' are the sum of the number of samples the protein was observed
#' in, the amount of information contained in the missing values,
#' and the degrees of freedom of the variance prior.}
#' \item{avg_abundance}{the estimate of the average abundance of
#' the protein across all samples.}
#' \item{n_approx}{the information available for estimating
#' the protein features, expressed as multiple of the information
#' contained in one observed value.}
#' \item{n_obs}{the number of samples a protein was observed in}
#' }
#'
#'
#' @seealso The contrast argument is inspired by
#' \code{limma::makeContrasts}.
#'
#' @examples
#' # "t-test"
#' syn_data <- generate_synthetic_data(n_proteins = 10)
#' fit <- proDA(syn_data$Y, design = syn_data$groups)
#' result_names(fit)
#' test_diff(fit, Condition_1 - Condition_2)
#'
#'
#' suppressPackageStartupMessages(library(SummarizedExperiment))
#' se <- generate_synthetic_data(n_proteins = 10,
#' n_conditions = 3,
#' return_summarized_experiment = TRUE)
#' colData(se)$age <- rnorm(9, mean=45, sd=5)
#' colData(se)
#' fit <- proDA(se, design = ~ group + age)
#' result_names(fit)
#' test_diff(fit, "groupCondition_2",
#' n_max = 3, sort_by = "pval")
#'
#' # F-test
#' test_diff(fit, reduced_model = ~ group)
#'
#'
#' @export
test_diff <- function(fit, contrast,
reduced_model = ~ 1,
alternative = c("two.sided", "greater", "less"),
pval_adjust_method = "BH",
sort_by = NULL,
decreasing = FALSE,
n_max = Inf,
verbose = FALSE){
alternative <- match.arg(alternative, c("two.sided", "greater", "less"))
if(missing(contrast)){
# Do F test against reduced model
# Handle the design parameter
if(is.matrix(reduced_model)){
red_model_matrix <- reduced_model
}else if((is.vector(reduced_model) || is.factor(reduced_model)) && length(reduced_model) == ncol(fit)){
red_model_matrix <- convert_chr_vec_to_model_matrix(reduced_model, fit$reference_level)
}else if(inherits(reduced_model,"formula")){
red_model_matrix <- convert_formula_to_model_matrix(reduced_model, colData(fit), fit$reference_level)
}else{
stop(paste0("red_model_matrix argment of class ", class(reduced_model), " is not supported. Please ",
"specify a `model_matrix`, a `character vector`, or a `formula`."))
}
if(verbose){
message("F test: comparing the reduced model with the original model.")
}
rownames(red_model_matrix) <- colnames(fit)
check_valid_model_matrix(red_model_matrix, fit)
if(ncol(red_model_matrix) >= ncol(design(fit))){
stop("Reduced model is larger or has the same size as the reference model.")
}
f_res <- run_nested_model_comparison(fit, red_model_matrix, verbose)
res <- data.frame(name = rownames(fit),
pval = f_res[, "pval"],
adj_pval = p.adjust(f_res[, "pval"], method = pval_adjust_method),
# deviance_full = f_res[, "deviance_full"],
# deviance_reduced = f_res[, "deviance_reduced"],
f_statistic = f_res[, "f_statistic"],
df1 = f_res[, "df1"],
df2 = f_res[, "df2"],
avg_abundance = rowMeans(predict(fit, type="response")),
n_approx = feature_parameters(fit)$n_approx,
n_obs = feature_parameters(fit)$n_obs,
stringsAsFactors = FALSE)
}else{
if(any(reduced_model != formula(~ 1))){
stop("You can only specify the contrast argument or the reduced_model argument.")
}
cntrst <- parse_contrast(contrast, levels = colnames(design(fit)),
reference_level = reference_level(fit),
direct_call = FALSE)
if(verbose){
message("Wald test: comparing ", deparse(substitute(contrast)), " with zero.")
}
wald_res <- run_wald_parameter_test(fit, cntrst, alternative)
res <- data.frame(name = rownames(fit),
pval = wald_res[, "pval"],
adj_pval = p.adjust(wald_res[, "pval"], method = pval_adjust_method),
diff = wald_res[, "diff"],
t_statistic = wald_res[, "t_statistic"],
se = wald_res[, "se"],
df = wald_res[, "df"],
avg_abundance = rowMeans(predict(fit, type="response")),
n_approx = feature_parameters(fit)$n_approx,
n_obs = feature_parameters(fit)$n_obs,
stringsAsFactors = FALSE)
}
rownames(res) <- NULL
res <- if(is.null(sort_by)){
res
}else{
res[order(res[[sort_by]], decreasing = decreasing), ]
}
res[seq_len(min(nrow(res), n_max)), ]
}
run_wald_parameter_test <- function(fit, contrast, alternative){
dm <- design(fit)
diff <- coefficients(fit) %*% contrast
diff_var <- vapply(coefficient_variance_matrices(fit), function(mat) c(t(contrast) %*% mat %*% contrast),
FUN.VALUE = 0.0)
diff_df <- nrow(dm) - ncol(dm)
t_stat <- diff / sqrt(diff_var)
pval <- pt(t_stat, df=diff_df)
if(alternative == "two.sided"){
pval <- 2 * pmin(pval, 1-pval)
}else if(alternative == "greater"){
pval <- 1 - pval
}
res <- cbind(diff,
sqrt(diff_var),
diff_df,
t_stat,
pval)
colnames(res) <- c("diff", "se", "df",
"t_statistic", "pval")
res
}
run_nested_model_comparison <- function(fit, red_model, verbose=FALSE){
location_prior_mean <- fit$hyper_parameters$location_prior_mean
location_prior_scale <- fit$hyper_parameters$location_prior_scale
variance_prior_df <- fit$hyper_parameters$variance_prior_df
variance_prior_scale <- fit$hyper_parameters$variance_prior_scale
location_prior_df <- fit$hyper_parameters$location_prior_df
moderate_location <- ! is.na(location_prior_mean)
moderate_variance <- ! is.na(variance_prior_scale)
# Fit the loglikelihood with the fixed \phi
# using the reduced model
res_mat <- mply_dbl(seq_len(nrow(fit)), function(idx){
full_model <- design(fit)
y <- fit$abundances[idx, ]
yo <- y[! is.na(y)]
rho <- fit$hyper_parameters$dropout_curve_position[is.na(y)]
zeta <- fit$hyper_parameters$dropout_curve_scale[is.na(y)]
sigma2 <- fit$feature_parameters$s2[idx]
zetastar <- zeta * sqrt(1 + sigma2/zeta^2)
df_full <- fit$feature_parameters$df[idx]
if(is.na(sigma2)){
return(c(f_statistic = NA, pval = NA,
df1 = ncol(full_model) - ncol(red_model), df2 = df_full,
deviance_full = NA, deviance_reduced = NA))
}
nl_res_full <- nlminb(start = fit$coefficients[idx, ], objective = function(beta){
- objective_fnc(y, yo,
X = full_model,
Xm = full_model[is.na(y), , drop=FALSE],
Xo = full_model[!is.na(y), , drop=FALSE],
beta, sigma2, rho, zetastar,
location_prior_mean, location_prior_scale,
variance_prior_df, variance_prior_scale,
location_prior_df, moderate_location, moderate_variance)
})
if(nl_res_full$convergence >= 2){
if(verbose){
warning("Model didn't not properly converge\n")
warning(nl_res_full$message, "\n")
warning(y,"\n")
}
lik_full <- NA
}else{
lik_full <- nl_res_full$objective
}
beta_init <- vapply(colnames(red_model), function(n){
if(is.na(nl_res_full$par[n])){
0
}else{
nl_res_full$par[n]
}
}, FUN.VALUE = 0.0)
red_res <- nlminb(start = beta_init, objective = function(beta){
- objective_fnc(y, yo,
X = red_model,
Xm = red_model[is.na(y), , drop=FALSE],
Xo = red_model[!is.na(y), , drop=FALSE],
beta, sigma2, rho, zetastar,
location_prior_mean, location_prior_scale,
variance_prior_df, variance_prior_scale,
location_prior_df, moderate_location, moderate_variance)
})
if(red_res$convergence >= 2){
if(verbose){
warning("Model didn't not properly converge\n")
warning(red_res$message, "\n")
warning(y,"\n")
}
lik_red <- NA
}else{
lik_red <- red_res$objective
}
deviance_red <- lik_red * 2 * sigma2
deviance_full <- lik_full * 2 * sigma2
LR_stat <- (deviance_red - deviance_full) / (ncol(full_model) - ncol(red_model)) / sigma2
pval <- pf(LR_stat, df1 = ncol(full_model) - ncol(red_model), df2 = df_full,
lower.tail = FALSE)
c(f_statistic = LR_stat, pval = pval,
df1 = ncol(full_model) - ncol(red_model), df2 = df_full,
deviance_full = deviance_full, deviance_reduced = deviance_red)
}, ncol = 6)
}
parse_contrast <- function(contrast, levels, reference_level = NULL, direct_call=TRUE){
env <- if(direct_call){
environment()
}else{
parent.frame()
}
if(missing(contrast)){
stop("No contrast argument was provided! The option is any linear combination of:\n",
paste0(levels, collapse = ", "))
}
cnt_capture <- substitute(contrast, env = env)
stopifnot(! is.null(levels))
if(is.factor(levels)){
levels <- levels(levels)
}else if(! is.character(levels)){
stop("levels must be either a character vector or a factor")
}
indicators <- diag(nrow=length(levels))
rownames(indicators) <- levels
colnames(indicators) <- levels
level_environment <- new.env(parent = parent.frame(n = 1 + (!direct_call)))
for(lvl in levels){
ind <- indicators[, lvl]
names(ind) <- levels
assign(lvl, ind, level_environment)
}
if(! is.null(reference_level)){
assign(reference_level, NA_real_, level_environment)
}
res <- eval(cnt_capture, envir= level_environment)
if(! is.numeric(res)){
if(is.character(res)){
# If contrast was a string, eval will just spit it out the same way
res <- eval(parse(text = res), envir= level_environment)
}
}
if(any(is.na(res))){
stop("Error the reference_level ", reference_level, " was used in the contrast argument. \n",
"But it is already absorbed into the intercept, so it is not necessary to subtract it here.")
}
res
}
#' @rdname test_diff
#' @export
setMethod("result_names", signature = "proDAFit", function(fit){
names <- colnames(coefficients(fit))
# Check if names contains any illegal characters
# this regex is approx. correct, but false positives are not so problematic
valid <- grepl("^[_.]?[[:alpha:]]+[[:alnum:]_.]*$", names)
ifelse(valid, names, paste0("`", names, "`"))
})