Home > data-access > get_survey_by_age.m

get_survey_by_age

PURPOSE ^

GET_SURVEY_BY_AGE Find survey measurements that fall within an age range

SYNOPSIS ^

function matches = get_survey_by_age(exps, survey, age_interval, varargin)

DESCRIPTION ^

GET_SURVEY_BY_AGE Find survey measurements that fall within an age range

 get_survey_by_age(EXPERIMENTS, SURVEY_NAME, AGE_INTERVAL)
   Loads the data in SURVEY_NAME for the given EXPERIMENTS.  Finds a
   column that starts with 'age', and selects only the rows of the survey
   data where the 'age' column falls within AGE_INTERVAL.

 AGE_INTERVAL is a two-element array, [lower_bound, upper_bound].  To be
 included, a measurement must take place within the closed interval
 defined by the bounds.  If more than one measurement of the same subject
 took place within the bounds, the one closest to the center of the bounds
 is selected.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function matches = get_survey_by_age(exps, survey, age_interval, varargin)
0002 %GET_SURVEY_BY_AGE Find survey measurements that fall within an age range
0003 %
0004 % get_survey_by_age(EXPERIMENTS, SURVEY_NAME, AGE_INTERVAL)
0005 %   Loads the data in SURVEY_NAME for the given EXPERIMENTS.  Finds a
0006 %   column that starts with 'age', and selects only the rows of the survey
0007 %   data where the 'age' column falls within AGE_INTERVAL.
0008 %
0009 % AGE_INTERVAL is a two-element array, [lower_bound, upper_bound].  To be
0010 % included, a measurement must take place within the closed interval
0011 % defined by the bounds.  If more than one measurement of the same subject
0012 % took place within the bounds, the one closest to the center of the bounds
0013 % is selected.
0014 %
0015 
0016 
0017 % todo: argument for setting age column if it's not determined
0018 % automatically, argument for saying what columns should be selected, ...
0019 %
0020 % argument for selecting *all* within the interval, not just the "best" one
0021 p = inputParser;
0022 p.addRequired('exps');
0023 p.addRequired('survey', @ischar);
0024 p.addRequired('age_interval', @(x) numel(x) == 2 && isnumeric(x));
0025 p.parse(exps, survey, age_interval, varargin{:});
0026 
0027 survey_data = load_survey(exps, survey);
0028 
0029 % Find the "age" column --- whatever column starts with the letters 'age'
0030 var_names = get(survey_data, 'VarNames');
0031 age_var = guess_age_var(var_names);
0032 
0033 % Start choosing measurements
0034 matches = survey_data(survey_data.(age_var) >= age_interval(1) ...
0035     & survey_data.(age_var) <= age_interval(2), :);
0036 
0037 % Eliminate duplicates by choosing the "best" one: the one with the age
0038 % closest to the center of the interval
0039 interval_center = mean(age_interval);
0040 to_keep = false(size(matches.subject));
0041 all_subjects = unique(matches.subject);
0042 for S = 1:length(all_subjects);
0043     subject = all_subjects(S);
0044     indices = find(matches.subject == subject);
0045     
0046     if length(indices) == 1
0047         to_keep(indices) = true;
0048         % no duplicates to mess with
0049         continue
0050     end
0051     
0052     ages = matches.(age_var)(indices);
0053     [m, idx] = min(abs(ages - interval_center));
0054     
0055     to_keep(indices(idx)) = true;
0056     % the rest are assumed to be deleted, since to_keep starts all false
0057 end
0058 
0059 
0060 matches = matches(to_keep, :);
0061 
0062 
0063 
0064 end
0065 
0066 
0067 function age_var = guess_age_var(var_names)
0068 looks_like_age = cellfun(@(name) strncmpi(name, 'age', 3), var_names);
0069 if ~ any(looks_like_age)
0070     disp(var_names);
0071     error('get_survey_by_age:no_age_var', ['Can''t find any columns ' ...
0072         'that appear to tell the age.  Try using the AgeVar argument.']);
0073 elseif sum(looks_like_age) > 1
0074     disp(var_names{looks_like_age});
0075     error('get_survey_by_age:more_age_vars', ...
0076         'Found more than one variable in the survey that seems to be age');
0077 end
0078 age_var = var_names{looks_like_age};
0079 end

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005