Home > data-analysis > stats > cont > cont_cal_stats.m

cont_cal_stats

PURPOSE ^

CONT_CAL_STATS Report various stypes of statistics of continue type data

SYNOPSIS ^

function results = cont_cal_stats(chunks, input)

DESCRIPTION ^

CONT_CAL_STATS Report various stypes of statistics of continue type data 
chunks according to user input.
 
 INPUT: 
   CHUNKS: a cell of data. See also GET_VARIABLE,
   GET_VARIABLE_BY_GROUPING, GET_VARIABLE_BY_SUBJECT,
   GET_VARIABLE_BY_TRIAL, GET_VARIABLE_BY_EVENT, GET_VARIABLE_BY_CEVENT.
   INPUT: (optional parameter) a struct that specifies the types of
   statistics user wants to calculate. Optional fields are:
       grouping: the grouping method of data chunks, see
       GET_VARIABLE_BY_GROUPING;
       hist_bins: value bins for generating histogram.
       high_threshold: for calculating the proportion of time that data
       has a value larger than a certain value;
       low_threshold: for calculating the proportion of time that data
       has a value lower than a certain value;
       whence: value 'start' or 'end', indicating whether the chunks of
       data were extracted based on onset/offser of a certain types of
       events/cevents.
       interval: used with the parameter WHENCE, indicating the starting
       time and ending time when chunks were extracted based on
       events/cevents.
 OUTPUT:
   RESULTS: a struct containing all the statistics. Also, individual
   statistic within one chunk of data will be reported accordingly.

 EXAMPLE:
   sub_list = list_subjects(18);
   var_name = 'cont_cam1_all_obj';
   grouping = 'event';
   input.event_name = 'event_obj1_inhand_child';
   input.whence = 'start';
   input.interval = [-2 0];
   chunks = get_variable_by_grouping('sub', sub_list, var_name, ...
       grouping, input)

   input.hist_bins = [0 1 2 3 4 5 6 7 8 9 10 100];
   input.low_threshold = 2;
   input.high_threshold = 5;
   results = cont_cal_stats(chunks, input);
 Example results:
     results =

                         mean: 13.3893
              individual_mean: [107x1 double]
                       median: 13.7772
            individual_median: [107x1 double]
                         hist: [1x12 double]
              individual_hist: [107x12 double]
                     low_prop: 0.1533
          individual_low_prop: [107x1 double]
                    high_prop: 0.1790
         individual_high_prop: [107x1 double]
                temporal_time: [20x1 double]
                temporal_mean: [20x1 double]
               temporal_chunk: [20x81 double]

   See also: GET_VARIABLE_BY_GROUPING

 For more example, go to:
 https://einstein.psych.indiana.edu/trac/browser/projects/txu_remodule/txu_test_stats_cont.m

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function results = cont_cal_stats(chunks, input)
0002 
0003 %CONT_CAL_STATS Report various stypes of statistics of continue type data
0004 %chunks according to user input.
0005 %
0006 % INPUT:
0007 %   CHUNKS: a cell of data. See also GET_VARIABLE,
0008 %   GET_VARIABLE_BY_GROUPING, GET_VARIABLE_BY_SUBJECT,
0009 %   GET_VARIABLE_BY_TRIAL, GET_VARIABLE_BY_EVENT, GET_VARIABLE_BY_CEVENT.
0010 %   INPUT: (optional parameter) a struct that specifies the types of
0011 %   statistics user wants to calculate. Optional fields are:
0012 %       grouping: the grouping method of data chunks, see
0013 %       GET_VARIABLE_BY_GROUPING;
0014 %       hist_bins: value bins for generating histogram.
0015 %       high_threshold: for calculating the proportion of time that data
0016 %       has a value larger than a certain value;
0017 %       low_threshold: for calculating the proportion of time that data
0018 %       has a value lower than a certain value;
0019 %       whence: value 'start' or 'end', indicating whether the chunks of
0020 %       data were extracted based on onset/offser of a certain types of
0021 %       events/cevents.
0022 %       interval: used with the parameter WHENCE, indicating the starting
0023 %       time and ending time when chunks were extracted based on
0024 %       events/cevents.
0025 % OUTPUT:
0026 %   RESULTS: a struct containing all the statistics. Also, individual
0027 %   statistic within one chunk of data will be reported accordingly.
0028 %
0029 % EXAMPLE:
0030 %   sub_list = list_subjects(18);
0031 %   var_name = 'cont_cam1_all_obj';
0032 %   grouping = 'event';
0033 %   input.event_name = 'event_obj1_inhand_child';
0034 %   input.whence = 'start';
0035 %   input.interval = [-2 0];
0036 %   chunks = get_variable_by_grouping('sub', sub_list, var_name, ...
0037 %       grouping, input)
0038 %
0039 %   input.hist_bins = [0 1 2 3 4 5 6 7 8 9 10 100];
0040 %   input.low_threshold = 2;
0041 %   input.high_threshold = 5;
0042 %   results = cont_cal_stats(chunks, input);
0043 % Example results:
0044 %     results =
0045 %
0046 %                         mean: 13.3893
0047 %              individual_mean: [107x1 double]
0048 %                       median: 13.7772
0049 %            individual_median: [107x1 double]
0050 %                         hist: [1x12 double]
0051 %              individual_hist: [107x12 double]
0052 %                     low_prop: 0.1533
0053 %          individual_low_prop: [107x1 double]
0054 %                    high_prop: 0.1790
0055 %         individual_high_prop: [107x1 double]
0056 %                temporal_time: [20x1 double]
0057 %                temporal_mean: [20x1 double]
0058 %               temporal_chunk: [20x81 double]
0059 %
0060 %   See also: GET_VARIABLE_BY_GROUPING
0061 %
0062 % For more example, go to:
0063 % https://einstein.psych.indiana.edu/trac/browser/projects/txu_remodule/txu_test_stats_cont.m
0064 
0065 % check fileds in 'input'
0066 if ~exist('input', 'var')
0067     % this line of code is just to prevent from generating errors when
0068     % script checks whether a certain field exists.
0069     input.none_filed = 'No information here';
0070 end
0071 
0072 % if isfield(input, 'var_name')
0073 %     if ~strcmp(get_data_type(input.var_name), 'cont')
0074 %         error('Error! This function can only accept CONT data type');
0075 %     end
0076 % end
0077 
0078 if isfield(input, 'sub_list')
0079     results.sub_list = input.sub_list;
0080 end
0081 
0082 if isfield(input, 'grouping')    
0083     grouping = input.grouping;
0084 else
0085     grouping = '';
0086 end
0087 
0088 chunks(cellfun(@isempty, chunks)) = {[NaN NaN]};
0089 %% calculate statistics
0090 cat_chunks = cat(1,chunks{:});
0091 
0092 % mean and median
0093 res_mean = cellfun(@cont_mean,chunks,'UniformOutput', false);
0094 res_std = cellfun(@(chunk) nanstd(chunk(:,2:end)), chunks,'UniformOutput', false);
0095 res_median = cellfun(@cont_median,chunks,'UniformOutput', false);
0096 res_min = cellfun(@cont_min,chunks,'UniformOutput', false);
0097 res_max = cellfun(@cont_max,chunks,'UniformOutput', false);
0098 res_nonnan = cellfun(@(chunk) sum(~isnan(chunk(:,2)))/size(chunk,1), chunks,'UniformOutput', false);
0099 results.mean = cont_mean(cat_chunks);
0100 results.individual_mean = cat(1,res_mean{:});
0101 results.mean_mean = nanmean(results.individual_mean, 1);
0102 results.std = nanstd(cat_chunks(:,2:end));
0103 results.individual_std = cat(1,res_std{:});
0104 results.mean_std = nanmean(results.individual_std, 1);
0105 results.median = cont_median(cat_chunks);
0106 results.individual_median = cat(1,res_median{:});
0107 results.mean_median = nanmean(results.individual_median, 1);
0108 results.min = nanmin(cat_chunks(:,2:end));
0109 results.individual_min = cat(1,res_min{:});
0110 results.mean_min = nanmean(results.individual_min, 1);
0111 results.max = nanmax(cat_chunks(:,2:end));
0112 results.individual_max = cat(1,res_max{:});
0113 results.mean_max = nanmean(results.individual_max, 1);
0114 results.max = nanmax(cat_chunks(:,2:end));
0115 results.nonnan = sum(~isnan(cat_chunks(:,2)))/size(cat_chunks,1);
0116 results.individual_nonnan = cat(1,res_nonnan{:});
0117 
0118 % hist_bins
0119 if isfield(input, 'hist_bins')
0120     res_hist = nan(length(chunks), length(input.hist_bins));
0121     for i = 1: length(chunks)
0122         res_hist(i,:) = cont_hist(chunks{i}, input.hist_bins, 'separate');
0123         res_hist(i,:) = res_hist(i,:)/sum(res_hist(i,:));
0124     end
0125     
0126     results.hist_bins = input.hist_bins;
0127     results.hist = cont_hist(cat_chunks, input.hist_bins, 'separate');
0128     results.hist = results.hist/sum(results.hist);
0129     results.individual_hist = res_hist;
0130 else
0131     [results.hist hist_bins] = hist(cat_chunks(:,2));
0132     results.hist_bins = hist_bins;
0133     
0134     res_hist = nan(length(chunks), length(hist_bins));
0135     for i = 1: length(chunks)
0136         res_hist(i,:) = cont_hist(chunks{i}, hist_bins, 'separate');
0137         res_hist(i,:) = res_hist(i,:)/sum(res_hist(i,:));
0138     end
0139     
0140     results.hist = results.hist/sum(results.hist);
0141     results.individual_hist = res_hist;
0142 end
0143 
0144 % low shreshold
0145 if isfield(input, 'low_threshold')
0146     res_low_prop = nan(length(chunks), 1);    
0147     for i = 1: length(chunks)
0148         res_low_prop(i) = cont_below(chunks{i},input.low_threshold);
0149     end
0150     
0151     results.low_prop = cont_below(cat_chunks, input.low_threshold);
0152     results.individual_low_prop = res_low_prop;
0153 end
0154 
0155 % high shreshold
0156 if isfield(input, 'high_threshold')
0157     res_high_prop = nan(length(chunks), 1);    
0158     for i = 1: length(chunks)
0159         res_high_prop(i) = cont_above(chunks{i},input.high_threshold);
0160     end
0161     
0162     results.high_prop =  cont_above(cat_chunks, input.high_threshold);
0163     results.individual_high_prop = res_high_prop;    
0164 end
0165 
0166 % calculate temporal profile
0167 is_cal_temporal = 0;
0168 offset = 0;
0169 chunks_len = cellfun(@(chunk) length(chunk), chunks, 'UniformOutput', 0);
0170 % calculate temporal profile automatically if all the chunks have the same
0171 % length
0172 if length(unique(cell2mat(chunks_len))) == 1
0173     is_cal_temporal = 1;
0174     time_base = chunks{1};
0175     time_base = time_base(:,1);
0176 end
0177 if length(grouping) > 0 && strcmp(grouping(end-4:end), 'event') && ...
0178     isfield(input, 'whence') && isfield(input, 'interval')
0179     is_cal_temporal = 1;
0180     offset = input.interval(1);
0181     chunks_len_temp = vertcat(chunks_len{:});
0182     [max_v max_idx] = max(chunks_len_temp);
0183     time_base = chunks{max_idx};
0184     time_base = time_base(:,1);
0185 end
0186     
0187 if is_cal_temporal
0188     res_temporal = align_streams(time_base, chunks, 'ForceZero');
0189     results.temporal_time = time_base - time_base(1) + offset;
0190     results.temporal_mean = nanmean(res_temporal, 2);
0191     results.temporal_chunk = res_temporal;
0192 end

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005