Created
February 12, 2017 11:03
-
-
Save erikhuizinga/cdde44204001a3548b738d8517279b36 to your computer and use it in GitHub Desktop.
MATLAB: Fisher's Iris data set nomogram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Load data | |
clear | |
close all | |
load fisheriris | |
selection = 51:length(meas); | |
length = meas(selection, 1); % sepal length | |
width = meas(selection, 2); % sepal width | |
species = categorical(species(selection)); % flower species | |
%% Fit a model | |
% Store the data in a table | |
T = table(length, width, species); | |
% Fit a logistic binomial regression model | |
model = fitglm(T, 'Distribution', 'binomial'); | |
%% Create a nomogram (contour plot) | |
% Prepare contour plot calculation, select lengths and widths to predict | |
lengths = min(length) : .1 : max(length); | |
widths = min(width) : .1 : max(width); | |
% Force column vectors | |
lengths = lengths(:); | |
widths = widths(:); | |
% Preallocate predictions | |
predictions = nan(numel(lengths), numel(widths)); | |
% Predict probabilities | |
for j = 1:numel(widths) | |
predictions(:, j) = model.predict( ... | |
[lengths, widths(j) * ones(size(lengths))] ... | |
); | |
end | |
% Plot the nomogram | |
figure | |
subplot 211 | |
contour(lengths, widths, predictions', 'ShowText', 'on', 'LineColor', 'k') | |
grid on | |
title(['Nomogram of probabilities for species ''virginica'' vs. ' ... | |
'''versicolor'' by sepal length and width']) | |
xlabel 'sepal length' | |
ylabel 'sepal width' | |
% or | |
subplot 212 | |
contourf(lengths, widths, predictions', 'ShowText', 'on') | |
xlabel 'sepal length' | |
ylabel 'sepal width' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment