###############################################################
# INFORME FINAL DE MODELO PREDICTIVO (COLUMNAS CORREGIDAS)
# Todo el texto, narrativa, explicaciones y conclusiones integrados en comentarios
###############################################################
# -------------------------------------------------------------
# PLANTEAMIENTO REVISADO DE LA PREGUNTA PREDICTIVA
# --------------------------------------------------------------
# ¿En qué medida las características morfológicas (Largo y ancho # del Sépalo, ancho del pétalo) permiten
#predecir el largo del petalo?
#
# Variable dependiente: Largo del petalo
# Variables predictoras: Largo y ancho del Sépalo, ancho del. # pétalo
# --------------------------------------------------------------
# ---------------------------------------------------------------
# DATASET Y VARIABLES SELECCIONADAS
# ---------------------------------------------------------------
library(car)
## Loading required package: carData
datos <- read.csv("/Users/rafaelpg/Downloads/Base_iris_trabajoR.csv")
str(datos)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal_lenght: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal_width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal_length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal_width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Variety : chr "Setosa" "Setosa" "Setosa" "Setosa" ...
summary(datos)
## Sepal_lenght Sepal_width Petal_length Petal_width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Variety
## Length:150
## Class :character
## Mode :character
##
##
##
head(datos)
## Sepal_lenght Sepal_width Petal_length Petal_width Variety
## 1 5.1 3.5 1.4 0.2 Setosa
## 2 4.9 3.0 1.4 0.2 Setosa
## 3 4.7 3.2 1.3 0.2 Setosa
## 4 4.6 3.1 1.5 0.2 Setosa
## 5 5.0 3.6 1.4 0.2 Setosa
## 6 5.4 3.9 1.7 0.4 Setosa
vars_num <- c("Sepal_lenght", "Sepal_width", "Petal_length", "Petal_width")
datos_sel <- datos[, vars_num]
summary(datos_sel)
## Sepal_lenght Sepal_width Petal_length Petal_width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
# ---------------------------------------------------------------
# VISUALIZACIONES CLAVE
# ---------------------------------------------------------------
correlaciones <- cor(datos_sel)
correlaciones
## Sepal_lenght Sepal_width Petal_length Petal_width
## Sepal_lenght 1.0000000 -0.1175698 0.8717538 0.8179411
## Sepal_width -0.1175698 1.0000000 -0.4284401 -0.3661259
## Petal_length 0.8717538 -0.4284401 1.0000000 0.9628654
## Petal_width 0.8179411 -0.3661259 0.9628654 1.0000000
pairs(datos_sel,
main = "Matriz de dispersión de variables morfológicas del Iris")

# Comentario:
# Largo del petalo suele correlacionar fuertemente con el ancho # del petalo.
# El ancho del sépalo aporta menos información predictiva.
# ---------------------------------------------------------------
# MODELO PRELIMINAR
modelo_completo <- lm(Petal_length ~ Sepal_lenght + Sepal_width + Petal_width,
data = datos_sel)
summary(modelo_completo)
##
## Call:
## lm(formula = Petal_length ~ Sepal_lenght + Sepal_width + Petal_width,
## data = datos_sel)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.99333 -0.17656 -0.01004 0.18558 1.06909
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.26271 0.29741 -0.883 0.379
## Sepal_lenght 0.72914 0.05832 12.502 <2e-16 ***
## Sepal_width -0.64601 0.06850 -9.431 <2e-16 ***
## Petal_width 1.44679 0.06761 21.399 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.319 on 146 degrees of freedom
## Multiple R-squared: 0.968, Adjusted R-squared: 0.9674
## F-statistic: 1473 on 3 and 146 DF, p-value: < 2.2e-16
# DIAGNÓSTICO DEL MODELO PRELIMINAR
par(mfrow = c(2, 2))
plot(modelo_completo)

par(mfrow = c(1, 1))
# ---------------------------------------------------------------------------
# EVALUACIÓN DE COLINEALIDAD
# ---------------------------------------------------------------------------
vif(modelo_completo)
## Sepal_lenght Sepal_width Petal_width
## 3.415733 1.305515 3.889961
# ---------------------------------------------------------------------------
# d) COMPARACIÓN DE MODELOS Y MODELO FINAL
# ---------------------------------------------------------------------------
# MODELO 1 (COMPLETO)
modelo_1 <- modelo_completo
# MODELO 2 (REDUCIDO) eliminando Sepal_width
modelo_2 <- lm(Petal_length ~ Sepal_lenght + Petal_width,
data = datos_sel)
summary(modelo_1)
##
## Call:
## lm(formula = Petal_length ~ Sepal_lenght + Sepal_width + Petal_width,
## data = datos_sel)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.99333 -0.17656 -0.01004 0.18558 1.06909
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.26271 0.29741 -0.883 0.379
## Sepal_lenght 0.72914 0.05832 12.502 <2e-16 ***
## Sepal_width -0.64601 0.06850 -9.431 <2e-16 ***
## Petal_width 1.44679 0.06761 21.399 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.319 on 146 degrees of freedom
## Multiple R-squared: 0.968, Adjusted R-squared: 0.9674
## F-statistic: 1473 on 3 and 146 DF, p-value: < 2.2e-16
summary(modelo_2)
##
## Call:
## lm(formula = Petal_length ~ Sepal_lenght + Petal_width, data = datos_sel)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.15506 -0.21920 -0.02115 0.25986 1.35204
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.50714 0.33696 -4.473 1.54e-05 ***
## Sepal_lenght 0.54226 0.06934 7.820 9.41e-13 ***
## Petal_width 1.74810 0.07533 23.205 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4032 on 147 degrees of freedom
## Multiple R-squared: 0.9485, Adjusted R-squared: 0.9478
## F-statistic: 1354 on 2 and 147 DF, p-value: < 2.2e-16
anova(modelo_2, modelo_1)
## Analysis of Variance Table
##
## Model 1: Petal_length ~ Sepal_lenght + Petal_width
## Model 2: Petal_length ~ Sepal_lenght + Sepal_width + Petal_width
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 147 23.902
## 2 146 14.853 1 9.0488 88.947 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Métricas del modelo final
summary(modelo_2)
##
## Call:
## lm(formula = Petal_length ~ Sepal_lenght + Petal_width, data = datos_sel)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.15506 -0.21920 -0.02115 0.25986 1.35204
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.50714 0.33696 -4.473 1.54e-05 ***
## Sepal_lenght 0.54226 0.06934 7.820 9.41e-13 ***
## Petal_width 1.74810 0.07533 23.205 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4032 on 147 degrees of freedom
## Multiple R-squared: 0.9485, Adjusted R-squared: 0.9478
## F-statistic: 1354 on 2 and 147 DF, p-value: < 2.2e-16
rmse_m2 <- sqrt(mean(residuals(modelo_2)^2))
rmse_m2
## [1] 0.3991807
par(mfrow = c(2, 2))
plot(modelo_2)

par(mfrow = c(1, 1))
# ---------------------------------------------------------------------------
# INTERPRETACIÓN DEL MODELO FINAL
# ---------------------------------------------------------------------------
# Fórmula:
# Largo del petalo = β0 + β1*largo del sépalo + β2*ancho del petalo + ε
#
# β1: efecto del largo del sépalo manteniendo constante el ancho # del petalo
# β2: efecto del ancho del petalo manteniendo constante # el largo del sepalo.
# Ancho del petalo suele ser el predictor más fuerte.
# R² y R² ajustado muy altos.
# ---------------------------------------------------------------
# ---------------------------------------------------------------
# DISCUSIÓN DEL MODELO
# ---------------------------------------------------------------
# Qué predice:
# Predice largo del petalo a partir del largo del sepañol y # del ancho del petalo.
# Fortalezas:
# Excelente ajuste, parsimonioso, interpretable.
# ---------------------------------------------------------------
# CONCLUSIONES FINALES
# ---------------------------------------------------------------
# 1. El modelo final (largo del sepalo + ancho del petalo) predice muy bien el largo del petalo.
# 2. Ancho del petalo aporta poco y puede generar redundancia.
# 3. ANOVA respalda el modelo reducido.
# 4. Modelo final: interpretabilidad + gran capacidad predictiva.
# ---------------------------------------------------------------