Home > database > ensemble_data_struct_corr.m

ensemble_data_struct_corr

PURPOSE ^

Calculates correlations between variables that are contained in the

SYNOPSIS ^

function out_st = ensemble_data_struct_corr(data_st,params)

DESCRIPTION ^

 Calculates correlations between variables that are contained in the
 Ensemble data structure (data_st), and that are specified in
 params.corr.vars. 

 params.filt - filtering applied by ensemble_filter() prior to calculation
 of correlation matrix

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function out_st = ensemble_data_struct_corr(data_st,params)
0002 % Calculates correlations between variables that are contained in the
0003 % Ensemble data structure (data_st), and that are specified in
0004 % params.corr.vars.
0005 %
0006 % params.filt - filtering applied by ensemble_filter() prior to calculation
0007 % of correlation matrix
0008 
0009 % 11Mar2014 Petr Janata
0010 
0011 % Initialize the output data structure
0012 out_st = ensemble_init_data_struct;
0013 out_st.type = 'correlation_matrix';
0014 out_st.vars = {'corrvars','corrmtx','corrpval'};
0015 ocols = set_var_col_const(out_st.vars);
0016 
0017 % Make sure variables between which we want to calculate correlations are
0018 % specified
0019 if ~isfield(params,'corr') || ~isfield(params.corr,'vars') || ~iscell(params.corr.vars)
0020   error('Must specify variables to correlate as a cell array of strings in params.corr.vars')
0021 else
0022   corrvars = params.corr.vars;
0023 end
0024 
0025 % Apply filtering params
0026 if isfield(params,'filt')
0027   data_st = ensemble_filter(data_st,params.filt);
0028 end
0029 
0030 % Extract the data into a matrix
0031 varmask = ismember(data_st.vars, corrvars);
0032 extracted_vars = data_st.vars(varmask);
0033 nvars = length(extracted_vars);
0034 data = cell2mat(data_st.data(varmask));
0035 N = size(data,1)-sum(any(isnan(data),2));
0036 
0037 % Report any issues with NaNs
0038 numnans = sum(isnan(data));
0039 if any(numnans)
0040   fprintf('Encountered NaNs in the data:\n');
0041   for ivar = 1:length(extracted_vars);
0042     fprintf('\t%s: %d\n', extracted_vars{ivar}, numnans(ivar));
0043   end
0044 end
0045 
0046 % Calculate the correlation for complete rows
0047 [corrdata, corrpval]= corrcoef(data,'rows','complete');
0048 out_st.data{ocols.corrmtx} = corrdata;
0049 out_st.data{ocols.corrpval} = corrpval;
0050 
0051 out_st.data{ocols.corrvars} = extracted_vars;
0052 
0053 % Make a plot if we want it
0054 if isfield(params,'report') && isfield(params.report,'figs')
0055   if isfield(params.report.figs, 'plot')
0056     doPlot = params.report.figs.plot;
0057   else
0058     doPlot = true;
0059   end
0060   
0061   if isfield(params.report.figs, 'write2file')
0062     write2file = params.report.figs.write2file;
0063   else
0064     write2file = false;
0065   end
0066 end
0067 
0068 if doPlot
0069   figure
0070   load new_seismic
0071   imagesc(corrdata), axis xy image
0072   set(gca,'clim',[-1 1])
0073   colormap(new_seismic)
0074   colorbar
0075   
0076   set(gca,'ytick',1:nvars,'yticklabel', extracted_vars)
0077   
0078   set(gca,'xticklabel','')
0079   for ivar = 1:nvars
0080     text(ivar,0.4,strrep(extracted_vars{ivar},'_','\_'), ...
0081       'rotation',-90,'horizontalalign','left','verticalalign','middle');
0082   end
0083   
0084   title(sprintf('N = %d', N),'fontsize',18)
0085 end
0086 
0087 if write2file
0088   figfname = fullfile(params.paths.figures, [params.report.figs.fstub '.eps']);
0089   fprintf('Printing figure: %s\n', figfname);
0090   print(figfname, '-depsc')
0091 end
0092 
0093 return

Generated on Sun 25-Aug-2019 04:00:39 by m2html © 2003