├── .gitignore ├── LICENSE ├── README.md └── robust_toolbox ├── CANlab_robust_regression_demo.mlx ├── Robust_regression_walkthrough ├── Sample_analysis │ ├── EXPT.mat │ └── robust0001 │ │ ├── Results_Mask.png │ │ ├── SETUP.mat │ │ ├── cl_rob_p_0001_001_005_05_k5_1_1_prune.mat │ │ ├── cl_rob_p_0002_001_005_05_k5_1_1_prune.mat │ │ ├── irls-ols_p_0001.hdr │ │ ├── irls-ols_p_0001.img │ │ ├── irls-ols_p_0002.hdr │ │ ├── irls-ols_p_0002.img │ │ ├── irls-ols_z_0001.hdr │ │ ├── irls-ols_z_0001.img │ │ ├── irls-ols_z_0002.hdr │ │ ├── irls-ols_z_0002.img │ │ ├── mask.hdr │ │ ├── mask.img │ │ ├── nsubjects.hdr │ │ ├── nsubjects.img │ │ ├── ols_beta_0001.hdr │ │ ├── ols_beta_0001.img │ │ ├── ols_beta_0002.hdr │ │ ├── ols_beta_0002.img │ │ ├── ols_p_0001.hdr │ │ ├── ols_p_0001.img │ │ ├── ols_p_0002.hdr │ │ ├── ols_p_0002.img │ │ ├── ols_tmap_0001.hdr │ │ ├── ols_tmap_0001.img │ │ ├── ols_tmap_0002.hdr │ │ ├── ols_tmap_0002.img │ │ ├── orth_rob_p_0001_001_005_05_k5_1_1_prune_axial.png │ │ ├── orth_rob_p_0001_001_005_05_k5_1_1_prune_coronal.png │ │ ├── orth_rob_p_0001_001_005_05_k5_1_1_prune_sagittal.png │ │ ├── orth_rob_p_0002_001_005_05_k5_1_1_prune_axial.png │ │ ├── orth_rob_p_0002_001_005_05_k5_1_1_prune_coronal.png │ │ ├── orth_rob_p_0002_001_005_05_k5_1_1_prune_sagittal.png │ │ ├── rob_beta_0001.hdr │ │ ├── rob_beta_0001.img │ │ ├── rob_beta_0002.hdr │ │ ├── rob_beta_0002.img │ │ ├── rob_p_0001.hdr │ │ ├── rob_p_0001.img │ │ ├── rob_p_0001_001_005_05_k5_1_1_prune_log.txt │ │ ├── rob_p_0001_001_005_05_k5_1_1_prune_results.txt │ │ ├── rob_p_0002.hdr │ │ ├── rob_p_0002.img │ │ ├── rob_p_0002_001_005_05_k5_1_1_prune_log.txt │ │ ├── rob_p_0002_001_005_05_k5_1_1_prune_results.txt │ │ ├── rob_tmap_0001.hdr │ │ ├── rob_tmap_0001.img │ │ ├── rob_tmap_0002.hdr │ │ ├── rob_tmap_0002.img │ │ ├── robust_results_batch_report.txt │ │ ├── weights.hdr │ │ └── weights.img ├── robust_regression_walkthrough_objectoriented.mlx ├── robust_regression_walkthrough_toolbox.html └── robust_regression_walkthrough_toolbox.mlx ├── Robust_stats_functions ├── robust_correlation_working.m ├── robust_mean.m ├── robust_nonparam_displayregions.m ├── robust_nonparam_results.m ├── robust_reg_matrix.m ├── robust_reg_nonparam.m ├── robust_ttest2.m ├── t_image_ztest.m └── weighted_corrcoef.m ├── Scripts_for_comparing_ols ├── rob_vs_ols_imaging_compare_script.m ├── rob_vs_ols_matrix.m ├── rob_vs_univar_matrix.m └── rob_vs_univar_vs_ols_matrix.m ├── Simulation_scripts ├── robust_vs_ols_corr1.m ├── robust_vs_ols_corr2.m ├── robust_vs_ols_test1.m ├── robust_vs_ols_test1.txt └── robust_vs_ols_test2.m ├── older_functions ├── ROBUST_REGRESSION_HELP.doc ├── robust_act_corr_intersection_2008.m ├── robust_results2.m ├── robust_results3.m ├── robust_results_2012_plugin.m ├── robust_results_act_plus_corr.m ├── robust_results_batch_pre2020.m ├── robust_results_make_invp_maps.m ├── robust_results_threshold.m └── robust_results_threshold_2014.m ├── publish_robust_regression_report.m ├── robfit.m ├── robseed.m ├── robseed_results.m ├── robust_htw_results_plots.m ├── robust_nonparametric_subfunctions ├── Robust_nonparam_flowchart.ppt ├── robust_max_partial_corr.m ├── robust_nonpar_cluster_tables.m ├── robust_nonpar_set_level_svc.m ├── robust_nonparam_get_sig_clusters.m ├── robust_nonparam_interactive_scatterplot.m ├── robust_nonparam_orthviews.m └── robust_pooled_weight_core.m ├── robust_reg_load_files_to_objects.m ├── robust_results_batch.m └── robust_results_fdr_threshold.m /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RobustToolbox 2 | 3 | Robust regression is an alternative to Ordinary Least Squares regression. It uses iterative algorithms to identify and down-weight potential outliers. In robust regression, these are defined as observations that lie far from the regression slope (or plane). 4 | 5 | • Outliers can cause violations of statistical assumptions, which can increase false positives and false negatives 6 | 7 | • Outliers have very large effects on regression coefficients (slopes). These slopes are interpreted as measures of activation or connectivity in neuroimaging studies. Even one or a few outliers can completely alter the results, particularly if they are high-leverage data points. 8 | 9 | • Normally, a data analyst would check the assumptions and data distribution in any statistical model, and make adjustments as needed. But this is not possible with neuroimaging and other applications where hundreds or thousands of models are tested in parallel. 10 | 11 | • When assumptions cannot be checked for every regression model, automatic procedures for weighting based on outlier status can be advantageous 12 | 13 | • Robust regression is an automatic procedure for identifying cases that are potential outliers and down-weighting them. 14 | 15 | The robust regression toolbox was created by Tor Wager. If you use it, please see (and consider citing) the accompanying paper: 16 | Wager, T. D., Keller, M. C., Lacey, S. C., & Jonides, J. (2005). Increased sensitivity in neuroimaging analyses using robust regression. Neuroimage, 26(1), 99-113. 17 | 18 | ## Rationale 19 | 20 | One way to think of outliers is as observations that come from a different *generative model* (i.e., different population), with a different distribution, from the rest of the observations. With outliers, this distribution is higher variance than the distribution for the main dataset, causing observations to have more spread. Since the "pull" of observations on the regression line is proportional to the square of the distance from the line, these observations from a high-variance distribution tend to dominate if their observed values are extreme. This "pull" also depends on the *leverage*, which is a function of how extreme an observations predicted value is. High-leverage outliers can completely change regression results even in large samples. 21 | 22 | If some observations come from a different generative model with a different distribution, this violates the IID assumptions underlying classical statistical inference -- i.e., that the observations conditional on the model (i.e., the residuals) are independent and come from an identical distribution. This situation also violates the equality of variance (homoscedasticity) assumption when one is using classical P-values for inference, and can also cause violations of the normality assumption. 23 | 24 | Robust regression uses an iterative algorithm to identify observations with large residuals and down-weight them. 25 | The Robust Toolbox uses the Iteratively Reweighted Least Squares (IRLS) algorithm, with the following steps: 26 | 27 | 1. Fit the regression model using weighted least squares, with weights set to 1/leverage for each point 28 | 29 | 2. Normalize the residuals by their Median Absolute Deviation and apply a weight function based on normalized residuals. 30 | 31 | 3. Fit the regression model using weighted least squares, using weights from Step 2. 32 | Repeat Steps 2-3 until convergence. 33 | 34 | 4. Adjust variance, degrees of freedom, and P-values to account for reweighting 35 | 36 | This is quite useful for second-level (group) analyses with one contrast image per participant entered as input data. But it can also be used for first-level (time series) analysis. 37 | 38 | ## Installation and setup 39 | 40 | To use the CANlab Robust Regression toolbox, you'll need Matlab and three toolboxes on your Matlab path: SPM12, the CANlab Core Tools repository, and the CANlab Robust Regression toolbox. These tools include the sample dataset used in the robust regression help walkthrough, and other datasets as well. 41 | 42 | For help installing CANlab tools, walkthroughs, and more [canlab.github.io](https://canlab.github.io) 43 | 44 | CANlab code repositories are at [CANlab Github](https://github.com/canlab) 45 | 46 | ## Using the toolbox 47 | 48 | After installation, there are two ways to use CANlab robust regression within Matlab. The first way uses this toolbox and operates on Nifti (.nii) or Analyze (.img) files, and writes output files to disk. The second way uses CANLab objects in an object-oriented interactive framework. Both methods rely on Matlab's **robustfit** function, which implements the core method. Thus, they will produce the same results. The CANLab Core tools can be used to visualize and make tables of results obtained using either method. 49 | 50 | - The Robust toolbox includes a subfolder called **Robust_regression_walkthrough**, with a walkthrough including an example dataset for each use case. 51 | 52 | 53 | ### The classic Robust Regression toolbox 54 | 55 | - Using this toolbox takes a set of image filenames and regressors (a GLM design matrix) as input, and runs robust regression at every voxel in the dataset. 56 | - The main function in the toolbox is called **robfit**. This creates a series of directories, one for each set of images you pass in, and writes images containing maps of T-values, P-values, observation weights, and brain coverage for each analysis. A SETUP.mat file includes the design matrix, image file names, and other meta-data to track what was done. 57 | - **Output**: Each regressor is assigned a number. 0001 is the intercept, and 0002 and on are regressors you enter. Regressors are mean-centered on average so that the intercept map can be interpreted as the group average. Here are some output files: 58 | 59 | | File | Description | 60 | | -------- | ------------------------------------------------------------ | 61 | | rob_beta_0001.nii | Intercept activation values (group mean activation) | 62 | | rob_tmap_0001.nii | Intercept t-values | 63 | | rob_p_0001.nii | Intercept p-values | 64 | | rob_beta_0002.nii | Regression slopes (activation values) for first user-entered regressor | 65 | | rob_tmap_0002.nii | t-values for first user-entered regressor | 66 | | rob_p_0002.nii | p-values for first user-entered regressor | 67 | | weights.nii | 4-D image of regression weights for each input image | 68 | 69 | - **robust_results_batch** is a script that loads the files from disk into CANlab objects and generates a series of visualizations and tables. 70 | - **publish_robust_regression_report** is a script that publishes an HTML report with the results of the analysis. 71 | - **robust_regression_walkthrough_toolbox.mlx** is a Matlab live script that walks you through a sample analysis. 72 | 73 | Both of these can be customized for your application, and the code contains more information about how to use CANlab tools and generate other kinds of output. 74 | 75 | ### Object-oriented tools 76 | 77 | - The second way uses the regress() method for fmri_data in CANlab object-oriented toolbox. It does not use this toolbox, but uses the same robust regression algorithm. It returns statistic_image class objects, which can be visualized and written to disk (e.g., as Nifti files), but it does not write files to disk by default. 78 | 79 | - **robust_regression_walkthrough_objectoriented.mlx** is a Matlab live script that walks you through a sample analysis. 80 | -------------------------------------------------------------------------------- /robust_toolbox/CANlab_robust_regression_demo.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/CANlab_robust_regression_demo.mlx -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/EXPT.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/EXPT.mat -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/Results_Mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/Results_Mask.png -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/SETUP.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/SETUP.mat -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/cl_rob_p_0001_001_005_05_k5_1_1_prune.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/cl_rob_p_0001_001_005_05_k5_1_1_prune.mat -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/cl_rob_p_0002_001_005_05_k5_1_1_prune.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/cl_rob_p_0002_001_005_05_k5_1_1_prune.mat -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_p_0001.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_p_0001.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_p_0001.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_p_0001.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_p_0002.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_p_0002.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_p_0002.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_p_0002.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_z_0001.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_z_0001.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_z_0001.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_z_0001.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_z_0002.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_z_0002.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_z_0002.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/irls-ols_z_0002.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/mask.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/mask.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/mask.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/mask.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/nsubjects.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/nsubjects.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/nsubjects.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/nsubjects.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_beta_0001.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_beta_0001.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_beta_0001.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_beta_0001.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_beta_0002.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_beta_0002.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_beta_0002.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_beta_0002.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_p_0001.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_p_0001.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_p_0001.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_p_0001.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_p_0002.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_p_0002.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_p_0002.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_p_0002.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_tmap_0001.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_tmap_0001.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_tmap_0001.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_tmap_0001.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_tmap_0002.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_tmap_0002.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_tmap_0002.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/ols_tmap_0002.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0001_001_005_05_k5_1_1_prune_axial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0001_001_005_05_k5_1_1_prune_axial.png -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0001_001_005_05_k5_1_1_prune_coronal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0001_001_005_05_k5_1_1_prune_coronal.png -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0001_001_005_05_k5_1_1_prune_sagittal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0001_001_005_05_k5_1_1_prune_sagittal.png -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0002_001_005_05_k5_1_1_prune_axial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0002_001_005_05_k5_1_1_prune_axial.png -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0002_001_005_05_k5_1_1_prune_coronal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0002_001_005_05_k5_1_1_prune_coronal.png -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0002_001_005_05_k5_1_1_prune_sagittal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/orth_rob_p_0002_001_005_05_k5_1_1_prune_sagittal.png -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_beta_0001.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_beta_0001.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_beta_0001.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_beta_0001.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_beta_0002.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_beta_0002.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_beta_0002.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_beta_0002.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0001.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0001.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0001.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0001.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0001_001_005_05_k5_1_1_prune_results.txt: -------------------------------------------------------------------------------- 1 | Summary of output images: 2 | rob_p_0001.img 3 | Results clusters clpos and clneg are returned for the LAST image in this set. 4 | 5 | Printing Tables. 6 | NOTE: FULL MEDIATION TABLES DO NOT WORK YET FOR SINGLE-LEVEL RESULTS 7 | Printing abbreviated tables. 8 | Positive effects 9 | 10 | Z field contains: Robust regression predictor. (shown in maxstat) 11 | 12 | Name index x y z corr voxels volume_mm3 maxstat 13 | R1 1 10 17 27 NaN 5507 -292828 20.49 14 | Negative effects 15 | 16 | Z field contains: Robust regression predictor. (shown in maxstat) 17 | 18 | Name index x y z corr voxels volume_mm3 maxstat 19 | R1 1 -17 -45 -18 NaN 174 -9252 9.27 20 | R2 2 52 -21 22 NaN 204 -10847 8.35 21 | Summary of output images: 22 | rob_p_0001.img 23 | Results clusters clpos and clneg are returned for the LAST image in this set. 24 | 25 | Printing Tables. 26 | NOTE: FULL MEDIATION TABLES DO NOT WORK YET FOR SINGLE-LEVEL RESULTS 27 | Printing abbreviated tables. 28 | Positive effects 29 | 30 | Z field contains: Robust regression predictor. (shown in maxstat) 31 | 32 | Name index x y z corr voxels volume_mm3 maxstat 33 | R1 1 10 17 27 NaN 5507 -292828 20.49 34 | Negative effects 35 | 36 | Z field contains: Robust regression predictor. (shown in maxstat) 37 | 38 | Name index x y z corr voxels volume_mm3 maxstat 39 | R1 1 -17 -45 -18 NaN 174 -9252 9.27 40 | R2 2 52 -21 22 NaN 204 -10847 8.35 41 | -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0002.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0002.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0002.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0002.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_p_0002_001_005_05_k5_1_1_prune_results.txt: -------------------------------------------------------------------------------- 1 | Summary of output images: 2 | rob_p_0002.img 3 | Results clusters clpos and clneg are returned for the LAST image in this set. 4 | 5 | Printing Tables. 6 | NOTE: FULL MEDIATION TABLES DO NOT WORK YET FOR SINGLE-LEVEL RESULTS 7 | Printing abbreviated tables. 8 | Positive effects 9 | 10 | Z field contains: Robust regression predictor. (shown in maxstat) 11 | 12 | Name index x y z corr voxels volume_mm3 maxstat 13 | R1 1 3 -41 9 NaN 3204 -170369 19.81 14 | R2 2 -3 31 40 NaN 1579 -83961 13.27 15 | R3 3 -58 -28 32 NaN 70 -3722 10.17 16 | Negative effects 17 | 18 | Z field contains: Robust regression predictor. (shown in maxstat) 19 | 20 | Name index x y z corr voxels volume_mm3 maxstat 21 | R1 1 65 0 22 NaN 26 -1383 10.56 22 | Summary of output images: 23 | rob_p_0002.img 24 | Results clusters clpos and clneg are returned for the LAST image in this set. 25 | 26 | Printing Tables. 27 | NOTE: FULL MEDIATION TABLES DO NOT WORK YET FOR SINGLE-LEVEL RESULTS 28 | Printing abbreviated tables. 29 | Positive effects 30 | 31 | Z field contains: Robust regression predictor. (shown in maxstat) 32 | 33 | Name index x y z corr voxels volume_mm3 maxstat 34 | R1 1 3 -41 9 NaN 3204 -170369 19.81 35 | R2 2 -3 31 40 NaN 1579 -83961 13.27 36 | R3 3 -58 -28 32 NaN 70 -3722 10.17 37 | Negative effects 38 | 39 | Z field contains: Robust regression predictor. (shown in maxstat) 40 | 41 | Name index x y z corr voxels volume_mm3 maxstat 42 | R1 1 65 0 22 NaN 26 -1383 10.56 43 | -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_tmap_0001.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_tmap_0001.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_tmap_0001.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_tmap_0001.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_tmap_0002.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_tmap_0002.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_tmap_0002.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/rob_tmap_0002.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/robust_results_batch_report.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/robust_results_batch_report.txt -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/weights.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/weights.hdr -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/weights.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/Sample_analysis/robust0001/weights.img -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/robust_regression_walkthrough_objectoriented.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/robust_regression_walkthrough_objectoriented.mlx -------------------------------------------------------------------------------- /robust_toolbox/Robust_regression_walkthrough/robust_regression_walkthrough_toolbox.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/Robust_regression_walkthrough/robust_regression_walkthrough_toolbox.mlx -------------------------------------------------------------------------------- /robust_toolbox/Robust_stats_functions/robust_correlation_working.m: -------------------------------------------------------------------------------- 1 | function robust_correlation_working 2 | 3 | % robust correlation 4 | rr = robustfit(scale(x),scale(y)); rr = rr(2); 5 | 6 | nullrr = zeros(1000,1); 7 | y = scale(y); 8 | 9 | warning off 10 | for i = 1:1000 11 | xi = getRandom(y); 12 | rr = robustfit(xi,y); 13 | nullrr(i) = rr(2); 14 | 15 | end 16 | 17 | warning on 18 | 19 | -------------------------------------------------------------------------------- /robust_toolbox/Robust_stats_functions/robust_mean.m: -------------------------------------------------------------------------------- 1 | function [br,tr,pr,se, w] = robust_mean(d) 2 | % [mean,t,p,se, w] = robust_mean(d) 3 | % 4 | % takes a data matrix d and returns the robust mean 5 | % of each column, as estimated with IRLS 6 | % 7 | % also t and p values for Ho: u = 0 on each column. 8 | % 9 | % tor wager 10 | 11 | 12 | 13 | for i = 1:size(d,2) 14 | 15 | try 16 | [br(i),stats] = robustfit(ones(size(d,1),1),d(:,i),'bisquare',[],'off'); 17 | tr(i) = stats.t; 18 | pr(i) = stats.p; 19 | se(i) = stats.se; 20 | 21 | w(:, i) = stats.w; 22 | catch 23 | tr(i) = NaN; 24 | pr(i) = NaN; 25 | se(i) = NaN; 26 | br(i) = NaN; 27 | end 28 | 29 | end -------------------------------------------------------------------------------- /robust_toolbox/Robust_stats_functions/robust_nonparam_displayregions.m: -------------------------------------------------------------------------------- 1 | function [A,R,cl_extent,dat_extent,cl_extent_pos,cl_extent_neg] = robust_nonparam_displayregions(R,wh_regressor,wh_field,cortype,varargin) 2 | % [A,R,cl_extent,dat_extent,cl_extent_pos,cl_extent_neg] = robust_nonparam_displayregions(R,wh_regressor,wh_field,cortype,[optional args]) 3 | % 4 | % wh_regressor = 1; % number of regressor to get results for 5 | % wh_field = 't'; % t or f %%% Only works now for t %%% 6 | % cortype = 'svc'; % mapwise or svc 7 | % 8 | % optional: 9 | % 'overlay', followed by overlay 10 | % 'extended', get partial correlations in output tables 11 | % and save data from significant voxels in A.cl_sig for plots 12 | % 'data', followed by data matrix 13 | % (data in R.Y, R.image_names, or entered here is necessary for 14 | % extended output 15 | % 16 | % tor wager, july 2006 17 | % 18 | % TO-DO: fill in other cortype and wh_field options 19 | % step-down test option 20 | % 21 | % Example: Get regressor 3 with extended correlation output 22 | % [A,R] = robust_nonparam_displayregions(R,3,'t','svc','overlay',EXPT.overlay,'extended'); 23 | 24 | % ------------------------------------------------------------------- 25 | % Set up input arguments 26 | % ------------------------------------------------------------------- 27 | doextended = 0; 28 | overlay = which('scalped_single_subj_T1.img'); 29 | cl_extent = []; 30 | 31 | % inputs 32 | for i = 1:length(varargin) 33 | arg = varargin{i}; 34 | if ischar(arg) 35 | switch lower(arg) 36 | case 'overlay', overlay = varargin{i+1}; 37 | case 'data', R.Y = varargin{i+1}; 38 | 39 | case 'extended', doextended = 1; 40 | end 41 | end 42 | end 43 | 44 | % ------------------------------------------------------------------- 45 | % Sizes and data, print banner 46 | % ------------------------------------------------------------------- 47 | 48 | % numc = R.volInfo.c; % number of clusters 49 | % n = R.volInfo.nvox; 50 | [k,v] = size(R.correct.t); 51 | 52 | % all voxels in cluster 53 | allvox = ones(v,1); % for actual t-values : R.correct.t(wh_regressor,:)'; 54 | A.cl_all = iimg_indx2clusters(allvox,R.volInfo); 55 | 56 | % banner -- and check fields 57 | % This runs robust_nonparam_results if the critical fields are missing. 58 | % If so, that function will print a results table. 59 | [gook,R] = print_banner(R,wh_regressor,wh_field,cortype); 60 | if ~gook, return, end 61 | 62 | % ------------------------------------------------------------------- 63 | % Get significant clusters 64 | % ------------------------------------------------------------------- 65 | [A,R] = robust_nonparam_get_sig_clusters(R,wh_regressor,doextended); 66 | 67 | 68 | % ------------------------------------------------------------------- 69 | % Display images on orthviews 70 | % Setup to print tables 71 | % 72 | % Create cl_extent, dat_extent (pos or neg sig. image vectors) 73 | % dat, data at all thresholds in columns of image vector (for tables) 74 | % 75 | % Other useful outputs not used directly: cl_uncor, cl_uncor_neg 76 | % ------------------------------------------------------------------- 77 | showrois = 1; 78 | 79 | [dat,dat_pos,dat_neg,A] = robust_nonparam_orthviews(R,wh_regressor,overlay,showrois,A); 80 | 81 | dat_extent = sum(dat,2); 82 | cl_extent = iimg_indx2clusters(dat_extent,R.volInfo); 83 | 84 | cl_extent_pos = iimg_indx2clusters(sum(dat_pos,2),R.volInfo); 85 | cl_extent_neg = iimg_indx2clusters(sum(dat_neg,2),R.volInfo); 86 | 87 | 88 | 89 | % --------------------------------------------------------------------- 90 | % Make Tables 91 | % --------------------------------------------------------------------- 92 | uthr = [.005 .01 .05]; % uncorrected thresholds for display 93 | dat = [A.p_sig>0 dat]; 94 | robust_nonpar_cluster_tables(R,doextended,wh_regressor,A.cl_all,A.cl_sig,A.cl_pos,A.cl_neg,A.rfieldname,A.pfieldname,uthr,dat,cl_extent_pos,cl_extent_neg) 95 | 96 | 97 | 98 | domont = input('Make montage? '); 99 | if domont 100 | montage_clusters(overlay,A.cl_all,cl_extent_pos,cl_extent_neg,A.cl_pos,A.cl_neg,{'g' 'r' 'c' 'y' 'b'},'nooverlap'); 101 | end 102 | 103 | 104 | return 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | function [gook,R] = print_banner(R,wh_regressor,wh_field,cortype) 113 | gook = 1; 114 | 115 | if ~isfield(R,'svc') || ~isfield(R,'mapwise') 116 | R = robust_nonparam_results(R); 117 | end 118 | 119 | fprintf(1,'\n* ========================================================================') 120 | fprintf(1,'\n* robust_nonparam_displayregions ') 121 | 122 | switch wh_field 123 | case 't' 124 | fprintf(1,'\n* t-test for Regressor: %s, Correction: %s',R.names{wh_regressor},cortype) 125 | case 'f' 126 | fprintf(1,'\n* t-test for Regressor: %s, Correction: %s',R.names{wh_regressor},cortype) 127 | otherwise 128 | fprintf(1,'\n* Error: wh_field must be t or f\n') 129 | gook = 0; 130 | end 131 | 132 | fprintf(1,'\n* Tail is: %s',R.mytail) 133 | fprintf(1,'\n* Corrected alpha level is: %3.2f',R.mapwise.alpha) 134 | fprintf(1,'\n* ========================================================================\n') 135 | 136 | if ~( strcmp(cortype,'svc') || strcmp(cortype,'mapwise') ) 137 | fprintf(1,'\n* Error: cortype must be svc or mapwise\n') 138 | gook = 0; 139 | end 140 | 141 | return 142 | 143 | 144 | 145 | 146 | 147 | 148 | % these subfunctions are no longer needed because they're in 149 | % % robust_nonparam_get_sig_clusters 150 | % 151 | % % ------------------------------------------------------------------- 152 | % % DATA: check for R.Y and load if necessary. Check image names 153 | % % ------------------------------------------------------------------- 154 | % 155 | % function [R,Y] = load_data_subfunction(R) 156 | % sprintf('Loading data. '); 157 | % 158 | % if isfield(R,'Y') && ~isempty(R.Y) 159 | % disp('R.Y data field found. Using it.'); 160 | % Y = R.Y; 161 | % 162 | % if ~exist(deblank(R.maskname),'file') 163 | % fprintf(1,'\n* Error: R.maskname is not a valid file.\nImage: %s\nPlease select.\n',R.maskname) 164 | % R.maskname = spm_get(1,'*','Select new mask image'); 165 | % end 166 | % 167 | % else 168 | % % check image names and get data 169 | % 170 | % if ~exist(deblank(R.image_names(1,:)),'file') 171 | % fprintf(1,'\n* Error: R.image_names are not valid files. Assuming names are correct but path is wrong!\n') 172 | % R.image_names = filename_get_new_root_dir(R.image_names, ... 173 | % spm_get(-1,'*','Select root dir for files'),2); 174 | % end 175 | % 176 | % if ~exist(deblank(R.maskname),'file') 177 | % fprintf(1,'\n* Error: R.maskname is not a valid file.\nImage: %s\nPlease select.\n',R.maskname) 178 | % R.maskname = spm_get(1,'*','Select new mask image'); 179 | % end 180 | % 181 | % % load data and save in R 182 | % Y = iimg_get_data(R.maskname,R.image_names); 183 | % R.Y = Y; 184 | % end 185 | % if ~isfield(R,'volInfo'), R.volInfo = iimg_read_img(R.maskname); end 186 | % 187 | % return 188 | % 189 | % % ------------------------------------------------------------------- 190 | % % DATA: max partial correlation in a cluster (or set of voxels generally) 191 | % % ------------------------------------------------------------------- 192 | % 193 | % function [p,r] = get_max_partial_cor(X,Y,vox_index,wh_regressor) 194 | % 195 | % warning('off','stats:statrobustfit:IterationLimit'); 196 | % 197 | % dat = Y(:,vox_index); % for mean of region: mean(R.Y(:,in_cluster == j), 2); 198 | % 199 | % % get max partial correlation and min p 200 | % % partialcor uses robust by default 201 | % nvox = size(dat,2); 202 | % rrob = zeros(nvox,1); 203 | % prob = rrob; 204 | % for vox = 1:nvox 205 | % [x,y,rrob(vox),prob(vox)] = partialcor(X,dat(:,vox),wh_regressor); 206 | % end 207 | % 208 | % p = min(prob); 209 | % p = p(1); 210 | % r = rrob(prob == p); 211 | % r = r(1); 212 | % 213 | % warning on 214 | % return 215 | % 216 | % 217 | 218 | -------------------------------------------------------------------------------- /robust_toolbox/Robust_stats_functions/robust_reg_matrix.m: -------------------------------------------------------------------------------- 1 | function [b,t,p,sig,f,fp,fsig,stat] = robust_reg_matrix(X,Y,dochk) 2 | % [b,t,p,sig,F,fp,fsig,stat] = robust_reg_matrix(X,dat,[do checks and verbose output (1/0)]) 3 | % 4 | % takes a data matrix Y (voxels x obs.) and model matrix X ( and returns the robust reg 5 | % coefficients and other things, and significance 6 | % 7 | % X should already contain an intercept. 8 | % Empty X will perform a one-sample t-test 9 | % 10 | % F-values for full model (including intercept!) can be requested 11 | % (slower...) 12 | % 13 | % Fastest: 14 | % tic, [b,t] = robust_reg_matrix(X,Y,0); toc 15 | % 16 | % tor wager, july 06 17 | 18 | % use typical robust regression right now; slower, but easier 19 | % 20 | % compare weighted_glmfit using robust weights w with output of robustfit 21 | % betas are same, stes are larger for robustfit 22 | % 23 | % stat is a structure with: wts,se,resid, s from robust regression 24 | 25 | % ------------------------------------------------------------------- 26 | % Setup and check 27 | % ------------------------------------------------------------------- 28 | sig = []; fsig = []; 29 | 30 | if nargin < 3, dochk = 1; end 31 | 32 | [n2,v] = size(Y); 33 | if isempty(X), X = ones(n2,1); end 34 | [n,k] = size(X); 35 | 36 | if dochk 37 | if n ~= n2, error('data and model sizes do not match.'); end 38 | 39 | if no_intercept(X), error('X should contain an intercept.'); end 40 | 41 | if no_variance(Y), error('Some Y vectors have no variability. You must remove these before running.'); end 42 | end 43 | 44 | % ------------------------------------------------------------------- 45 | % Setup outputs 46 | % ------------------------------------------------------------------- 47 | b = zeros(k,v); 48 | t = b; 49 | if nargout > 2 50 | p = b; 51 | f = zeros(1,v); 52 | fp = f; 53 | end 54 | 55 | if nargout > 7 56 | stat.wts = zeros(n,v); 57 | stat.se = zeros(k,v); 58 | stat.s = zeros(1,v); 59 | stat.resid = zeros(n,v); 60 | end 61 | 62 | % ------------------------------------------------------------------- 63 | % Loop through data vectors 64 | % ------------------------------------------------------------------- 65 | warning('off','stats:statrobustfit:IterationLimit'); 66 | 67 | if dochk 68 | str = sprintf('Running robust regression for %3.0f data vectors: Done %03d%%',v,0); fprintf(1,str); 69 | updateiterations = 1:round(v ./ 100):v; 70 | updateperc = round(linspace(0,100,length(updateiterations))); 71 | end 72 | 73 | for i = 1:v 74 | 75 | % Robust version 76 | [b(:,i),stats] = robustfit(X,Y(:,i),'bisquare',[],'off'); 77 | 78 | % OLS version 79 | % [b,dev,stats] = glmfit(X,Y(:,i),'normal','constant','off'); 80 | 81 | t(:,i) = stats.t; 82 | if nargout > 2 83 | p(:,i) = stats.p; 84 | end 85 | 86 | if nargout > 4 87 | [f(:,i), fp(:,i)] = F_test_no_intercept(X,Y(:,i),stats.s); 88 | end 89 | 90 | if nargout > 7 91 | stat.wts(:,i) = stats.w; 92 | stat.se(:,i) = stats.se; 93 | stat.resid(:,i) = stats.resid; 94 | stat.s(1,i) = stats.s; 95 | end 96 | 97 | if dochk 98 | % print string 99 | update = updateiterations == i; 100 | if any(update), fprintf(1,'\b\b\b\b%03d%%',updateperc(update)); end 101 | end 102 | 103 | end 104 | 105 | if dochk 106 | erase_string(str); 107 | %fprintf(1,'\n'); 108 | 109 | sig = p < .05; 110 | end 111 | 112 | if dochk && nargout > 4 113 | fsig = fp < .05; 114 | end 115 | 116 | warning('on','stats:statrobustfit:IterationLimit'); 117 | 118 | 119 | return 120 | 121 | % ------------------------------------------------------------------- 122 | % Sub-functions 123 | % ------------------------------------------------------------------- 124 | 125 | function val = no_intercept(X) 126 | 127 | val = all(any(diff(X))); 128 | 129 | return 130 | 131 | function val = no_variance(Y) 132 | 133 | val = ~all(any(diff(Y))); 134 | 135 | return 136 | 137 | function erase_string(str1) 138 | fprintf(1,repmat('\b',1,length(str1))); % erase string 139 | return 140 | 141 | -------------------------------------------------------------------------------- /robust_toolbox/Robust_stats_functions/robust_ttest2.m: -------------------------------------------------------------------------------- 1 | function [br,tr,pr,se] = robust_ttest2(d,grp) 2 | % [mean,t,p,se] = robust_ttest2(d,grp) 3 | % 4 | % takes a data matrix d and returns a robust 2-sample t-test 5 | % on each column, as estimated with IRLS 6 | % 7 | % grp is a model matrix 8 | % grouping vector, should be [1 -1] contrast 9 | % 10 | % also t and p values for Ho: u = 0 on each column. 11 | % 12 | % tor wager 13 | 14 | 15 | 16 | for i = 1:size(d,2) 17 | 18 | try 19 | [br(i),stats] = robustfit(ones(size(d,1),1),d(:,i),'bisquare',[],'off'); 20 | tr(i) = stats.t; 21 | pr(i) = stats.p; 22 | se(i) = stats.se; 23 | catch 24 | tr(i) = NaN; 25 | pr(i) = NaN; 26 | se(i) = NaN; 27 | br(i) = NaN; 28 | end 29 | 30 | end -------------------------------------------------------------------------------- /robust_toolbox/Robust_stats_functions/t_image_ztest.m: -------------------------------------------------------------------------------- 1 | function t_image_ztest(P,df) 2 | % 3 | % do Z test comparing reliability of two t images 4 | % 5 | % t_image_ztest(P,df) 6 | % e.g., t_image_ztest([],5) 7 | % 8 | % P is string matrix of 2 t-image file names, or empty to choose with GUI 9 | % df is degrees of freedom going into each t-image (must be same), e.g, df 10 | % = 9 for 10 subject one-sample t-test. 11 | % 12 | % Warning: 13 | % The z-test here is not truly a test on normally distributed data 14 | % because it divides t-scores by their distribution standard deviation, 15 | % computes differences between standardized t-scores, and then dividides by 16 | % sqrt(2) to correct for number of tests. 17 | % Simulation shows that this test is too conservative with small df (e.g., 18 | % p-values are too high by a factor of 1.7 with 10 df, but this drops as the 19 | % threshold gets more extreme). P-values are accurate with df = 40, but at 20 | % the tails (high threshold, Z = 3) the test may become slightly anticonservative. 21 | % 22 | 23 | 24 | if isempty(P), P = spm_get(2,'*img','Choose two t-images');,end 25 | 26 | V = spm_vol(P); v = spm_read_vols(V); 27 | v(any(v == 0 | isnan(v),4)) = NaN; 28 | 29 | % * calculate Z-test for comparison 30 | % ---------------------------------------------------- 31 | 32 | % SIMULATION: shows this test is too conservative 33 | % [mn,vv] = tstat(df); 34 | %x = trnd(df,2,5000)'; x = x ./ vv; 35 | %zdiff = x(:,2) - x(:,1); zdiff = zdiff ./ sqrt(2); 36 | %[xax,h] = hist(zdiff,50); figure;plot(h,xax) 37 | %xn = randn(5000,1); 38 | %[xnx] = hist(xn,h); hold on; plot(h,xnx,'r') 39 | %figure; 40 | %h = normplot(zdiff); 41 | % SIMULATION SUGGESTS that P-values are too high by a factor of 1.7 with 10 42 | % df 43 | % for z= 1.6:.01:3 44 | %nor(end+1)=sum(xn>z) ./ length(xn); 45 | %zd(end+1)=sum(zdiff>z) ./ length(zdiff); 46 | %end 47 | %figure;plot(nor,'kx');hold on; plot(zd,'ro') 48 | %plot(nor./zd,'gs') 49 | 50 | [mn,vv] = tstat(df); 51 | 52 | zdiff = squeeze(v(:,:,:,1)) ./ sqrt(vv) - squeeze(v(:,:,:,2)) ./ sqrt(vv); % z-scores by dividing by t-distribution variance 53 | zdiff = zdiff ./ sqrt(2); % diff between z-scores is distributed with var=sum of var(z1) + var(z2) 54 | % ...thus,sigma 55 | % (z1 - z2) = sqrt(2) 56 | % e.g., - http://www.mathpages.com/home/kmath046.htm 57 | pdiff = 2 * (1 - normcdf(abs(zdiff))); % 2-tailed 58 | 59 | 60 | zthr = zdiff; zthr(abs(zdiff) < 1.96) = NaN; 61 | 62 | V = V(1); 63 | V.fname = fullfile(pwd,'z_difference.img'); 64 | V.descrip = [P(1,:) P(2,:)]; 65 | spm_write_vol(V,zdiff); 66 | 67 | V.fname = fullfile(pwd,'p_difference.img'); 68 | V.descrip = [P(1,:) P(2,:)]; 69 | spm_write_vol(V,pdiff); 70 | 71 | V.fname = fullfile(pwd,'z_diff_thresholded.img'); 72 | V.descrip = [P(1,:) P(2,:)]; 73 | spm_write_vol(V,zdiff); 74 | 75 | try, 76 | spm_image('init','z_diff_thresholded.img'); colormap jet 77 | %cl = mask2clusters('z_diff_thresholded.img'); 78 | %montage_clusters([],cl,{'r'}) 79 | % icbm_localize(cl) 80 | catch 81 | end 82 | 83 | 84 | 85 | return 86 | 87 | 88 | -------------------------------------------------------------------------------- /robust_toolbox/Robust_stats_functions/weighted_corrcoef.m: -------------------------------------------------------------------------------- 1 | function [r,xy,v,wmean] = weighted_corrcoef(x,w) 2 | % [r,xy,v,wmean] = weighted_corrcoef(x,w) 3 | % 4 | % x is m subjects by p variables data matrix 5 | % w is a vector of case weights 6 | % 7 | % r = weighted correlation coeff across columns of x 8 | % xy = weighted covariance matrix 9 | % d = weighted variance estimates for each column of x 10 | % wmean = weighted mean of each column of X 11 | % 12 | % Weights are as from IRLS robustfit, stat.w 13 | % Assumed to have mean of 1 14 | % To get from weights of inv var, e.g., subtract mean weight and add 1 15 | % 16 | % tor wager 17 | 18 | if abs(mean(w)) - 1 > eps, 19 | error('weighted corrcoef: Weights should have mean of 1'); 20 | end 21 | 22 | 23 | W = diag(w); 24 | 25 | % weighted covariance 26 | [m,n] = size(x); 27 | xc = x - repmat(sum(x)/m,m,1); % Remove mean 28 | xy = xc' * W * xc / (m-1); % sum of squared dev. / m-1 29 | 30 | xy = 0.5*(xy+xy'); % Remove rounding error 31 | 32 | 33 | %xy = cov(x) 34 | v = diag(xy); 35 | r = xy./sqrt(v*v'); 36 | 37 | v = v'; % make a row vector 38 | if nargout > 3 39 | wmean = sum(W*x) ./ m; 40 | end 41 | 42 | return 43 | 44 | % statrobustfit 45 | %[n,p] = size(X); 46 | %[Q,R,perm] = qr(X,0); 47 | %tol = abs(R(1)) * max(n,p) * eps(class(R)); 48 | %xrank = sum(abs(diag(R)) > tol) 49 | 50 | %rank(X) 51 | %dfe = n - xrank 52 | -------------------------------------------------------------------------------- /robust_toolbox/Scripts_for_comparing_ols/rob_vs_ols_imaging_compare_script.m: -------------------------------------------------------------------------------- 1 | % go to results directory for robust000* 2 | df = input('Enter degrees of freedom (n images - k parameters): '); 3 | 4 | p = dir('*0001.img'); p = str2mat(p.name) 5 | 6 | [P2,P,s,sn] = threshold_imgs(p([5 8],:),tinv(1-.005,df),0,'both'); 7 | [p2,p1] = threshold_imgs('irls-ols_z_0001.img',norminv(.95),0,'both'); % this is one tailed... 8 | 9 | %h = image_scatterplot(str2mat(P,p1),'avgvs3'); 10 | %xlabel('Average OLS and Robust t-value'), ylabel('Z-score of Robust - OLS difference') 11 | 12 | figure('Color','w') 13 | compare_filtered_t([],P2(1,:),P2(2,:),p2) 14 | 15 | disp('OLS (left), Robust (right), Difference (bottom left)') 16 | 17 | -------------------------------------------------------------------------------- /robust_toolbox/Scripts_for_comparing_ols/rob_vs_ols_matrix.m: -------------------------------------------------------------------------------- 1 | function [t,tr,tvdiff,pdiff] = rob_vs_ols_matrix(d) 2 | % [t,tr,zdiff,pdiff] = rob_vs_ols_matrix(d) 3 | % 4 | % takes a data matrix d and does t-tests on the columns, robust and OLS 5 | % plots output. 6 | 7 | b = mean(d); 8 | t = mean(d) ./ ste(d); 9 | p = 2*(1 - tcdf(t,size(d,1)-1)); 10 | 11 | for i = 1:size(d,2) 12 | 13 | [br(i),stats] = robustfit(ones(size(d,1),1),d(:,i),'bisquare',[],'off'); 14 | tr(i) = stats.t; 15 | pr(i) = stats.p; 16 | 17 | 18 | end 19 | 20 | df = (size(d,1) - 1); 21 | sigma = sqrt( (1/df) + (1/df) ); 22 | z1 = spm_t2z(t,df); 23 | z2 = spm_t2z(tr,df); % spm_t2z progressively underestimates z for large values (> 5, starts at >3) 24 | zdiff = abs((z1 - z2) ./ sigma); 25 | pdiff = 2 * (1 - normcdf(zdiff)); % 2-tailed 26 | % which is correct? 27 | 28 | [mn,v] = tstat(df); 29 | tdiff = abs(t - tr); 30 | sigma = sqrt( (v/df) + (v/df) ); % something is wrong with this one, but I'm not sure what 31 | tdiff = tdiff ./ sigma; 32 | pdiff = 2 * (1 - normcdf(tdiff)); % 2-tailed 33 | 34 | tv = t ./ sqrt(v); trv = tr ./ sqrt(v); 35 | tvdiff = abs(tv - trv) ./ sqrt(2); % z-scores by dividing by t-distribution variance; var(z1-z2) = sqrt(2) 36 | pdiff = 2 * (1 - normcdf(tvdiff)); % 2-tailed 37 | 38 | figure('Color','w'); set(gca,'FontSize',18); hold on; 39 | 40 | bar([t' tr']); colormap gray 41 | %tor_bar_steplot(t,ste(t),{'k'},-.13) 42 | %tor_bar_steplot(rmean(:,2)',rste(:,2)',{'k'},.13) 43 | %set(gca,'YLim',[.5 1]) 44 | title('T-values for OLS and IRLS') 45 | ylabel('t-score') 46 | xlabel('Task Condition') 47 | set(gca,'XLim',[0 size(d,2)+1],'XTick',1:size(d,2),'XTickLabel',1:size(d,2)) 48 | 49 | for i = 1:size(d,2), 50 | if pdiff(i) < .001, 51 | text(i-.3,max([t(i) tr(i)]) + .1,'***','FontSize',18);, 52 | elseif pdiff(i) < .01, 53 | text(i-.15,max([t(i) tr(i)]) + .1,'**','FontSize',18);, 54 | elseif pdiff(i) < .05, 55 | text(i,max([t(i) tr(i)]) + .1,'*','FontSize',18);, 56 | end 57 | end 58 | 59 | legend({'OLS' 'Robust IRLS'}) 60 | 61 | return 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /robust_toolbox/Scripts_for_comparing_ols/rob_vs_univar_matrix.m: -------------------------------------------------------------------------------- 1 | function [t,tr,tvdiff,pdiff] = rob_vs_ols_matrix(d) 2 | % [t,tr,zdiff,pdiff] = rob_vs_ols_matrix(d) 3 | % 4 | % takes a data matrix d and does t-tests on the columns, robust and OLS 5 | % plots output. 6 | 7 | b = mean(d); 8 | t = mean(d) ./ ste(d); 9 | p = 2*(1 - tcdf(t,size(d,1)-1)); 10 | 11 | for i = 1:size(d,2) 12 | 13 | z = (d(:,i) - mean(d(:,i))) ./ std(d(:,i)); wh = find(abs(z) >= 1.96); 14 | tmp = d(:,i); tmp(wh) = []; 15 | 16 | br(i) = mean(tmp); tr(i) = mean(tmp) ./ ste(tmp); pr(i) = 2*(1 - tcdf(tr(i),size(tmp,1)-1)); 17 | 18 | 19 | end 20 | 21 | df = (size(d,1) - 1); 22 | sigma = sqrt( (1/df) + (1/df) ); 23 | z1 = spm_t2z(t,df); 24 | z2 = spm_t2z(tr,df); % spm_t2z progressively underestimates z for large values (> 5, starts at >3) 25 | zdiff = abs((z1 - z2) ./ sigma); 26 | pdiff = 2 * (1 - normcdf(zdiff)); % 2-tailed 27 | % which is correct? 28 | 29 | [mn,v] = tstat(df); 30 | tdiff = abs(t - tr); 31 | sigma = sqrt( (v/df) + (v/df) ); % something is wrong with this one, but I'm not sure what 32 | tdiff = tdiff ./ sigma; 33 | pdiff = 2 * (1 - normcdf(tdiff)); % 2-tailed 34 | 35 | tv = t ./ sqrt(v); trv = tr ./ sqrt(v); 36 | tvdiff = abs(tv - trv); % z-scores by dividing by t-distribution variance 37 | pdiff = 2 * (1 - normcdf(tvdiff)); % 2-tailed 38 | 39 | figure('Color','w'); set(gca,'FontSize',18); hold on; 40 | 41 | bar([t' tr']); colormap gray 42 | %tor_bar_steplot(t,ste(t),{'k'},-.13) 43 | %tor_bar_steplot(rmean(:,2)',rste(:,2)',{'k'},.13) 44 | %set(gca,'YLim',[.5 1]) 45 | title('T-values for OLS and trimmed OLS') 46 | ylabel('t-score') 47 | xlabel('Task Condition') 48 | set(gca,'XLim',[0 size(d,2)+1],'XTick',1:size(d,2),'XTickLabel',1:size(d,2)) 49 | 50 | for i = 1:size(d,2), 51 | if pdiff(i) < .001, 52 | text(i-.3,max([t(i) tr(i)]) + .1,'***','FontSize',18);, 53 | elseif pdiff(i) < .01, 54 | text(i-.15,max([t(i) tr(i)]) + .1,'**','FontSize',18);, 55 | elseif pdiff(i) < .05, 56 | text(i,max([t(i) tr(i)]) + .1,'*','FontSize',18);, 57 | end 58 | end 59 | 60 | legend({'OLS' 'Univar'}) 61 | 62 | return 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /robust_toolbox/Scripts_for_comparing_ols/rob_vs_univar_vs_ols_matrix.m: -------------------------------------------------------------------------------- 1 | function [t,tr,tvdiff,pdiff] = rob_vs_ols_matrix(d) 2 | % [t,tr,zdiff,pdiff] = rob_vs_ols_matrix(d) 3 | % 4 | % takes a data matrix d and does t-tests on the columns, robust and OLS 5 | % plots output. 6 | 7 | b = mean(d); 8 | t = mean(d) ./ ste(d); 9 | p = 2*(1 - tcdf(t,size(d,1)-1)); 10 | 11 | for i = 1:size(d,2) 12 | 13 | z = (d(:,i) - mean(d(:,i))) ./ std(d(:,i)); wh = find(abs(z) >= 3); 14 | tmp = d(:,i); tmp(wh) = []; 15 | 16 | % trimmed univariate outliers - OLS 17 | br2(i) = mean(tmp); tr2(i) = mean(tmp) ./ ste(tmp); pr2(i) = 2*(1 - tcdf(tr2(i),size(tmp,1)-1)); 18 | 19 | % Least Trimmed Squares (univariate) 20 | %[res]=fastltsm_noplot(ones(size(d,1),1),d(:,i),struct('intercept',0,'alpha',.8)); 21 | %br2(i) = res.coefficients; tr2(i) = res.coefficients ./ (res.scale ./ sqrt(size(d,1))); 22 | %pr2(i) = 2*(1 - tcdf(tr2(i),size(d,1)-1)); 23 | 24 | 25 | % IRLS 26 | 27 | [br(i),stats] = robustfit(ones(size(d,1),1),d(:,i),'bisquare',[],'off'); 28 | tr(i) = stats.t; 29 | pr(i) = stats.p; 30 | 31 | 32 | end 33 | 34 | df = (size(d,1) - 1); 35 | sigma = sqrt( (1/df) + (1/df) ); 36 | z1 = spm_t2z(t,df); 37 | z2 = spm_t2z(tr,df); % spm_t2z progressively underestimates z for large values (> 5, starts at >3) 38 | zdiff = abs((z1 - z2) ./ sigma); 39 | pdiff = 2 * (1 - normcdf(zdiff)); % 2-tailed 40 | % which is correct? 41 | 42 | [mn,v] = tstat(df); 43 | tdiff = abs(t - tr); 44 | sigma = sqrt( (v/df) + (v/df) ); % something is wrong with this one, but I'm not sure what 45 | tdiff = tdiff ./ sigma; 46 | pdiff = 2 * (1 - normcdf(tdiff)); % 2-tailed 47 | 48 | tv = t ./ sqrt(v); trv = tr ./ sqrt(v); 49 | tvdiff = abs(tv - trv); % z-scores by dividing by t-distribution variance 50 | pdiff = 2 * (1 - normcdf(tvdiff)); % 2-tailed 51 | 52 | tv = t ./ sqrt(v); tr2v = tr2 ./ sqrt(v); 53 | tv2diff = abs(tv - tr2v); % z-scores by dividing by t-distribution variance 54 | pdiff2 = 2 * (1 - normcdf(tv2diff)); % 2-tailed 55 | 56 | 57 | figure('Color','w'); set(gca,'FontSize',18); hold on; 58 | 59 | bar([t' tr' tr2']); colormap gray 60 | %tor_bar_steplot(t,ste(t),{'k'},-.13) 61 | %tor_bar_steplot(rmean(:,2)',rste(:,2)',{'k'},.13) 62 | %set(gca,'YLim',[.5 1]) 63 | title('T-values for OLS and robust methods') 64 | ylabel('t-score') 65 | xlabel('Task Condition') 66 | set(gca,'XLim',[0 size(d,2)+1],'XTick',1:size(d,2),'XTickLabel',1:size(d,2)) 67 | 68 | for i = 1:size(d,2), 69 | if pdiff(i) < .001, 70 | text(i-.25,max([t(i) tr(i)]) + .1,'***','FontSize',18);, 71 | elseif pdiff(i) < .01, 72 | text(i-.15,max([t(i) tr(i)]) + .1,'**','FontSize',18);, 73 | elseif pdiff(i) < .05, 74 | text(i,max([t(i) tr(i)]) + .1,'*','FontSize',18);, 75 | end 76 | end 77 | 78 | for i = 1:size(d,2), 79 | if pdiff2(i) < .001, 80 | text(i-.1,max([t(i) tr2(i)]) + .1,'***','FontSize',18);, 81 | elseif pdiff2(i) < .01, 82 | text(i+.1,max([t(i) tr2(i)]) + .1,'**','FontSize',18);, 83 | elseif pdiff2(i) < .05, 84 | text(i+.2,max([t(i) tr2(i)]) + .1,'*','FontSize',18);, 85 | end 86 | end 87 | 88 | legend({'OLS' 'Robust IRLS' 'Univar'}) 89 | 90 | return 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /robust_toolbox/Simulation_scripts/robust_vs_ols_corr1.m: -------------------------------------------------------------------------------- 1 | function rob_vs_ols_corr1(it,ns) 2 | %rob_vs_ols_corr1(it,ns) 3 | % it = pop size of realizations 4 | % ns = [10 20 30] vector of group sample sizes 5 | 6 | %it = 5000; % population size 7 | 8 | nind = 1; 9 | for n = ns %[10 20 30 40] % number of subjects 10 | 11 | g = randn(n,it*2); 12 | r = []; nout = []; rr = []; 13 | 14 | for i = 1:2:size(g,2) 15 | 16 | tmp = corrcoef(g(:,i:i+1)); 17 | r(end+1) = tmp(1,2); 18 | 19 | 20 | [res]=fastmcd_noplot([g(:,i) g(:,i+1)]); 21 | % remove n most extreme outliers and recompute correlation 22 | 23 | wh = res.flag==0; nout(end+1) = sum(res.flag==0); 24 | tmp = g(:,i:i+1); tmp(wh,:) = []; 25 | tmp = corrcoef(tmp); 26 | rr(end+1) = tmp(1,2); 27 | 28 | if mod(i,100)==0, fprintf(1,'.'),end 29 | 30 | end 31 | 32 | figure('Color','w'); 33 | [h1,x]=hist(r,50); h2 = hist(rr,x); bar(h1,'b'); hold on; bar(h2,'r');alpha(.5) 34 | R{nind} = r; RR{nind} = rr; 35 | 36 | % fpr: false positive rates 37 | 38 | [rci,sig]=r2z(r,n,.05); 39 | fpr(nind) = sum(sig) ./ it; 40 | 41 | for i = 1:it 42 | [tmp,sig(i)] = r2z(rr(i),n-nout(i),.05); 43 | end 44 | sig(isnan(sig)) = []; 45 | fprr(nind) = sum(sig)./ length(sig); 46 | 47 | % number of outliers removed 48 | meanout(nind) = mean(nout); 49 | 50 | % fnr: false negative rates 51 | % not done yet 52 | 53 | % can do signal detection - Zhr - Zfar 54 | 55 | nind = nind + 1; 56 | drawnow 57 | end 58 | 59 | 60 | save robust_ols_corr_output 61 | 62 | figure;plot(fpr,'bo-','LineWidth',2) 63 | hold on;plot(fprr,'rs-','LineWidth',2) 64 | legend({'OLS' 'Robust MCD'}) 65 | set(gca,'XTick',1:length(ns),'XTickLabel',ns) 66 | saveas(gcf,'rob_vs_ols','fig') 67 | 68 | keyboard 69 | 70 | return 71 | 72 | -------------------------------------------------------------------------------- /robust_toolbox/Simulation_scripts/robust_vs_ols_corr2.m: -------------------------------------------------------------------------------- 1 | function rob_vs_ols_corr1(it,ns) 2 | %rob_vs_ols_corr1(it,ns) 3 | % it = pop size of realizations 4 | % ns = [10 20 30] vector of group sample sizes 5 | 6 | %it = 5000; % population size 7 | 8 | 9 | 10 | nind = 1; 11 | for n = ns %[10 20 30 40] % number of subjects 12 | for rep = 1:10 13 | 14 | g = randn(n,it*2); 15 | r = []; nout = []; rr = []; r2 = []; r3 = []; t2 = []; t3 = []; t = []; p = []; rt = []; 16 | rp = []; p2 = []; p3 = []; rt2 =[]; rp2 = []; rt3 =[]; rp3 = [];Mp = [];Mp2 = []; Mp3 = []; 17 | Mr = []; Mr2 = []; Mr3 = []; Mt = []; Mt2 = []; Mt3 = []; 18 | 19 | if n == 30 & rep == 1, figure('Color','w');,end 20 | 21 | for i = 1:2:size(g,2) 22 | 23 | % null hypothesis 24 | tmp = corrcoef(g(:,i:i+1)); 25 | r(end+1) = tmp(1,2); 26 | [B,dev,stat]=glmfit(g(:,i),g(:,i+1)); 27 | t(end+1) = stat.t(2); 28 | p(end+1) = stat.p(2); 29 | 30 | % first is intercept, 2nd is correlation 31 | [B,stat] = robustfit(g(:,i),g(:,i+1)); 32 | rt(end+1) = stat.t(2); 33 | rp(end+1) = stat.p(2); 34 | 35 | % robust MCD 36 | [res]=fastmcd_noplot([g(:,i) g(:,i+1)]); 37 | wh = res.flag==0; nout(end+1) = sum(res.flag==0); 38 | tmp = g(:,i:i+1); tmp(wh,:) = []; 39 | tmp2 = corrcoef(tmp); Mr(end+1) = tmp2(1,2); 40 | [B,dev,stat]=glmfit(tmp(:,1),tmp(:,2)); 41 | Mt(end+1) = stat.t(2); 42 | Mp(end+1) = stat.p(2); 43 | 44 | 45 | % alternative - d' = .5?; 46 | tmp = g(:,i) + 2 * g(:,i+1); tmp2 = corrcoef(tmp,g(:,i)); 47 | r2(end+1) = tmp2(1,2); 48 | [B,dev,stat]=glmfit(g(:,i),tmp); 49 | t2(end+1) = stat.t(2); 50 | p2(end+1) = stat.p(2); 51 | 52 | [B,stat] = robustfit(g(:,i),tmp); 53 | rt2(end+1) = stat.t(2); 54 | rp2(end+1) = stat.p(2); 55 | 56 | [res]=fastmcd_noplot([g(:,i) tmp]); 57 | wh = res.flag==0; nout(end+1) = sum(res.flag==0); 58 | tmp3 = [g(:,i) tmp]; tmp3(wh,:) = []; 59 | tmp2 = corrcoef(tmp3); Mr2(end+1) = tmp2(1,2); 60 | [B,dev,stat]=glmfit(tmp3(:,1),tmp3(:,2)); 61 | Mt2(end+1) = stat.t(2); 62 | Mp2(end+1) = stat.p(2); 63 | 64 | 65 | % alternative with 2 outliers 66 | tmp(1:2) = tmp(1:2) + 10 * randn(2,1); tmp2 = corrcoef(tmp,g(:,i)); 67 | r3(end+1) = tmp2(1,2); 68 | [B,dev,stat]=glmfit(g(:,i),tmp); 69 | t3(end+1) = stat.t(2); 70 | p3(end+1) = stat.p(2); 71 | 72 | [B,stat] = robustfit(g(:,i),tmp); 73 | rt3(end+1) = stat.t(2); 74 | rp3(end+1) = stat.p(2); 75 | 76 | [res]=fastmcd_noplot([g(:,i) tmp]); 77 | wh = res.flag==0; nout(end+1) = sum(res.flag==0); 78 | tmp3 = [g(:,i) tmp]; tmp3(wh,:) = []; 79 | tmp2 = corrcoef(tmp3); Mr3(end+1) = tmp2(1,2); 80 | clear stat 81 | [B,dev,stat]=glmfit(tmp3(:,1),tmp3(:,2)); 82 | Mt3(end+1) = stat.t(2); 83 | Mp3(end+1) = stat.p(2); 84 | 85 | if mod(i,100)==0, fprintf(1,'.'),end 86 | 87 | end 88 | if n == 30 & rep == 1, 89 | figure('Color','w') 90 | subplot(1,3,1),tmp3 = [];tmp3{1} = []; 91 | [h1,x]=hist(t,50); h2 = hist(rt,x); bar(h1,'b'); hold on; bar(h2,'r');alpha(.5) 92 | tmp = 1:10:length(x);, for ii = 1:length(tmp),tmp3{ii}=sprintf('%3.2f',x(tmp(ii)));,end 93 | set(gca,'XTick',tmp,'XTickLabel',tmp3,'FontSize',16) 94 | T{nind} = t; RT{nind} = rt; legend({'OLS' 'Robust IRLS' 'Robust MCD'}) 95 | P{nind} = p; RP{nind} = rp; 96 | title(['No effect: Ho, n = ' num2str(n)]) 97 | 98 | subplot(1,3,2) 99 | [h1,x]=hist(t2,50); h2 = hist(rt2,x); bar(h1,'b'); hold on; bar(h2,'r');alpha(.5) 100 | for ii = 1:length(tmp),tmp3{ii}=sprintf('%3.2f',x(tmp(ii)));,end 101 | set(gca,'XTick',tmp,'XTickLabel',tmp3,'FontSize',16) 102 | T2{nind} = t2; RT2{nind} = rt2; %legend({'OLS' 'Robust IRLS'}) 103 | P2{nind} = p2; RP2{nind} = rp2; 104 | title(['True effect: H1']) 105 | 106 | subplot(1,3,3) 107 | [h1,x]=hist(t3,50); h2 = hist(rt3,x); bar(h1,'b'); hold on; bar(h2,'r');alpha(.5) 108 | tmp = get(gca,'XTick'); tmp = 1:10:50; 109 | for ii = 1:length(tmp),tmp3{ii}=sprintf('%3.2f',x(tmp(ii)));,end 110 | set(gca,'XTick',tmp,'XTickLabel',tmp3,'FontSize',16) 111 | T3{nind} = t3; RT3{nind} = rt3; %legend({'OLS' 'Robust IRLS'}) 112 | P3{nind} = p3; RP3{nind} = rp3; 113 | title(['H1 with 2 outliers']) 114 | drawnow 115 | end 116 | % fpr: false positive rates and false neg rates 117 | 118 | fpr(rep,nind) = sum(p < .05) ./ it; 119 | fprr(rep,nind) = sum(rp < .05)./ (it); 120 | fprM(rep,nind) = sum(Mp < .05)./ (it); 121 | 122 | fpr2(rep,nind) = sum(p2 < .05) ./ it; 123 | fprr2(rep,nind) = sum(rp2 < .05)./ (it); 124 | fprM2(rep,nind) = sum(Mp2 < .05)./ (it); 125 | 126 | fpr3(rep,nind) = sum(p3 < .05) ./ it; 127 | fprr3(rep,nind) = sum(rp3 < .05)./ (it); 128 | fprM3(rep,nind) = sum(Mp3 < .05)./ (it); 129 | 130 | % number of outliers removed 131 | %meanout(nind) = mean(nout); 132 | 133 | % fnr: false negative rates 134 | % not done yet 135 | 136 | % can do signal detection - Zhr - Zfar 137 | 138 | end % reps 139 | 140 | nind = nind + 1; 141 | drawnow 142 | end 143 | 144 | save robust_ols_corr_output2 145 | 146 | figure('Color','w'); subplot(1,3,1);set(gca,'FontSize',16) 147 | plot(mean(fpr),'bo-','LineWidth',2) 148 | hold on;plot(mean(fprr),'rs-','LineWidth',2) 149 | legend({'OLS' 'Robust IRLS' 'Robust MCD'}) 150 | set(gca,'XTick',1:length(ns),'XTickLabel',ns) 151 | plot(mean(fprM),'g^-','LineWidth',2) 152 | tor_bar_steplot(mean(fpr),std(fpr)./sqrt(10),{'b'}) 153 | tor_bar_steplot(mean(fprr),std(fprr)./sqrt(10),{'r'}) 154 | 155 | 156 | tor_bar_steplot(mean(fprM),std(fprM)./sqrt(10),{'g'}) 157 | 158 | title('False positive rate','FontSize',16) 159 | 160 | hh(1) = subplot(1,3,2);set(gca,'FontSize',16) 161 | plot(mean(fpr2),'bo-','LineWidth',2) 162 | hold on;plot(mean(fprr2),'rs-','LineWidth',2) 163 | set(gca,'XTick',1:length(ns),'XTickLabel',ns) 164 | tor_bar_steplot(mean(fpr2),std(fpr)./sqrt(10),{'b'}) 165 | tor_bar_steplot(mean(fprr2),std(fprr)./sqrt(10),{'r'}) 166 | 167 | plot(mean(fprM2),'g^-','LineWidth',2) 168 | tor_bar_steplot(mean(fprM2),std(fprM2)./sqrt(10),{'g'}) 169 | 170 | title(['Power, r = ' sprintf('%3.2f',mean(r2))],'FontSize',16) 171 | 172 | xlabel('Number of subjects (n)') 173 | 174 | hh(2) = subplot(1,3,3);set(gca,'FontSize',16) 175 | plot(mean(fpr3),'bo-','LineWidth',2) 176 | hold on;plot(mean(fprr3),'rs-','LineWidth',2) 177 | plot(mean(fprM3),'g^-','LineWidth',2) 178 | set(gca,'XTick',1:length(ns),'XTickLabel',ns) 179 | tor_bar_steplot(mean(fpr3),std(fpr)./sqrt(10),{'b'}) 180 | tor_bar_steplot(mean(fprr3),std(fprr)./sqrt(10),{'r'}) 181 | 182 | 183 | tor_bar_steplot(mean(fprM3),std(fprM3)./sqrt(10),{'g'}) 184 | 185 | title(['Power: r = ' sprintf('%3.2f',mean(r3)) ' (2 outliers)'],'FontSize',16) 186 | equalize_axes(hh) 187 | 188 | saveas(gcf,'rob_vs_ols2','fig') 189 | 190 | return 191 | 192 | -------------------------------------------------------------------------------- /robust_toolbox/Simulation_scripts/robust_vs_ols_test1.m: -------------------------------------------------------------------------------- 1 | n = 20; 2 | iter = 500; 3 | 4 | % UNDER THE NULL HYPOTHESIS 5 | 6 | clear b, clear t, clear br, clear tr 7 | 8 | t1 = clock; 9 | for i = 1:iter 10 | 11 | x = randn(n,1); y = randn(size(x)); X = [x ones(size(x))]; 12 | [bb,stats]=robustfit(x,y); br(i) = bb(2); tr(i)=stats.t(2);, 13 | [bb,bint,r,rint,stats]=regress(y,X);,b(i)=bb(1); t(i)=stats(2).^.5 * sign(bb(1)); 14 | 15 | end 16 | fprintf(1,['Time elapsed for %3.0f iterations is %4.2f s\n'],iter,etime(clock,t1)) 17 | 18 | [h,xxb] = hist([b br],20); 19 | [h,xxt] = hist([t tr],20); 20 | xxb = [xxb(1)-xxb(end):mean(diff(xxb)):xxb(1)-mean(diff(xxb)) xxb xxb(end):mean(diff(xxb)):max(xxb).*2]; 21 | xxt = [xxt(1)-xxt(end):mean(diff(xxt)):xxt(1)-mean(diff(xxt)) xxt xxt(end):mean(diff(xxt)):max(xxt).*2]; 22 | 23 | figure('Color','w'); subplot 221; [h] = hist(b,xxb); plot(xxb,h./sum(h)),title('OLS betas') 24 | 25 | subplot 222; [h] = hist(br,xxb); plot(xxb,h./sum(h)),title('Robust IRLS betas') 26 | 27 | subplot 223; [h] = hist(t,xxt); plot(xxt,h./sum(h)),title('OLS t-scores') 28 | 29 | subplot 224; [h] = hist(tr,xxt); plot(xxt,h./sum(h)),title('Robust IRLS t-scores') 30 | 31 | diary robust_vs_ols_test1.txt 32 | fprintf(1,'Normal Ho Data, simulation of n = %3.0f\n----------------------------------------------------------------\n',n) 33 | fprintf(1,'Bias under : %3.4f for OLS and %3.4f for IRLS\n',mean(b)-1,mean(br)-1) 34 | fprintf(1,'SE under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement in beta stability for IRLS\n',std(b),std(br),100*(std(b)./std(br))) 35 | fprintf(1,'Mean t-score under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',mean(t),mean(tr),100*(mean(t)./mean(tr))) 36 | fprintf(1,'SE of t-scores under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',std(t),std(tr),100*(std(t)./std(tr))) 37 | diary off 38 | 39 | 40 | for i = 1:iter 41 | 42 | x = randn(n,1); y = randn(size(x)); X = [x ones(size(x))]; 43 | 44 | % add an outlier 45 | y(1) = y(1) + randn(1) * 10; 46 | mystd(i) = max((y - mean(y)) ./ std(y(2:end))); 47 | 48 | [bb,stats]=robustfit(x,y); br(i) = bb(2); tr(i)=stats.t(2);, 49 | [bb,bint,r,rint,stats]=regress(y,X);,b(i)=bb(1); t(i)=stats(2).^.5 * sign(bb(1)); 50 | 51 | end 52 | 53 | 54 | hh(1)=subplot(2,2,1); hold on; [h] = hist(b,xxb); plot(xxb,h./sum(h),'r'),title('OLS betas') 55 | legend({'Normal Ho data' 'Ho with 1 outlier'}) 56 | 57 | hh(2)=subplot(2,2,2); hold on; [h] = hist(br,xxb); plot(xxb,h./sum(h),'r'),title('Robust IRLS betas') 58 | 59 | hh(3)=subplot(2,2,3); hold on; [h] = hist(t,xxt); plot(xxt,h./sum(h),'r'),title('OLS t-scores') 60 | 61 | hh(4)=subplot(2,2,4); hold on; [h] = hist(tr,xxt); plot(xxt,h./sum(h),'r'),title('Robust IRLS t-scores') 62 | 63 | equalize_axes(hh(1:2)); equalize_axes(hh(3:4)); 64 | legend({'Normal Ho data' 'Ho with 1 outlier'}) 65 | 66 | diary on 67 | fprintf(1,'Normal Ho Data with Outlier at %3.2f st. deviations, simulation of n = %3.0f\n----------------------------------------------------------------\n',mean(mystd),n) 68 | fprintf(1,'Bias under : %3.4f for OLS and %3.4f for IRLS\n',mean(b)-1,mean(br)-1) 69 | fprintf(1,'SE under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement in beta stability for IRLS\n',std(b),std(br),100*(std(b)./std(br))) 70 | fprintf(1,'Mean t-score under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',mean(t),mean(tr),100*(mean(t)./mean(tr))) 71 | fprintf(1,'SE of t-scores under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',std(t),std(tr),100*(std(t)./std(tr))) 72 | diary off 73 | 74 | 75 | 76 | 77 | 78 | % UNDER THE ALTERNATIVE HYPOTHESIS, WITH SNR = 1 79 | 80 | clear b, clear t, clear br, clear tr 81 | 82 | for i = 1:iter 83 | 84 | x = randn(n,1); y = x + randn(size(x)); X = [x ones(size(x))]; 85 | [bb,stats]=robustfit(x,y); br(i) = bb(2); tr(i)=stats.t(2);, 86 | [bb,bint,r,rint,stats]=regress(y,X);,b(i)=bb(1); t(i)=stats(2).^.5 * sign(bb(1)); 87 | 88 | end 89 | 90 | [h,xxb] = hist([b br],20); 91 | [h,xxt] = hist([t tr],20); 92 | xxb = [xxb(1)-xxb(end):mean(diff(xxb)):xxb(1)-mean(diff(xxb)) xxb xxb(end):mean(diff(xxb)):max(xxb).*2]; 93 | xxt = [xxt(1)-xxt(end):mean(diff(xxt)):xxt(1)-mean(diff(xxt)) xxt xxt(end):mean(diff(xxt)):max(xxt).*2]; 94 | 95 | figure('Color','w'); subplot 221; [h] = hist(b,xxb); plot(xxb,h./sum(h)),title('OLS betas') 96 | 97 | subplot 222; [h] = hist(br,xxb); plot(xxb,h./sum(h)),title('Robust IRLS betas') 98 | 99 | subplot 223; [h] = hist(t,xxt); plot(xxt,h./sum(h)),title('OLS t-scores') 100 | 101 | subplot 224; [h] = hist(tr,xxt); plot(xxt,h./sum(h)),title('Robust IRLS t-scores') 102 | 103 | diary on 104 | fprintf(1,'Normal Ha Data, SNR = 1, simulation of n = %3.0f\n----------------------------------------------------------------\n',n) 105 | fprintf(1,'Bias under Ha: %3.4f for OLS and %3.4f for IRLS\n',mean(b)-1,mean(br)-1) 106 | fprintf(1,'SE under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement in beta stability for IRLS\n',std(b),std(br),100*(std(b)./std(br))) 107 | fprintf(1,'Mean t-score under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',mean(t),mean(tr),100*(mean(t)./mean(tr))) 108 | fprintf(1,'SE of t-scores under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',std(t),std(tr),100*(std(t)./std(tr))) 109 | diary off 110 | 111 | 112 | 113 | for i = 1:iter 114 | 115 | x = randn(n,1); y = x + randn(size(x)); X = [x ones(size(x))]; 116 | 117 | % add an outlier 118 | y(1) = y(1) + randn(1) * 10; 119 | mystd(i) = max((y - mean(y)) ./ std(y(2:end))); 120 | 121 | [bb,stats]=robustfit(x,y); br(i) = bb(2); tr(i)=stats.t(2);, 122 | [bb,bint,r,rint,stats]=regress(y,X);,b(i)=bb(1); t(i)=stats(2).^.5 * sign(bb(1)); 123 | 124 | end 125 | 126 | hh(1)=subplot(2,2,1); hold on; [h] = hist(b,xxb); plot(xxb,h./sum(h),'r'),title('OLS betas') 127 | legend({'Normal Ha data' 'Ha with 1 outlier'}) 128 | 129 | hh(2)=subplot(2,2,2); hold on; [h] = hist(br,xxb); plot(xxb,h./sum(h),'r'),title('Robust IRLS betas') 130 | 131 | hh(3)=subplot(2,2,3); hold on; [h] = hist(t,xxt); plot(xxt,h./sum(h),'r'),title('OLS t-scores') 132 | 133 | hh(4)=subplot(2,2,4); hold on; [h] = hist(tr,xxt); plot(xxt,h./sum(h),'r'),title('Robust IRLS t-scores') 134 | 135 | equalize_axes(hh(1:2)); equalize_axes(hh(3:4)); 136 | legend({'Normal Ha data' 'Ha with 1 outlier'}) 137 | 138 | diary on 139 | fprintf(1,'Normal Ha Data (SNR = 1) with Outlier at %3.2f st. deviations, simulation of n = %3.0f\n----------------------------------------------------------------\n',mean(mystd),n) 140 | fprintf(1,'Bias under Ha: %3.4f for OLS and %3.4f for IRLS\n',mean(b)-1,mean(br)-1) 141 | fprintf(1,'SE under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement in beta stability for IRLS\n',std(b),std(br),100*(std(b)./std(br))) 142 | fprintf(1,'Mean t-score under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',mean(t),mean(tr),100*(mean(t)./mean(tr))) 143 | fprintf(1,'SE of t-scores under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',std(t),std(tr),100*(std(t)./std(tr))) 144 | diary off 145 | -------------------------------------------------------------------------------- /robust_toolbox/Simulation_scripts/robust_vs_ols_test1.txt: -------------------------------------------------------------------------------- 1 | Normal Ho Data, simulation of n = 20 2 | ---------------------------------------------------------------- 3 | Bias under : -0.9993 for OLS and -1.0019 for IRLS 4 | SE under : 0.2426 for OLS and 0.2561 for IRLS: 94.73% improvement in beta stability for IRLS 5 | Mean t-score under : 0.0079 for OLS and -0.0055 for IRLS: -144.25% improvement for IRLS 6 | SE of t-scores under : 1.0606 for OLS and 1.1071 for IRLS: 95.80% improvement for IRLS 7 | Normal Ho Data with Outlier at 5.21 st. deviations, simulation of n = 20 8 | ---------------------------------------------------------------- 9 | Bias under : -0.9990 for OLS and -1.0030 for IRLS 10 | SE under : 0.5898 for OLS and 0.2645 for IRLS: 222.97% improvement in beta stability for IRLS 11 | Mean t-score under : -0.0058 for OLS and -0.0125 for IRLS: 46.66% improvement for IRLS 12 | SE of t-scores under : 1.0642 for OLS and 0.9063 for IRLS: 117.43% improvement for IRLS 13 | Normal Ha Data, SNR = 1, simulation of n = 20 14 | ---------------------------------------------------------------- 15 | Bias under Ha: -0.0003 for OLS and 0.0002 for IRLS 16 | SE under Ha: 0.2437 for OLS and 0.2565 for IRLS: 95.03% improvement in beta stability for IRLS 17 | Mean t-score under Ha: 4.4914 for OLS and 4.3738 for IRLS: 102.69% improvement for IRLS 18 | SE of t-scores under Ha: 1.5366 for OLS and 1.5924 for IRLS: 96.50% improvement for IRLS 19 | Normal Ha Data (SNR = 1) with Outlier at 4.04 st. deviations, simulation of n = 20 20 | ---------------------------------------------------------------- 21 | Bias under Ha: 0.0033 for OLS and -0.0026 for IRLS 22 | SE under Ha: 0.6054 for OLS and 0.2695 for IRLS: 224.61% improvement in beta stability for IRLS 23 | Mean t-score under Ha: 2.5403 for OLS and 3.3882 for IRLS: 74.97% improvement for IRLS 24 | SE of t-scores under Ha: 1.6865 for OLS and 1.4077 for IRLS: 119.80% improvement for IRLS 25 | -------------------------------------------------------------------------------- /robust_toolbox/Simulation_scripts/robust_vs_ols_test2.m: -------------------------------------------------------------------------------- 1 | n = 20; 2 | iter = 500; 3 | 4 | % UNDER THE NULL HYPOTHESIS 5 | 6 | clear b, clear t, clear br, clear tr 7 | 8 | t1 = clock; 9 | for i = 1:iter 10 | 11 | x = randn(n,1); y = randn(size(x)); X = [x ones(size(x))]; 12 | %[bb,stats]=robustfit(x,y); 13 | [res,raw] = fastltsm_noplot(x,y); bb = res.coefficients; 14 | br(i) = bb(2); tr(i) = 0; %tr(i)=stats.t(2);, 15 | [bb,bint,r,rint,stats]=regress(y,X);,b(i)=bb(1); t(i)=stats(2).^.5 * sign(bb(1)); 16 | 17 | end 18 | fprintf(1,['Time elapsed for %3.0f iterations is %4.2f s\n'],iter,etime(clock,t1)) 19 | 20 | [h,xxb] = hist([b br],20); 21 | [h,xxt] = hist([t tr],20); 22 | xxb = [xxb(1)-xxb(end):mean(diff(xxb)):xxb(1)-mean(diff(xxb)) xxb xxb(end):mean(diff(xxb)):max(xxb).*2]; 23 | xxt = [xxt(1)-xxt(end):mean(diff(xxt)):xxt(1)-mean(diff(xxt)) xxt xxt(end):mean(diff(xxt)):max(xxt).*2]; 24 | 25 | figure('Color','w'); subplot 221; [h] = hist(b,xxb); plot(xxb,h./sum(h)),title('OLS betas') 26 | 27 | subplot 222; [h] = hist(br,xxb); plot(xxb,h./sum(h)),title('Robust IRLS betas') 28 | 29 | subplot 223; [h] = hist(t,xxt); plot(xxt,h./sum(h)),title('OLS t-scores') 30 | 31 | subplot 224; [h] = hist(tr,xxt); plot(xxt,h./sum(h)),title('Robust IRLS t-scores') 32 | 33 | diary robust_vs_ols_test1.txt 34 | fprintf(1,'Normal Ho Data, simulation of n = %3.0f\n----------------------------------------------------------------\n',n) 35 | fprintf(1,'Bias under : %3.4f for OLS and %3.4f for IRLS\n',mean(b)-1,mean(br)-1) 36 | fprintf(1,'SE under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement in beta stability for IRLS\n',std(b),std(br),100*(std(b)./std(br))) 37 | fprintf(1,'Mean t-score under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',mean(t),mean(tr),100*(mean(t)./mean(tr))) 38 | fprintf(1,'SE of t-scores under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',std(t),std(tr),100*(std(t)./std(tr))) 39 | diary off 40 | 41 | 42 | for i = 1:iter 43 | 44 | x = randn(n,1); y = randn(size(x)); X = [x ones(size(x))]; 45 | 46 | % add an outlier 47 | y(1) = y(1) + randn(1) * 10; 48 | mystd(i) = max((y - mean(y)) ./ std(y(2:end))); 49 | 50 | [bb,stats]=robustfit(x,y); br(i) = bb(2); tr(i)=stats.t(2);, 51 | [res,raw] = fastltsm_noplot(x,y); bb = res.coefficients; 52 | br(i) = bb(2); tr(i) = 0; %tr(i)=stats.t(2);, 53 | [bb,bint,r,rint,stats]=regress(y,X);,b(i)=bb(1); t(i)=stats(2).^.5 * sign(bb(1)); 54 | 55 | end 56 | 57 | 58 | hh(1)=subplot(2,2,1); hold on; [h] = hist(b,xxb); plot(xxb,h./sum(h),'r'),title('OLS betas') 59 | legend({'Normal Ho data' 'Ho with 1 outlier'}) 60 | 61 | hh(2)=subplot(2,2,2); hold on; [h] = hist(br,xxb); plot(xxb,h./sum(h),'r'),title('Robust IRLS betas') 62 | 63 | hh(3)=subplot(2,2,3); hold on; [h] = hist(t,xxt); plot(xxt,h./sum(h),'r'),title('OLS t-scores') 64 | 65 | hh(4)=subplot(2,2,4); hold on; [h] = hist(tr,xxt); plot(xxt,h./sum(h),'r'),title('Robust IRLS t-scores') 66 | 67 | equalize_axes(hh(1:2)); equalize_axes(hh(3:4)); 68 | legend({'Normal Ho data' 'Ho with 1 outlier'}) 69 | 70 | diary on 71 | fprintf(1,'Normal Ho Data with Outlier at %3.2f st. deviations, simulation of n = %3.0f\n----------------------------------------------------------------\n',mean(mystd),n) 72 | fprintf(1,'Bias under : %3.4f for OLS and %3.4f for IRLS\n',mean(b)-1,mean(br)-1) 73 | fprintf(1,'SE under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement in beta stability for IRLS\n',std(b),std(br),100*(std(b)./std(br))) 74 | fprintf(1,'Mean t-score under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',mean(t),mean(tr),100*(mean(t)./mean(tr))) 75 | fprintf(1,'SE of t-scores under : %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',std(t),std(tr),100*(std(t)./std(tr))) 76 | diary off 77 | 78 | 79 | 80 | 81 | 82 | % UNDER THE ALTERNATIVE HYPOTHESIS, WITH SNR = 1 83 | 84 | clear b, clear t, clear br, clear br2, clear tr 85 | 86 | for i = 1:iter 87 | 88 | x = randn(n,1); y = x + randn(size(x)); X = [x ones(size(x))]; 89 | [bb,stats]=robustfit(x,y); br(i) = bb(2); tr(i)=stats.t(2);, 90 | [bb,bint,r,rint,stats]=regress(y,X);,b(i)=bb(1); t(i)=stats(2).^.5 * sign(bb(1)); 91 | 92 | end 93 | 94 | [h,xxb] = hist([b br],20); 95 | [h,xxt] = hist([t tr],20); 96 | xxb = [xxb(1)-xxb(end):mean(diff(xxb)):xxb(1)-mean(diff(xxb)) xxb xxb(end):mean(diff(xxb)):max(xxb).*2]; 97 | xxt = [xxt(1)-xxt(end):mean(diff(xxt)):xxt(1)-mean(diff(xxt)) xxt xxt(end):mean(diff(xxt)):max(xxt).*2]; 98 | 99 | figure('Color','w'); subplot 221; [h] = hist(b,xxb); plot(xxb,h./sum(h)),title('OLS betas') 100 | 101 | subplot 222; [h] = hist(br,xxb); plot(xxb,h./sum(h)),title('Robust IRLS betas') 102 | 103 | subplot 223; [h] = hist(t,xxt); plot(xxt,h./sum(h)),title('OLS t-scores') 104 | 105 | subplot 224; [h] = hist(tr,xxt); plot(xxt,h./sum(h)),title('Robust IRLS t-scores') 106 | 107 | diary on 108 | fprintf(1,'Normal Ha Data, SNR = 1, simulation of n = %3.0f\n----------------------------------------------------------------\n',n) 109 | fprintf(1,'Bias under Ha: %3.4f for OLS and %3.4f for IRLS\n',mean(b)-1,mean(br)-1) 110 | fprintf(1,'SE under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement in beta stability for IRLS\n',std(b),std(br),100*(std(b)./std(br))) 111 | fprintf(1,'Mean t-score under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',mean(t),mean(tr),100*(mean(t)./mean(tr))) 112 | fprintf(1,'SE of t-scores under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',std(t),std(tr),100*(std(t)./std(tr))) 113 | diary off 114 | 115 | % this is the critical one for testing lts 116 | 117 | for i = 1:iter 118 | 119 | x = randn(n,1); y = x + randn(size(x)); X = [x ones(size(x))]; 120 | 121 | % add an outlier 122 | y(1) = y(1) + randn(1) * 10; 123 | mystd(i) = max((y - mean(y)) ./ std(y(2:end))); 124 | 125 | [bb,stats]=robustfit(x,y); br(i) = bb(2); tr(i)=stats.t(2);, 126 | [res,raw] = fastltsm_noplot(x,y); bb = res.coefficients; 127 | br2(i) = bb(1); br2int(i) = bb(2); 128 | [bb,bint,r,rint,stats]=regress(y,X);,b(i)=bb(1); t(i)=stats(2).^.5 * sign(bb(1)); 129 | 130 | end 131 | 132 | hh(1)=subplot(1,3,1); hold on; [h] = hist(b,xxb); plot(xxb,h./sum(h),'r'),title('OLS betas') 133 | legend({'Normal Ha data' 'Ha with 1 outlier'}) 134 | 135 | hh(2)=subplot(1,3,2); hold on; [h] = hist(br,xxb); plot(xxb,h./sum(h),'r'),title('Robust IRLS betas') 136 | 137 | hh(3)=subplot(1,3,3); hold on; [h] = hist(br2,xxb); plot(xxb,h./sum(h),'r'),title('Robust LTS betas') 138 | 139 | %hh(4)=subplot(2,2,4); hold on; [h] = hist(tr,xxt); plot(xxt,h./sum(h),'r'),title('Robust IRLS t-scores') 140 | 141 | equalize_axes(hh(1:3)); %equalize_axes(hh(3:4)); 142 | legend({'Normal Ha data' 'Ha with 1 outlier'}) 143 | 144 | diary on 145 | fprintf(1,'Normal Ha Data (SNR = 1) with Outlier at %3.2f st. deviations, simulation of n = %3.0f\n----------------------------------------------------------------\n',mean(mystd),n) 146 | fprintf(1,'Bias under Ha: %3.4f for OLS and %3.4f for IRLS\n',mean(b)-1,mean(br)-1) 147 | fprintf(1,'SE under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement in beta stability for IRLS\n',std(b),std(br),100*(std(b)./std(br))) 148 | fprintf(1,'Mean t-score under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',mean(t),mean(tr),100*(mean(t)./mean(tr))) 149 | fprintf(1,'SE of t-scores under Ha: %3.4f for OLS and %3.4f for IRLS: %3.2f%% improvement for IRLS\n',std(t),std(tr),100*(std(t)./std(tr))) 150 | diary off 151 | -------------------------------------------------------------------------------- /robust_toolbox/older_functions/ROBUST_REGRESSION_HELP.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/older_functions/ROBUST_REGRESSION_HELP.doc -------------------------------------------------------------------------------- /robust_toolbox/older_functions/robust_act_corr_intersection_2008.m: -------------------------------------------------------------------------------- 1 | function [cl_me_pos, cl_me_neg, ... 2 | cl_mepos_covpos, cl_mepos_covneg, cl_meneg_covpos, cl_meneg_covneg] ... 3 | = robust_act_corr_intersection_2008(me_pthresh, cov_pthresh, size_thresh) 4 | %robust_act_corr_intersection_2008(me_pthresh, cov_pthresh, size_thresh) 5 | % 6 | % Documentation not complete. 7 | % Tor Wager, Nov 2008 8 | % 9 | % Example: 10 | % [cl_me_pos, cl_me_neg, ... 11 | % cl_mepos_covpos, cl_mepos_covneg, cl_meneg_covpos, cl_meneg_covneg] ... 12 | % = robust_act_corr_intersection_2008(me_pthresh, cov_pthresh) 13 | 14 | 15 | me_image = 'rob_p_0001.img'; % the input image used to define the mask 16 | me_sign_image = 'rob_tmap_0001.img'; 17 | %me_pthresh = .005; % the p-value threshold used to define the mask 18 | me_mask = 'main_effect_mask.img'; % the output image 19 | 20 | cov_image = 'rob_p_0002.img'; % the covariate image we want to mask 21 | %cov_pthresh = .05; % the p-value threshold used to define the mask 22 | cov_sign_image = 'rob_tmap_0002.img'; 23 | 24 | % me_mask = 'main_effect_mask.img'; % the output image 25 | 26 | %size_thresh = 10; 27 | 28 | cov_pvals_me_mask = mask_image(cov_image, me_image, me_mask, 'maxmask', me_pthresh); 29 | % These are only covariate p-values with significant group effects 30 | cov_pvals_me_mask(cov_pvals_me_mask == 0) = NaN; 31 | 32 | % Cov p-values with significant effects in both me and cov 33 | cov_pvals_masked = cov_pvals_me_mask; 34 | cov_pvals_masked(cov_pvals_masked > cov_pthresh) = NaN; 35 | 36 | % now we need to get information about the signs of each effect 37 | me_tvals = spm_read_vols(spm_vol(me_sign_image)); 38 | cov_tvals = spm_read_vols(spm_vol(cov_sign_image)); 39 | 40 | V = spm_vol(me_mask); 41 | 42 | % main effects clusters 43 | 44 | cl_me_pos = mask2clusters(double(cov_pvals_me_mask > 0 & me_tvals > 0), V); 45 | cl_me_neg = mask2clusters(double(cov_pvals_me_mask > 0 & me_tvals < 0), V); 46 | 47 | % covariate clusters 48 | cl_mepos_covpos = mask2clusters(cov_pvals_masked .* (me_tvals > 0) .* (cov_tvals > 0), V); 49 | cl_mepos_covneg = mask2clusters(cov_pvals_masked .* (me_tvals > 0) .* (cov_tvals < 0), V); 50 | 51 | cl_meneg_covpos = mask2clusters(cov_pvals_masked .* (me_tvals < 0) .* (cov_tvals > 0), V); 52 | cl_meneg_covneg = mask2clusters(cov_pvals_masked .* (me_tvals < 0) .* (cov_tvals < 0), V); 53 | 54 | % 55 | % % impose size threshold 56 | cl_me_pos(cat(1, cl_me_pos.numVox) < size_thresh) = []; 57 | cl_me_neg(cat(1, cl_me_neg.numVox) < size_thresh) = []; 58 | 59 | cl_mepos_covpos(cat(1, cl_mepos_covpos.numVox) < size_thresh) = []; 60 | cl_mepos_covneg(cat(1, cl_mepos_covneg.numVox) < size_thresh) = []; 61 | 62 | cl_meneg_covpos(cat(1, cl_meneg_covpos.numVox) < size_thresh) = []; 63 | cl_meneg_covneg(cat(1, cl_meneg_covneg.numVox) < size_thresh) = []; 64 | 65 | if length(cl_me_pos) == 0, cl_me_pos = []; end 66 | if length(cl_me_neg) == 0, cl_me_neg = []; end 67 | if length(cl_mepos_covpos) == 0, cl_mepos_covpos = []; end 68 | if length(cl_mepos_covneg) == 0, cl_mepos_covneg = []; end 69 | if length(cl_meneg_covpos) == 0, cl_meneg_covpos = []; end 70 | if length(cl_meneg_covneg) == 0, cl_meneg_covneg = []; end 71 | 72 | fprintf('Group avg Positive (Top Orthviews panel): %3.0f regions\n', length(cl_me_pos)); 73 | fprintf('Group avg Negative (Bottom Orthviews panel): %3.0f regions\n', length(cl_me_neg)); 74 | 75 | fprintf('Group avg Positive, Covariate Positive (Red): %3.0f regions\n', length(cl_mepos_covpos)); 76 | fprintf('These regions show A > B overall, and greater A > B with positive covariate values.\n\n'); 77 | 78 | fprintf('Group avg Positive, Covariate Negative (Blue): %3.0f regions\n', length(cl_mepos_covneg)); 79 | fprintf('These regions show A > B overall, and reduced A > B with positive covariate values.\n\n'); 80 | 81 | fprintf('Group avg Negative, Covariate Positive (Blue): %3.0f regions\n', length(cl_meneg_covpos)); 82 | fprintf('These regions show B > A overall, and reduced B > A with positive covariate values.\n\n'); 83 | 84 | fprintf('Group avg Negative, Covariate Negative (Red): %3.0f regions\n', length(cl_meneg_covneg)); 85 | fprintf('These regions show B > A overall, and greater B > A with positive covariate values.\n\n'); 86 | 87 | %% 88 | overlay = which('spm2_single_subj_T1_scalped.img'); 89 | nimgs = 2; 90 | spm_check_registration(repmat(overlay, nimgs, 1)); 91 | 92 | cluster_orthviews(cl_me_pos, {[0 1 0]}, 'solid', 'handle', 1, 'add'); 93 | cluster_orthviews(cl_me_neg, {[0 1 0]}, 'solid', 'handle', 2, 'add'); 94 | 95 | % kludge: do this many times so colors come out 96 | for i = 1:8 97 | cluster_orthviews(cl_mepos_covpos, {[1 0 0]}, 'solid', 'handle', 1, 'add'); 98 | cluster_orthviews(cl_mepos_covneg, {[0 .3 1]}, 'solid', 'handle', 1, 'add'); 99 | 100 | cluster_orthviews(cl_meneg_covpos, {[0 .3 1]}, 'solid', 'handle', 2, 'add'); 101 | cluster_orthviews(cl_meneg_covneg, {[1 0 0]}, 'solid', 'handle', 2, 'add'); 102 | 103 | end 104 | 105 | spm_orthviews_name_axis('Group avg (A>B, Pos)', 1); 106 | spm_orthviews_name_axis('Group avg (B>A, Neg)', 2); 107 | -------------------------------------------------------------------------------- /robust_toolbox/older_functions/robust_results2.m: -------------------------------------------------------------------------------- 1 | function robust_results2(EXPT,u,k) 2 | %function robust_results2(EXPT,u,k) 3 | % u and k are uncorrected threshold or 'FDR' and extent threshold 4 | % FDR IS NOW VALID ONLY FOR INCREASES 5 | 6 | warning off 7 | 8 | % convert u to t-threshold 9 | df = length(EXPT.subjects)-length(dir('rob_p_0*.img')); 10 | fprintf(1,'robust_results2 thinks there are %3.0f degrees of freedom.',df); 11 | 12 | uo = u; 13 | 14 | % if uncorrected threshold 15 | if ~isstr(u) 16 | t = tinv(1-u,df); 17 | str = sprintf('Height thresh: t = %3.2f (%3.0f Ss, %3.0f df @ p < %3.4f, extent = %3.0f\n',t,length(EXPT.subjects),df,uo,k); 18 | disp(str) 19 | else 20 | uo = 0.05; 21 | end 22 | 23 | ovl = []; 24 | if isfield(EXPT,'overlay'); 25 | disp(['Using overlay image: ' EXPT.overlay]); 26 | ovl = EXPT.overlay; 27 | end 28 | 29 | 30 | % intercept results 31 | % ------------------------------- 32 | fprintf(1,'\n---------------------------------------------\nOverall pos activation\n---------------------------------------------\n'); 33 | 34 | %FDR threshold 35 | if isstr(u) 36 | if strcmp(u,'FDR') 37 | [u2,Ps,Ts] = spm_uc_FDR(.05,[1 df],'T',1,spm_vol('rob_tmap_0001.img'),0); 38 | t = u2; 39 | str = sprintf('Height thresh FDR-corr: t = %3.2f (%3.0f Ss, %3.0f df @ p < %3.4f, extent = %3.0f\n',t,length(EXPT.subjects),df,uo,k); 40 | disp(str) 41 | end 42 | end 43 | 44 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0001.img',t,[k],['pos']); 45 | d=fileparts(P2); [d,f]=fileparts(d); 46 | cl = mask2clusters(P2); 47 | eval(['save ' f '_intercept_pos cl']) 48 | 49 | fprintf(1,'\n---------------------------------------------\nOverall neg activation\n---------------------------------------------\n') 50 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0001.img',t,[k],['neg']); 51 | cl = mask2clusters(P2); 52 | eval(['save ' f '_intercept_neg cl']) 53 | 54 | 55 | 56 | if exist('rob_tmap_0002.img') == 2 57 | 58 | fprintf(1,'\n---------------------------------------------\nCovariate pos effect\n---------------------------------------------\n') 59 | %FDR threshold 60 | if isstr(u) 61 | if strcmp(u,'FDR') 62 | [u2,Ps,Ts] = spm_uc_FDR(.05,[1 df],'T',1,spm_vol('rob_tmap_0002.img'),0); 63 | t = u2; 64 | str = sprintf('Height thresh FDR-corr: t = %3.2f (%3.0f Ss, %3.0f df @ p < %3.4f, extent = %3.0f\n',t,length(EXPT.subjects),df,uo,k); 65 | disp(str) 66 | end 67 | end 68 | 69 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0002.img',t,[k],['pos']); 70 | cl = mask2clusters(P2); 71 | eval(['save ' f '_0002_pos cl']) 72 | 73 | fprintf(1,'\n---------------------------------------------\nCovariate neg effect\n---------------------------------------------\n') 74 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0002.img',t,[k],['neg']); 75 | cl = mask2clusters(P2); 76 | eval(['save ' f '_0002_neg cl']) 77 | end 78 | 79 | if exist('rob_tmap_0003.img') == 2 80 | 81 | fprintf(1,'\n---------------------------------------------\nCovariate 2 pos effect\n---------------------------------------------\n') 82 | %FDR threshold 83 | if isstr(u) 84 | if strcmp(u,'FDR') 85 | [u2,Ps,Ts] = spm_uc_FDR(.05,[1 df],'T',1,spm_vol('rob_tmap_0003.img'),0); 86 | t = u2; 87 | str = sprintf('Height thresh FDR-corr: t = %3.2f (%3.0f Ss, %3.0f df @ p < %3.4f, extent = %3.0f\n',t,length(EXPT.subjects),df,uo,k); 88 | disp(str) 89 | end 90 | end 91 | 92 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0003.img',t,[k],['pos']); 93 | cl = mask2clusters(P2); 94 | eval(['save ' f '_0003_pos cl']) 95 | 96 | fprintf(1,'\n---------------------------------------------\nCovariate 2 neg effect\n---------------------------------------------\n') 97 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0003.img',t,[k],['neg']); 98 | cl = mask2clusters(P2); 99 | eval(['save ' f '_0003_neg cl']) 100 | end 101 | 102 | if exist('rob_tmap_0004.img') == 2 103 | 104 | fprintf(1,'\n---------------------------------------------\nCovariate 3 pos effect\n---------------------------------------------\n') 105 | %FDR threshold 106 | if isstr(u) 107 | if strcmp(u,'FDR') 108 | [u2,Ps,Ts] = spm_uc_FDR(.05,[1 df],'T',1,spm_vol('rob_tmap_0004.img'),0); 109 | t = u2; 110 | str = sprintf('Height thresh FDR-corr: t = %3.2f (%3.0f Ss, %3.0f df @ p < %3.4f, extent = %3.0f\n',t,length(EXPT.subjects),df,uo,k); 111 | disp(str) 112 | end 113 | end 114 | 115 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0004.img',t,[k],['pos']); 116 | cl = mask2clusters(P2); 117 | eval(['save ' f '_0004_pos cl']) 118 | 119 | fprintf(1,'\n---------------------------------------------\nCovariate 3 neg effect\n---------------------------------------------\n') 120 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0004.img',t,[k],['neg']); 121 | cl = mask2clusters(P2); 122 | eval(['save ' f '_0004_neg cl']) 123 | end 124 | 125 | 126 | if exist('rob_tmap_0005.img') == 2 127 | 128 | fprintf(1,'\n---------------------------------------------\nCovariate 4 pos effect\n---------------------------------------------\n') 129 | %FDR threshold 130 | if isstr(u) 131 | if strcmp(u,'FDR') 132 | [u2,Ps,Ts] = spm_uc_FDR(.05,[1 df],'T',1,spm_vol('rob_tmap_0005.img'),0); 133 | t = u2; 134 | str = sprintf('Height thresh FDR-corr: t = %3.2f (%3.0f Ss, %3.0f df @ p < %3.4f, extent = %3.0f\n',t,length(EXPT.subjects),df,uo,k); 135 | disp(str) 136 | end 137 | end 138 | 139 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0005.img',t,[k],['pos']); 140 | cl = mask2clusters(P2); 141 | eval(['save ' f '_0005_pos cl']) 142 | 143 | fprintf(1,'\n---------------------------------------------\nCovariate 4 neg effect\n---------------------------------------------\n') 144 | [P2,P,sigmat] = threshold_imgs('rob_tmap_0005.img',t,[k],['neg']); 145 | cl = mask2clusters(P2); 146 | eval(['save ' f '_0005_neg cl']) 147 | end 148 | 149 | 150 | 151 | 152 | fprintf(1,'\n---------------------------------------------\nDisplaying Intercept\n---------------------------------------------\n') 153 | cl = multi_threshold('rob_tmap_0001.img','T',df,ovl); 154 | %save cl_intercept cl 155 | 156 | input('Press return to continue') 157 | 158 | if exist('rob_tmap_0002.img') == 2 159 | fprintf(1,'\n---------------------------------------------\nDisplaying Cov 1\n---------------------------------------------\n') 160 | cl = multi_threshold('rob_tmap_0002.img','T',df,ovl); 161 | input('Press return to continue') 162 | %save cl_0002 cl 163 | end 164 | 165 | if exist('rob_tmap_0003.img') == 2 166 | fprintf(1,'\n---------------------------------------------\nDisplaying Cov 2\n---------------------------------------------\n') 167 | cl = multi_threshold('rob_tmap_0003.img','T',df,ovl); 168 | input('Press return to continue') 169 | end 170 | 171 | if exist('rob_tmap_0004.img') == 2 172 | fprintf(1,'\n---------------------------------------------\nDisplaying Cov 3\n---------------------------------------------\n') 173 | cl = multi_threshold('rob_tmap_0004.img','T',df,ovl); 174 | input('Press return to continue') 175 | end 176 | 177 | if exist('rob_tmap_0005.img') == 2 178 | fprintf(1,'\n---------------------------------------------\nDisplaying Cov 4\n---------------------------------------------\n') 179 | cl = multi_threshold('rob_tmap_0005.img','T',df,ovl); 180 | input('Press return to continue') 181 | end 182 | 183 | warning on 184 | 185 | return 186 | -------------------------------------------------------------------------------- /robust_toolbox/older_functions/robust_results3.m: -------------------------------------------------------------------------------- 1 | % function robust_results3(EXPT, u, k, ['display thresholds', thresholds], ['overlay', ovl], ['result names', resultnames], ... 2 | % ['write files', 0|1], ['contrast name', contrastname], ['cov names', covariatenames], ['pause for display', 0|1]) 3 | % 4 | % Displays the results of a random effects analysis contained in a robust* directory. By default, saves .png 5 | % files in axial and medial views. 6 | % 7 | % u: uncorrected threshold (e.g., .001) or 'FDR' % only for rob*img files that are produced, NOT for display 8 | % k: extent threshold (minimum size of cluster) 9 | % 10 | % E.g.: 11 | % % After loading an EXPT object... 12 | % robdirs = filenames('robust00[0-9][0-9]'); 13 | % for i=1:length(robdirs) 14 | % cd(robdirs{i}); 15 | % robust_results3(EXPT, .005, 10, 'result names', EXPT.SNPM.connames{i}); 16 | % close all; 17 | % cd('..'); 18 | % end 19 | % 20 | % To not display FDR, run: 21 | % for i=1:length(robdirs) 22 | % cd(robdirs{i}); 23 | % robust_results3(EXPT, .005, 10, 'result names', EXPT.SNPM.connames{i}, 'display thresholds', [.001 .005]); 24 | % close all; 25 | % cd('..'); 26 | % end 27 | % 28 | % To pause after each one, run: 29 | % for i=1:length(robdirs) 30 | % cd(robdirs{i}); 31 | % robust_results3(EXPT, .005, 10, 'result names', EXPT.SNPM.connames{i}, 'pause for display', 1); 32 | % close all; 33 | % cd('..'); 34 | % end 35 | % 36 | % To not write out result files (cls and imgs), run: 37 | % for i=1:length(robdirs) 38 | % cd(robdirs{i}); 39 | % robust_results3(EXPT, .005, 10, 'result names', EXPT.SNPM.connames{i}, 'write files', 0); 40 | % close all; 41 | % cd('..'); 42 | % end 43 | 44 | function robust_results3(EXPT, u, k, varargin) 45 | [tmp currentdir] = fileparts(pwd()); %#ok - this is a signal to mlint to ignore the fact that tmp is never used 46 | 47 | result_names = {'Intercept' 'Cov 1' 'Cov 2' 'Cov 3' 'Cov 4' 'Cov 5' 'Cov 6' 'Cov 7' 'Cov 8' 'Cov 9' 'Cov 10' 'Cov 11' 'Cov 12'}; 48 | pause_for_display = 0; 49 | write_files = 1; 50 | thresholds = [Inf .001 .005]; 51 | ovl = []; 52 | 53 | 54 | for i=1:length(varargin) 55 | if(ischar(varargin{i})) 56 | switch(varargin{i}) 57 | case {'contrast name', 'contrastname'} 58 | result_names{1} = varargin{i+1}; 59 | case {'cov names', 'covnames'} 60 | result_names = {'Intercept' varargin{i+1}{:}}; 61 | case {'result names', 'resultnames'} 62 | result_names = cellstr(varargin{i+1}); 63 | case 'display thresholds' 64 | thresholds = varargin{i+1}; 65 | case {'overlay', 'ovl'} 66 | ovl = varargin{i+1}; 67 | case {'pausefordisplay' 'pause for display'} 68 | pause_for_display = varargin{i+1}; 69 | case {'writefiles' 'write files'} 70 | write_files = varargin{i+1}; 71 | end 72 | end 73 | end 74 | 75 | warning off 76 | 77 | % convert u to t-threshold 78 | df = length(EXPT.subjects) - length(dir('rob_p_0*.img')); 79 | fprintf(1, '\n\n\nrobust_results3 thinks there are %d degrees of freedom.\n', df); 80 | 81 | uorig = u; 82 | 83 | % if uncorrected threshold 84 | if ~ischar(u) 85 | t = tinv(1-u, df); 86 | fprintf(1, 'Height thresh: t = %3.2f (%3.0f Ss, %3.0f df @ p < %3.4f, extent = %3.0f\n', t(1), length(EXPT.subjects), df, uorig(1), k(1)); 87 | else 88 | uorig = 0.05; 89 | end 90 | 91 | if isempty(ovl) && isfield(EXPT, 'overlay') 92 | disp(['Using overlay image: ' EXPT.overlay]); 93 | ovl = EXPT.overlay; 94 | end 95 | 96 | if(write_files) 97 | save_cl_results(); 98 | end 99 | display_activations(); 100 | 101 | 102 | warning on 103 | 104 | 105 | 106 | %------------------% 107 | % Nested functions % 108 | %------------------% 109 | 110 | function save_cl_results() 111 | for i=1:length(result_names) 112 | tmap_file = sprintf('rob_tmap_%04d.img', i); 113 | if exist(tmap_file, 'file') 114 | switch i 115 | case 1 % intercept results 116 | headers = {'Overall pos activation' 'Overall neg activation'}; 117 | mat_files = {[currentdir '_intercept_pos'] [currentdir '_intercept_neg']}; 118 | otherwise 119 | headers = {sprintf('%s pos effect', result_names{i}) sprintf('%s neg effect', result_names{i})}; 120 | mat_files = {sprintf('%s_%04d_pos', currentdir, i) sprintf('%s_%04d_neg', currentdir, i)}; 121 | end 122 | 123 | fprintf(1, '\n---------------------------------------------\n%s\n---------------------------------------------\n', headers{1}); 124 | %FDR threshold 125 | if (ischar(u) && strcmp(u, 'FDR')) || isinf(u) 126 | t = spm_uc_FDR(.05, [1 df], 'T', 1, spm_vol(tmap_file), 0); 127 | fprintf(1, 'Height thresh FDR-corr: t = %3.2f (%3.0f Ss, %3.0f df @ p < %3.4f, extent = %3.0f\n', ... 128 | t(1), length(EXPT.subjects), df, uorig, k(1)); 129 | end 130 | 131 | pos_thresh_imgs = threshold_imgs(tmap_file, t(1), k(1), 'pos'); 132 | cl = mask2clusters(pos_thresh_imgs); %#ok 133 | save(mat_files{1}, 'cl'); 134 | 135 | fprintf(1, '\n---------------------------------------------\n%s\n---------------------------------------------\n', headers{2}) 136 | neg_thresh_imgs = threshold_imgs(tmap_file, t(1), k(1), 'neg'); 137 | cl = mask2clusters(neg_thresh_imgs); %#ok 138 | save(mat_files{2}, 'cl'); 139 | end 140 | end 141 | end 142 | 143 | function display_activations() 144 | result_names = cellstr(result_names); 145 | thr_string = strtrim(num2str(thresholds, '%0.3g ')); 146 | thr_string = strrep(thr_string, 'Inf', 'FDR'); 147 | for i=1:length(result_names) 148 | tmap_file = sprintf('rob_tmap_%04d.img', i); 149 | if exist(tmap_file, 'file') 150 | fprintf(1, '\n---------------------------------------------\nDisplaying %s\n---------------------------------------------\n', result_names{i}); 151 | fig_title = sprintf('%s - %s - %s', currentdir, result_names{i}, thr_string); 152 | if(~isempty(thresholds)) 153 | cl = multi_threshold2(tmap_file, 'T', df, 'overlay', ovl, 'title', fig_title, ... 154 | 'save images', 1, 'thresholds', thresholds); %#ok 155 | else 156 | cl = multi_threshold2(tmap_file, 'T', df, 'overlay', ovl, 'title', fig_title, ... 157 | 'save images', 1); %#ok 158 | end 159 | if(pause_for_display) 160 | input('Press return/enter to continue'); 161 | end 162 | end 163 | end 164 | end 165 | end 166 | 167 | 168 | -------------------------------------------------------------------------------- /robust_toolbox/older_functions/robust_results_2012_plugin.m: -------------------------------------------------------------------------------- 1 | function robust_results_2012_plugin(R) 2 | % robust_results_2012_plugin(R) 3 | % 4 | % For robust nonparametric analysis: 5 | % Load the variable R from the saved .mat file and run 6 | % 7 | % This script: 8 | % 1) shows the mask 9 | % 2) shows histograms of the observed and null max T-values 10 | % 3) prints information about the whole-mask threshold 11 | % 4) writes thresholded (whole-mask) .img files to disk in a subfolder 12 | % 13 | % It is designed to be called by a wrapper script invoked by the publish 14 | % command, for saving results in HTML archives. 15 | 16 | %% Mask used in analysis 17 | 18 | % display mask and set up fmridisplay o2 object 19 | % --------------------------------------------------------- 20 | r = region(R.maskname); 21 | o2 = canlab_results_fmridisplay(r); 22 | 23 | set(o2.activation_maps{1}.blobhandles, 'FaceColor', [0 1 0], 'FaceAlpha', .4) 24 | set(o2.activation_maps{2}.blobhandles, 'Color', [0 .4 0]) 25 | 26 | %% Null and observed t-value histograms 27 | 28 | % threshold for whole-mask correction and histogram 29 | % --------------------------------------------------------- 30 | 31 | k = size(R.maxt, 1); 32 | create_figure('Histograms', 1, k); 33 | 34 | for i = 1:k 35 | 36 | create_figure('Histograms', 1, k, 1); 37 | subplot(1, k, i); 38 | 39 | t = R.correct.t(i, :)'; 40 | nullt = R.maxt(i, :)'; 41 | nulltn = -nullt; % for both + and - tails 42 | 43 | x = linspace(min([nullt; nulltn; t]), max([nullt; nulltn; t]), 100); 44 | 45 | h1 = hist([nullt; nulltn], x); 46 | 47 | h2 = hist(t, x); 48 | 49 | han1 = bar(x, h1, 'FaceColor', [.5 .5 .5], 'EdgeColor', 'none'); 50 | han2 = bar(x, h2, 'FaceColor', [0 0 1], 'EdgeColor', 'none'); 51 | 52 | plot_vertical_line(prctile(nulltn, 5)); 53 | plot_vertical_line(prctile(nullt, 95)); 54 | 55 | patchhan = get(han2, 'Children'); 56 | set(patchhan, 'FaceAlpha', .7); 57 | 58 | if i == 1 59 | legend([han1 han2], {'Null (max)' 'Observed'}) 60 | end 61 | 62 | xlabel('T-value'); 63 | ylabel('Frequency'); 64 | 65 | title(R.names{i}); 66 | 67 | end 68 | 69 | drawnow; 70 | snapnow; 71 | 72 | %% Whole-mask thresholded maps (FWE p < .05) 73 | 74 | % threshold and reconstruct maps 75 | % ------------------------------------------------------- 76 | 77 | % set up for writing images 78 | dat = statistic_image('image_names', R.maskname); 79 | 80 | outputdir = fullfile(pwd, 'thresholded_images'); 81 | if ~exist(outputdir, 'dir'), mkdir(outputdir); end 82 | 83 | z = '------------------------------------------------------'; 84 | fprintf('%s\n%s\n', z, z); 85 | 86 | fprintf('Thresholds (t-values) for p < .05 corrected across mask\n'); 87 | 88 | for i = 1:k 89 | 90 | t = R.correct.t(i, :)'; 91 | nullt = R.maxt(i, :)'; 92 | nulltn = -nullt; % for both + and - tails 93 | 94 | negthr = prctile(nulltn, 5); 95 | posthr = prctile(nullt, 95); 96 | 97 | % Table of thresholds and sig vox 98 | % ----------------------------------------------------------- 99 | fprintf('%s\n%s\n%s\n', z, R.names{i}, z); 100 | 101 | fprintf('Param %3.0f - %s: t < %3.3f or t > %3.3f\t', i, R.names{i}, negthr, posthr) 102 | 103 | fprintf('Sig -:\t%3.0f\tvoxels\tSig +:\t%3.0f\tvoxels\n', sum(t < negthr), sum(t > posthr)); 104 | 105 | 106 | % write map with sig. voxels 107 | tmap = dat; 108 | tmap.type = 'T'; 109 | tmap.dat = t; 110 | tmap.sig = (t < negthr) | (t > posthr); 111 | 112 | outname = fullfile(outputdir, ['Thresholded_FWE05_' R.names{i} '.img']); 113 | 114 | tmap.fullpath = outname; 115 | write(tmap); 116 | 117 | % display on fmridisplay 118 | o2 = removeblobs(o2); 119 | 120 | if sum(tmap.sig > 0) 121 | cl = region(tmap); 122 | o2 = addblobs(o2, cl, 'splitcolor', {[0 0 1] [0 1 1] [1 .5 0] [1 1 0]}); 123 | end 124 | 125 | o2 = addblobs(o2, r, 'maxcolor', [0 .2 0], 'outline'); 126 | 127 | drawnow; 128 | snapnow; 129 | end 130 | 131 | fprintf('%s\n%s\n', z, z); 132 | 133 | end % function 134 | 135 | 136 | -------------------------------------------------------------------------------- /robust_toolbox/older_functions/robust_results_act_plus_corr.m: -------------------------------------------------------------------------------- 1 | % res = robust_results_act_plus_corr(u1, u2, k1, k2, [covfield], [['mask', mask], ['overlay', overlay], ['writeimgs', 0|1]]) 2 | % 3 | % gets activation blobs that overlap with at least one covariate blob, and 4 | % vice versa 5 | % generates output orthviews and tables 6 | % 7 | % tor wager 8 | 9 | function res = robust_results_act_plus_corr(u1, u2, k1, k2, covfield, varargin) 10 | writeimgs = 0; 11 | overlay = []; 12 | 13 | if(~exist('covfield', 'var') || isempty(covfield)), covfield = 'cov1'; end 14 | for i=1:2:length(varargin) 15 | switch(varargin{i}) 16 | case 'mask' 17 | mask = varargin{i+1}; 18 | case 'overlay' 19 | overlay = varargin{i+1}; 20 | case 'writeimgs' 21 | writeimgs = varargin{i+1}; 22 | end 23 | end 24 | 25 | % ------------------------------------------------------------------------- 26 | % Threshold activation and covariate images 27 | % ------------------------------------------------------------------------- 28 | 29 | % first image: Activation 30 | [res.act.clpos, res.act.clneg, res.act.dat, volInfo] = robust_results_threshold(u1, k1, 'mask', mask, 'overlay', overlay); 31 | 32 | % second image: Correlation 33 | [res.(covfield).clpos, res.(covfield).clneg, res.(covfield).dat] = robust_results_threshold(u2, k2, covfield, 'mask', mask, 'overlay', overlay); 34 | 35 | % ------------------------------------------------------------------------- 36 | % Find which clusters have any overlapping values 37 | % ------------------------------------------------------------------------- 38 | 39 | % intersection of both 40 | [res.act.cloverlap, res.(covfield).cloverlap, res.act.overlapdat, res.(covfield).overlapdat] = ... 41 | iimg_cluster_intersect(res.act.dat, res.(covfield).dat, volInfo); 42 | 43 | % save max stats in each set of clusters for later printout in table 44 | [clindx, nvox] = iimg_cluster_index(res.act.overlapdat, volInfo.xyzlist'); 45 | for i = 1:length(res.act.cloverlap) 46 | %%%get touching clusters for this overlap area 47 | whcl = (clindx == i); 48 | 49 | res.act.cloverlap(i).act_tmax = get_maxstat_from_dat(whcl, res.act.dat, volInfo); 50 | res.act.cloverlap(i).cov_tmax = get_maxstat_from_dat(whcl, res.(covfield).dat, volInfo); 51 | end 52 | 53 | [clindx, nvox] = iimg_cluster_index(res.(covfield).overlapdat, volInfo.xyzlist'); 54 | for i = 1:length(res.(covfield).cloverlap) 55 | %%%get touching clusters for this overlap area 56 | whcl = (clindx == i); 57 | 58 | res.(covfield).cloverlap(i).act_tmax = get_maxstat_from_dat(whcl, res.act.dat, volInfo); 59 | res.(covfield).cloverlap(i).cov_tmax = get_maxstat_from_dat(whcl, res.(covfield).dat, volInfo); 60 | end 61 | 62 | %cluster_orthviews(cl1, 'bivalent'); 63 | %cluster_orthviews(cl2, 'bivalent'); 64 | 65 | % print tables 66 | fprintf(1, '\n\nActivated clusters overlapping with correlated clusters\n') 67 | fprintf(1, 'Height threshold is p < %3.6f, Extent is %3.0f contiguous voxels\n', u1, k1) 68 | fprintf(1, '---------------------------------------------------------------------\n\n') 69 | 70 | cluster_table(res.act.cloverlap, 0, 0, 'act_tmax', 'cov_tmax'); 71 | 72 | fprintf(1, '\n\nCorrelated clusters overlapping with correlated clusters\n') 73 | fprintf(1, 'Height threshold is p < %3.6f, Extent is %3.0f contiguous voxels\n', u2, k2) 74 | fprintf(1, '---------------------------------------------------------------------\n\n') 75 | 76 | cluster_table(res.(covfield).cloverlap, 0, 0, 'act_tmax', 'cov_tmax'); 77 | 78 | fprintf(1, '\n\n') 79 | 80 | % ------------------------------------------------------------------------- 81 | % Find overlapping voxels 82 | % ------------------------------------------------------------------------- 83 | 84 | if writeimgs 85 | iname = ['act_u' num2str(u1) 'k' num2str(k1) '_' covfield '_u' num2str(u2) 'k' num2str(k2) '_overlap.img']; 86 | [int_dat] = iimg_intersection(res.act.overlapdat, res.(covfield).overlapdat, volInfo, 'name', iname, 'posneg'); 87 | else 88 | [int_dat] = iimg_intersection(res.act.overlapdat, res.(covfield).overlapdat, volInfo, 'posneg'); 89 | end 90 | 91 | % get clusters 92 | res.cl_pospos = iimg_indx2clusters(int_dat(:, 1), volInfo); 93 | res.cl_posneg = iimg_indx2clusters(int_dat(:, 2), volInfo); 94 | res.cl_negpos = iimg_indx2clusters(int_dat(:, 3), volInfo); 95 | res.cl_negneg = iimg_indx2clusters(int_dat(:, 4), volInfo); 96 | 97 | 98 | cluster_orthviews(res.cl_pospos, {[1 0 0]}, 'overlay', overlay); 99 | cluster_orthviews(res.cl_posneg, {[1 .5 0]}, 'add'); 100 | cluster_orthviews(res.cl_negpos, {[0 .5 1]}, 'add'); 101 | cluster_orthviews(res.cl_negneg, {[0 0 1]}, 'add'); 102 | 103 | combined_cls = {res.cl_pospos, res.cl_posneg, res.cl_negpos, res.cl_negneg}; 104 | cl_colors = {[1 0 0] [1 .5 0] [0 .5 1] [0 0 1]}; 105 | whempty = cellfun(@isempty, combined_cls); 106 | combined_cls(whempty) = []; 107 | cl_colors(whempty) = []; 108 | montage_clusters(overlay, combined_cls{:}, cl_colors); 109 | montage_clusters_medial(overlay, combined_cls{:}, cl_colors); 110 | 111 | res.volInfo = volInfo; 112 | 113 | iname = ['act_u' num2str(u1) 'k' num2str(k1) '_' covfield '_u' num2str(u2) 'k' num2str(k2) '_overlap']; 114 | fprintf(1, '\nDone: %s\n', iname); 115 | fprintf(1, 'res.act contains activation clusters and data vectors for + and - voxels.\n'); 116 | fprintf(1, 'res.%s contains covariate clusters and data vectors for + and - voxels.\n', covfield); 117 | fprintf(1, 'res.act.cloverlap contains activation clusters overlapping with covariate.\n'); 118 | fprintf(1, 'res.%s.cloverlap contains covariate clusters overlapping with activation.\n', covfield); 119 | fprintf(1, 'res.volInfo contains mask volume information\n'); 120 | fprintf(1, '\n\n') 121 | end 122 | 123 | 124 | 125 | function maxstat = get_maxstat(cl) 126 | % max absolute value 127 | if size(cl.Z, 2) ~= size(cl.XYZmm, 2) 128 | cl.Z = cl.Z'; 129 | end 130 | [maxabs, whmax] = max(abs(cl.Z(1, :))); 131 | maxstat = cl.Z(1, whmax(1)); 132 | end 133 | 134 | 135 | function maxstat = get_maxstat_from_dat(whcl, dat, volInfo) 136 | [tmp, tmp, tmp, touching_this_cl] = ... 137 | iimg_cluster_intersect(whcl, dat, volInfo); 138 | touching_this_cl( touching_this_cl == 0) = []; 139 | [maxabs, whmax] = max(abs(touching_this_cl)); 140 | maxstat = touching_this_cl(whmax(1)); 141 | end 142 | 143 | -------------------------------------------------------------------------------- /robust_toolbox/older_functions/robust_results_batch_pre2020.m: -------------------------------------------------------------------------------- 1 | function robust_results_batch(varargin) 2 | %robust_results_batch(varargin) 3 | % 4 | % This function runs and saves tables, images, and clusters 5 | % for all effects of a robust regression model 6 | % 7 | % You need Tor's mediation toolbox on your path. 8 | % 9 | % You also need to enter optional inputs as you would to 10 | % mediation_brain_results. 11 | % Enter any arguments to mediation_brain_results: 12 | % e.g., ... 'thresh', [.005 .01 .05], 'size', [3 1 1], 'prune', 'overlay', 13 | % EXPT.overlay, 'mask', EXPT.mask); 14 | % 15 | % You MUST enter a mask image name. robfit does not currently save a 16 | % default one, and mediation_brain_results requires one. 17 | % 18 | % 19 | % Optional inputs: 20 | % ------------------------------------------------------------------------- 21 | % case 'mask', mask = varargin{i+1}; varargin{i} = []; varargin{i+1} = []; 22 | % case 'overlay', overlay = varargin{i+1}; varargin{i} = []; varargin{i+1} = []; 23 | % 24 | % case 'thresh', pthresh = varargin{i+1}; varargin{i} = []; varargin{i+1} = []; 25 | % case 'size', kthresh = varargin{i+1}; varargin{i} = []; varargin{i+1} = []; 26 | % 27 | % case 'reversecolors', reversecolors = 1; 28 | % 29 | % case 'skipmask', skipmask = 1; 30 | % 31 | % case {'rob', 'rob0', 'rob1', 'rob2', 'rob3', 'rob4'} 32 | % 33 | % Examples: 34 | % ------------------------------------------------------------------------- 35 | % robust_results_batch('overlay', EXPT.overlay, 'mask', EXPT.mask, 'thresh', [.001 .005 .05], 'size', [5 1 1], 'prune'); 36 | % follow up with surface plots and a surface movie: 37 | % load cl_rob_p_0002_001_005_05_k5_1_1_prune.mat % this is for the covariate effect 38 | % mediation_brain_surface_figs(clpos, clneg); 39 | 40 | % Tor Wager, March 2010 41 | % Updated: Aug 2012 42 | % - compatibility with publish 43 | % - new default slice views, compatible with canlab_results_fmridisplay slice format 44 | % - added some diagnostic plots (qchist) as standard output 45 | % Updated: Jan 2020 by Tor; code maintenance 46 | 47 | mask = 'mask.nii'; 48 | overlay = []; 49 | pthresh = [.001 .01 .05]; %[.005 .01 .05]; %[.001 .005 .01]; %'fdr'; % specify 'fdr' or a series of p thresholds 50 | kthresh = [3 1 1]; % specify a series of extent thresholds 51 | reversecolors = 0; 52 | orig_varargin = varargin; 53 | % overlay = which('SPM8_colin27T1_seg.img'); 54 | overlay = which('keuken_2014_enhanced_for_underlay.img'); 55 | skipmask = 0; 56 | 57 | % special inputs that we do extra things with; 58 | % all optional inputs are passed to mediation_brain_results 59 | for i = 1:length(varargin) 60 | if ischar(varargin{i}) 61 | switch varargin{i} 62 | % functional commands 63 | case 'mask', mask = varargin{i+1}; varargin{i} = []; varargin{i+1} = []; 64 | case 'overlay', overlay = varargin{i+1}; varargin{i} = []; varargin{i+1} = []; 65 | 66 | case 'thresh', pthresh = varargin{i+1}; varargin{i} = []; varargin{i+1} = []; 67 | case 'size', kthresh = varargin{i+1}; varargin{i} = []; varargin{i+1} = []; 68 | 69 | case 'reversecolors', reversecolors = 1; 70 | 71 | case 'skipmask', skipmask = 1; 72 | 73 | case {'rob', 'rob0', 'rob1', 'rob2', 'rob3', 'rob4'} 74 | warning('Do not enter rob* inputs to robust_results_batch'); 75 | varargin{i} = []; orig_varargin{i} = []; 76 | 77 | % omit warning because we may have other valid inputs to pass forward 78 | %otherwise, warning(['Unknown input string option:' varargin{i}]); 79 | end 80 | end 81 | end 82 | 83 | if isempty(mask), error('You must enter a mask image or omit mask for the default: rob_beta_0001.img.'); end 84 | if isempty(overlay), error('You must enter an overlay (anatomical) image, or omit overlay for the default: SPM8_colin27T1_seg.img.'); end 85 | 86 | currdir = pwd; 87 | setupfile = fullfile(currdir, 'SETUP.mat'); 88 | if ~exist(setupfile, 'file'); error('SETUP.mat does not exist in current dir; go to valid robust results directory.'); end 89 | 90 | 91 | % --------------------------------------------------------- 92 | % Report header 93 | % --------------------------------------------------------- 94 | 95 | load(setupfile) 96 | ncols = size(SETUP.X, 2); 97 | 98 | reportfile = fullfile(pwd, 'robust_results_batch_report.txt'); 99 | diary(reportfile) 100 | 101 | hdrline = '=========================='; 102 | sepline = '________________________________'; 103 | 104 | fprintf('%s\n=\n= robust_results_batch.m\n=\n%s\n', hdrline, hdrline) 105 | fprintf('Directory: %s\nDate report generated: %s\n%s\n', currdir, date, sepline); 106 | 107 | %pause(1) 108 | fprintf('\n\nModel info:\n%s\nImages and regressors (intercept + %3.0f covs)\n', sepline, ncols - 1); 109 | imgs = mat2cell(SETUP.files, ones(size(SETUP.files, 1), 1), size(SETUP.files, 2)); 110 | 111 | % imgs may be single 4-D file 112 | if size(imgs, 1) == size(SETUP.X, 1) % 3D 113 | print_matrix(SETUP.X, [], imgs); 114 | else % maybe 4D 115 | print_matrix(SETUP.X); 116 | end 117 | 118 | fprintf('\n%s\n', hdrline) 119 | 120 | fprintf('\n\nResults info:%s\nInput options:\n', sepline); 121 | for i = 1:length(orig_varargin) 122 | if ischar(orig_varargin{i}) 123 | fprintf('%s\t', orig_varargin{i}); 124 | elseif ishandle(orig_varargin{i}) 125 | else 126 | print_matrix(orig_varargin{i}) 127 | end 128 | end 129 | 130 | fprintf('\nResults inputs\n%s\n', sepline); 131 | fprintf('\nMask: %s\nOverlay: %s\n', mask, overlay); 132 | fprintf('\nP-value thresholds: '); print_matrix(pthresh); 133 | fprintf('\nExtent thresholds: '); print_matrix(kthresh); 134 | 135 | % --------------------------------------------------------- 136 | % Mask figure 137 | % --------------------------------------------------------- 138 | 139 | % clear existing windows. No longer necessary, but good anyway. 140 | figh = findobj('Tag', 'montage_axial'); 141 | if ishandle(figh), clf(figh); end 142 | 143 | figh = findobj('Tag', 'montage_coronal'); 144 | if ishandle(figh), clf(figh); end 145 | 146 | figh = findobj('Tag', 'montage_sagittal'); 147 | if ishandle(figh), clf(figh); end 148 | 149 | if ~skipmask 150 | 151 | cltmp = mask2clusters(mask); 152 | cluster_orthviews(cltmp, {[0 1 0]}, 'trans'); 153 | cluster_orthviews_montage(6, 'axial', overlay, 'onerow'); 154 | %snapnow 155 | %mask_fig_handle = montage_clusters(mask); 156 | mask_fig_handle = findobj('Tag', 'montage_axial'); 157 | set(mask_fig_handle, 'Name', 'Mask image'); 158 | scn_export_papersetup(500); saveas(mask_fig_handle, fullfile(currdir, 'Results_Mask.png')); 159 | 160 | cluster_orthviews_montage(10, 'sagittal', overlay, 'onerow'); 161 | 162 | f = findobj('Tag', 'Graphics'); 163 | set(f, 'Visible', 'off'); 164 | 165 | snapnow 166 | end 167 | 168 | % --------------------------------------------------------- 169 | % Histogram+ on input data 170 | % --------------------------------------------------------- 171 | if ~isempty(check_valid_imagename(SETUP.files, 0)) 172 | 173 | fprintf('%s\nImage dataset visualization and diagnostics\n%s\n', sepline, sepline); 174 | qchist(SETUP.files); 175 | sfig = findobj('Tag', 'Graphics'); 176 | set(sfig, 'Visible', 'off'); 177 | snapnow 178 | else 179 | disp('Cannot find images in SETUP.files. Skipping QC histograms.') 180 | end 181 | 182 | % --------------------------------------------------------- 183 | % Case (subject) weights 184 | % --------------------------------------------------------- 185 | 186 | fprintf('%s\nCase (subjects) weights: Low indicates likely outliers\n%s\n', sepline, sepline); 187 | 188 | if exist(fullfile(pwd, 'weights.nii'), 'file') % newer 189 | dat = fmri_data('weights.nii'); 190 | elseif exist(fullfile(pwd, 'weights.img'), 'file') % legacy 191 | dat = fmri_data('weights.img'); 192 | else 193 | error('Cannot find weights.nii'); 194 | end 195 | 196 | plot(dat) 197 | sfig = findobj('Tag', 'Graphics'); 198 | set(sfig, 'Visible', 'off'); 199 | snapnow 200 | 201 | 202 | % --------------------------------------------------------- 203 | % Report results info, including FDR correction 204 | % --------------------------------------------------------- 205 | 206 | fprintf('\nFDR correction info (FDR not necessarily used here; just for info):%s\n', sepline); 207 | SETUP = robust_results_fdr_threshold('fdr', 'mask', mask); 208 | 209 | fprintf('\n%s\nColors: ', sepline); 210 | if reversecolors, fprintf('Reversed\n%s\n', hdrline); 211 | else fprintf('Normal\n%s\n', hdrline); 212 | end 213 | 214 | if reversecolors 215 | negcolors = { [1 1 0] [1 .5 0] [1 .3 .3] }; 216 | poscolors = { [0 0 1] [0 .5 1] [.3 .3 1] }; 217 | else 218 | poscolors = { [1 1 0] [1 .5 0] [1 .3 .3] }; 219 | negcolors = { [0 0 1] [0 .5 1] [.3 .3 1] }; 220 | end 221 | 222 | % --------------------------------------------------------- 223 | % Run for each effect 224 | % --------------------------------------------------------- 225 | 226 | for i = 1:ncols 227 | 228 | robstring = ['rob' num2str(i - 1)]; 229 | 230 | fprintf('%s\n=\n= Results for %s\n=\n%s\n', hdrline, robstring, hdrline) 231 | 232 | [clpos, clneg, clpos_data, clneg_data, clpp2, clnn2] = mediation_brain_results ... 233 | (robstring, 'thresh', pthresh, 'size', kthresh, ... 234 | 'tables', 'slices', 'save', 'overlay', overlay, 'mask', mask, 'poscolors', poscolors, 'negcolors', negcolors, varargin{:}); 235 | 236 | f = findobj('Tag', 'Graphics'); 237 | set(f, 'Visible', 'off'); 238 | 239 | snapnow 240 | end 241 | 242 | diary off 243 | 244 | end % function 245 | 246 | 247 | -------------------------------------------------------------------------------- /robust_toolbox/older_functions/robust_results_make_invp_maps.m: -------------------------------------------------------------------------------- 1 | function robust_results_make_invp_maps(varargin) 2 | % robust_results_make_invp_maps([options]) 3 | % DESCRIPTION 4 | % run in robfit directory 5 | % takes rob_p_* maps and makes invpmaps/*_invp maps 6 | % if EXPT.SNPM.connames and/or EXPT.covnames exist, will use in naming 7 | % otherwise: invpmaps/lcon_hcon_invp.nii 8 | % converts NaNs to 0s in output maps 9 | % OPTIONS 10 | % 'space', 11 | % resample inverse p maps to space of 12 | % make blank string for no resampling 13 | % DEFAULT: avg152T1.nii 14 | % 'd', 15 | % DEFAULT: pwd 16 | % 17 | % NOTES 18 | % to view, try the shell function ~ruzicl/scripts/invpview (wrapper for FSLview) 19 | % 20 | % Luka Ruzic (2012) 21 | % 22 | 23 | RESAMPLE = which('avg152T1.nii'); 24 | WD = pwd; 25 | 26 | i=1; 27 | while i<=numel(varargin) 28 | if ischar(varargin{i}) 29 | switch varargin{i} 30 | case 'space' 31 | i=i+1; 32 | RESAMPLE = varargin{i}; 33 | case 'd' 34 | i=i+1; 35 | WD = varargin{i}; 36 | otherwise 37 | error(sprintf('Unrecognized argument: %s',varargin{i})) 38 | end 39 | 40 | else 41 | disp(varargin{i}) 42 | error('ABOVE ARGUMENT UNRECOGNIZED') 43 | end 44 | i=i+1; 45 | end 46 | 47 | 48 | cd(WD) 49 | 50 | robdirs = filenames('robust[0-9]*','absolute'); 51 | if isempty(robdirs), error('No directories: robust[0-9]*'); end 52 | 53 | dout = fullfile(pwd,'invpmaps'); 54 | if ~exist(dout,'dir'), mkdir(dout); end 55 | 56 | lconnames = {}; 57 | hconnames = {}; 58 | try 59 | load('EXPT.mat') 60 | 61 | if isfield(EXPT.SNPM,'connames') 62 | lconnames = cellstr(EXPT.SNPM.connames); 63 | lconnames = regexprep(lconnames,' ',''); 64 | lconnames = regexprep(lconnames,'[^A-Za-z0-9_+.-]',''); 65 | 66 | fprintf('Lower level contrast names:\n') 67 | disp(lconnames) 68 | end 69 | 70 | if isfield(EXPT,'covnames') 71 | hconnames = cellstr(EXPT.covnames); 72 | hconnames = regexprep(hconnames,' ',''); 73 | hconnames = regexprep(hconnames,'[^A-Za-z0-9_+.-]',''); 74 | 75 | fprintf('Higher level covariate names:\n') 76 | disp(hconnames) 77 | end 78 | end 79 | 80 | fprintf('... CREATING inverse p maps\n') 81 | fout={}; 82 | for i = 1:numel(robdirs) 83 | l = regexprep(robdirs{i},'.*/robust',''); 84 | if isempty(lconnames) 85 | lconname = sprintf('lcon%s',l); 86 | else 87 | lconname = lconnames{str2num(l)}; 88 | end 89 | 90 | pmaps = filenames(fullfile(robdirs{i},'rob_p_*.img'),'absolute'); 91 | for j = 1:numel(pmaps) 92 | [dummy f e] = fileparts(pmaps{j}); 93 | 94 | h = regexprep(f,'rob_p_',''); 95 | if isempty(hconnames) 96 | hconname = sprintf('hcon%s',h); 97 | else 98 | hconname = hconnames{str2num(h)}; 99 | end 100 | 101 | 102 | fout{end+1} = fullfile(dout,[lconname '_' hconname '_invp.nii']); 103 | 104 | fprintf('%-7s %s\n','pmap',pmaps{j},'invpmap',fout{end}); 105 | 106 | % [pvol pdat] = iimg_read_img(pmaps{j}); 107 | % [tvol tdat] = iimg_read_img(regexprep(pmaps{j},'_p_','_tmap_')); 108 | pvol = spm_vol(pmaps{j}); 109 | pdat = spm_read_vols(pvol); 110 | tvol = spm_vol(regexprep(pmaps{j},'_p_','_tmap_')); 111 | tdat = spm_read_vols(tvol); 112 | 113 | % invert 114 | pdat = 1 - pdat; 115 | 116 | % add valence 117 | pdat(tdat<0) = pdat(tdat<0) * -1; 118 | 119 | % write out file 120 | pvol.fname = fout{end}; 121 | pvol.descrip = 'IRLS robust regression inverse p values'; 122 | spm_write_vol(pvol,pdat); 123 | % iimg_write_images(pdat,pvol,fout{end}); 124 | 125 | if ~isempty(RESAMPLE) 126 | fprintf('resampling to space of: %s\n',RESAMPLE); 127 | scn_map_image(fout{end},RESAMPLE,'write',fout{end}); 128 | end 129 | end 130 | fprintf('\n') 131 | end 132 | fprintf('... REMOVING NaNs\n') 133 | nans2zeros(fout); 134 | 135 | end 136 | 137 | -------------------------------------------------------------------------------- /robust_toolbox/older_functions/robust_results_threshold.m: -------------------------------------------------------------------------------- 1 | % [clpos, clneg, dat, volInfo] = robust_results_threshold(pthr, kthr, [cmd strings]) 2 | % 3 | % Threshold a p-value image and return values in a second image (designed 4 | % for t-images, but can be anything.) 5 | % 6 | % Command strings 7 | % 'mask', followed by mask image name 8 | % 'overlay', followed by overlay image name 9 | % 'p', followed by p-value image name 10 | % 't', followed by t-value image name 11 | % 'intercept', pimg = 'rob_p_0001.img'; timg = 'rob_tmap_0001.img'; 12 | % 'cov1', pimg = 'rob_p_0002.img'; timg = 'rob_tmap_0002.img'; 13 | % 'cov2', pimg = 'rob_p_0003.img'; timg = 'rob_tmap_0003.img'; 14 | % 'cov3', pimg = 'rob_p_0004.img'; timg = 'rob_tmap_0004.img'; 15 | % 'fmap', pimg = 'rob_pmap_full.img'; timg = 'rob_fmap_full.img'; 16 | % 'write', write thresholded output images to disk 17 | % 'nodisplay', do not display results on brain 18 | % 'fdr', do FDR correction 19 | % 'handle', followed by which orthviews plot to use 20 | % case 'add', add to existing orthviews 21 | % 22 | % Examples 23 | % ------------------------------------------------------------------------- 24 | % Default: use rob_p_0001.img and t_0001.img 25 | % [clpos, clneg, dat, volInfo] = robust_results_threshold(.005, 10); 26 | % 27 | % Do not display orthviews, but write masks 28 | % [clpos, clneg, dat, volInfo] = robust_results_threshold(.001, 10, 'nodisplay', 'write'); 29 | % 30 | % Threshold correlation map (rob_p_0002.img) and mask with pos intercept activation 31 | % [clpos, clneg, dat, volInfo] = robust_results_threshold(.005, 10, 'cov1', 'mask', '../mask_images/hr_corr_pos_fdr05.img'); 32 | % 33 | % robust_results_threshold(.01, 5, 'cov1', 'overlay', EXPT.overlay); 34 | % 35 | % %% put results in second orthviews pane: 36 | % robust_results_threshold(.01, 5, 'cov1', 'overlay', EXPT.overlay, 'handle', 2); 37 | 38 | function [clpos, clneg, dat, volInfo] = robust_results_threshold(pthr, kthr, varargin) 39 | % -------------------------------------- 40 | % * Set up arguments 41 | % -------------------------------------- 42 | 43 | % defaults 44 | 45 | switch spm('Ver') 46 | case 'SPM2' 47 | % spm_defaults is a script 48 | disp('WARNING: spm defaults not set for spm2. Make sure your defaults are set correctly'); 49 | 50 | case 'SPM5' 51 | % spm_defaults is a function 52 | spm_defaults() 53 | end 54 | 55 | pimg = 'rob_p_0001.img'; 56 | timg = 'rob_tmap_0001.img'; 57 | writeoutput = 0; 58 | mask = []; 59 | display = 1; 60 | clpos = []; clneg = []; 61 | dofdr = 0; 62 | overlay = []; 63 | orth_handle = 1; 64 | addstr = 'do not add.'; 65 | 66 | % inputs 67 | for i = 1:length(varargin) 68 | arg = varargin{i}; 69 | if ischar(arg) 70 | switch lower(arg) 71 | case 'mask', mask = varargin{i+1}; varargin{i+1} = []; 72 | case 'overlay', overlay = varargin{i+1}; varargin{i+1} = []; 73 | case 'p', pimg = varargin{i+1}; varargin{i+1} = []; 74 | case 't', timg = varargin{i+1}; varargin{i+1} = []; 75 | case 'intercept', pimg = 'rob_p_0001.img'; timg = 'rob_tmap_0001.img'; 76 | case 'cov1', pimg = 'rob_p_0002.img'; timg = 'rob_tmap_0002.img'; 77 | case 'cov2', pimg = 'rob_p_0003.img'; timg = 'rob_tmap_0003.img'; 78 | case 'cov3', pimg = 'rob_p_0004.img'; timg = 'rob_tmap_0004.img'; 79 | case 'cov4', pimg = 'rob_p_0005.img'; timg = 'rob_tmap_0005.img'; 80 | 81 | case 'fmap', pimg = 'rob_pmap_full.img'; timg = 'rob_fmap_full.img'; 82 | case 'write', writeoutput = 1; 83 | case 'nodisplay', display = 0; 84 | case 'fdr', dofdr = 1; 85 | 86 | case 'handle', orth_handle = varargin{i + 1}; 87 | case 'add', addstr = 'add'; 88 | 89 | otherwise 90 | warning(['Unrecognized command string : ' arg]) 91 | 92 | end 93 | end 94 | end 95 | 96 | % -------------------------------------- 97 | % * Read data and apply mask, if specified 98 | % -------------------------------------- 99 | 100 | % check for images in current dir 101 | if ~exist(fullfile(pwd, pimg)) || ~exist(fullfile(pwd, timg)) 102 | disp('Cannot find p- or t-image. You must go to a valid robust0??? results directory.'); 103 | end 104 | 105 | % return p-values for in-analysis voxels only 106 | if isempty(mask) 107 | [volInfo, dat] = iimg_read_img(pimg, 1); 108 | else 109 | [dat, volInfo] = iimg_mask(mask, pimg); 110 | end 111 | dat = dat(volInfo.wh_inmask); 112 | 113 | eligible_vox = ~isnan(dat) & (dat > 0); 114 | mp = min(dat(eligible_vox)); 115 | 116 | 117 | % -------------------------------------- 118 | % * Begin output report 119 | % -------------------------------------- 120 | 121 | fprintf(1, '\n\n---------------------------------------------------------------------\n') 122 | fprintf(1, 'Robust Regression output report') 123 | fprintf(1, '\n---------------------------------------------------------------------\n\n') 124 | 125 | fprintf(1, 'p-value image thresholded: %s\n', pimg) 126 | fprintf(1, '\nIn-analysis voxels: %3.0f total\n', sum(eligible_vox)); 127 | 128 | 129 | fprintf(1, '\nMin p-value = %3.6f, z = %3.2f\n', mp, norminv(1-mp)); 130 | 131 | % -------------------------------------- 132 | % * Threshold p-values 133 | % -------------------------------------- 134 | if isinf(pthr) 135 | dofdr = 1; 136 | pthr = .05; 137 | end 138 | 139 | if dofdr 140 | pt = FDR(dat(eligible_vox), pthr); % get threshold for FDR 141 | if isempty(pt) || isinf(pt), fprintf(1, 'No voxels pass FDR threshold.\n'); return, end 142 | fprintf(1, 'FDR: threshold is p < %3.4f\n', pt); 143 | else 144 | % uncorrected threshold 145 | pt = pthr; 146 | % overkill %[dat, volInfo] = iimg_threshold(dat, 'thr', [0 pthr], 'k', kthr); 147 | end 148 | 149 | dat(dat>pt) = 0; 150 | 151 | fprintf(1, '\nSignificant voxels at p < %3.6f:\nBefore size threshold: %3.0f ', pt, sum(dat>0)); 152 | 153 | % height threshold based on p-value 154 | [dat, nvox] = iimg_cluster_extent(dat, volInfo, kthr); 155 | 156 | %fprintf(1, '\nAfter size threshold: %3.0f, in %3.0f clusters of size %3.0f to %3.0f voxels\n', sum(dat>0), length(nvox{1}), min(nvox{1}), max(nvox{1})); 157 | fprintf(1, ' After size threshold: %3.0f\n', sum(dat>0)); 158 | 159 | % -------------------------------------- 160 | % * get t-values 161 | % -------------------------------------- 162 | 163 | % get t-values, masked with significant p- voxels 164 | dat = iimg_mask(dat, timg, volInfo); 165 | 166 | fprintf(1, ' + %3.0f significant voxels\n ', sum(dat>0)); fprintf(1, '- %3.0f significant voxels\n', sum(dat<0)); 167 | 168 | % -------------------------------------- 169 | % * write output images 170 | % -------------------------------------- 171 | 172 | if writeoutput 173 | % write output images 174 | if dofdr, fdrstr = '_fdr'; else fdrstr = []; end 175 | pstr = num2str(pthr); wh = find(pstr == '.'); wh = wh(1); pstr = pstr(wh+1:end); 176 | toutname = [timg(1:end-4) '_thr_p' fdrstr pstr timg(end-3:end)]; 177 | tposname = [timg(1:end-4) '_pos_mask_p' fdrstr pstr timg(end-3:end)]; 178 | tnegname = [timg(1:end-4) '_neg_mask_p' fdrstr pstr timg(end-3:end)]; 179 | end 180 | 181 | if writeoutput 182 | voldata = iimg_reconstruct_3dvol(dat, volInfo, 'outname', toutname); 183 | iimg_reconstruct_3dvol(dat>0, volInfo, 'outname', tposname); 184 | iimg_reconstruct_3dvol(dat<0, volInfo, 'outname', tnegname); 185 | else 186 | %voldata = iimg_reconstruct_3dvol(dat, volInfo); 187 | end 188 | 189 | % -------------------------------------- 190 | % * make clusters 191 | % -------------------------------------- 192 | 193 | % make clusters 194 | posdat = dat; posdat(posdat<0) = 0; 195 | negdat = dat; negdat(negdat>0) = 0; 196 | 197 | % still use size threshold in case a cluster above is both + and - 198 | clpos = iimg_indx2clusters(posdat, volInfo, eps, kthr); % save vals > eps 199 | clneg = iimg_indx2clusters(negdat, volInfo, [-Inf eps], kthr); % save vals between -Inf and eps 200 | 201 | for i = 1:length(clpos) 202 | clpos(i).Z_descrip = 't-values'; 203 | end 204 | 205 | for i = 1:length(clneg) 206 | clneg(i).Z_descrip = 't-values'; 207 | end 208 | 209 | if ~isempty(clpos), szp = cat(1, clpos.numVox); else szp = 0; end 210 | if ~isempty(clneg), szn = cat(1, clneg.numVox); else szn = 0; end 211 | 212 | fprintf(1, 'Suprathreshold clusters: \n + %3.0f (%3.0f to %3.0f voxels) ', length(clpos), min(szp), max(szp)); 213 | fprintf(1, '\n - %3.0f (%3.0f to %3.0f voxels)\n', length(clneg), min(szn), max(szn)); 214 | 215 | % -------------------------------------- 216 | % * display 217 | % -------------------------------------- 218 | 219 | if display && (~isempty(clpos) || ~isempty(clneg)) 220 | % cluster_orthviews([clpos clneg], 'bivalent', 'overlay', overlay, 'handle', orth_handle, addstr); 221 | cluster_orthviews([clpos clneg], 'overlay', overlay, 'handle', orth_handle, addstr); 222 | end 223 | 224 | % -------------------------------------- 225 | % * table 226 | % -------------------------------------- 227 | fprintf(1, '\nPositive results\n----------------------------\n') 228 | cluster_table(clpos, 1, 0); 229 | fprintf(1, '\nNegative results\n----------------------------\n') 230 | cluster_table(clneg, 1, 0); 231 | end -------------------------------------------------------------------------------- /robust_toolbox/older_functions/robust_results_threshold_2014.m: -------------------------------------------------------------------------------- 1 | function [o2, t] = robust_results_threshold_2014(varargin) 2 | % Makes maps of results from a CANlab Robust Regression Toolbox directory 3 | % IN DEVELOPMENT - VALID BUT NOT COMPLETE IN TERMS OF OPTIONS, ETC. 4 | % Other robust_results methods are also valid. 5 | % 6 | % Usage: 7 | % ------------------------------------------------------------------------- 8 | % o2 = robust_results_threshold_2014([optional inputs]) 9 | % 10 | % Author and copyright information: 11 | % ------------------------------------------------------------------------- 12 | % Copyright (C) 2014 Tor Wager 13 | % 14 | % This program is free software: you can redistribute it and/or modify 15 | % it under the terms of the GNU General Public License as published by 16 | % the Free Software Foundation, either version 3 of the License, or 17 | % (at your option) any later version. 18 | % 19 | % This program is distributed in the hope that it will be useful, 20 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | % GNU General Public License for more details. 23 | % 24 | % You should have received a copy of the GNU General Public License 25 | % along with this program. If not, see . 26 | % 27 | % Inputs: 28 | % ------------------------------------------------------------------------- 29 | % p Followed by (uncorrected) p-value threshold 30 | % k Followed by cluster extent threshold (k contiguous voxels) 31 | % 32 | % Outputs: 33 | % ------------------------------------------------------------------------- 34 | % o2 fmridisplay object with handles/info about display elements. 35 | % t A series of statistic_image objects for all t-maps in dir 36 | % 37 | % Examples: 38 | % ------------------------------------------------------------------------- 39 | % 40 | % give examples here 41 | % 42 | % See also: 43 | % * list other functions related to this one, and alternatives* 44 | 45 | % Programmers' notes: 46 | % List dates and changes here, and author of changes 47 | 48 | % ------------------------------------------------------------------------- 49 | % DEFAULTS AND INPUTS 50 | % ------------------------------------------------------------------------- 51 | 52 | % Defaults 53 | % ----------------------------------- 54 | pthr = .005; 55 | kthr = 1; 56 | 57 | 58 | % optional inputs with default values 59 | % ----------------------------------- 60 | 61 | for i = 1:length(varargin) 62 | if ischar(varargin{i}) 63 | switch varargin{i} 64 | % reserved keywords 65 | 66 | case {'p', 'pthr'}, pthr = varargin{i+1}; varargin{i+1} = []; 67 | case {'k', 'kthr'}, kthr = varargin{i+1}; varargin{i+1} = []; 68 | 69 | otherwise, warning(['Unknown input string option:' varargin{i}]); 70 | end 71 | end 72 | end 73 | 74 | % ------------------------------------------------------------------------- 75 | % LOAD SETUP AND ALL MAPS 76 | % ------------------------------------------------------------------------- 77 | 78 | [dd, dirname] = fileparts(pwd); 79 | 80 | setupfile = fullfile(pwd, 'SETUP.mat'); 81 | 82 | if ~exist(setupfile, 'file') 83 | error('You must be in a Robust Toolbox results directory. SETUP.mat is missing.') 84 | else 85 | load(setupfile) 86 | end 87 | 88 | names = {'Intercept (group activation)'}; 89 | 90 | tnames{1} = fullfile(pwd, 'rob_tmap_0001.img'); %dir(fullfile(pwd, 'rob_tmap*img')); 91 | pnames{1} = fullfile(pwd, 'rob_p_0001.img'); %dir(fullfile(pwd, 'rob_tmap*img')); 92 | 93 | indx = 2; 94 | while exist(tnames{end}, 'file') 95 | tnames{end + 1} = fullfile(pwd, sprintf('rob_tmap_%04d.img', indx)); 96 | pnames{end + 1} = fullfile(pwd, sprintf('rob_p_%04d.img', indx)); 97 | names{end + 1} = sprintf('Cov %d', indx-1); 98 | indx = indx + 1; 99 | end 100 | tnames(end) = []; 101 | pnames(end) = []; 102 | names(end) = []; 103 | 104 | if isempty(tnames), error('You must be in a Robust Toolbox results directory. No valid t-maps.'), end 105 | 106 | n = length(tnames); 107 | 108 | for i = 1:n 109 | t{i} = statistic_image('image_names', tnames{i}, 'type', 'T', 'dfe', SETUP.dfe); 110 | p{i} = statistic_image('image_names', pnames{i}); 111 | 112 | if size(t{i}.dat, 1) ~= size(p{i}.dat, 1), error('T and p images do not match in size/num voxels.'); end 113 | 114 | % replace p-values with robust regression ones - correct p-vals 115 | 116 | t{i}.p = p{i}.dat; 117 | end 118 | 119 | clear p 120 | 121 | % ------------------------------------------------------------------------- 122 | % THRESHOLD AND DISPLAY ALL MAPS 123 | % ------------------------------------------------------------------------- 124 | 125 | o2 = []; 126 | 127 | for i = 1:n 128 | t{i} = threshold(t{i}, pthr, 'unc', 'k', kthr); 129 | o2 = canlab_results_fmridisplay(t{i}, o2, 'nooutline', 'addmontages'); 130 | 131 | title([dirname ' ' names{i}], 'FontSize', 18) 132 | end 133 | 134 | 135 | end % function 136 | -------------------------------------------------------------------------------- /robust_toolbox/publish_robust_regression_report.m: -------------------------------------------------------------------------------- 1 | % Runs batch analyses and publishes HTML report with figures and stats to 2 | % results/published_output in local study-specific analysis directory. 3 | 4 | % Run this from the main CANLab robust regression results directory (basedir) 5 | 6 | close all 7 | 8 | resultsdir = pwd; 9 | 10 | fprintf('Creating HTML report for results in:\n%s\n', resultsdir); 11 | 12 | % ------------------------------------------------------------------------ 13 | % Check that we have a valid mediation results dir 14 | % ------------------------------------------------------------------------ 15 | 16 | is_robust_dir = exist(fullfile(resultsdir, 'SETUP.mat')); 17 | is_run = exist(fullfile(resultsdir, 'rob_tmap_0001.nii')) | exist(fullfile(resultsdir, 'rob_tmap_0001.img')); 18 | 19 | if ~is_robust_dir 20 | fprintf('%s\nis not a CANlab robust results directory because SETUP.mat is missing.\nSkipping report.\n', resultsdir); 21 | return 22 | end 23 | 24 | if ~is_run 25 | fprintf('Robust regression (robfit.m) does not appear to have run correctly because rob_tmap_0001.nii is missing.\nSkipping report.\n'); 26 | return 27 | end 28 | 29 | % ------------------------------------------------------------------------ 30 | % Set HTML report filename and options 31 | % ------------------------------------------------------------------------ 32 | 33 | pubdir = fullfile(resultsdir, 'published_output'); 34 | if ~exist(pubdir, 'dir'), mkdir(pubdir), end 35 | 36 | pubfilename = ['robust_regression_report_' scn_get_datetime]; 37 | 38 | p = struct('useNewFigure', false, 'maxHeight', 800, 'maxWidth', 1600, ... 39 | 'format', 'html', 'outputDir', fullfile(pubdir, pubfilename), 'showCode', false); 40 | 41 | % ------------------------------------------------------------------------ 42 | % Run and report status 43 | % ------------------------------------------------------------------------ 44 | 45 | publish('robust_results_batch', p) 46 | 47 | myhtmlfile = fullfile(pubdir, pubfilename, 'robust_results_batch.html'); 48 | 49 | if exist(myhtmlfile, 'file') 50 | 51 | fprintf('Saved HTML report:\n%s\n', myhtmlfile); 52 | 53 | web(myhtmlfile); 54 | 55 | else 56 | 57 | fprintf('Failed to create HTML report:\n%s\n', myhtmlfile); 58 | 59 | end 60 | 61 | -------------------------------------------------------------------------------- /robust_toolbox/robseed_results.m: -------------------------------------------------------------------------------- 1 | function [allcl] = robseed_results(pthresh,kthresh,df,varargin) 2 | % function [allcl] = robseed_results(pthresh,kthresh,df,['seeds',wh seeds],['overlay',overlayimg],['imagenames',extract_data_img_names]) 3 | % 4 | % can extract data, given image names from the corresponding files 5 | % 6 | % Example: 7 | % 5 vox, 15 df (17 subjects-2 params), seed 1 only, Extract data 8 | % cl = robseed_results(.005,5,15,1,EXPT.SNPM.P{1}); 9 | % 10 | % to plot correlated cluster 2 against seed 1: 11 | % load ../seed_clusters.mat 12 | % tor_fig(1,2);montage_clusters_maxslice([],clcorr(2),{'r'}); 13 | % subplot(1,2,2); plot_correlation_samefig(EXPT.seeds(:,1),cl(2).timeseries,[],'ko',0,1); 14 | % xlabel('L Amygdala seed region (Look - Reapp Neg)') 15 | % 16 | % seedcl = robseed_results(.005,3,19,'seeds',1,'overlay','../mean_funct.img'); 17 | 18 | allcl = []; 19 | names = {'rob_tmap_0002.img' 'rob_tmap_0003.img' 'rob_tmap_0004.img' 'rob_tmap_0005.img'}; 20 | datanames = []; 21 | ovl = []; 22 | 23 | for i = 1:length(varargin) 24 | if isstr(varargin{i}) 25 | switch varargin{i} 26 | % reserved keywords 27 | case 'overlay',ovl = varargin{i+1}; varargin{i+1} = []; 28 | case 'seeds',whseeds = varargin{i+1}; 29 | case 'imagenames',datanames = varargin{i+1}; varargin{i+1} = []; 30 | 31 | otherwise, warning(['Unknown input string option:' varargin{i}]); 32 | end 33 | end 34 | end 35 | 36 | if ~exist('whseeds','var'), whseeds = 1:length(names); end 37 | 38 | disp('Robseed_results.m') 39 | disp('Running for each of these results images: ') 40 | disp(char(names{whseeds})) 41 | disp(' ') 42 | 43 | 44 | [dum,dirnm] = fileparts(pwd); 45 | 46 | % % whseeds = 1:length(names); 47 | % % if length(varargin) > 0, whseeds = varargin{1}; end 48 | % % 49 | % % datanames = []; 50 | % % if length(varargin) > 1, datanames = varargin{2}; end 51 | % % 52 | % % ovl = []; 53 | % % if length(varargin) > 2, ovl = varargin{3}; end 54 | 55 | 56 | for i = whseeds 57 | 58 | figname = ['Contrast ' dirnm ', Seed ' num2str(i) '.']; 59 | 60 | if exist(names{i}) == 2 61 | disp(names{i}); disp('_________________________________'); 62 | p2 = threshold_imgs(names{i},tinv(1-pthresh,df),kthresh,'both'); 63 | cl = mask2clusters(p2); 64 | montage_clusters(ovl,cl,{'r'},[.5 .5]); 65 | set(gcf,'Name',figname) 66 | 67 | 68 | montage_clusters_medial(ovl,cl,{'r'},[.5 .5]); 69 | set(gcf,'Name',figname) 70 | disp(' '); 71 | 72 | 73 | % extract data, if names entered 74 | if ~isempty(datanames), cl = tor_extract_rois(datanames,cl); end 75 | 76 | allcl{i} = cl; 77 | end 78 | 79 | 80 | end 81 | 82 | if isempty(allcl), disp('No seeds found. (Are you in a robseed directory?) Exiting.'); end 83 | 84 | return 85 | -------------------------------------------------------------------------------- /robust_toolbox/robust_htw_results_plots.m: -------------------------------------------------------------------------------- 1 | function varargout = robust_htw_results_plots(meth, varargin) 2 | % 3 | % This function is a specialized function for the scnlab htw estimation 4 | % tools 5 | % For use with robust regression directories specifically 6 | % IN this framework, first level models must be run with SPM 5/8, and 7 | % apply_derivative_boost.m must be used to reconstruct htw (height, time to 8 | % peak, amplitude images) in each first-level directory. 9 | % This function should actually work for any robust results directory with 10 | % SPM models with basis functions at the first level, though. 11 | % It does not use robust reg weights (yet), unfortunately. 12 | % 13 | % This function makes plots of reconstructed HRFs across subjects for significant 14 | % regions stored in clusters .mat files 15 | % 16 | % This function has several methods, and can be run in the following 17 | % sequence of steps: 18 | % 19 | %cd('robust0001'); % change to a robust results directory 20 | % robust_htw_results_plots('setup'); % Gather image names and append to SETUP.mat 21 | % robust_htw_results_plots('define'); % Define conditions to plot/average over and append to SETUP.mat 22 | % clpos_data = robust_htw_results_plots('extract', clpos_data); % extract data from images 23 | % robust_htw_results_plots('plot', clpos_data); % Plot and save pngs 24 | 25 | 26 | load SETUP 27 | imgs = SETUP.files; 28 | n = size(imgs, 1); 29 | 30 | switch meth 31 | case 'setup' 32 | % =================================================== 33 | % Get list of files with image names 34 | 35 | for i = 1:n 36 | 37 | dd = fileparts(SETUP.files(i, :)); 38 | 39 | subjdirs{i} = dd; 40 | 41 | imglistnames{i} = fullfile(dd, 'db_amplitude_names.mat'); 42 | 43 | if ~exist(imglistnames{i}, 'file') 44 | disp(['Warning! Cannot find ' imglistnames{i}]) 45 | end 46 | 47 | spmmatnames{i} = fullfile(dd, 'SPM.mat'); 48 | 49 | if ~exist(imglistnames{i}, 'file') 50 | disp(['Warning! Cannot find ' spmmatnames{i}]) 51 | end 52 | end 53 | 54 | SETUP.HTWinfo.subjdirs = subjdirs; 55 | SETUP.HTWinfo.imglistnames = imglistnames; 56 | SETUP.HTWinfo.spmmatnames = spmmatnames; 57 | 58 | %% Load sample SPM mat and get trial types 59 | for i = 1:n 60 | 61 | [SETUP.HTWinfo.trialtypes{i}, SETUP.HTWinfo.nsess{i}, SETUP.HTWinfo.ntrialtypes{i}, SETUP.HTWinfo.bf{i}, SETUP.HTWinfo.betas_of_interest{i}, SETUP.HTWinfo.beta_names{i}] ... 62 | = get_spm_htw_info(spmmatnames{i}); 63 | 64 | end 65 | 66 | save SETUP -append SETUP 67 | %% 68 | 69 | case 'define' % define trial types to average and plot 70 | % =================================================== 71 | disp(SETUP.HTWinfo.trialtypes{1}) 72 | emptytostop = 1; 73 | conditions = zeros(SETUP.HTWinfo.ntrialtypes{1}, 1); 74 | 75 | indx = 1; 76 | gook = 1; 77 | while gook 78 | disp(['Enter vector of ones and zeros for condition ' num2str(indx) 'in brackets (e.g., [1 0 0 1 0 0]), or return to exit']); 79 | disp('Entries of 1 on the same row will be averaged over in plots.'); 80 | 81 | v = input(': '); 82 | if isempty(v) 83 | gook = 0; 84 | elseif length(v) ~= SETUP.HTWinfo.ntrialtypes{1}, disp('Invalid length.'); 85 | else 86 | disp('Accepted.'); 87 | conditions(:, indx) = v ./ sum(abs(v)); 88 | indx = indx + 1; 89 | end 90 | end 91 | 92 | disp('Entered conditions (weights for weighted sum in each condition to plot): '); 93 | SETUP.HTWinfo.conditions = conditions; 94 | print_matrix(conditions); 95 | 96 | for i = 1:size(conditions, 2) 97 | SETUP.HTWinfo.condnames{i} = input(['Enter string name for contrast ' num2str(i)], 's'); 98 | end 99 | 100 | disp(' '); 101 | disp('Adding contrast vectors to SETUP.HTWinfo and saving'); 102 | for i = 1:n 103 | SETUP.HTWinfo.contrastvectors{i} = repmat(conditions, SETUP.HTWinfo.nsess{i}, 1); 104 | end 105 | 106 | save SETUP -append SETUP 107 | 108 | 109 | case 'extract' % extract data from clusters 110 | % =================================================== 111 | cl = varargin{1}; 112 | 113 | fprintf('Extracting betas and reshaping: Subject %03d', 0); 114 | 115 | for i = 1:n 116 | fprintf('\b\b\b%03d', i); 117 | 118 | v = SETUP.HTWinfo.contrastvectors{i}; 119 | cli = tor_extract_rois(SETUP.HTWinfo.beta_names{i}, cl); 120 | 121 | for cc = 1:length(cl) 122 | cl(cc).HTW.betas{i} = cli(cc).timeseries; 123 | 124 | % Parse by basis function 125 | nbf = size(SETUP.HTWinfo.bf{i}, 2); 126 | cl(cc).HTW.betas_by_bf{i} = cell(1, nbf); 127 | for bb = 1:nbf 128 | cl(cc).HTW.betas_by_bf{i}{bb} = cl(cc).HTW.betas{i}(bb:nbf:end); 129 | end 130 | 131 | % Average : basis function x condition (rows x cols) 132 | for bb = 1:nbf , bf_x_condavg(:, bb) = (cl(cc).HTW.betas_by_bf{i}{bb}' * v)'; end 133 | cl(cc).HTW.bf_x_condavg{i} = bf_x_condavg; 134 | 135 | % Fits 136 | cl(cc).HTW.fit{i} = SETUP.HTWinfo.bf{i} * cl(cc).HTW.bf_x_condavg{i}; 137 | cl(cc).HTW.condnames = SETUP.HTWinfo.condnames; 138 | 139 | for jj = 1:size(v, 2) % for each condition 140 | cl(cc).HTW.group_fit{jj}(i, :) = cl(cc).HTW.fit{i}(:, jj)'; 141 | end 142 | 143 | 144 | 145 | end % cc, cluster loop 146 | end % i, subject loop 147 | 148 | for i = 1:n 149 | for cc = 1:length(cl) 150 | v = SETUP.HTWinfo.contrastvectors{i}; % same for all subjects 151 | 152 | % Weights 153 | for jj = 1:size(v, 2) % for each condition 154 | [avg,t,p,se, w] = robust_mean(mean(cl(cc).HTW.group_fit{jj}, 2)); 155 | w(isnan(w)) = 0; 156 | w = w ./ sum(w); 157 | cl(cc).HTW.weights{jj} = w; 158 | end 159 | 160 | % Weighted averages 161 | for jj = 1:size(v, 2) % for each condition 162 | % cl(cc).HTW.weighted_avg{jj} = cl(cc).HTW.group_fit{jj} * cl(cc).HTW.weights{jj}; 163 | end 164 | end 165 | end 166 | 167 | varargout{1} = cl; 168 | 169 | case 'plot' % extract data from clusters 170 | % =================================================== 171 | dirname = input('Enter dir name to save pngs: ', 's'); 172 | mkdir(fullfile(pwd, dirname)) 173 | 174 | cl = varargin{1}; 175 | 176 | colors = {'b' 'g' 'r'}; 177 | 178 | for cc = 1:length(cl) 179 | f1 = create_figure('HRF plot'); 180 | for jj = 1:length(cl(cc).HTW.group_fit) 181 | hh(jj) = tor_fill_steplot(cl(cc).HTW.group_fit{jj}, colors(jj), 0); 182 | end 183 | 184 | legend(hh, cl(cc).HTW.condnames) 185 | 186 | rlabel = ['Region' num2str(cc)]; 187 | savename = fullfile(pwd, dirname, [rlabel '.png']); 188 | 189 | title([dirname ' ' rlabel]); 190 | xlabel('Time'); 191 | ylabel('BOLD units'); 192 | 193 | saveas(f1, savename); 194 | 195 | end 196 | 197 | otherwise error('Unknown method input string.') 198 | 199 | end % case 200 | 201 | 202 | end % main function 203 | 204 | 205 | function [trialtypes, nsess, ntrialtypes, bf, betas_of_interest, beta_names] = get_spm_htw_info(spmmatname) 206 | 207 | load(spmmatname) 208 | trialtypes = []; 209 | nsess = length(SPM.Sess); 210 | ntrialtypes = length(SPM.Sess(1).U); 211 | 212 | for i = 1:ntrialtypes 213 | trialtypes = strvcat(trialtypes, SPM.Sess(1).U(i).name{1}); 214 | end 215 | 216 | disp(trialtypes) 217 | 218 | % Basis set 219 | bf = SPM.xBF.bf; 220 | nbf = size(bf, 2); 221 | 222 | % Betas of interest 223 | betas_of_interest = []; 224 | 225 | for i = 1:nsess 226 | %trialtypes = strvcat(trialtypes, SPM.Sess(1).U(i).name{1}); 227 | ntrialtypes = length(SPM.Sess(i).U); 228 | 229 | betas_of_interest = [betas_of_interest SPM.Sess(i).col(1:ntrialtypes * nbf)]; 230 | end 231 | 232 | create_figure; imagesc(zscore(SPM.xX.X(:, betas_of_interest))); set(gca, 'YDir', 'reverse'); axis tight; colormap gray; drawnow 233 | title('All columns of interest -- verify that these correspond to conditions of interest') 234 | 235 | % Get list of beta names 236 | % ------------------------------------------------- 237 | [dd, ff, ee] = fileparts(spmmatname); 238 | 239 | 240 | beta_names = cell(length(betas_of_interest), 1); 241 | for i = 1:length(betas_of_interest) 242 | beta_names{i} = sprintf('%s%sbeta_%04d.img', dd, filesep, betas_of_interest(i)); 243 | end 244 | 245 | beta_names = char(beta_names{:}); 246 | 247 | end % get_spm_htw_info 248 | 249 | %% 250 | 251 | -------------------------------------------------------------------------------- /robust_toolbox/robust_nonparametric_subfunctions/Robust_nonparam_flowchart.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/canlab/RobustToolbox/fd067f201faad887cff7713548ec1fe1652ff17a/robust_toolbox/robust_nonparametric_subfunctions/Robust_nonparam_flowchart.ppt -------------------------------------------------------------------------------- /robust_toolbox/robust_nonparametric_subfunctions/robust_max_partial_corr.m: -------------------------------------------------------------------------------- 1 | function [rrob,prob,Ymaxeffect,whY,Xadj,Yadj] = robust_max_partial_corr(X,Y,doplots,doprint) 2 | % function [rrob,prob,Ymaxeffect,whY,Xadj,Yadj] = robust_max_partial_corr(X,Y,doplots,doprint) 3 | % 4 | % tor wager, aug. 06 5 | % 6 | % finds maximum partial corr. for each column of X in a set of data columns 7 | % Y. Plots are optional (default = no plots, no stat printout to screen). 8 | % 9 | % Example: 10 | % Y = cl_pos(1).CONTRAST.all_data; 11 | % X = R.X; %3 columns, 3rd is intercept 12 | % [rrob,prob,Ymaxeffect,whY,Xadj,Yadj] = robust_max_partial_corr(X,Y,1); 13 | 14 | warning('off','stats:statrobustfit:IterationLimit'); 15 | 16 | if nargin < 3, doplots = 0; end 17 | if nargin < 4, doprint = 0; end 18 | 19 | % run robust reg. on each col of Y 20 | [b,t,p,sig,f,fp,fsig,stat] = robust_reg_matrix(X,Y,1); 21 | 22 | % get voxels x regs matrix of max effect 23 | whmat = abs(t') == repmat(max(abs(t'),[],1),size(t,2),1); 24 | 25 | % equivalent: 26 | % whmat = (p') == repmat(min((p'),[],1),size(p,2),1); 27 | 28 | % whY is which Y vector shows max effect 29 | [whY,Yindx] = find(whmat); 30 | if size(whY,2) < length(whY), whY = whY'; end 31 | if size(Yindx,2) < length(Yindx),Yindx = Yindx'; end 32 | 33 | % Obs. x regressors matrix of Y data with max effect for each reg. 34 | Ymaxeffect = Y(:,whY); 35 | 36 | % partial correlations 37 | % rrob and prob show max partial corr for each regressor 38 | for i = Yindx 39 | [Xadj(:,i),Yadj(:,i),rrob(i),prob(i)] = partialcor(X,Ymaxeffect(:,i),i,doprint); 40 | end 41 | 42 | 43 | % plotting stuff 44 | % --------------------------------------- 45 | if doplots 46 | wh_intercept = find_intercept(X); 47 | k = length(Yindx); 48 | tor_fig(1,k) 49 | 50 | for i = Yindx 51 | subplot(1,k,i) 52 | if i == wh_intercept 53 | barplot_columns(Yadj(:,i),[],[],'nofig','dorob'); 54 | else 55 | plot_correlation(X,Ymaxeffect(:,i),'col',i,'robust'); 56 | end 57 | 58 | ylabel(['Y column ' num2str(whY(i))]) 59 | xlabel(['Regressor ' num2str(i)]); 60 | end 61 | 62 | end 63 | 64 | 65 | warning('on','stats:statrobustfit:IterationLimit'); 66 | 67 | return 68 | 69 | 70 | % ------------------------------------------------------------------- 71 | % COMPUTE: misc functions 72 | % ------------------------------------------------------------------- 73 | 74 | % ------------------------------------------------------------------- 75 | 76 | function val = find_intercept(X) 77 | 78 | val = find(~any(diff(X))); 79 | 80 | if isempty(val), error('X should contain an intercept.'); end 81 | return 82 | 83 | 84 | -------------------------------------------------------------------------------- /robust_toolbox/robust_nonparametric_subfunctions/robust_nonpar_cluster_tables.m: -------------------------------------------------------------------------------- 1 | function robust_nonpar_cluster_tables(R,doextended,wh_regressor,cl_all,cl_sig,cl_pos,cl_neg,rfieldname,pfieldname,uthr,dat,cl_extent_pos,cl_extent_neg) 2 | % robust_nonpar_cluster_tables(R,doextended,wh_regressor,cl_all,cl_sig,cl_pos,cl_neg,rfieldname,pfieldname,uthr,dat,cl_extent_pos,cl_extent_neg) 3 | % 4 | % 5 | % tor wager 6 | % used in robust_nonparam_displayregions 7 | % run that high-level function to configure necessary inputs 8 | % 9 | % dat is matrix of image at different thresholds (one thr.image per column) 10 | % [dat,dat_pos,dat_neg,A] = robust_nonparam_orthviews(R,wh_regressor,overlay,showrois,A); 11 | % robust_nonpar_cluster_tables(R,1,1,A.cl_all,A.cl_sig,A.cl_pos,A.cl_neg,A.rfieldname,A.pfieldname,.05,dat,A.cl_uncor_pos,A.cl_uncor_neg); 12 | 13 | numc = R.volInfo.c; % number of clusters 14 | %n = R.volInfo.nvox; 15 | %[k,v] = size(R.correct.t); 16 | 17 | if ~isempty(cl_sig) 18 | 19 | % Tables: ROIs with sig values in them 20 | % --------------------------------------------------------------------- 21 | 22 | fprintf(1,'\n* Note: maxstat is %s','abs(t threshold)') 23 | 24 | fprintf(1,'\n* Significant ROIs ') 25 | fprintf(1,'\n* ========================================================================\n') 26 | cluster_table(cl_all,1,0,'threshold'); 27 | 28 | % --------------------------------------------------------------------- 29 | 30 | 31 | % Tables: Significant contig regions within ROIs 32 | % --------------------------------------------------------------------- 33 | 34 | fprintf(1,'\n* Significant regions within ROIs ') 35 | fprintf(1,'\n* ========================================================================\n') 36 | if doextended 37 | fprintf(1,'\n> Positive ') 38 | extended_table(cl_pos,R,rfieldname,pfieldname) 39 | fprintf(1,'\n> Negative ') 40 | extended_table(cl_neg,R,rfieldname,pfieldname) 41 | else 42 | fprintf(1,'\n> Positive ') 43 | cluster_table(cl_pos,1,0,'threshold','from_cluster'); 44 | fprintf(1,'\n> Negative ') 45 | cluster_table(cl_neg,1,0,'threshold','from_cluster'); 46 | end 47 | 48 | % --------------------------------------------------------------------- 49 | 50 | 51 | % Tables: Extent at uncorrected thresholds 52 | % --------------------------------------------------------------------- 53 | 54 | fprintf(1,'\nExtent at uncorrected thresholds (numbers are voxels corrected or at this uncorr. thresh. or higher)\n') 55 | 56 | % add number significant at uncorrected thresholds and print table 57 | nthresh = length(uthr); 58 | 59 | ndat = size(dat,2); 60 | if ndat == nthresh + 1 61 | % we have significant 'corrected' in 1st col. of dat 62 | addcol = 1; 63 | elseif ndat == ndat 64 | % we have uncorrected thresh only in dat 65 | addcol = 0; 66 | else 67 | error('number of thresholds does not match size of dat input.') 68 | end 69 | 70 | for j = 1:nthresh 71 | dats = sum(dat(:,1:j+addcol)>0,2); % signficant uncor. voxels at this threshold 72 | 73 | for c = 1:numc 74 | 75 | sigvox = R.svc.tsig{c}(:,wh_regressor); 76 | 77 | % get sig voxels (dats) adjacent to at least one 78 | dats_pruned = iimg_cluster_prune(dats,sigvox,R.volInfo); 79 | 80 | nsig(c,j) = sum(dats_pruned); % num sig at this threshold in or adjacent 81 | 82 | end 83 | 84 | end 85 | 86 | % header 87 | fprintf(1,'\nCluster\t') 88 | for nt = 1:nthresh 89 | fprintf(1,'%s %3.3f\t','N @ ', uthr(nt)) 90 | end 91 | fprintf(1,'\n'); 92 | 93 | % body 94 | for c = 1:numc 95 | if any(nsig(c,:)) 96 | fprintf(1,'Cl. %3.0f\t', c) 97 | fprintf(1,repmat('%3.0f\t',1,nthresh),nsig(c,:)) 98 | fprintf(1,'\n'); 99 | end 100 | end 101 | fprintf(1,'\n'); 102 | 103 | % --------------------------------------------------------------------- 104 | 105 | 106 | fprintf(1,'\n* Significant regions : uncorrected p < %3.3f within ROIs ',uthr(end)) 107 | fprintf(1,'\n* ========================================================================\n') 108 | fprintf(1,'\n> Positive ') 109 | cluster_table(cl_extent_pos,1,0); 110 | fprintf(1,'\n> Negative ') 111 | cluster_table(cl_extent_neg,1,0); 112 | 113 | end 114 | 115 | return 116 | 117 | 118 | 119 | % ------------------------------------------------------------------- 120 | % TABLE: extended output table for a set of clusters 121 | % ------------------------------------------------------------------- 122 | 123 | function extended_table(cl_sig,R,rfieldname,pfieldname) 124 | %cluster_table(cl_sig,1,0,'p','threshold','from_cluster'); 125 | str = 'cluster_table(cl_sig,1,0,''p'',''threshold'',''from_cluster'''; 126 | for myregressor = 1:size(R.X,2) 127 | str = [str ',''' rfieldname{myregressor} ''',''' pfieldname{myregressor} '''']; 128 | end 129 | str = [str ');']; 130 | eval(str) 131 | 132 | return 133 | 134 | -------------------------------------------------------------------------------- /robust_toolbox/robust_nonparametric_subfunctions/robust_nonpar_set_level_svc.m: -------------------------------------------------------------------------------- 1 | function robust_nonpar_set_level_svc(R,wh_cl) 2 | % robust_nonpar_set_level_svc(R,wh_cl) 3 | % 4 | % Computes number of significant ROIs needed to reject omnibus hypothesis 5 | % of no significant activation in any ROI. 6 | % 7 | % numc is the number of ROIs. Optional input wh_cl (vector of which clusters) overrides the total 8 | % number of ROIs saved in the R structure, in case you want to correct over 9 | % a different number (i.e., a subset of ROIs of primary interest). 10 | 11 | if nargin < 2 12 | numc = R.volInfo.c; 13 | wh_cl = 1:numc; 14 | else 15 | numc = length(wh_cl); 16 | end 17 | 18 | 19 | niter = size(R.maxt,2); 20 | 21 | % Number of significant ROIs expected (mapwise correction for # ROIS) 22 | % ------------------------------------------------------------------- 23 | ntoprint = min(8,numc); 24 | pvals = 1 - binomcdf(1:ntoprint,numc,.05); 25 | fprintf(1,'\nSet-level p-values for number of significant ROIs') 26 | fprintf(1,'\nThis provides a correction based on the number of') 27 | fprintf('\n significant contiguous ROIs in your search mask') 28 | fprintf('\n for each column in your design matrix.') 29 | fprintf('\n significant p-values indicate more ROIs show effects') 30 | fprintf('\n with small-volume correction than would be expected by chance.') 31 | fprintf(1,'\n-------------------------------------------------\n') 32 | fprintf(1,'%3.0f\t',1:ntoprint) 33 | fprintf(1,'\nBinomial model: One-tailed:\n') 34 | fprintf(1,'%3.4f\t',pvals) 35 | fprintf(1,'\nBinomial model: Two-tailed:\n') 36 | fprintf(1,'%3.4f\t',min(1,2*pvals)) 37 | 38 | n_sig_needed = min(find(pvals <= .05)); 39 | n_sig_needed2 = min(find(pvals <= .025)); 40 | fprintf(1,'\n\n# sig. ROIs needed to reject mapwise omnibus of no activation\n'); 41 | fprintf(1,'%3.0f one-tailed, %3.0f two-tailed\n',n_sig_needed,n_sig_needed2); 42 | 43 | % nonparametric version 44 | fprintf(1,'\nNonparametric: \n') 45 | k = size(R.X,2); 46 | 47 | for col = 1:k 48 | % t values for this regressor in this cluster 49 | dat = zeros(numc, niter); 50 | 51 | % get list of which ROIs activated in the Monte Carlo simulation for 52 | % each iteration and for this regressor 53 | for i = 1:length(wh_cl) 54 | dat(i,:) = R.maxt_by_cl{wh_cl(i)}(col,:); 55 | end 56 | thr = repmat(R.svc.tcrit(wh_cl, col), 1, niter); 57 | dat = sum(dat > thr); 58 | 59 | % get p-values for small numbers of sig. ROIs 60 | for i = 1:ntoprint 61 | pvals(i) = sum(dat >= i) ./ niter; 62 | end 63 | 64 | fprintf(1,'\n%s, One-tailed nonparametric p-values:\n',R.names{col}) 65 | fprintf(1,'%3.4f\t',pvals) 66 | 67 | % get number of sig. ROIs needed 68 | n_sig_needed = prctile(dat,95); 69 | n_sig_needed2 = prctile(dat,97.5); 70 | fprintf(1,'\nCritical number: %3.0f one-tailed, %3.0f two-tailed\n',n_sig_needed,n_sig_needed2); 71 | fprintf(1,'\n') 72 | 73 | end 74 | 75 | return -------------------------------------------------------------------------------- /robust_toolbox/robust_nonparametric_subfunctions/robust_nonparam_get_sig_clusters.m: -------------------------------------------------------------------------------- 1 | function [A,R] = robust_nonparam_get_sig_clusters(R,wh_regressor,doextended) 2 | % function [A,R] = robust_nonparam_get_sig_clusters(R,wh_regressor,doextended) 3 | % 4 | % A has: 5 | %A.cl_all,A.cl_sig,A.wh_sig,A.p_sig,R,A.cl_pos,A.cl_neg,A.p_pos,A.p_neg, 6 | %A.pfieldname,A.rfieldname 7 | % 8 | % Get cluster significance information: indices of significant values and 9 | % clusters structures 10 | % from robust_nonpar structure R, given contrast and effect of interest 11 | % 12 | % tor wager 13 | 14 | % make sure we have data, if we're doing partial correlations and stuff 15 | if doextended 16 | R = load_data_subfunction(R); 17 | end 18 | 19 | % ------------------------------------------------------------------- 20 | % Sizes and data 21 | % ------------------------------------------------------------------- 22 | 23 | numc = R.volInfo.c; % number of clusters 24 | n = R.volInfo.nvox; 25 | [k,v] = size(R.correct.t); 26 | 27 | % all voxels in cluster 28 | allvox = ones(v,1); % for actual t-values : R.correct.t(wh_regressor,:)'; 29 | A.cl_all = iimg_indx2clusters(allvox,R.volInfo); 30 | 31 | 32 | A.cl_sig = cell(1,numc); % significant clusters 33 | A.wh_sig = logical(zeros(1,numc)); % which clusters 34 | A.p_sig = (zeros(v,1)); 35 | A.p_pos = A.p_sig; 36 | A.p_neg = A.p_sig; 37 | 38 | 39 | % ------------------------------------------------------------------- 40 | % Loop through ROIs 41 | % ------------------------------------------------------------------- 42 | 43 | for c = 1:numc 44 | 45 | % this is signed, pos or neg. 46 | sigvox = R.svc.tsig{c}(:,wh_regressor); 47 | 48 | whpos = find(sigvox > 0); 49 | whneg = find(sigvox < 0); 50 | 51 | % all sig. voxels 52 | whvox = abs(sigvox) > 0; 53 | 54 | % for t-values (signed) 55 | sigvox = R.correct.t(wh_regressor,:)' .* whvox; 56 | 57 | % save vector of all significant results for visualization (p-values) 58 | A.p_sig(whvox) = R.correct.p(wh_regressor,whvox); 59 | 60 | % significant voxels only 61 | [A.cl_sig{c},in_cluster] = iimg_indx2clusters(sigvox,R.volInfo,R.svc.tcrit(c,wh_regressor)); 62 | 63 | % positive and negative clusters and p-values 64 | sigpos = sigvox; 65 | sigpos(sigpos < 0) = 0; 66 | 67 | A.p_pos(whpos) = R.correct.p(wh_regressor,whpos); 68 | 69 | A.cl_pos{c} = iimg_indx2clusters(sigpos,R.volInfo,R.svc.tcrit(c,wh_regressor)); 70 | 71 | signeg = sigvox; 72 | signeg(signeg > 0) = 0; 73 | A.p_neg(whneg) = R.correct.p(wh_regressor,whneg); 74 | 75 | A.cl_neg{c} = iimg_indx2clusters(signeg,R.volInfo,R.svc.tcrit(c,wh_regressor)); 76 | 77 | 78 | % save additional info: from_cluster, and partial corrs if requested 79 | % ------------------------------------------------------------------- 80 | if isfield(R,'Y'), str = sprintf('Getting partial correlations for cluster %03d',c); fprintf(1,str); end 81 | 82 | [A.cl_sig,A.pfieldname,A.rfieldname] = get_partial_correlations(A.cl_sig,c,in_cluster,R,wh_regressor); 83 | 84 | A.cl_pos = get_partial_correlations(A.cl_pos,c,in_cluster,R,wh_regressor); 85 | A.cl_neg = get_partial_correlations(A.cl_neg,c,in_cluster,R,wh_regressor); 86 | 87 | 88 | A.wh_sig(c) = ~isempty(A.cl_sig{c}); 89 | 90 | if isfield(R,'Y'), erase_string(str); end 91 | end 92 | 93 | A.cl_all = A.cl_all(A.wh_sig); 94 | A.cl_sig = cat(2,A.cl_sig{:}); 95 | 96 | A.cl_pos = cat(2,A.cl_pos{:}); 97 | A.cl_neg = cat(2,A.cl_neg{:}); 98 | 99 | return 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | % ------------------------------------------------------------------- 112 | % DATA: check for R.Y and load if necessary. Check image names 113 | % ------------------------------------------------------------------- 114 | 115 | function [R,Y] = load_data_subfunction(R) 116 | sprintf('Loading data. '); 117 | 118 | if isfield(R,'Y') && ~isempty(R.Y) 119 | disp('R.Y data field found. Using it.'); 120 | Y = R.Y; 121 | 122 | if ~exist(deblank(R.maskname),'file') 123 | fprintf(1,'\n* Error: R.maskname is not a valid file.\nImage: %s\nPlease select.\n',R.maskname) 124 | R.maskname = spm_get(1,'*','Select new mask image'); 125 | end 126 | 127 | else 128 | % check image names and get data 129 | 130 | if ~exist(deblank(R.image_names(1,:)),'file') 131 | fprintf(1,'\n* Error: R.image_names are not valid files. Assuming names are correct but path is wrong!\n') 132 | R.image_names = filename_get_new_root_dir(R.image_names, ... 133 | spm_get(-1,'*','Select root dir for files'),2); 134 | end 135 | 136 | if ~exist(deblank(R.maskname),'file') 137 | fprintf(1,'\n* Error: R.maskname is not a valid file.\nImage: %s\nPlease select.\n',R.maskname) 138 | R.maskname = spm_get(1,'*','Select new mask image'); 139 | end 140 | 141 | % load data and save in R 142 | Y = iimg_get_data(R.maskname,R.image_names); 143 | R.Y = Y; 144 | end 145 | if ~isfield(R,'volInfo'), R.volInfo = iimg_read_img(R.maskname); end 146 | 147 | return 148 | 149 | 150 | % ------------------------------------------------------------------- 151 | % DATA: from_cluster and max partial corrs for a series of clusters in cell 152 | % c 153 | % ------------------------------------------------------------------- 154 | 155 | function [cl_sig,pfieldname,rfieldname] = get_partial_correlations(cl_sig,c,in_cluster,R,wh_regressor) 156 | 157 | % names 158 | k = size(R.X,2); 159 | pfieldname = cell(1,k); 160 | rfieldname = cell(1,k); 161 | 162 | for myregressor = 1:k 163 | 164 | pfieldname{myregressor} = [R.names{myregressor} '_p']; 165 | rfieldname{myregressor} = [R.names{myregressor} '_partialr_or_mean']; 166 | if any(pfieldname{myregressor} == ' '), error('No spaces or special chars allowed in R.names!!'); end 167 | end 168 | 169 | % correlations 170 | for j = 1:length(cl_sig{c}) 171 | cl_sig{c}(j).from_cluster = c; 172 | 173 | cl_all(c).threshold = R.svc.tcrit(c,wh_regressor); 174 | cl_sig{c}(j).threshold = R.svc.tcrit(c,wh_regressor); 175 | 176 | if isfield(R,'Y') 177 | 178 | % get max effect or partial correlation for the effect of 179 | % interest 180 | %[cl_sig{c}(j).p,cl_sig{c}(j).correl] = ... 181 | % get_max_partial_cor(R.X,R.Y,in_cluster == j,wh_regressor); 182 | 183 | % get max effect or partial correlation for the other 184 | % predictors 185 | % for myregressor = 1:size(R.X,2) 186 | % 187 | % [cl_sig{c}(j).(pfieldname{myregressor}),cl_sig{c}(j).(rfieldname{myregressor})] = ... 188 | % get_max_partial_cor(R.X,R.Y,in_cluster == j,myregressor); 189 | % 190 | % 191 | % end 192 | 193 | % get max effect or partial correlation for all 194 | % predictors 195 | [rrob,prob,Ymaxeffect,whY,Xadj,Yadj] = robust_max_partial_corr(R.X,R.Y(:,in_cluster==j),0); 196 | for myregressor = 1:size(R.X,2) 197 | cl_sig{c}(j).(rfieldname{myregressor}) = rrob(myregressor); 198 | cl_sig{c}(j).(pfieldname{myregressor}) = prob(myregressor); 199 | end 200 | 201 | cl_sig{c}(j).Ymaxeffect = Ymaxeffect; 202 | cl_sig{c}(j).whY = whY; 203 | 204 | % get max effect or partial correlation for the effect of 205 | % interest 206 | cl_sig{c}(j).p = prob(wh_regressor); 207 | cl_sig{c}(j).correl = rrob(wh_regressor); 208 | end 209 | end 210 | 211 | return 212 | 213 | 214 | % ------------------------------------------------------------------- 215 | % DATA: max partial correlation in a cluster (or set of voxels generally) 216 | % ------------------------------------------------------------------- 217 | 218 | % function [p,r] = get_max_partial_cor(X,Y,vox_index,wh_regressor) 219 | % 220 | % warning('off','stats:statrobustfit:IterationLimit'); 221 | % 222 | % dat = Y(:,vox_index); % for mean of region: mean(R.Y(:,in_cluster == j), 2); 223 | % 224 | % 225 | % 226 | % % get max partial correlation and min p 227 | % % partialcor uses robust by default 228 | % nvox = size(dat,2); 229 | % rrob = zeros(nvox,1); 230 | % prob = rrob; 231 | % for vox = 1:nvox 232 | % [x,y,rrob(vox),prob(vox)] = partialcor(X,dat(:,vox),wh_regressor); 233 | % end 234 | % 235 | % p = min(prob); 236 | % p = p(1); 237 | % r = rrob(prob == p); 238 | % r = r(1); 239 | % 240 | % warning on 241 | % return 242 | 243 | 244 | 245 | function erase_string(str1) 246 | fprintf(1,repmat('\b',1,length(str1))); % erase string 247 | return 248 | -------------------------------------------------------------------------------- /robust_toolbox/robust_nonparametric_subfunctions/robust_nonparam_interactive_scatterplot.m: -------------------------------------------------------------------------------- 1 | function cl = robust_nonparam_interactive_scatterplot(meth,X,cl,wh_interest,image_names) 2 | % cl = robust_nonparam_interactive_scatterplot(meth,X,cl,wh_interest,image_names) 3 | % 4 | % meth: Method: 5 | % 'max' plots max correlation, uses cl.Ymaxdata field (see 6 | % robust_nonparam_get_sigregions.m) 7 | % 'mean' plots mean correlation; you must enter valid image_names 8 | % 9 | % wh_interest: which column of X is of interest 10 | % Works on existing orthviews figure 11 | % 12 | % tor wager, aug. 06 13 | % 14 | % Examples: 15 | % partial correlations of first column of R.X, for cl_pos clusters 16 | % robust_nonparam_interactive_scatterplot('max',R.X,cl_pos,1); 17 | % 18 | % partial correlations of 1st col. with input images, mean of cluster 19 | % robust_nonparam_interactive_scatterplot('mean',R.X,[A.cl_pos 20 | % A.cl_neg],1,R.image_names); 21 | % 22 | % 23 | 24 | 25 | %wh_interest = 1; 26 | 27 | % --------------------------------------------------- 28 | % setup model 29 | % --------------------------------------------------- 30 | nuis = X; 31 | nuis(:,wh_interest) = []; 32 | for i = 1:length(cl) 33 | cl(i).xdat = X(:,wh_interest); 34 | cl(i).nuisance = nuis; 35 | end 36 | 37 | 38 | % --------------------------------------------------- 39 | % setup data 40 | % --------------------------------------------------- 41 | switch meth 42 | case 'max' 43 | % max effect 44 | for i = 1:length(cl), cl(i).timeseries = cl(i).Ymaxeffect(:,1); end 45 | case 'mean' 46 | cl = extract_contrast_data({image_names},cl); 47 | for i = 1:length(cl), cl(i).timeseries = cl(i).CONTRAST.data(:,1); end 48 | 49 | otherwise 50 | error('Unknown method') 51 | end 52 | 53 | 54 | %R.image_names = filename_get_new_root_dir(R.image_names,spm_get(-1),2); 55 | % cl = extract_contrast_data({R.image_names},cl); 56 | % for i = 1:length(cl) 57 | % cl(i).xdat = X(:,1); 58 | % cl(i).nuisance = X(:,2); 59 | % cl(i).timeseries = cl(i).CONTRAST.data(:,1); 60 | % end 61 | 62 | % --------------------------------------------------- 63 | % interactive scatterplot 64 | % --------------------------------------------------- 65 | [xdat,ydat] = cluster_interactive_scatterplot(cl); 66 | 67 | doauto = input('Save pngs of clusters automatically? '); 68 | 69 | if doauto 70 | 71 | % This automatically executes the windowbtnup function 72 | cl = cluster_export_pngs(cl,1,[],1); 73 | 74 | end 75 | 76 | return 77 | 78 | 79 | 80 | 81 | 82 | 83 | % --------------------------------------------------- 84 | % subfunctions needed for 'auto' mode only 85 | % --------------------------------------------------- 86 | 87 | function cluster_interactive_callback(varargin) 88 | 89 | % get data stored in figure 90 | % should contain: clusterfield, xlab, ylab 91 | % if doesn't exist, create as default: 'timeseries' 92 | fh = findobj('Tag','Graphics'); figure(fh); 93 | data = guidata(fh); 94 | 95 | N = fieldnames(data); 96 | for i=1:length(N) 97 | eval([N{i} ' = data.' N{i} ';']); 98 | end 99 | 100 | % check for data in specified field of cl 101 | if ~isfield(cl,clusterfield), display_error('nodata',clusterfield); end 102 | if ~isfield(cl,'xdat'), display_error('noxdata',clusterfield); end 103 | 104 | spm_orthviews_showposition; 105 | 106 | % activate scatterplot axis 107 | axes(axish); 108 | 109 | % find closest cluster and return index 110 | % --------------------------------------------------- 111 | pos = spm_orthviews('Pos')'; 112 | 113 | 114 | % check to see if we're in a cluster 115 | wh = 0; 116 | centers = cat(1,cl.mm_center); 117 | 118 | % find closest cluster, based on center 119 | d = distance(pos,centers); wh = find(d == min(d)); wh = wh(1); 120 | 121 | % only accept if cursor is w/i 2 mm of a voxel in the cluster 122 | d = distance(pos,cl(wh).XYZmm'); d = min(d); if d > 15, wh = 0;,end 123 | 124 | 125 | 126 | if wh 127 | %cluster_table(cl(wh)); 128 | fprintf(1,'Cl. %3.0f, Voxels: %3.0f, Coords: %3.0f, %3.0f, %3.0f\n',wh,cl(wh).numVox,cl(wh).mm_center(1), cl(wh).mm_center(2),cl(wh).mm_center(3)); 129 | 130 | %spm_orthviews('Reposition',cl(wh).mm_center); 131 | 132 | % get data from structure 133 | ydat = cl(wh).(clusterfield); 134 | xdat = cl(wh).xdat; 135 | 136 | % check for data 137 | if isempty(ydat),display_error('nodata',clusterfield); end 138 | if isempty(xdat), display_error('noxdata',clusterfield), end 139 | 140 | cla; 141 | 142 | % get design matrix, with 1st column as regressor of interest 143 | dorobust = 1; 144 | if isfield(cl, 'nuisance') && ~isempty(cl(wh).nuisance) 145 | X = [xdat cl(wh).nuisance]; 146 | else 147 | X = xdat; 148 | end 149 | if no_intercept(X), X(:,end+1) = 1; end 150 | 151 | %[tmp,tmp,r] = partialcor(X,ydat,1,1,dorobust); 152 | 153 | plot_correlation(X,ydat,'robust','xlabel',data.xlab,'ylabel',data.ylab); 154 | 155 | % if isfield(cl, 'nuisance') && ~isempty(cl(wh).nuisance) 156 | % [r,str,sig,ry,rx,h,rr] = prplot(ydat,[xdat cl(wh).nuisance],1,1,{'ko'}); 157 | % else 158 | % plot_correlation_samefig(xdat,ydat,[],'ko',0,1); 159 | % end 160 | %axis auto; 161 | % xlabel(data.xlab); 162 | % ylabel(data.ylab); 163 | 164 | drawnow 165 | else 166 | cla; 167 | str = sprintf('No nearby cluster. Distance is %3.0f',d); 168 | fprintf(1,str); pause(.5); fprintf(repmat('\b',1,length(str))); 169 | end 170 | 171 | return 172 | 173 | 174 | 175 | function display_error(meth,clusterfield) 176 | 177 | switch meth 178 | case 'nodata' 179 | disp(['You have asked to get data from cl.' clusterfield ', but there is no data there.']); 180 | error('For a fix, try cl = tor_extract_rois(my_image_name_matrix_here,cl);'); 181 | 182 | case 'noxdat' 183 | error('You must assign the x-axis data to plot to cl(*).xdat);'); 184 | end 185 | 186 | return 187 | 188 | 189 | function val = no_intercept(X) 190 | 191 | val = all(any(diff(X))); 192 | 193 | return -------------------------------------------------------------------------------- /robust_toolbox/robust_nonparametric_subfunctions/robust_nonparam_orthviews.m: -------------------------------------------------------------------------------- 1 | function [dat,dat_pos,dat_neg,A] = robust_nonparam_orthviews(R,wh_regressor,overlay,showrois,A) 2 | % [dat,dat_pos,dat_neg,A] = robust_nonparam_orthviews(R,wh_regressor,overlay,showrois,[A]) 3 | % 4 | % p_sig,cl_pos, and cl_neg inputs can be empty or missing, in which case 5 | % robust_nonparam_get_sig_clusters is run to get it. 6 | % 7 | % A is "activation" structure with cl_pos and cl_neg in it; see 8 | % robust_nonparam_get_sig_clusters. If not input, 9 | % robust_nonparam_get_sig_clusters is run here. 10 | % 11 | % Examples: 12 | % To run using saved clusters from previous 13 | % robust_nonparam_displayregions (get 3rd regressor stats): 14 | % [dat,dat_pos,dat_neg] = robust_nonparam_orthviews(R,3,EXPT.overlay,1,A); 15 | % 16 | % To run without saved clusters, creating them here: 17 | % [dat,dat_pos,dat_neg,A] = robust_nonparam_orthviews(R,wh_regressor,overlay,showrois); 18 | % 19 | % tor wager 20 | 21 | % wh_regressor = 3; 22 | % overlay = EXPT.overlay; 23 | % showrois = 1; 24 | 25 | pvals = R.correct.p(wh_regressor,:)'; 26 | tvals = R.correct.t(wh_regressor,:)'; 27 | 28 | 29 | uthr = [.005 .01 .05]; % uncorrected thresholds for display 30 | poscolors = [1 1 0; 1 .5 0; 1 .3 .3; .8 0 0; 0 1 0]; % corrected first; last is color for ROIs if transseed 31 | negcolors = [0 0 1; 0 .5 1; .3 .5 1; .3 .3 1; 0 1 0]; 32 | 33 | % need all voxels even at lowest threshold 34 | % dat needed as output for main program 35 | % now handled below 36 | %[cl_uncor,dat] = iimg_multi_threshold(pvals,'p','thresh',uthr,'size',[1 1 1], ... 37 | %'pruneseed',p_sig,'volInfo',R.volInfo,'overlay',overlay,'colors',poscolors); 38 | 39 | 40 | % just need p_sig 41 | if nargin < 5 42 | A = robust_nonparam_get_sig_clusters(R,wh_regressor,0); 43 | end 44 | 45 | 46 | fprintf(1,'\n* Orthviews: All results within ROIs ') 47 | % --------------------------------------------------------------------- 48 | % Orthviews: Sig. within ROIs (all regions) 49 | % --------------------------------------------------------------------- 50 | % only in extended sig region; to get DAT 51 | % this returns only areas within ROIs that are significant, because of 52 | % the implicit mask 53 | 54 | % Without regions of interest (already done above) 55 | %[cl_uncor,dat] = iimg_multi_threshold(pvals,'p','thresh',uthr,'size',[1 1 1], ... 56 | %'pruneseed',A.p_sig,'volInfo',R.volInfo,'overlay',overlay,'colors',poscolors); 57 | 58 | fprintf('\nShowing results for uncorrected thresholds in orange/red for positive results'); 59 | fprintf('\nand blue/purple for negative results.') 60 | fprintf('\nResults with corrected significance will be shown in yellow or dark blue.'); 61 | fprintf('\n') 62 | 63 | % --------------------------------------------------------------------- 64 | 65 | % Orthviews: Positive 66 | % --------------------------------------------------------------------- 67 | % 68 | % anywhere within with ROIs 69 | % this is enforced implicitly because of the ROI mask applied earlier 70 | [A.cl_uncor_pos,dat_pos] = iimg_multi_threshold(pvals,'p','thresh',uthr,'size',[1 1 1], ... 71 | 'volInfo',R.volInfo,'overlay',overlay, ... 72 | 'colors',poscolors(2:end,:),'pos',tvals); 73 | 74 | addstr = 'add'; 75 | if isempty(A.cl_uncor_pos{end}), addstr = 'noadd'; end 76 | 77 | % --------------------------------------------------------------------- 78 | 79 | % Orthviews: Negative 80 | % --------------------------------------------------------------------- 81 | [A.cl_uncor_neg,dat_neg] = iimg_multi_threshold(pvals,'p','thresh',uthr,'size',[1 1 1], ... 82 | 'volInfo',R.volInfo,'overlay',overlay, ... 83 | 'colors',negcolors(2:end,:),'neg',tvals,addstr); 84 | 85 | % save dat for either pos or neg for later use 86 | dat = dat_pos | dat_neg; 87 | 88 | % --------------------------------------------------------------------- 89 | 90 | % Orthviews: Significant within ROIs x pos. or neg. results 91 | % --------------------------------------------------------------------- 92 | fprintf('\nCorrected positive results: %3.0f separate regions', length(A.cl_pos)); 93 | fprintf('\nCorrected negative results: %3.0f separate regions', length(A.cl_neg)); 94 | fprintf('\n'); 95 | 96 | % significant with correction 97 | if ~isempty(A.cl_pos) 98 | cluster_orthviews(A.cl_pos,{poscolors(1,:)},'solid','add'); 99 | end 100 | if ~isempty(A.cl_neg) 101 | cluster_orthviews(A.cl_neg,{negcolors(1,:)},'solid','add'); 102 | end 103 | 104 | if showrois 105 | fprintf(1,'Showing extent of ROIs with voxels sig. at all thresholds.'); 106 | allsigseed = sum(dat,2); 107 | roivec = ones(size(pvals)); %~allsigseed; % % all vox in mask %R.volInfo.image_indx; 108 | 109 | roivec = iimg_cluster_prune(roivec,allsigseed,R.volInfo); 110 | roivec(allsigseed>0) = 0; 111 | cl_all2 = iimg_indx2clusters(roivec,R.volInfo); 112 | 113 | cluster_orthviews(cl_all2,{poscolors(end,:)},'trans','add'); %'overlay',overlay); 114 | 115 | makelegend({'Corrected (+)' 'p < .005' 'p < .01' 'p < .05' 'Corrected (-)' 'p < .005' 'p < .01' 'p < .05' 'ROI extent'},[poscolors(1:4,:); negcolors(1:4,:); poscolors(end,:)],1); 116 | else 117 | makelegend({'Corrected (+)' 'p < .005' 'p < .01' 'p < .05' 'Corrected (-)' 'p < .005' 'p < .01' 'p < .05'},[poscolors(1:4,:); negcolors(1:4,:)],1); 118 | end 119 | 120 | % --------------------------------------------------------------------- 121 | 122 | % Option to save figures here 123 | % --------------------------------------------------------------------- 124 | 125 | savefigs = input('Save PNG images of these clusters? (1/0) '); 126 | 127 | if savefigs 128 | cl = merge_clusters(A.cl_pos,A.cl_neg); % preserve names if these are input 129 | cl = cluster_export_pngs(cl,1,overlay); % save names if entered here 130 | 131 | % transfer names 132 | if isfield(cl,'shorttitle') 133 | npos = length(A.cl_pos); 134 | nneg = length(A.cl_neg); 135 | for i = 1:npos 136 | A.cl_pos(i).shorttitle = cl(i).shorttitle; 137 | end 138 | for i = 1:nneg 139 | A.cl_neg(i).shorttitle = cl(i+npos).shorttitle; 140 | end 141 | end 142 | end 143 | 144 | % --------------------------------------------------------------------- 145 | 146 | % Option to add extent of activation outside ROIs 147 | % --------------------------------------------------------------------- 148 | 149 | add_extent_outside_rois(R,uthr,overlay,poscolors,negcolors); 150 | 151 | % --------------------------------------------------------------------- 152 | 153 | % Option to save figures again with extent 154 | % --------------------------------------------------------------------- 155 | 156 | savefigs = input('Save PNG images of these clusters now? (1/0) '); 157 | 158 | if savefigs 159 | cl = merge_clusters(A.cl_pos,A.cl_neg); % preserve names if these are input 160 | cl = cluster_export_pngs(cl,1,overlay); 161 | 162 | % transfer names 163 | if isfield(cl,'shorttitle') 164 | npos = length(A.cl_pos); 165 | nneg = length(A.cl_neg); 166 | for i = 1:npos 167 | A.cl_pos(i).shorttitle = cl(i).shorttitle; 168 | end 169 | for i = 1:nneg 170 | A.A.cl_neg(i).shorttitle = cl(i+npos).shorttitle; 171 | end 172 | end 173 | end 174 | 175 | return 176 | 177 | 178 | 179 | 180 | % ------------------------------------------------------------------- 181 | % DISPLAY: add regions contiguous with (but outside) ROIs to color mapped 182 | % orthviews 183 | % ------------------------------------------------------------------- 184 | 185 | 186 | function add_extent_outside_rois(R,uthr,overlay,poscolors,negcolors) 187 | doextent = input('Show extent of regions contiguous with but outside ROIs? (1/0) '); 188 | if doextent 189 | z = zeros(R.volInfo.nvox,1); %whos z 190 | %z(R.volInfo.wh_inmask) = dat_extent; % this line for getting only 191 | % clusters contiguous with sig. nonparam clusters 192 | 193 | z(R.volInfo.wh_inmask) = 1; % to get entire ROI extent 194 | seeddat = z; 195 | 196 | switch spm('Ver') 197 | case 'SPM2' 198 | pimg = spm_get(1,'*img','Select brain-wise p-map to use'); 199 | timg = spm_get(1,'*img','Select matching t-map to use'); 200 | 201 | case 'SPM5' 202 | % spm_defaults is a function 203 | spm_defaults() 204 | pimg = spm_select(1,'image','Select brain-wise p-map to use'); 205 | timg = spm_select(1,'image','Select matching t-map to use'); 206 | 207 | case 'SPM8' 208 | % spm_defaults is a function 209 | spm_defaults() 210 | pimg = spm_select(1,'image','Select brain-wise p-map to use'); 211 | timg = spm_select(1,'image','Select matching t-map to use'); 212 | 213 | otherwise 214 | % unknown SPM 215 | disp('Unknown version of SPM!'); 216 | spm_defaults() 217 | end 218 | 219 | 220 | 221 | % ROIs have smoothed weights. Stats in p-map may not match those from 222 | % Take previous orthviews and add extent around it (anything contig. 223 | % with extended activation in ROI) 224 | 225 | iimg_multi_threshold(pimg,'thresh',uthr,'size',[1 1 1],'p','pruneseed',seeddat,'overlay',overlay, ... 226 | 'pos',timg,'colors',poscolors,'add','hideseed'); 227 | 228 | %cluster_orthviews(cl_sig,{[1 1 0]},'solid','add'); 229 | 230 | iimg_multi_threshold(pimg,'thresh',uthr,'size',[1 1 1],'p','pruneseed',seeddat, ... 231 | 'neg',timg,'colors',negcolors,'add','hideseed'); 232 | 233 | end 234 | 235 | return 236 | 237 | -------------------------------------------------------------------------------- /robust_toolbox/robust_nonparametric_subfunctions/robust_pooled_weight_core.m: -------------------------------------------------------------------------------- 1 | function [t,p,maxt,maxt_by_cl, primary_uncor] = robust_pooled_weight_core(X,Y,b,resid,wts,niter,wh_intercept,clindx,varargin) 2 | % 3 | % [t,p,maxt,maxt_by_cl, primary_uncor] = robust_pooled_weight_core(X,Y,[],[],[],2000,3,R.volInfo.clindx,[existing maxt,maxtbycl]); 4 | % 5 | % [R.correct.weightedt,R.correct.weightedp,R.maxt,R.maxt_by_cl] = ... 6 | % robust_pooled_weight_core(X,Y,R.correct.b,R.correct.resid, ... 7 | % R.correct.wts,2000,R.wh_intercept,R.volInfo.clindx); 8 | % 9 | % clindx = R.volInfo.clindx 10 | % 11 | % Updated: Jan 2008, by Tor Wager, to add primary uncorrected thresholds 12 | 13 | % -------------------------------------- 14 | % * Setup, and run robust reg if needed 15 | % -------------------------------------- 16 | [n,v] = size(Y); 17 | k = size(X,2); 18 | 19 | t = zeros(k,v); 20 | p = t; 21 | 22 | % set up chunks of iterations that will fit in memory 23 | maxmemsize = 2^28; 24 | memneeded = prod([niter v k]) * 8; 25 | chunks = ceil(memneeded./maxmemsize); % how many "chunks" of iterations needed to fill the request 26 | maxiter = floor(maxmemsize ./ (8*v*k)); % max number of iterations we can run 27 | if maxiter == 0, error('Not enough memory. You must increase maxmemsize in robust_pooled_weight_core.'); end 28 | maxiter = min(maxiter,niter); 29 | 30 | iterst = 1:maxiter:niter; % starting indices of iterations 31 | iteren = [iterst(2:end)-1 niter]; % ending indices of iterations for each chunk 32 | iterations = iteren - iterst + 1; % number of iterations for each chunk 33 | fprintf(1,'%3.0f total iterations. %3.0f chunks, each of %3.0f iterations.\n',niter,chunks,maxiter) 34 | fprintf(1,'Adding iterations to analysis : %3.0f through %3.0f\n',iterst(1),iteren(end)); 35 | 36 | if isempty(b) || isempty(resid) || isempty(wts) 37 | [b,t,p,sig,f,fp,fsig,stat] = robust_reg_matrix(X,Y); 38 | 39 | resid = stat.resid; 40 | wts = stat.wts; 41 | 42 | end 43 | 44 | if isempty(clindx), clindx = ones(1,v); end 45 | 46 | % pooling se is a bad idea w/multiple columns, because of predictors 47 | % account for lots of variance, this will produce low se, but it'll be 48 | % averaged with surrounding noise. 49 | 50 | % Set up primary uncorrected thresholds 51 | primary_uncor.primary_pthr = [.0005 .001 .0025 .005 .01 .025 .05]; 52 | primary_uncor.primary_pthr_descrip = 'one-tailed primary p-value thresholds'; 53 | 54 | primary_tthr = tinv(1 - primary_uncor.primary_pthr, size(X, 1) - size(X, 2)); % one-tailed p-vals 55 | primary_uncor.primary_tthr = primary_tthr; 56 | 57 | 58 | % -------------------------------------- 59 | % * Do iterations 60 | % -------------------------------------- 61 | 62 | 63 | nclust = max(clindx); 64 | maxt_by_cl = cell(1,nclust); 65 | 66 | % replace old estimates with pooled-weight least squares for output 67 | fprintf(1,'\nPooling weights: crunching by robust_pooled_weight_core.m\n'); 68 | 69 | % break into "chunks" 70 | 71 | for thischunk = 1:chunks 72 | 73 | cstr = sprintf('Chunk %3.0f. ',thischunk); fprintf(1,cstr); 74 | 75 | % Run a set of iterations (a chunk) 76 | % ------------------------------------------------------------------- 77 | [t,p,tnull] = pooled_weight_lsq(X, b, resid, wts, wh_intercept, iterations(thischunk)); 78 | 79 | % Save counts at uncorrected primary threshold for these iterations 80 | % ------------------------------------------------------------------- 81 | % tnull is voxels x iterations x regressors 82 | for pt = 1:length(primary_tthr) 83 | sumt = squeeze(sum(tnull > primary_tthr(pt)))'; % a matrix of counts, regressors x iterations 84 | 85 | primary_uncor.null_sig{pt}(:,iterst(thischunk):iteren(thischunk)) = sumt; 86 | end 87 | 88 | % Save maxima for these iterations 89 | % ------------------------------------------------------------------- 90 | % mapwise max 91 | maxtnull = squeeze(max(tnull)); 92 | 93 | try 94 | maxt(:,iterst(thischunk):iteren(thischunk)) = maxtnull; 95 | catch 96 | disp('transpose issue'); 97 | maxt(:,iterst(thischunk):iteren(thischunk)) = maxtnull'; 98 | end 99 | 100 | % tnull: voxels x iterations (x regressors) 101 | 102 | for c = 1:max(clindx) 103 | 104 | wh = clindx == c; 105 | 106 | try 107 | maxt_by_cl{c}(:,iterst(thischunk):iteren(thischunk)) = squeeze(max(tnull(wh,:,:),[],1)); %tmax(:,wh); 108 | catch 109 | disp('transpose issue'); 110 | maxt_by_cl{c}(:,iterst(thischunk):iteren(thischunk)) = squeeze(max(tnull(wh,:,:),[],1))'; 111 | end 112 | end 113 | 114 | clear tnull 115 | 116 | erase_string(cstr) 117 | 118 | end % chunk 119 | 120 | return 121 | 122 | 123 | % ------------------------------------------------------------------- 124 | % 125 | % 126 | % Sub-functions 127 | % 128 | % 129 | % ------------------------------------------------------------------- 130 | 131 | % ------------------------------------------------------------------- 132 | % Run a set of iterations (a chunk) 133 | % ------------------------------------------------------------------- 134 | 135 | function [t,p,tnull] = pooled_weight_lsq(X,b,resid,wts,wh_intercept,niter) 136 | % weighted least squares for each effect 137 | % pooling weights over all voxels 138 | 139 | sstr = sprintf('Allocating memory. '); fprintf(1,sstr); 140 | 141 | 142 | [n,v] = size(resid); 143 | k = size(X,2); 144 | 145 | tnull = zeros(v,niter,k); 146 | 147 | dfe = n - k; 148 | 149 | t = zeros(k,v); 150 | p = zeros(k,v); 151 | %tmax = zeros(k,v); 152 | %nulltmax = zeros(k,niter); 153 | 154 | erase_string(sstr) 155 | 156 | for wh_reg = 1:k 157 | 158 | kstr = sprintf('Reg. %02d: ',wh_reg); fprintf(1,kstr); 159 | 160 | 161 | 162 | % -------------------------------------- 163 | % * Y (data) adjusted for effects of 164 | % no (current) interest 165 | % -------------------------------------- 166 | Yadj = X(:,wh_reg) * b(wh_reg,:) + resid; 167 | 168 | % -------------------------------------- 169 | % * Get betas, t, std errs based on pooled weights 170 | % and robust standard errors 171 | % -------------------------------------- 172 | 173 | [t(wh_reg,:),p(wh_reg,:)] = weighted_glmfit_reddf(X(:,wh_reg),Yadj,mean(wts,2),dfe); 174 | 175 | % -------------------------------------- 176 | % * permutations 177 | % -------------------------------------- 178 | str = sprintf('Iteration %05d',0); fprintf(1,str); 179 | 180 | for i = 1:niter 181 | 182 | if wh_reg == wh_intercept 183 | % permute signs 184 | signmtx = repmat(sign(randn(n,1)),1,v); 185 | Yp = Yadj .* signmtx; 186 | else 187 | % permute order of rows 188 | Yp = Yadj(randperm(n)',:); 189 | end 190 | 191 | % tnull: voxels x iterations (x regressors) 192 | tnull(:,i,wh_reg) = weighted_glmfit_reddf(X(:,wh_reg),Yp,mean(wts,2),dfe)'; 193 | %nulltmax(wh_reg,i) = max(tnull); 194 | 195 | if mod(i,100) == 0 196 | fprintf(1,'\b\b\b\b\b%05d',i) 197 | end 198 | 199 | end 200 | 201 | %tmax(wh_reg,:) = max(tnull); 202 | 203 | erase_string(str) 204 | erase_string(kstr) 205 | end 206 | 207 | %%****allocate tmax 208 | % squeeze down to k x voxels 209 | %tmax = squeeze(max(tnull))'; 210 | %clear tnull 211 | 212 | 213 | 214 | return 215 | 216 | 217 | 218 | function [t,p,b,v] = weighted_glmfit_reddf(X,Y,wts,dfe) 219 | 220 | n = size(Y,2); 221 | 222 | 223 | W = diag(wts); % Weight matrix 224 | invxwx = inv(X'*W*X); 225 | bform = invxwx * X'* W; % beta-forming matrix. hat = X * bform 226 | % 227 | % % rows are columns of design (X), cols are Y variables 228 | b = bform*Y; 229 | k = size(b,1); 230 | 231 | e = Y - X * b; % residuals 232 | 233 | % % standard error 234 | MSE = zeros(1,n); 235 | for i=1:n, MSE(i) = e(:,i)'*W*e(:,i); end, MSE = MSE/dfe; 236 | 237 | v = repmat(diag(invxwx),1,n) .* repmat(MSE,k,1); 238 | 239 | t = b ./ sqrt(v); 240 | 241 | if nargout > 1, p = 2 * ( 1 - tcdf(abs(t),dfe) ); end 242 | 243 | return 244 | 245 | 246 | function erase_string(str1) 247 | fprintf(1,repmat('\b',1,length(str1))); % erase string 248 | return 249 | -------------------------------------------------------------------------------- /robust_toolbox/robust_reg_load_files_to_objects.m: -------------------------------------------------------------------------------- 1 | function [trob, names, mask_obj, nsubjects, weights, SETUP] = robust_reg_load_files_to_objects(robust_reg_directory) 2 | % 3 | % Load files from a CANLab robust regression directory into objects 4 | % Objects contain information needed for results display and tables. 5 | % 6 | % trob statistic_image object with one image per contrast (the model intercept is the first image) 7 | % names cell array of names of each image (intercept, regressor 1, etc.) 8 | % mask_obj image defining the set of voxels analyzed 9 | % nsubjects image summarizing number of subjects with valid data in each voxel 10 | % weights fmri_data object with weight maps for each subject (i.e., input image) 11 | % SETUP struct saved in directory, including design matrix SETUP.X 12 | 13 | % Programmers' notes: 14 | % Tor Wager: Modified Nov 2020 to load old .img or standard .nii 15 | 16 | if ~exist(fullfile(robust_reg_directory, 'SETUP.mat'), 'file') 17 | error('robust_reg_directory must be a CANLab robust regression directory with SETUP.mat'); 18 | end 19 | 20 | 21 | dashes = '----------------------------------------------'; 22 | printstr = @(dashes) disp(dashes); 23 | printhdr = @(str) fprintf('%s\n%s\n%s\n', dashes, str, dashes); 24 | 25 | printhdr('Robust regression'); 26 | fprintf('Loading files from %s\n', robust_reg_directory); 27 | disp(' '); 28 | 29 | load SETUP 30 | disp(char({SETUP.dir SETUP.name})); 31 | 32 | % mask_obj = fmri_data(fullfile(robust_reg_directory, 'mask.nii'), 'noverbose'); 33 | % nsubjects = fmri_data(fullfile(robust_reg_directory, 'nsubjects.nii'), 'noverbose'); 34 | % weights = fmri_data(fullfile(robust_reg_directory, 'weights.nii'), 'noverbose'); 35 | 36 | mask_obj = load_robreg_file_into_object(robust_reg_directory, 'mask'); 37 | nsubjects = load_robreg_file_into_object(robust_reg_directory, 'nsubjects'); 38 | weights = load_robreg_file_into_object(robust_reg_directory, 'weights'); 39 | 40 | [n, k] = size(SETUP.X); 41 | 42 | names = {'Group average (Intercept)'}; 43 | for i = 2:k, names = [names sprintf('Regressor %d', i - 1)]; end 44 | 45 | % Load all t images as statistic_image objects 46 | d = dir('rob_tmap*nii'); 47 | 48 | if isempty(d) 49 | d = dir('rob_tmap*img'); 50 | end 51 | 52 | if isempty(d) 53 | error('Error finding tmap names') 54 | end 55 | 56 | trob = statistic_image('image_names', char(d.name), 'type', 't', 'dfe', n - k); 57 | 58 | % Replace P-values with those from robust reg, as dfe varies 59 | d = dir('rob_p_*nii'); 60 | 61 | if isempty(d) 62 | d = dir('rob_p_*img'); 63 | end 64 | 65 | if isempty(d) 66 | error('Error finding tmap names') 67 | end 68 | 69 | prob = statistic_image('image_names', char(d.name)); 70 | 71 | trob = replace_empty(trob); 72 | prob = replace_empty(prob); 73 | trob.p = prob.dat; 74 | trob = remove_empty(trob); 75 | 76 | % Print some output 77 | % -------------------------------------------------------------- 78 | 79 | 80 | if isfield(SETUP, 'name') && ~isempty(SETUP.name) 81 | 82 | printstr(SETUP.name) 83 | 84 | end 85 | 86 | if isfield(SETUP, 'dir') && ~isempty(SETUP.dir) 87 | 88 | printstr(SETUP.dir) 89 | 90 | end 91 | 92 | printhdr('Design Matrix') 93 | 94 | ynstr = {'No' 'Yes'}; 95 | ok2string = @(isok) ynstr{double(isok) + 1} ; 96 | 97 | isok = all(SETUP.X(:, 1) == mean(SETUP.X(:, 1), 1)); 98 | fprintf('First regressor (image) is intercept: %s\n', ok2string(isok)); 99 | 100 | isok = all(abs(mean(SETUP.X(:, 2:end))) < 1000 * eps) ; 101 | fprintf('Regressors mean-centered (intercept reflects group mean): %s\n', ok2string(isok)); 102 | 103 | fprintf('Number of regressors (including intercept): %d\n', k); 104 | 105 | printstr(' '); 106 | 107 | 108 | end 109 | 110 | 111 | function obj = load_robreg_file_into_object(robust_reg_directory, basename) 112 | % provide facility for checking .nii or .img 113 | 114 | myfile = fullfile(robust_reg_directory, [basename '.nii']); 115 | if ~exist(myfile, 'file') 116 | myfile = fullfile(robust_reg_directory, [basename '.img']); 117 | end 118 | if ~exist(myfile, 'file') 119 | error(['cannot find file: ' myfile]) 120 | end 121 | obj = fmri_data(myfile, 'noverbose'); 122 | end 123 | 124 | -------------------------------------------------------------------------------- /robust_toolbox/robust_results_batch.m: -------------------------------------------------------------------------------- 1 | %% Robust regression report 2 | 3 | %% About this report 4 | % 5 | % This script displays a summary of robust regression results 6 | % Stored in a CANLab robust regression folder 7 | % 8 | % It can be used with the publish() command to generate a published html 9 | % report. e.g., 10 | % 11 | %% 12 | % cd(my robust results directory) 13 | % publish('robust_results_batch') 14 | % 15 | 16 | %% 17 | % publish.m is a Matlab report-generating function that can print to PDF, 18 | % HTML, Powerpoint, and other formats. See |help publish| for more. 19 | % 20 | % To create reports and save them in the current robust results dir, try: 21 | % 22 | % publish_robust_regression_report 23 | % 24 | % This script can also be customized to create different types of reports 25 | 26 | % Load files from current directory 27 | % ----------------------------------------------------------------------- 28 | 29 | [trob, names, mask_obj, nsubjects, weights, SETUP] = robust_reg_load_files_to_objects(pwd); 30 | 31 | % Load files from a CANLab robust regression directory into objects 32 | % Objects contain information needed for results display and tables. 33 | % 34 | % trob statistic_image object with one image per contrast (the model intercept is the first image) 35 | % names cell array of names of each image (intercept, regressor 1, etc.) 36 | % mask_obj image defining the set of voxels analyzed 37 | % nsubjects image summarizing number of subjects with valid data in each voxel 38 | % weights fmri_data object with weight maps for each subject (i.e., input image) 39 | % SETUP struct saved in directory, including design matrix SETUP.X 40 | 41 | % Preliminaries 42 | % ----------------------------------------------------------------------- 43 | 44 | % Number of images, including intercept 45 | k = size(trob.dat, 2); 46 | 47 | % Display helpers 48 | dashes = '----------------------------------------------'; 49 | printstr = @(dashes) disp(dashes); 50 | printhdr = @(str) fprintf('%s\n%s\n%s\n', dashes, str, dashes); 51 | 52 | % Set up figure 53 | 54 | create_figure('slices'); axis off 55 | o2 = canlab_results_fmridisplay; 56 | 57 | %% Mask of in-analysis voxels and number of participants 58 | % This montage shows the voxels analyzed in green 59 | 60 | o2 = addblobs(o2, region(mask_obj), 'color', [0 .7 0], 'trans'); 61 | o2 = o2.title_montage(5, 'Analysis mask'); 62 | drawnow, snapnow 63 | 64 | o2 = removeblobs(o2); 65 | o2 = addblobs(o2, region(nsubjects), 'mincolor', [1 1 1], 'maxcolor', [0 0 1], 'trans'); 66 | o2 = o2.title_montage(5, 'Coverage (number of participants)'); 67 | o2 = legend(o2); 68 | 69 | drawnow, snapnow 70 | 71 | fprintf('Modal value for number of participants: %d\n', mode(nsubjects.dat)); 72 | 73 | %% Plot of subject weights 74 | % This plot shows a summary of the weight maps for each subject 75 | % Weights of less than one indicate that a subject is down-weighted for 76 | % the low-weight voxel. Weights of zero indcate the subject was dropped entirely for 77 | % that voxel. 78 | % 79 | % A subject with low weights across large areas of the brain is an outlier 80 | % across many brain areas. 81 | % 82 | % See |help fmri_data.plot| and the basic plot walkthrough on canlab.github,io 83 | % for more information about this plot 84 | % 85 | % In this plot, the 'carpet plot' of weights across all subjects is 86 | % currently mean-zeroed across the whole dataset. The relative weights 87 | % provide the key information. 88 | 89 | plot(weights); 90 | drawnow, snapnow 91 | 92 | % slices(weights, 'orientation', 'axial'); 93 | 94 | 95 | %% Unthresholded results maps 96 | % This plot shows the unthresholded t-maps 97 | % 98 | % The first image is the intercept in a standard CANlab robust regression analysis 99 | % If regressors are mean-centered, the intercept can be interpreted as the 100 | % activity for the average subject (when regressor values are all 0) 101 | % 102 | % Controlling for regressors (of interest or nuisance covariates) can be 103 | % very helpful in reducing known sources of error when assessing group-mean 104 | % activation. 105 | 106 | create_figure('robust t maps'); axis off 107 | o2 = canlab_results_fmridisplay([], 'multirow', k); 108 | 109 | for i = 1:k 110 | 111 | t = get_wh_image(trob, i); 112 | 113 | o2 = addblobs(o2, region(t), 'trans', 'wh_montages', 2*i - 1: 2*i); % 2 montages in registry per slice display 114 | o2 = title_montage(o2, 2*i, names{i}); 115 | end 116 | 117 | drawnow, snapnow 118 | 119 | %% FDR-corrected results maps and tables for each regressor 120 | % 121 | % This plot shows False Discovery Rate corrected t-maps for each regressor 122 | % Apply gray matter mask before FDR correction 123 | 124 | trob = apply_mask(trob, which('gray_matter_mask.img')); 125 | 126 | o2 = removeblobs(o2); 127 | 128 | for i = 1:k 129 | 130 | % disp(' ') 131 | % printhdr(sprintf('FDR-corrected: %s', names{i})) 132 | % disp(' ') 133 | % 134 | t = get_wh_image(trob, i); 135 | 136 | t = threshold(t, .05, 'fdr'); 137 | o2 = addblobs(o2, region(t), 'trans', 'wh_montages', 2*i - 1: 2*i); 138 | o2 = title_montage(o2, 2*i, names{i}); 139 | 140 | end 141 | 142 | drawnow, snapnow 143 | 144 | %% FDR-corrected results surfaces and tables for each regressor 145 | % 146 | % We apply a gray matter mask before FDR correction and correct within 147 | % gray-matter voxels. The mask is fairly liberal to avoid excluding brain 148 | % tissue of interest. (It assumes data are in MNI space). 149 | % 150 | % Correcting within gray matter applies the multiple comparisons correction 151 | % only within areas where we would plausibly expect signai. i.e., we are 152 | % not penalized for searching for activity in the ventricles. 153 | % 154 | % The table() function generates a table with region and network names 155 | % labeled using a standard CANlab atlas object. See the walkthrough on 156 | % atlas objects in canlab.github.io for more information. 157 | % The default atlas at time of writing this is the canlab2018_2mm atlas. 158 | 159 | for i = 1:k 160 | 161 | disp(' ') 162 | printhdr(sprintf('FDR-corrected: %s', names{i})) 163 | disp(' ') 164 | 165 | t = get_wh_image(trob, i); 166 | 167 | t = threshold(t, .05, 'fdr'); 168 | 169 | % Table 170 | r = region(t); 171 | r = table(r, 'nolegend'); 172 | 173 | % Surfaces and slices 174 | 175 | figure; 176 | surface_handles = surface(t); 177 | 178 | o2_surf = canlab_results_fmridisplay(r, 'montagetype', 'full', 'nooutline'); 179 | 180 | drawnow, snapnow 181 | 182 | end 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /robust_toolbox/robust_results_fdr_threshold.m: -------------------------------------------------------------------------------- 1 | function SETUP = robust_results_fdr_threshold(corr_type, varargin) 2 | % SETUP = robust_results_fdr_threshold(corr_type, ['mask', mask image defining regions], ['images', p-value images]) 3 | % 4 | % Multiple comparisons correction tool for mediation_brain 5 | % Calculates corrected threshold and saves in mediation_SETUP.mat, SETUP variable 6 | % Returns SETUP with corrected threshold. 7 | % You can then use this threshold in mediation_brain_results to get results 8 | % at corrected p-value thresholds. 9 | % 10 | % input: corr_type 11 | % Must be one of the following: 12 | % ------------------------------- 13 | % 'fdr' 14 | % False Discovery Rate correction across multiple effects 15 | % (multiple images). The idea is that you can find the threshold that 16 | % controls the overall FDR in a robust regression analysis, including all covariate images. 17 | % This threshold is a single threshold that provides control across the 18 | % different tests. 19 | % 20 | % P-VALUE IMAGES TO USE 21 | % ------------------------ 22 | % This function automatically uses all p-value images from a robust results 23 | % directory 24 | % You can enter your own image or set of images as well by using the 25 | % optional inputs: 26 | % 'images', followed by a string matrix of p-value images 27 | % 28 | % SEARCH AREA 29 | % ------------------------ 30 | % The search area is defined by default as the area in mask.img in the 31 | % mediation results directory. This image is written automatically when 32 | % mediation_brain analyses are run, but you have to specify one for robust reg. 33 | % You can enter your own mask by using the optional inputs: 34 | % 'mask', followed by the mask image defining the regions 35 | % This provides facility for ROI-based correction. 36 | % 37 | % Examples: 38 | % ------------------------ 39 | % In a mediation directory, type: 40 | % SETUP = robust_results_fdr_threshold('fdr'); 41 | % 42 | % To calculate FDR for only a single effect, e.g., the Path b effect in the 43 | % example below, enter the p-value image name you'd like to use. 44 | % You can actually do this for ANY p-value image, not just mediation analysis images: 45 | % SETUP = mediation_brain_corrected_threshold('fdr', 'mask', mask, 'imgs', 'M-Y_pvals.img'); 46 | % 47 | % Tor Wager, Dec 3, 2008 48 | 49 | % Set up optional inputs and defaults 50 | mask = 'mask.img'; 51 | imgs = []; 52 | 53 | for i = 1:length(varargin) 54 | if ischar(varargin{i}) 55 | switch varargin{i} 56 | 57 | % functional commands 58 | case 'mask', mask = varargin{i+1}; varargin{i + 1} = []; 59 | case {'images', 'imgs'}, imgs = varargin{i+1}; varargin{i + 1} = []; 60 | 61 | otherwise, warning('robfit:badInput', ['Unknown input string option:' varargin{i}]); 62 | end 63 | end 64 | end 65 | 66 | switch lower(corr_type) 67 | case 'fdr' 68 | SETUP = run_fdr(); 69 | 70 | otherwise 71 | error('Unknown correction type. See help.'); 72 | end 73 | 74 | % do not save, because we may want to run results with different masks, 75 | % etc. 76 | %save mediation_SETUP -append SETUP 77 | 78 | 79 | % -------------------------- 80 | % * 81 | % INLINE FUNCTIONS 82 | % * 83 | % -------------------------- 84 | 85 | function SETUP = run_fdr 86 | 87 | % Load SETUP 88 | % Choose sensible images to get FDR correction over 89 | % (Ignore images that do not vary across mediation tests.) 90 | 91 | load(fullfile(pwd, 'SETUP.mat'), 'SETUP'); 92 | 93 | if isempty(imgs) 94 | 95 | imgs = dir('rob_p_*.nii'); 96 | imgs = char(imgs.name); 97 | 98 | end 99 | 100 | if isempty(imgs) % legacy 101 | 102 | imgs = dir('rob_p_*.img'); 103 | imgs = char(imgs.name); 104 | 105 | end 106 | 107 | disp('Calculating FDR threshold across family of tests in these images:') 108 | disp(imgs) 109 | 110 | if ~exist(mask, 'file'), error('Cannot find mask image file.'); end 111 | 112 | % tor added 2017 to auto reslice 113 | maskobj = fmri_data(mask); 114 | pobj = fmri_data(imgs); 115 | pobj = apply_mask(pobj, maskobj); 116 | 117 | % old 118 | %maskInfo = iimg_read_img(mask, 2); 119 | %pvals = iimg_get_data(maskInfo, imgs); 120 | 121 | pvals = pobj.dat; 122 | fdr_p_thresh = FDR(pvals(:), .05); 123 | 124 | if isempty(fdr_p_thresh), fdr_p_thresh = -Inf; end 125 | 126 | fprintf('Total p-values: %7d\n', length(pvals(:))); 127 | 128 | fprintf('Combined FDR threshold is %7f\n', fdr_p_thresh); 129 | 130 | disp('Returning output in SETUP.fdr_p_thresh (you must save this yourself in the .mat file)'); 131 | 132 | SETUP.fdr_p_thresh = fdr_p_thresh; 133 | 134 | disp(' '); 135 | disp('Getting FDR thresholds for individual images:'); 136 | 137 | for i = 1:size(imgs, 1) 138 | fdr_p_thresh = FDR(pvals(i, :), .05); 139 | 140 | if isempty(fdr_p_thresh), fdr_p_thresh = Inf; end 141 | 142 | fprintf('%s\t FDR (q < .05) = %7f\n',imgs(i,:), fdr_p_thresh); 143 | 144 | SETUP.fdr_p_thresh_indiv(i) = fdr_p_thresh; 145 | end 146 | 147 | disp('Returning output in SETUP.fdr_p_thresh_indiv (you must save this yourself in the .mat file)'); 148 | 149 | % create_figure('FDR'); plot(sort(pvals)) 150 | % plot_horizontal_line(.001) 151 | % plot( .05 * (1:length(pvals(:))) ./ length(pvals(:)), 'r') 152 | 153 | end 154 | 155 | 156 | end 157 | --------------------------------------------------------------------------------