Sei sulla pagina 1di 4

12/4/23, 00:01 Copia de modeloproyeccion.

ipynb - Colaboratory

EXAMEN FINAL EJERCICIO 1

# Tratamiento de datos
# ==============================================================================
import pandas as pd
import numpy as np

# Gráficos
# ==============================================================================
import matplotlib.pyplot as plt
from matplotlib import style
import seaborn as sns

# Preprocesado y modelado
# ==============================================================================
from scipy.stats import pearsonr
from·sklearn.linear_model·import·LinearRegression
from·sklearn.model_selection·import·train_test_split
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
import statsmodels.api as sm
import statsmodels.formula.api as smf

# Configuración matplotlib
# ==============================================================================
plt.rcParams['image.cmap'] = "bwr"
#plt.rcParams['figure.dpi'] = "100"
plt.rcParams['savefig.bbox'] = "tight"
style.use('ggplot') or plt.style.use('ggplot')

# Configuración warnings
# ==============================================================================
import warnings
warnings.filterwarnings('ignore')

#datos
modelo= pd.read_excel("modelofinalxd.xlsx") # read in data downloaded to the local directory
modelo.head(n=12)

ventas publicidad

0 164 19

1 150 18

2 136 16

3 122 15

4 108 13

5 95 12

6 80 10

7 60 9

8 55 7

9 45 5

10 20 4

11 9 3

X = modelo[['publicidad']]
y = modelo['ventas']

X = sm.add_constant(X, prepend=True)
modelo = sm.OLS(endog=y, exog=X,)
modelo = modelo.fit()
print(modelo.summary())

OLS Regression Results


==============================================================================
Dep. Variable: ventas R-squared: 0.988
Model: OLS Adj. R-squared: 0.987
Method: Least Squares F-statistic: 845.9
Date: Tue, 11 Apr 2023 Prob (F-statistic): 5.38e-11
Time: 20:21:23 Log-Likelihood: -36.896
No. Observations: 12 AIC: 77.79
Df Residuals: 10 BIC: 78.76

https://colab.research.google.com/drive/15M64v9DE-Bx0ySVbMi5WQgYki0i19IHo#scrollTo=2nw3bvnG-JSR&printMode=true 1/4
12/4/23, 00:01 Copia de modeloproyeccion.ipynb - Colaboratory
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const -13.4322 3.830 -3.507 0.006 -21.965 -4.899
publicidad 9.1999 0.316 29.084 0.000 8.495 9.905
==============================================================================
Omnibus: 3.054 Durbin-Watson: 2.219
Prob(Omnibus): 0.217 Jarque-Bera (JB): 0.870
Skew: 0.576 Prob(JB): 0.647
Kurtosis: 3.641 Cond. No. 28.2
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

Haz doble clic (o ingresa) para editar

VENTAS= -13.4322+ 9.1999 PUBLICIDAD

#dividir la base de datos 
X_train, X_test, y_train, y_test = train_test_split(
                                        X,
                                        y.values.reshape(-1,1),
                                        train_size   = 0.7,
                                        random_state = 1234,
                                        shuffle      = True
                                    )

# A la matriz de predictores se le tiene que añadir una columna de 1s para el intercept del modelo
X_train = sm.add_constant(X_train, prepend=True)
modelo = sm.OLS(endog=y_train, exog=X_train,)
modelo = modelo.fit()
print(modelo.summary())

OLS Regression Results


==============================================================================
Dep. Variable: y R-squared: 0.990
Model: OLS Adj. R-squared: 0.989
Method: Least Squares F-statistic: 607.4
Date: Tue, 11 Apr 2023 Prob (F-statistic): 2.94e-07
Time: 20:21:34 Log-Likelihood: -23.480
No. Observations: 8 AIC: 50.96
Df Residuals: 6 BIC: 51.12
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const -8.8113 4.716 -1.868 0.111 -20.351 2.728
publicidad 8.9009 0.361 24.645 0.000 8.017 9.785
==============================================================================
Omnibus: 2.017 Durbin-Watson: 2.121
Prob(Omnibus): 0.365 Jarque-Bera (JB): 0.576
Skew: 0.655 Prob(JB): 0.750
Kurtosis: 2.893 Cond. No. 33.3
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

VENTAS = -8.8113+8.9009 PUBLICIDAD

y_train = y_train.flatten()
prediccion_train = modelo.predict(exog = X_train)
residuos_train   = prediccion_train - y_train

# Gráficos
# ==============================================================================
fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(9, 8))

axes[0, 0].scatter(y_train, prediccion_train, edgecolors=(0, 0, 0), alpha = 0.4)
axes[0, 0].plot([y_train.min(), y_train.max()], [y_train.min(), y_train.max()],
                'k--', color = 'black', lw=2)
axes[0, 0].set_title('Valor predicho vs valor real', fontsize = 10, fontweight = "bold")
axes[0, 0].set_xlabel('Real')
axes[0, 0].set_ylabel('Predicción')
axes[0, 0].tick_params(labelsize = 7)

axes[0 1] scatter(list(range(len(y train))) residuos train


https://colab.research.google.com/drive/15M64v9DE-Bx0ySVbMi5WQgYki0i19IHo#scrollTo=2nw3bvnG-JSR&printMode=true 2/4
12/4/23, 00:01 Copia de modeloproyeccion.ipynb - Colaboratory
axes[0, 1].scatter(list(range(len(y_train))), residuos_train,
                   edgecolors=(0, 0, 0), alpha = 0.4)
axes[0, 1].axhline(y = 0, linestyle = '--', color = 'black', lw=2)
axes[0, 1].set_title('Residuos del modelo', fontsize = 10, fontweight = "bold")
axes[0, 1].set_xlabel('id')
axes[0, 1].set_ylabel('Residuo')
axes[0, 1].tick_params(labelsize = 7)

sns.histplot(
    data    = residuos_train,
    stat    = "density",
    kde     = True,
    line_kws= {'linewidth': 1},
    color   = "firebrick",
    alpha   = 0.3,
    ax      = axes[1, 0]
)

axes[1, 0].set_title('Distribución residuos del modelo', fontsize = 10,
                     fontweight = "bold")
axes[1, 0].set_xlabel("Residuo")
axes[1, 0].tick_params(labelsize = 7)

sm.qqplot(
    residuos_train,
    fit   = True,
    line  = 'q',
    ax    = axes[1, 1], 
    color = 'firebrick',
    alpha = 0.4,
    lw    = 2
)
axes[1, 1].set_title('Q-Q residuos del modelo', fontsize = 10, fontweight = "bold")
axes[1, 1].tick_params(labelsize = 7)

axes[2, 0].scatter(prediccion_train, residuos_train,
                   edgecolors=(0, 0, 0), alpha = 0.4)
axes[2, 0].axhline(y = 0, linestyle = '--', color = 'black', lw=2)
axes[2, 0].set_title('Residuos del modelo vs predicción', fontsize = 10, fontweight = "bold")
axes[2, 0].set_xlabel('Predicción')
axes[2, 0].set_ylabel('Residuo')
axes[2, 0].tick_params(labelsize = 7)

# Se eliminan los axes vacíos
fig.delaxes(axes[2,1])

fig.tight_layout()
plt.subplots_adjust(top=0.9)
fig.suptitle('Diagnóstico residuos', fontsize = 12, fontweight = "bold");

https://colab.research.google.com/drive/15M64v9DE-Bx0ySVbMi5WQgYki0i19IHo#scrollTo=2nw3bvnG-JSR&printMode=true 3/4
12/4/23, 00:01 Copia de modeloproyeccion.ipynb - Colaboratory

Productos pagados de Colab - Cancela los contratos aquí

https://colab.research.google.com/drive/15M64v9DE-Bx0ySVbMi5WQgYki0i19IHo#scrollTo=2nw3bvnG-JSR&printMode=true 4/4

Potrebbero piacerti anche