Plotting error bars on curve
显示 更早的评论
Hello,
I have code that compares two devices and I am plotting the error on y-axis throughout a gait cycle % (on the x axis).
I am trying to get the error bars to plot the standard error but I am having an unexpected result when plotting the curve.
This is the code I am working with.
Initially x, ave_diff and SE are 1X1000 and I would like to plot it over a gait cycle from 0-100% hence why I have resampled the signals.
However, when I run this code I have extra values at the end of the plot, yet the resmapled variables are all 101X1?
I cannot spot my error and was wondering if someone my point me in the correct direction or suggest where my fault lies.
I have attached the curve and the area where the issue lies, you can see the standard error bars also become strange.
Thanks in advance
figure()
x = smooth(resample((gc),101,length(gc),'Dimension',1));
ave_diff_resample = smooth(resample((ave_diff),101,length(ave_diff),'Dimension',1));
SE_resampled = smooth(resample((SE),101,length(SE),'Dimension',1));
set(gcf,'Color','w');
%standard error plot
plot(x, ave_diff_resample,'-','Color',[0 0 0],'linewidth',0.5)
hold on
% Adding error bars
errorbar(x, ave_diff_resample, SE_resampled, 'vertical');
hold on
hline = refline(0, 0);
hline.Color = 'k';
xlim([0 100]);
ylim([-15 15]);
legend('Signed Difference', 'Standard error bar')
xlabel('Gait cycle %')
ylabel('Difference (deg)')
采纳的回答
I really can’t do anything without the data. The rest of the code (that imports the data and processes it including calculating the standard errors) would be helpful as well. (An image may be worth a thousand words, however the data files and code are worth a thousand images on MATLAB Answers.)
pngs = dir('*.png');
for k = 1:numel(pngs)
figure
imshow(imread(pngs(k).name))
end


.
11 个评论
Thank you for posting the code.
I really need the data to see what the problem is. We will have to wait until tomorrow.
Alternatively, if you have exhausted your daily limit, did you upload the file to another post? If so, please post the URL for that post.
I have not posted it to another previous post as this is new data sadly, will post it first thing tomorrow.
Thanks
My pleasure!
I will be looking for it.
Hi, the data was too big, even with it zipped and only running a small portion, however, I found a way to plot it without the data extening past the length of the data. Not worked out why it was happening though.
Thank you!
The problem is not obvious from your code. That is the reason at least a representative part of your data would have been helpful.
How large are the data?
It is 11 787 KB, I will try run the data for a single cycle, for a single participant and that will provide you with representative data, however, will not really make sense with the plots, as it will not really compare anything but may help to see where the issue lies.
Thank you again!
I do not see any problems with this data set. Am I missing something?
clear all
%File location of Results to analyze
% load ('C:\Users\lexil\Documents\Patient_Study\Healthy Participants\Participant_data/T07b.mat')%young
LD = load('T07b.mat') % Load Into Structure
LD = struct with fields:
dat_store: {[1x1000 double] [10x1000 double] [10x1000 double] [10x6 double] [3.9835 3.7506 2.3008 1.0702 1.3536 1.0355 1.1663 0.7576 1.3172 1.4355]}
dat_store = LD.dat_store
dat_store = 1x5 cell array
{1x1000 double} {10x1000 double} {10x1000 double} {10x6 double} {1x10 double}
%If subjects need to be excluded post hoc, you can list the number here
omits = [];
alphaVicon_GC=[];
alpha_IMU=[];
Vicon_GC=[];
IMU_GC=[];
minV = [];
minIMU = [];
maxV = [];
maxIMU = [];
ROMV = [];
ROMRD = [];
RMSE_IMU = [];
p = [];
diff = [];
std_diff = [];
SE = [];
% dat_store{c,1} = gc; %Gait cycle 0-100
% dat_store{c,2} = alphaVicon_GC; %Vicon
% dat_store{c,3} = alpha1D_GC; %RD
% dat_store{c,4} = alpha_MS_GC;
% dat_store{c,5} = ROM;
% dat_store{c,6} = RMSE;
j=0;
sz = size(dat_store);
for i = 1:sz(1)-length(omits) %
if i == omits
else
ROM_all = dat_store{i,4};
ROMIMU_diff(i,:) = mean(ROM_all(:,1:3))-mean(ROM_all(:,4:6));
ROMIMU(i,:) = mean(ROM_all(:,4:6));
ROMVic(i,:) = mean(ROM_all(:,1:3));
alphaVicon_GC = [alphaVicon_GC;(dat_store{i,2})];
%alpha1D_GC = [alpha1D_GC;dat_store{i,3}];
alpha_IMU = [alpha_IMU;dat_store{i,3}];
% Average gc for each individual
Vicon_GC = [Vicon_GC;mean(dat_store{i,2})];
IMU_GC = [IMU_GC;mean(dat_store{i,3})];
RMSE = dat_store{i,5};
%RMSE_1D = [RMSE_1D ;RMSE(2,:)];
RMSE_IMU = [RMSE_IMU ;RMSE(1,:)];
%CORR_1D(i) = corr(reshape(dat_store{i,2},[],1),reshape(dat_store{i,3},[],1));
CORR_IMU(i) = corr(reshape(dat_store{i,2},[],1),reshape(dat_store{i,3},[],1));
end
end
Vicon_GC = Vicon_GC';
IMU_GC = IMU_GC';
diff = Vicon_GC - IMU_GC;
ave_diff = mean(Vicon_GC') - mean(IMU_GC');
std_diff = std(diff,0,2);
SE = std_diff/sqrt(i);
var = ["alphaIMU"];
RMSEtable = [mean(mean(RMSE_IMU,2)) std(mean(RMSE_IMU,2))];
table(var,RMSEtable)
ans = 1x2 table
var RMSEtable
__________ ________________
"alphaIMU" 1.8171 0
var2 = ["alpha"];
CORR = [mean(CORR_IMU) std(CORR_IMU)];
table(var2,CORR)
ans = 1x2 table
var2 CORR
_______ __________________
"alpha" 0.99735 0
GC_all_IMU = reshape(alphaVicon_GC,[],1)-reshape(alpha_IMU,[],1);
GC_all_IMU(abs(GC_all_IMU)<30);
RMSE_IMUa = sqrt(mean(GC_all_IMU.^2))
RMSE_IMUa = 2.1215
%ROM
minV = min(ROM_all(:,1));
minIMU = min(ROM_all(:,4));
maxV = max(ROM_all(:,2));
maxIMU = max(ROM_all(:,5));
minstd_V = std(ROM_all(:,1));
minstd_IMU = std(ROM_all(:,4));
maxstd_V = std(ROM_all(:,2));
maxstd_IMU = std(ROM_all(:,5));
ROMstd_V = std(ROM_all(:,3));
ROMstd_IMU = std(ROM_all(:,6));
ROMV = maxV - minV;
ROMRD = maxIMU - minIMU;
gc = dat_store{1,1};
sig = 1.96; %95% Confidence intervals but if sd then replace with 1
figure
patch([gc,flip(gc)],[mean(alphaVicon_GC)-sig*std(alphaVicon_GC) flip(mean(alphaVicon_GC)+sig*std(alphaVicon_GC)) ],[0 0 0],'facealpha',0.2,'edgealpha',0)
hold on
patch([gc,flip(gc)],[mean(alpha_IMU)-sig*std(alpha_IMU) flip(mean(alpha_IMU)+sig*std(alpha_IMU)) ],[1 0 0],'facealpha',0.1,'edgealpha',0)
%patch([gc,flip(gc)],[mean(alpha1D_GC)-sig*std(alpha1D_GC) flip(mean(alpha1D_GC)+sig*std(alpha1D_GC)) ],[0 0 1],'facealpha',0.1,'edgealpha',0)
plot(gc ,mean(alpha_IMU),'r--','LineWidth',1)
%plot(gc ,mean(alpha1D_GC),'b:','LineWidth',1)
plot(gc, mean(alphaVicon_GC),'k-','LineWidth',1)
%xlim([0 TimeEnd])
ylim([-20 80])
grid on
ylabel('Knee flexion angle (\circ)')
xlabel('Gait Cycle (%)')
xticklabels(["0%","20%","40%","60%","80%","100%"])
% g(1) = patch(NaN,NaN,[1 0 0],'facealpha',0.2);
% g(2) = patch(NaN,NaN,[0 0 0],'facealpha',0.2);
% g(3) = patch(NaN,NaN,[0 0 1],'facealpha',0.2);
g(1) = plot(NaN,NaN,'k-','LineWidth',1);
g(2) = plot(NaN,NaN,'r--','LineWidth',1);
rgb = [0 0 0];
FaceAlpha = (0.1);
g(3) = patch([NaN],[NaN],rgb,'EdgeAlpha', 0, 'FaceAlpha',FaceAlpha);
legend(g,'Camera-Marker','IMU','± 1.96*SD','location','best')%'northwest')

figure()
subplot(3,1,1)
plot(gc ,mean(alpha_IMU),'k--','LineWidth',1)
hold on
plot(gc, mean(alphaVicon_GC),'k-','LineWidth',1)
legend('IMU','Vicon')
xlabel('Gait cycle %')
ylabel('Knee Angle (deg)')
ylim([-20 80]);
grid on
subplot(3,1,2)
plot(gc, (mean(alphaVicon_GC)-mean(alpha_IMU)),'k-','LineWidth',1)
legend('Signed difference')
xlabel('Gait cycle %')
ylabel('Difference (deg)')
ylim([-10 10]);
grid on
subplot(3,1,3)
plot(gc, abs(mean(alphaVicon_GC)-mean(alpha_IMU)),'k-','LineWidth',1)
legend('Absolute difference')
xlabel('Gait cycle %')
ylabel('Difference (deg)')
ylim([-10 10]);
grid on

figure()
x = smooth(resample((gc),101,length(gc),'Dimension',1));
ave_diff_resample = smooth(resample((ave_diff),101,length(ave_diff),'Dimension',1));
SE_resampled = smooth(resample((SE),101,length(SE),'Dimension',1));
set(gcf,'Color','w');
%standard error plot
plot(x, ave_diff_resample,'-','Color',[0 0 0],'linewidth',0.5)
hold on
% Adding error bars
errorbar(x, ave_diff_resample, SE_resampled, 'vertical');
hold on
% hline = refline(0, 0);
% hline.Color = 'k';
yline(0, 'k')
xlim([0 100]);
ylim([-15 15]);
legend('Signed Difference', 'Standard error bar')
xlabel('Gait cycle %')
ylabel('Difference (deg)')

%% Average Bland-Altman plot
% Plot mean difference line
figure()
set(gcf,'Color','w');
ave_vic = mean(Vicon_GC');
ave_imu = mean(IMU_GC');
mean_data = (ave_vic + ave_imu)/2;
mean_data = resample((mean_data),101,length(mean_data),'Dimension',1);
std_diff = resample((std_diff),101,length(std_diff),'Dimension',1);
first_index = 1;
last_index = numel(mean_data);
scatter(mean_data(first_index), ave_diff_resample(first_index),80, '*');
hold on
scatter(mean_data(last_index), ave_diff_resample(last_index),80, '^');
hold on
scatter(mean_data(2:100), ave_diff_resample(2:100), 'filled','MarkerFaceColor', 'black');
hold on;
%+- 2SD
plot([0 120], [mean(ave_diff_resample)'+ 2*mean(std_diff), mean(ave_diff_resample)' + 2*mean(std_diff)], 'r--');
plot([0 120], [mean(ave_diff_resample)' - 2*mean(std_diff), mean(ave_diff_resample)' - 2*mean(std_diff)], 'r--');
%mean diff line
plot([0 120], [mean(ave_diff_resample), mean(ave_diff_resample)], 'k-', 'LineWidth', 1);
%calculate error bars
%error bars are the standard error of the sample
error_bar_y = errorbar(mean_data,ave_diff_resample,SE_resampled, 'vertical', 'LineStyle', 'none');
set(error_bar_y, 'color', 'k', 'LineWidth', 0.5)
legend({'HS', 'HS', 'Average gait cycle Data', '+ 2 SD', '- 2 SD', 'Mean Difference', 'SE'});
hold on
xlim([0 120])
ylim([-10 10])
xlabel('Mean($\o - \bar{\o}$) (deg)', 'Interpreter','Latex')
ylabel('Difference (deg)')
title('Cycling - Younger Adults')

.
alexandra ligeti
2024-6-24
移动:Star Strider
2024-6-24
Hi,
No you have not missed anything, the issue arises when I have more participants over more gait cycles. For some reason the code works for a small portion of data, but when it becomes larger the dimensions of the variables change (as in the plot I am questioning ployts over 100). However, I am not sure what is causing this problem, whether it is the code or the data? Or what would be the best way to trouble shoot this?
My guess (and it only a guess) is that the reason could be that the data do not always have common independent vairable values. This can lead to the ends being different, even if the independent vairable values all begin at the same point (for example, 0). One fix for this could be to interpolate all the dependent variable values to the shortest independent variable, most likely using the interp1 function.
An example —
x1 = linspace(0, 0.9, 25).';
y1 = sin(2*pi*x1/max(x1));
x2 = linspace(0, 1.1, 28).';
y2 = sin(2*pi*x2/max(x2));
x3 = linspace(0, 1, 27).';
y3 = sin(2*pi*x3/max(x3));
figure
plot(x1, y1)
hold on
plot(x2, y2)
plot(x3, y3)
hold off
grid
xl = xlim;
title('Original')

y2i = interp1(x2, y2, x1);
y3i = interp1(x3, y3, x1);
figure
plot(x1, y1)
hold on
plot(x1, y2i)
plot(x1, y3i)
hold off
grid
xlim(xl)
title('Interpolated')

If this is the problem, then interpolating could be tthe solution.
.
I think this is exactly what is happening! Thank you :)
As always, my pleasure!
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Annotations 的更多信息
产品
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
