calcGroupStats

PURPOSE ^

calcGroupStats Calculate statistics such as mean or standard deviation for

SYNOPSIS ^

function [groupStat,groupList,groupCnt,zScore] = calcGroupStats(data,groups,statName,groupList,randStat,nRand)

DESCRIPTION ^

calcGroupStats Calculate statistics such as mean or standard deviation for
subgroups of a population

 [groupStat,groupList,groupCnt,zScore] =
 calcGroupStats(data,groups,statName,groupList,randStat,nRand)

 data      Matrix of data (individuals x variables)
 groups    Group identifier for each individual
 statName  Name of the statistic to be computed for each group:
               'mean': mean value for group (default)
               'std': standard deviation for group
               'median': median for group
               'count': sum total of variable values for group
 groupList List of group identifiers to be considered (optional, default
           all values occurring in groups)
 randStat  Perform randomization analysis
 nRand     # of randomizations

 Group identifier can be either strings or numerical values

 groupStat     Matrix of group statistic values for each group and variable
 groupList     List of group identifiers considered
 groupCount    Number of individuals in a group

 Markus Herrgard 2006

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [groupStat,groupList,groupCnt,zScore] = calcGroupStats(data,groups,statName,groupList,randStat,nRand)
0002 %calcGroupStats Calculate statistics such as mean or standard deviation for
0003 %subgroups of a population
0004 %
0005 % [groupStat,groupList,groupCnt,zScore] =
0006 % calcGroupStats(data,groups,statName,groupList,randStat,nRand)
0007 %
0008 % data      Matrix of data (individuals x variables)
0009 % groups    Group identifier for each individual
0010 % statName  Name of the statistic to be computed for each group:
0011 %               'mean': mean value for group (default)
0012 %               'std': standard deviation for group
0013 %               'median': median for group
0014 %               'count': sum total of variable values for group
0015 % groupList List of group identifiers to be considered (optional, default
0016 %           all values occurring in groups)
0017 % randStat  Perform randomization analysis
0018 % nRand     # of randomizations
0019 %
0020 % Group identifier can be either strings or numerical values
0021 %
0022 % groupStat     Matrix of group statistic values for each group and variable
0023 % groupList     List of group identifiers considered
0024 % groupCount    Number of individuals in a group
0025 %
0026 % Markus Herrgard 2006
0027 
0028 [nItems,nSets] = size(data);
0029 
0030 if (nargin < 3)
0031     statName = 'mean';
0032 end
0033 if (nargin < 4)
0034     groupList = unique(groups);
0035 end
0036 if (isempty(groupList))
0037     groupList = unique(groups);
0038 end
0039 if (nargin < 5)
0040     randStat = false;
0041 end
0042 if (nargin < 6)
0043     nRand = 1000;
0044 end
0045 
0046 if iscell(groups)
0047     cellFlag = true;
0048 else
0049     cellFlag = false;
0050 end
0051 
0052 for i = 1:length(groupList)
0053     if (cellFlag)
0054         selGroup = strcmp(groups,groupList{i});
0055     else
0056         selGroup = (groups == groupList(i));
0057     end
0058     selData = data(selGroup,:);
0059     groupCnt(i) = sum(selGroup);
0060     groupStat(i,:) = calcStatInternal(groupCnt(i),selData,statName,nSets);
0061 end
0062 
0063 groupCnt = groupCnt';
0064 
0065 if (randStat)
0066     groupCntList = unique(groupCnt);
0067 
0068     zScore = zeros(length(groupList),nSets);
0069 
0070     for i = 1:length(groupCntList)
0071         thisGroupCnt = groupCntList(i);
0072         selGroups = find(groupCnt == thisGroupCnt);
0073         if (thisGroupCnt > 0)
0074             for j = 1:nRand
0075                 randInd = randperm(nItems);
0076                 randData = data(randInd(1:thisGroupCnt),:);
0077                 groupStatRand(j,:) = calcStatInternal(thisGroupCnt,randData,statName,nSets);
0078             end
0079             groupStatRandMean = nanmean(groupStatRand);
0080             groupStatRandStd = nanstd(groupStatRand);
0081             nGroups = length(selGroups);
0082             zScore(selGroups,:) = (groupStat(selGroups,:)-repmat(groupStatRandMean,nGroups,1))./repmat(groupStatRandStd,nGroups,1);
0083         end
0084     end
0085 end
0086 
0087 function groupStat = calcStatInternal(groupCnt,data,statName,nSets)
0088 
0089 if (groupCnt > 0)
0090     switch lower(statName)
0091         case 'mean'
0092             if (groupCnt > 1)
0093                 groupStat = nanmean(data);
0094             else
0095                 groupStat = data;
0096             end
0097         case 'std'
0098             if (groupCnt > 1)
0099                 groupStat = nanstd(data);
0100             else
0101                 groupStat = zeros(1,nSets);
0102             end
0103         case 'median'
0104             if (groupCnt > 1)
0105                 groupStat = nanmedian(data);
0106             else
0107                 groupStat = data;
0108             end
0109         case 'count'
0110             if (groupCnt > 1)
0111                 groupStat = nansum(data);
0112             else
0113                 groupStat = data;
0114             end
0115     end
0116 
0117 else
0118     groupStat = ones(1,nSets)*NaN;
0119 end

Generated on Thu 21-Jun-2012 15:39:23 by m2html © 2003