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.
Este ejercicio forma parte del curso
Generalized Linear Models in Python
Instrucciones del ejercicio
- Using the data
wellsto plotarsenicon the x-axis andswitchon the y-axis. - Apply
y_jitterof 0.03 to spread the values of the response for easier visualization. - Use
Truefor argumentlogisticfor the plot to overlay the logistic function on the given data and set confidence intervals argumentcitoNonewhich will not display confidence interval, but it will speed up the computation. - Display the plot using the
plt.show().
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# Plot arsenic and switch and add overlay with the logistic fit
sns.regplot(x = ____, y = ____,
y_jitter = ____,
data = ____,
logistic = ____,
ci = ____)
# Display the plot
____