Visualize model fit using regplot()
After having fitted and analyzed the model we can visualize it by plotting the observation points and the fitted logistic regression.
Using the plot you can visually understand the relationship of the explanatory variable and the response for the range of values of the explanatory variable.
We can use the regplot()
function from the seaborn
module for this. The regplot()
function takes an argument logistic
, which allows you to specify whether you wish to estimate the logistic regression model for the given data using True
or False
values. This will also produce the plot of the fit.
Recall that the model that you fitted previously:
$$
\log\bigg(\frac{y}{1-y}\bigg) = -0.3055 + 0.3791*\text{arsenic}
$$
The dataset wells
is already loaded in your workspace.
This exercise is part of the course
Generalized Linear Models in Python
Exercise instructions
- Using the data
wells
to plotarsenic
on the x-axis andswitch
on the y-axis. - Apply
y_jitter
of 0.03 to spread the values of the response for easier visualization. - Use
True
for argumentlogistic
for the plot to overlay the logistic function on the given data and set confidence intervals argumentci
toNone
which will not display confidence interval, but it will speed up the computation. - Display the plot using the
plt.show()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Plot arsenic and switch and add overlay with the logistic fit
sns.regplot(x = ____, y = ____,
y_jitter = ____,
data = ____,
logistic = ____,
ci = ____)
# Display the plot
____