Visualización de diferentes tipos de modelos con lmplot() —#

  • 0:00 min | Última modificación: Octubre 13, 2021 | [YouTube]

[1]:
import matplotlib.pyplot as plt
import seaborn as sns
[2]:
anscombe = sns.load_dataset("anscombe")
tips = sns.load_dataset("tips")
[3]:
#
# Ajuste para uno de los subconjuntos de datos usando un modelo lineal
#
sns.lmplot(
    x="x",
    y="y",
    data=anscombe.query("dataset == 'I'"),
    ci=None,
    scatter_kws={"s": 80},
)

plt.show()
../../_images/02_seaborn_notebooks_5-51_regression_lmplot_other_models_3_0.png
[4]:
#
# Ajuste para el otro subconjunto de datos usando un modelo lineal
#
sns.lmplot(
    x="x",
    y="y",
    data=anscombe.query("dataset == 'II'"),
    ci=None,
    scatter_kws={"s": 80},
)

plt.show()
../../_images/02_seaborn_notebooks_5-51_regression_lmplot_other_models_4_0.png
[5]:
#
# AJuste usando un polinomio de grado 2.
#
sns.lmplot(
    x="x",
    y="y",
    data=anscombe.query("dataset == 'II'"),
    order=2,
    ci=None,
    scatter_kws={"s": 80},
)

plt.show()
../../_images/02_seaborn_notebooks_5-51_regression_lmplot_other_models_5_0.png
[6]:
#
# Ajuste en presencia de outliers.
#
sns.lmplot(
    x="x",
    y="y",
    data=anscombe.query("dataset == 'III'"),
    ci=None,
    scatter_kws={"s": 80},
)

plt.show()
../../_images/02_seaborn_notebooks_5-51_regression_lmplot_other_models_6_0.png
[7]:
#
# Regresión robusta. Requiere statsmodels.
#
sns.lmplot(
    x="x",
    y="y",
    data=anscombe.query("dataset == 'III'"),
    robust=True,
    ci=None,
    scatter_kws={"s": 80},
)

plt.show()
/usr/local/lib/python3.6/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  import pandas.util.testing as tm
../../_images/02_seaborn_notebooks_5-51_regression_lmplot_other_models_7_1.png
[8]:
#
# Regresión logística.
#
tips["big_tip"] = (tips.tip / tips.total_bill) > .15

sns.lmplot(
    x="total_bill",
    y="big_tip",
    data=tips,
    logistic=True,
    y_jitter=0.03,
)

plt.show()
../../_images/02_seaborn_notebooks_5-51_regression_lmplot_other_models_8_0.png
[9]:
#
# Regresión usando lowless
#
sns.lmplot(
    x="total_bill",
    y="tip",
    data=tips,
    lowess=True,
)

plt.show()
../../_images/02_seaborn_notebooks_5-51_regression_lmplot_other_models_9_0.png
[10]:
#
# Gráfico de residuos del modelo
#
sns.residplot(
    x="x",
    y="y",
    data=anscombe.query("dataset == 'I'"),
    scatter_kws={"s": 80},
)

plt.show()
../../_images/02_seaborn_notebooks_5-51_regression_lmplot_other_models_10_0.png
[11]:
#
# Residuos con estructura
#
sns.residplot(
    x="x",
    y="y",
    data=anscombe.query("dataset == 'II'"),
    scatter_kws={"s": 80},
)

plt.show()
../../_images/02_seaborn_notebooks_5-51_regression_lmplot_other_models_11_0.png