How to plot a partial regression plot for logistic regression (using fitglm)?

24 次查看(过去 30 天)
Dear All,
I am using a logistic regression model with a few predictors and a binary response model. I would like to a plot a partial regression plot for such a model. However, it seems that there is a function to plot partial regression only for linear regression. Is there an alternative of that kind for logistic regression?
Thank you.

回答(1 个)

Aman Banthia
Aman Banthia 2023-9-13
Hi Prashanti,
I understand that you are trying to plot a partial regression plot of a for logical regression model using ‘fitgml’ function.
Plotting a partial regression plot for logistic regression using the `fitglm` function in MATLAB can be a bit more involved compared to linear regression. Here's a step-by-step approach to create a partial regression plot for a single predictor in a logistic regression model:
1. Fit the logistic regression model: Start by fitting your logistic regression model using the `fitglm` function. Make sure to specify the appropriate family and link functions for logistic regression, such as 'binomial' for the family and 'logit' for the link function.
% Fit the logistic regression model
mdl = fitglm(X, y, 'Distribution', 'binomial', 'Link', 'logit');
Replace `X` with your predictor variables and `y` with the binary response variable.
2. Create a range of values for the predictor: Choose a range of values for the predictor variable for which you want to create the partial regression plot. You can use the `linspace` function to generate a sequence of evenly spaced values within a specified range.
% Create a range of values for the predictor
x_range = linspace(min(X), max(X), 100);
Adjust the number of values (100 in this example) based on the desired resolution of the plot.
3. Generate predicted probabilities: For each value in the range of the predictor variable, generate the predicted probabilities using the `predict` function on the logistic regression model object.
% Generate predicted probabilities for each value in the range
predicted_probs = predict(mdl, table(x_range', 'VariableNames', {'Predictor'}));
Replace `'Predictor'` with the name of your predictor variable.
4. Plot the partial regression plot: Finally, plot the partial regression plot by plotting the predicted probabilities against the range of the predictor variable.
% Plot the partial regression plot
plot(x_range, predicted_probs, 'LineWidth', 2);
xlabel('Predictor');
ylabel('Predicted Probability');
title('Partial Regression Plot');
Please refer to the following MATLAB Documentation to know more about ‘linspace’, ‘fitglm’ and ‘predict’ function:
Hope the above solution helps.
Best regards,
Aman Banthia

Community Treasure Hunt

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

Start Hunting!

Translated by