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()

[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()

[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()

[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()

[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

[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()

[9]:
#
# Regresión usando lowless
#
sns.lmplot(
x="total_bill",
y="tip",
data=tips,
lowess=True,
)
plt.show()

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

[11]:
#
# Residuos con estructura
#
sns.residplot(
x="x",
y="y",
data=anscombe.query("dataset == 'II'"),
scatter_kws={"s": 80},
)
plt.show()
