Trying to remove Nans when plotting histogram, pdf and cdf
4 views (last 30 days)
Show older comments
Nathaniel Porter
on 25 Feb 2022
Commented: Nathaniel Porter
on 26 Feb 2022
clear;
load InsulinReadings.mat
xX2 = InsulinReadings;
xX2(xX2==0)=missing;
A2 = mean(xX2,'all',"omitnan")
B2 = median(xX2,'all',"omitnan")
C2 = max(xX2,[],'all',"omitnan")
D2 = min(xX2,[],'all', "omitnan")
figure
histogram(InsulinReadings(~isnan(InsulinReadings),128,'Normalization')
xlabel('Insulin ng/dL')
%Now get pdf
[D PD] = allfitdist(xX2,'PDF');
xlabel('Insulin ng/dL');
%Now get the CDF
[D PD] = allfitdist(xGlucoseReadings,'CDF');
xlabel('Insulin ng/dL')
0 Comments
Accepted Answer
Voss
on 25 Feb 2022
I think you basically have it right. I just "fixed" a syntax error on the line where you call histogram(). ("fixed" is in quotes because I can't be sure what you're going for there.)
(Also, looks like allfitdist.m has been removed from the File Exchange, so I can't run it, but maybe your copy does the right thing here - I don't know.)
clear;
load InsulinReadings.mat
xX2 = InsulinReadings;
xX2(xX2==0)=missing;
A2 = mean(xX2,'all',"omitnan")
B2 = median(xX2,'all',"omitnan")
C2 = max(xX2,[],'all',"omitnan")
D2 = min(xX2,[],'all', "omitnan")
figure
histogram(InsulinReadings(~isnan(InsulinReadings)),128)%,'Normalization')
xlabel('Insulin ng/dL')
%Now get pdf
[D PD] = allfitdist(xX2,'PDF');
xlabel('Insulin ng/dL');
%Now get the CDF
[D PD] = allfitdist(xGlucoseReadings,'CDF');
xlabel('Insulin ng/dL')
6 Comments
Voss
on 25 Feb 2022
There probably are built-in functions, but I don't know what they are off the top of my head (maybe search the documentation).
It's relatively straighforward to calculate a PDF and CDF from the properties of the histogram:
clear;
load InsulinReadings.mat
xX2 = InsulinReadings;
xX2(xX2==0)=missing;
figure();
h = histogram(xX2(~isnan(xX2)),128);%,'Normalization')
% now make a new histogram with values in the first bin replaced with NaNs
edges = get(h,'BinEdges');
xX2(xX2 < edges(2)) = NaN;
figure();
h = histogram(xX2(~isnan(xX2)),128);%,'Normalization')
xlabel('Insulin ng/dL')
% pdf and cdf
figure();
edges = get(h,'BinEdges');
counts = get(h,'BinCounts');
bin_centers = (edges(1:end-1)+edges(2:end))/2;
total_counts = sum(counts);
pdf = counts/total_counts;
cdf = cumsum(counts)/total_counts;
plot(bin_centers,pdf,'LineWidth',2);
hold on
plot(bin_centers,cdf,'LineWidth',2);
xlim(bin_centers([1 end]));
legend('PDF','CDF');
More Answers (0)
See Also
Categories
Find more on Histograms 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!