graphing mean and SD of time-course data (grouping based on group number and time)

6 views (last 30 days)
Hello,
I am trying to graph the mean and SD deviation of time-course data. I need to use the mean and SD based on the group number at each time point. I will then overlay this time-course data with some simulations I've performed. Can anyone help provide me with the code to do this?
Thanks in advance!

Answers (1)

Pavan Sahith
Pavan Sahith on 5 Oct 2023
Hi Brett,
As per my understanding, you want to plot the mean and standard deviation of data based on group number at each time point. Additionally you want to overlay this time-course data with some simulations.
  • So, I assumed a simple time-course data example with 10 elements and some sample simulation data for better understanding.
time = [1 1 2 2 3 3 4 4 5 5]; % Time points
group = [1 1 1 2 2 2 3 3 3 4]; % Group numbers
data = [10 12 9 11 8 10 7 9 6 8]; % Time-course data
Which means,
at time=1, data =10 if it is from group =1 (mapping 1st element in all 3 arrays)
at time=3, data =8 if it is from group =2 (mapping 5th element in all 3 arrays)
uniqueGroups = unique(group);
uniqueTime = unique(time);
%initializing the mean and std matrices with all 0s
meanData = zeros(numel(uniqueGroups), numel(uniqueTime));
stdData = zeros(numel(uniqueGroups), numel(uniqueTime));
  • To find the mean and standard deviation you can use mean() and std() , Please refer the following MathWorks Documentation links for more information
% populating the matrices using mean and std functions
for i = 1:numel(uniqueGroups)
for j = 1:numel(uniqueTime)
idx = group == uniqueGroups(i) & time == uniqueTime(j);
meanData(i, j) = mean(data(idx));
stdData(i, j) = std(data(idx));
end
end
  • To plot mean and standard deviation based on groups at each time point , you can use errorbar as standard deviation is defined as a measure which shows how much variation (such as spread, dispersion, spread,) from the mean exists. For more details, please refer the following MathWorks Documentation link on errorbar-https://in.mathworks.com/help/matlab/ref/errorbar.html
figure;
hold on;
for i = 1:numel(uniqueGroups)
errorbar(uniqueTime, meanData(i, :), stdData(i, :), 'o-', 'LineWidth', 1.5);
end
% labelling the axes and setting a title
xlabel('Time');
ylabel('Data');
title('Mean and Standard Deviation of Time-Course Data');
grid on;
  • To overlay the simulations ,please refer to the following sample code which is using sample simulation data to plot.
% Overlay simulations with sample data
simulations = [9.5 11 8.5 10.5 7.5]; % Simulated data
timeSimulations = [1 2 3 4 5]; % Time points for simulations
groupSimulations = [1 2 3 4 1]; % Group numbers for simulations
for i = 1:numel(uniqueGroups)
idx = groupSimulations == uniqueGroups(i);
plot(timeSimulations(idx), simulations(idx), 'r--', 'LineWidth', 1.5);
end
legend('Group 1', 'Group 2', 'Group 3', 'Group 4','Simulations');
Please execute the MATLAB code to see the plot for the given sample data.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!