Binary Logistic Regression Curve

조회 수: 49 (최근 30일)
Jonathan Moorman
Jonathan Moorman 2020년 7월 2일
답변: Aditya Patil 2020년 8월 17일
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
Aditya Patil 2020년 8월 17일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by