|
1 | | -""" |
| 1 | +"""Coordinate-based meta-regression algorithms. |
2 | 2 |
|
3 | 3 | .. _metas_cbmr: |
4 | 4 |
|
5 | 5 | =========================================== |
6 | 6 | Coordinate-based meta-regression algorithms |
7 | 7 | =========================================== |
8 | 8 |
|
9 | | -A tour of Coordinate-based meta-regression (CBMR) algorithms in NiMARE |
| 9 | +A tour of Coordinate-based meta-regression (CBMR) algorithms in NiMARE. |
10 | 10 |
|
11 | 11 | CBMR is a generative framework to approximate smooth activation intensity function |
12 | 12 | and investigate the effect of study-level moderators (e.g., year of pubilication, |
|
24 | 24 | `online course <https://www.coursera.org/lecture/functional-mri-2/module-3-meta-analysis-Vd4zz>`_ |
25 | 25 | or a `brief overview <https://libguides.princeton.edu/neuroimaging_meta>`_. |
26 | 26 | """ |
| 27 | + |
27 | 28 | import numpy as np |
28 | 29 | import scipy |
29 | 30 | from nilearn.plotting import plot_stat_map |
|
75 | 76 | ############################################################################### |
76 | 77 | # Estimation of group-specific spatial intensity functions |
77 | 78 | # ----------------------------------------------------------------------------- |
78 | | -# CBMR can generate estimation of group-specific spatial internsity |
| 79 | +# CBMR can generate estimates of group-specific spatial intensity |
79 | 80 | # functions for multiple groups simultaneously, with different group-specific |
80 | 81 | # spatial regression coefficients. |
81 | 82 | # |
|
84 | 85 | # of moderators (shared by all groups). |
85 | 86 | # |
86 | 87 | # Note that study-level moderators can only have global effects instead of localized |
87 | | -# effects within CBMR framework. In the scenario that there're multiple subgroups |
| 88 | +# effects within CBMR framework. In the scenario that there are multiple subgroups |
88 | 89 | # within a group (e.g., indexed as subgroup-1 to subgroup-n, but one or more of them |
89 | 90 | # don't have enough number of studies to be inferred as a separate group). Using |
90 | 91 | # categorical encoding, CBMR can interpret the subgroups as categorical moderators |
91 | 92 | # for each study (either 0 or 1), and estimate the global activation intensity |
92 | 93 | # associated with each subgroup (comparing to the average). |
93 | 94 |
|
94 | | -from nimare.meta.cbmr import CBMREstimator |
| 95 | +from nimare.meta import CBMREstimator |
95 | 96 |
|
96 | 97 | studyset = StandardizeField(fields=["sample_sizes", "avg_age"]).transform(studyset) |
97 | 98 |
|
|
106 | 107 | model=models.PoissonEstimator, |
107 | 108 | penalty=False, |
108 | 109 | lr=1e-1, |
109 | | - tol=1e3, # a reasonable choice is 1e-2, 1e3 is for speed |
| 110 | + tol=1e3, # a reasonable choice is 1e-2, 1e3 is for speed |
110 | 111 | device="cpu", # "cuda" if you have GPU |
111 | 112 | ) |
112 | 113 | results = cbmr.fit(dataset=studyset) |
|
163 | 164 | ############################################################################### |
164 | 165 | # Generalized Linear Hypothesis (GLH) testing for spatial homogeneity |
165 | 166 | # ----------------------------------------------------------------------------- |
166 | | -# In the most basic scenario of spatial homogeneity test, contrast matrix `t_con_groups` |
167 | | -# can be generated by `create_contrast` function, with group names specified. |
168 | | -from nimare.meta.cbmr import CBMRInference |
169 | | - |
170 | | -inference = CBMRInference(device="cuda") |
171 | | -inference.fit(result=results) |
172 | | -t_con_groups = inference.create_contrast( |
173 | | - ["SchizophreniaYes", "SchizophreniaNo", "DepressionYes", "DepressionNo"], source="groups" |
174 | | -) |
175 | | -contrast_result = inference.transform(t_con_groups=t_con_groups) |
| 167 | +# In the most basic scenario of spatial homogeneity testing, the fitted CBMR result can run |
| 168 | +# inference directly. The available groups and moderators are discoverable from the result. |
| 169 | +print(results.describe_inference_inputs()) |
| 170 | +contrast_result = results.test_groups() |
176 | 171 |
|
177 | 172 | ############################################################################### |
178 | 173 | # Now that we have done spatial homogeneity tests, we can plot the z-score maps. |
|
231 | 226 | # Areas with significant p-values are highlighted (under significance level :math:`0.05`). |
232 | 227 |
|
233 | 228 | ############################################################################### |
234 | | -# Perform fasle discovery rate (FDR) correction on spatial homogeneity test |
| 229 | +# Perform false discovery rate (FDR) correction on spatial homogeneity test |
235 | 230 | # ----------------------------------------------------------------------------- |
236 | 231 | # The default FDR correction method is "indep", using Benjamini-Hochberg(BH) procedure. |
237 | 232 | from nimare.correct import FDRCorrector |
|
295 | 290 | ############################################################################### |
296 | 291 | # GLH testing for group comparisons among any two groups |
297 | 292 | # ----------------------------------------------------------------------------- |
298 | | -# In the most basic scenario of group comparison test, contrast matrix `t_con_groups` |
299 | | -# can be generated by `create_contrast` function, with `contrast_name` specified as |
300 | | -# "group1-group2". |
301 | | -t_con_groups = inference.create_contrast( |
| 293 | +# Pairwise group comparisons can also be expressed more directly with tuples. |
| 294 | +contrast_result = results.compare_groups( |
302 | 295 | [ |
303 | | - "SchizophreniaYes-SchizophreniaNo", |
304 | | - "SchizophreniaNo-DepressionNo", |
305 | | - "DepressionYes-DepressionNo", |
306 | | - ], |
307 | | - source="groups", |
| 296 | + ("SchizophreniaYes", "SchizophreniaNo"), |
| 297 | + ("SchizophreniaNo", "DepressionNo"), |
| 298 | + ("DepressionYes", "DepressionNo"), |
| 299 | + ] |
308 | 300 | ) |
309 | | -contrast_result = inference.transform(t_con_groups=t_con_groups, t_con_moderators=False) |
310 | 301 |
|
311 | 302 | ############################################################################### |
312 | 303 | # Now that we have done group comparison tests, |
|
358 | 349 | ############################################################################### |
359 | 350 | # GLH testing with contrast matrix specified |
360 | 351 | # ----------------------------------------------------------------------------- |
361 | | -# CBMR supports more flexible GLH test by specifying a contrast matrix. |
362 | | -# For example, group comparison test `2xgroup_0-1xgroup_1-1xgroup_2` can be |
363 | | -# represented as `t_con_group=[2, -1, -1, 0]`, as an input in `compute_contrast` |
364 | | -# function. Multiple independent GLH tests can be conducted simultaneously by |
365 | | -# including multiple contrast vectors/matrices in `t_con_group`. |
| 352 | +# CBMR supports more flexible GLH tests by specifying contrast vectors or matrices |
| 353 | +# directly through the result-level `infer` API. For example, the group comparison |
| 354 | +# `2xgroup_0-1xgroup_1-1xgroup_2` can be represented as |
| 355 | +# `group_contrasts=[[2, -1, -1, 0]]`. Multiple independent GLH tests can be |
| 356 | +# conducted simultaneously by including multiple contrast vectors or matrices in |
| 357 | +# `group_contrasts`. |
366 | 358 | # |
367 | | -# CBMR also allows simultaneous GLH tests (consisting of multiple contrast vectors) |
368 | | -# when it's represented as one of elements in `t_con_group` (datatype: list). |
| 359 | +# CBMR also allows simultaneous GLH tests consisting of multiple contrast vectors, |
| 360 | +# represented as one element of `group_contrasts`. |
369 | 361 | # Only if all of null hypotheses are rejected at voxel level, p-values are significant. |
370 | | -# For example, `t_con_group=[[1,-1,0,0], [1,0,-1,0], [0,0,1,-1]]` is used for testing |
371 | | -# the equality of spatial intensity estimation among all of four groups (finding the |
372 | | -# consistent activation regions). Note that only :math:`n-1` contrast vectors are necessary |
373 | | -# for testing the equality of :math:`n` groups. |
374 | | - |
375 | | -contrast_result = inference.transform( |
376 | | - t_con_groups=[[[1, -1, 0, 0], [1, 0, -1, 0], [0, 0, 1, -1]]], t_con_moderators=False |
| 362 | +# For example, `[[1, -1, 0, 0], [1, 0, -1, 0], [0, 0, 1, -1]]` tests the equality |
| 363 | +# of spatial intensity estimates across all four groups (finding consistent activation |
| 364 | +# regions). Note that only :math:`n-1` contrast vectors are necessary for testing the |
| 365 | +# equality of :math:`n` groups. |
| 366 | + |
| 367 | +contrast_result = results.infer( |
| 368 | + group_contrasts=[[[1, -1, 0, 0], [1, 0, -1, 0], [0, 0, 1, -1]]], |
| 369 | + moderator_contrasts=False, |
377 | 370 | ) |
378 | 371 |
|
379 | 372 | ############################################################################### |
|
395 | 388 | ############################################################################### |
396 | 389 | # GLH testing for study-level moderators |
397 | 390 | # ----------------------------------------------------------------------------- |
398 | | -# CBMR framework can estimate global study-level moderator effects, |
399 | | -# and allows inference on the existence of m. |
400 | | -contrast_name = results.estimator.moderators |
401 | | -t_con_moderators = inference.create_contrast(contrast_name, source="moderators") |
402 | | -contrast_result = inference.transform(t_con_moderators=t_con_moderators) |
| 391 | +# The CBMR framework can estimate global study-level moderator effects and allows |
| 392 | +# inference on whether those moderator effects differ from zero. |
| 393 | +contrast_result = results.test_moderators() |
403 | 394 | print(contrast_result.tables["moderators_regression_coef"]) |
404 | 395 | print( |
405 | 396 | "P-values of moderator effects `sample_sizes` is {}".format( |
|
420 | 411 | # a chosen subtype, spatial intensity estimations of the other :math:`4` subtypes of |
421 | 412 | # schizophrenia are moderatored globally. |
422 | 413 |
|
423 | | -t_con_moderators = inference.create_contrast( |
424 | | - ["standardized_sample_sizes-standardized_avg_age"], source="moderators" |
| 414 | +contrast_result = results.compare_moderators( |
| 415 | + [("standardized_sample_sizes", "standardized_avg_age")] |
425 | 416 | ) |
426 | | -contrast_result = inference.transform(t_con_moderators=t_con_moderators) |
427 | 417 | print( |
428 | | - "P-values of difference in two moderator effectors (`sample_size-avg_age`) is {}".format( |
| 418 | + "P-values of the difference between two moderator effects (`sample_size-avg_age`) is {}".format( |
429 | 419 | contrast_result.tables["p_standardized_sample_sizes-standardized_avg_age"] |
430 | 420 | ) |
431 | 421 | ) |
432 | 422 |
|
433 | 423 | ############################################################################### |
434 | 424 | # CBMR also allows flexible contrasts between study-level covariates. |
435 | | -# For example, we can write `contrast_name` (an input to `create_contrast` |
436 | | -# function) as `standardized_sample_sizes-standardized_avg_age` when exploring |
437 | | -# if the moderator effects of `sample_sizes` and `avg_age` are equivalent. |
| 425 | +# For example, we can express the comparison |
| 426 | +# `standardized_sample_sizes-standardized_avg_age` directly through |
| 427 | +# `results.compare_moderators(...)` when exploring whether the moderator effects |
| 428 | +# of `sample_sizes` and `avg_age` are equivalent. |
0 commit comments