Create mean y values by multiple groups in one graph
    1 次查看(过去 30 天)
  
       显示 更早的评论
    
  Dear all, I have a snippet of my panel
id 	Year 	y 	Cat1 	Cat2 	Cat3 	Cat4
1 	2000 	45 	1 	0 	0 	0
1 	2001 	56 	1 	0 	0 	0
1 	2002 	23 	1 	0 	0 	0
1 	2003 	56 	1 	0 	0 	0
1 	2004 	45 	1 	0 	0 	0
2 	2000 	45 	0 	1 	0 	0
2 	2001 	90 	0 	1 	0 	0
2 	2002 	35 	0 	1 	0 	0
2 	2003 	79 	0 	1 	0 	0
2 	2004 	32 	0 	1 	0 	0
Ofcourse my full panel has id=30 and Year=5 .
I want to find the mean values of y by Cat1, up to Cat4, when each Cat takes the value of 1. And then I want to  plot  a bar chart of these mean values of y against the X axis that will contain the labels Cat1,..., Cat4 in a vertical position.
Is that possible to do that in Matlab?
If it is not clear, please let me know!
Many thanks in advance!
4 个评论
  Mann Baidi
      
 2024-4-2
				Okay, thanks for the clarification.
As per my understanding of the question, you would like to plot the data from Cat1 Cat2.... and y column, right?
回答(2 个)
  Star Strider
      
      
 2024-4-2
        
      编辑:Star Strider
      
      
 2024-4-2
  
      Try this — 
T1 = array2table([1 	2000 	45 	1 	0 	0 	0
1 	2001 	56 	1 	0 	0 	0
1 	2002 	23 	1 	0 	0 	0
1 	2003 	56 	1 	0 	0 	0
1 	2004 	45 	1 	0 	0 	0
2 	2000 	45 	0 	1 	0 	0
2 	2001 	90 	0 	1 	0 	0
2 	2002 	35 	0 	1 	0 	0
2 	2003 	79 	0 	1 	0 	0
2 	2004 	32 	0 	1 	0 	0], 'VariableNames',{'id','Year','y','Cat1','Cat2','Cat3','Cat4'})
Cat_mean = NaN(2, 4);
for k = 1:4
Cat_mean(:,k) = accumarray(1+T1{:,k+3}, 1, [], @(x)mean(T1.y(x)));
end
Cat_mean
There are several ways to do this in MATLAB.  This is the approach that I usually use.  
for k = 1:4
    Catu(:,k) = unique(T1{:,k+3},'stable');
end
Catu
figure
hold on
for k = 1:4
    bar(k,Cat_mean(:,k), 'stacked')
end
hold off
xlabel('Cat')
ylabel('Counts')
EDIT — Added bar chart.  
.
4 个评论
  Star Strider
      
      
 2024-4-2
				The easiest way — 
T1 = array2table([1 	2000 	45 	1 	0 	0 	0
1 	2001 	56 	1 	0 	0 	0
1 	2002 	23 	1 	0 	0 	0
1 	2003 	56 	1 	0 	0 	0
1 	2004 	45 	1 	0 	0 	0
2 	2000 	45 	0 	1 	0 	0
2 	2001 	90 	0 	1 	0 	0
2 	2002 	35 	0 	1 	0 	0
2 	2003 	79 	0 	1 	0 	0
2 	2004 	32 	0 	1 	0 	0], 'VariableNames',{'id','Year','y','Cat1','Cat2','Cat3','Cat4'})
% Cat_mean = NaN(2, 4);
for k = 1:2%4
Cat_mean(:,k) = accumarray(T1{:,k+3}(T1{:,k+3} ~= 0), 1, [], @(x)mean(T1.y(x)));
end
Cat_mean
for k = 1:4
    Catu(:,k) = unique(T1{:,k+3},'stable');
end
Catu
figure
hold on
for k = 1:2%4
    bar(k,Cat_mean(:,k), 'stacked')
end
hold off
set(gca,'XTick',(1:4), 'XTickLabel',compose('Cat%d',1:4))
% xlabel('Cat')
ylabel('Counts')
Another option is to use categorical tick labels, however that causes significant problems if you want to add anything numeric later, so I would not recommend it.  
.
  Adam Danz
    
      
 2024-4-2
        
      编辑:Adam Danz
    
      
 2024-4-2
  
      Use groupsummary to compute the means of each group.  Then plot the results using bar() with categorical x values.
This assumes "1" does not appear in more than 1 group.
% Input table
T = array2table([1 	2000 	45 	1 	0 	0 	0
1 	2001 	56 	1 	0 	0 	0
1 	2002 	23 	1 	0 	0 	0
1 	2003 	56 	0 	1 	0 	0
1 	2004 	45 	0 	1 	0 	0
2 	2000 	45 	0 	0 	1 	0
2 	2001 	90 	0 	0 	1 	0
2 	2002 	35 	0 	0 	0 	1
2 	2003 	79 	0 	0 	0 	1], ...
'VariableNames',{'id','Year','y','Cat1','Cat2','Cat3','Cat4'});
% Compute group means
groups = {'Cat1','Cat2','Cat3','Cat4'};
mu = groupsummary(T,groups,'mean','y')
% Get the group order so ensure the groups are in the same order as the
% means.
groupOrder = varfun(@find,mu(:,1:numel(groups)),'OutputFormat','uniform');
means = mu.mean_y(groupOrder); 
% Plot results
bar(categorical(groups),means)
ylabel('mean')
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



