# Binary Logistic Regression Curve

105 views (last 30 days)
Jonathan Moorman on 2 Jul 2020
Hello! I am trying to create a logistical regression curve for my binary data in Figure 3. Is this possible to do in MATLAB, and if so, how could it be done? My code is below? Thanks
%Figure 2 Graphing
scatter(FactoredLength, FactoredAmplitude,5,'filled')
hold on
coefficients = polyfit(FactoredLength, FactoredAmplitude, 1);
xFit = linspace(min(FactoredLength), max(FactoredLength), 1000);
yFit = polyval(coefficients , xFit);
plot(xFit, yFit, 'r-', 'LineWidth', 2);
xlabel('Factored Length')
ylabel('Probability')
grid on;
hold off
figure
% Making data binary
Probability = ((exp(log10(FactoredAmplitude)))./(1+exp(log10(FactoredAmplitude))));
yHat(Probability > app.ThreshHoldValueEditField.Value) = 1;
yHat(Probability < app.ThreshHoldValueEditField.Value) = 0;
%Figure 3 Graphing
scatter(FactoredLength,yHat)
xlabel('Factored Length')
ylabel('Probability')

Aditya Patil on 17 Aug 2020
Use the fitglm function to fit logistic regression model to data. Check the following code for example,
% Create random data
x = rand(100, 1);
y = x > 0.5;
y(1:50) = x(1:50) > 0.3; % To avoid perfect seperation
% Fit model
mdl = fitglm(x, y, "Distribution", "binomial");
xnew = linspace(0,1,1000)'; % test data
ynew = predict(mdl, xnew);
scatter(x, y);
hold on;
plot(xnew, ynew);
This will give following output.

### Categories

Find more on Statistics and Machine Learning Toolbox 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!

Translated by