Problem in determining standard error and plotting 3 error bars
Show older comments
Hello everyone,
I am trying to make a figure where Y-axis is temperature and X-axis represents particle concentration. The I-type vertical bars indicate standard error. I tried this:
Data = table2array(WORK01S1); % convert it into array
Temp = double(Data(:,1)); % extract the column of Temperature from Data
Particle = double(Data(:,2)); % extract the column of Particle from Data
Particle = Particle*1000000000; % To change kg/um3 to ug/um3
CAPE = double(Data(:,3)); % extract the column of CAPE from Data
[N,edges,bins] = histcounts(Particle,10) % I divided the Particle concentration in to 10 bins (just to check I selected 10 bins)
% I used below line to calculate standard error but I am not sure whether
% its correct or not
std_err= std(Data,[],2)/sqrt(size(Data,2))
I am not able to calculate standard error properly and aslo I want 3 error bars as shown in the figure (values of CAPE too). What changes and additions should I make in my code? I can't figure out this. I'm grateful for any help. Data is attached.
7 Comments
Walter Roberson
on 13 Jun 2022
Why are you dividing std() by the size of the data? std() is already defined to divide by sqrt(N-1)
Zhou Ci
on 13 Jun 2022
dpb
on 13 Jun 2022
"Standard error of what?"
The standard deveiation (std function in MATLAB) is an estimate of the population variablility while the estimate of the variability of the mean of the population is std()/sqrt(n)
The latter is the estimate of how good the estimate of the statistic (the mean, here) is relative to the population mean; the former is about the variablilty of the distribution individual values.
Both are correct; they just represent different things -- we don't know and can't know which one you want...although it would be typical to put the population estimate on a plot of the data, but if the statistic shown is properly identified as to what it is, either could be chosen.
Zhou Ci
on 13 Jun 2022
dpb
on 13 Jun 2022
So what's the problem, then?
Zhou Ci
on 13 Jun 2022
Zhou Ci
on 13 Jun 2022
Accepted Answer
More Answers (1)
Peter Perkins
on 13 Jun 2022
Zhou, independently from how to make the figure, this code
Data = table2array(WORK01S1); % convert it into array
Temp = double(Data(:,1)); % extract the column of Temperature from Data
Particle = double(Data(:,2)); % extract the column of Particle from Data
Particle = Particle*1000000000; % To change kg/um3 to ug/um3
CAPE = double(Data(:,3)); % extract the column of CAPE from Data
seems much too complicated. I suggest perhaps
Temp = WORK01S1.Temp;
Particle = WORK01S1.ParticleConc*1000000000;
CAPE = WORK01S1.CAPE;
But more to answer your question:
Use the table. It will make your life easier.
>> WORK01S1 = readtable("WORK.xlsx");
>> WORK01S1.binnedCAPE = discretize(WORK01S1.CAPE,[0 500 900 Inf],"categorical");
>> WORK01S1.binnedParticleConc = discretize(WORK01S1.ParticleConc,10,"categorical");
>> meanTemp = varfun(@mean,WORK01S1,"GroupingVariables",["binnedCAPE" "binnedParticleConc" ],"InputVariables","Temp");
>> sem = @(x) std(x)/sqrt(length(x));
>> semTemp = varfun(sem,WORK01S1,"GroupingVariables",["binnedCAPE" "binnedParticleConc" ],"InputVariables","Temp")
>> meanTemp.SE = semTemp.Fun_Temp
meanTemp =
13×5 table
binnedCAPE binnedParticleConc GroupCount mean_Temp SE
__________ ____________________ __________ _________ _______
[0, 500) [0, 3.8e-08) 45 -26.889 0.52438
[0, 500) [3.8e-08, 7.6e-08) 13 -28.538 0.93106
[0, 500) [7.6e-08, 1.14e-07) 5 -29 0.89443
[0, 500) [1.14e-07, 1.52e-07) 2 -22.5 0.5
[0, 500) [1.52e-07, 1.9e-07) 2 -27.5 1.5
[0, 500) [1.9e-07, 2.28e-07) 2 -24.5 2.5
[0, 500) [2.66e-07, 3.04e-07) 2 -23 1
[0, 500) [3.04e-07, 3.42e-07) 3 -27.667 3.6667
[0, 500) [3.42e-07, 3.8e-07] 1 -26 0
[500, 900) [0, 3.8e-08) 14 -24.429 0.99291
[500, 900) [3.8e-08, 7.6e-08) 1 -29 0
[900, Inf] [0, 3.8e-08) 56 -23.661 0.48031
[900, Inf] [3.8e-08, 7.6e-08) 3 -26.333 3.6667
Now you can make the plot. One way is to loop over CAPE bins, where at each iteration, you get the data for one CAPE bin:
for cape_i = categories(meanTemp.binnedCAPE)'
i = meanTemp.binnedCAPE == cape_i;
Temp = meanTemp.mean_Temp(i);
Conc = meanTemp.binnedParticleConc(i);
% make plot
end
Of course, your data doesn't really support your figure, but that's for you to reconcile.
4 Comments
dpb
on 13 Jun 2022
"Now you can make the plot. One way is to loop over CAPE bins..."
Peter, why aren't there more plotting routines with options for grouping -- or methods tied in with rowfun, varfun, splitapply? gscatter is handy, but is lone in its arena -- although one could go back and set the 'linestyle' there; I almost did that.
I got frustrated that with grouping variables there's no way to sort the groups effectively so the plot will show up in ordered fashion. I built an index variable that was the order vector in the group thinking I could pass it to an anonymous function using plot() but it didn't work as expected and I ran out of time to try to figure out what was actually going on. One thing in these exercises is they're not easy to debug... :)
Peter Perkins
on 14 Jun 2022
Point taken, thanks. This really should be easier, I will see what can be done about that.
Thanks...I know there's more to do than time/resources to do it, but... :)
"... with grouping variables there's no way to sort the groups effectively ..."
This one ties in with the previous complaint/suggestions I've made that one can't return the sorting index from sort except as the second, optional output and there's no way to use anything except the first return in the anonymous function effectively for these purposes. I've built a customized sorti that does return the index first; I think there should be a flag parameter in sort for the purpose.
I still don't follow why my use of
@(x,y,ix) plot(x(ix),y(ix))
in splitapply wasn't in sorted order after building that sort index by group and passing it in the anonymous function, but don't have the time just now to pursue it further. I must've made a bonehead blunder somewhere, but it wasn't apparent where that was.
Walter Roberson
on 14 Jun 2022
In the case where duplicate values are to be assigned the same index, then findgroups() returns sort order.
Categories
Find more on Errorbars in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!