Intro a Tidymodels

Author

Karla Torres

Carga de librerias

pacman::p_load(tidyverse, 
               tidymodels,
               janitor,
               kableExtra,
               GGally,
               performance)

Datos

datos <- iris

Analisis Exploratorio de Datos

summary(datos)
  Sepal.Length    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  
       Species  
 setosa    :50  
 versicolor:50  
 virginica :50  
                
                
                
ggpairs(datos)
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Figure 1: Analisis gráfico multivariante

Particionamiento

set.seed(1234)
conf_part <- initial_split(datos, prop = 0.8)

datos_train <- training(conf_part)
datos_test <- testing(conf_part)
tabyl(datos_train$Species)
 datos_train$Species  n   percent
              setosa 37 0.3083333
          versicolor 42 0.3500000
           virginica 41 0.3416667
tabyl(datos_test$Species)
 datos_test$Species  n   percent
             setosa 13 0.4333333
         versicolor  8 0.2666667
          virginica  9 0.3000000
conf_part1 <- initial_split(datos, prop=0.8,
                            strata = Species)

datos_train1 <- training(conf_part1)
datos_test1 <- training(conf_part1)
tabyl(datos_train1$Species)
 datos_train1$Species  n   percent
               setosa 40 0.3333333
           versicolor 40 0.3333333
            virginica 40 0.3333333

Feature Engineering - recipes

datos_train1_sel <- datos_train1 |> 
  select(1:4)

datos_test1_sel <- datos_test1 |> 
  select(1:4)

receta <- recipe(Petal.Length ~ ., data = datos_train1_sel) |> 
  step_normalize(all_predictors()) |> 
  prep()
receta
── Recipe ──────────────────────────────────────────────────────────────────────
── Inputs 
Number of variables by role
outcome:   1
predictor: 3
── Training information 
Training data contained 120 data points and no incomplete rows.
── Operations 
• Centering and scaling for: Sepal.Length and Sepal.Width, ... | Trained

Aplicacion de la receta

datos_train1_sel_transf <- receta |> 
  juice()

mean(datos_train1_sel_transf$Sepal.Length)
[1] -3.91608e-15
sd(datos_train1_sel_transf$Sepal.Length)
[1] 1
datos_test1_sel_transf <- receta |> 
  bake(datos_test1_sel)

Especificaciones estadisticas del modelo

Revisar los modelos existentes en el marco de la libreria parsnip

https://www.tidymodels.org/find/parsnip/

modelo_lineal <- linear_reg() |> 
  set_engine("lm") |> 
  set_mode("regression")

Estamos ejecutando una tarea de regression.

Entrenamiento del modelo (fitting-ajuste)

ajuste_train_1 <- modelo_lineal |> 
  fit(Petal.Length ~ Sepal.Width, data = datos_train1_sel_transf)

Vamos a hacer un parentesis del flujo ML dentro de tidymodels

ajuste_train_1_lm <- lm(Petal.Length ~ Sepal.Width, data = datos_train1_sel_transf)

summary(ajuste_train_1_lm)

Call:
lm(formula = Petal.Length ~ Sepal.Width, data = datos_train1_sel_transf)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.7511 -1.4868  0.1675  1.2275  4.1561 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   3.7850     0.1501  25.222  < 2e-16 ***
Sepal.Width  -0.7153     0.1507  -4.747 5.86e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.644 on 118 degrees of freedom
Multiple R-squared:  0.1603,    Adjusted R-squared:  0.1532 
F-statistic: 22.53 on 1 and 118 DF,  p-value: 5.862e-06
ajuste_train_1 |> 
  pluck("fit") |> 
  summary()

Call:
stats::lm(formula = Petal.Length ~ Sepal.Width, data = data)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.7511 -1.4868  0.1675  1.2275  4.1561 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   3.7850     0.1501  25.222  < 2e-16 ***
Sepal.Width  -0.7153     0.1507  -4.747 5.86e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.644 on 118 degrees of freedom
Multiple R-squared:  0.1603,    Adjusted R-squared:  0.1532 
F-statistic: 22.53 on 1 and 118 DF,  p-value: 5.862e-06

Aplicacion del modelo a la particion testing

ml_predicc_test <- ajuste_train_1 |> predict(datos_test1_sel_transf) |> 
  bind_cols(datos_test1_sel_transf)

ml_predicc_test_augment <- augment(ajuste_train_1, datos_test1_sel_transf)

EVALUACION DEL MODELO (METRICAS DE AJUSTE)

ml_predicc_metricas <- ml_predicc_test |> 
  metrics(truth = Petal.Length, estimate = .pred)
  

ml_predicc_metricas
# A tibble: 3 × 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 rmse    standard       1.63 
2 rsq     standard       0.160
3 mae     standard       1.40 

Verificación de supuestos estadísticos

Metodo grafico

check_model(ajuste_train_1_lm)

Metodos pruebas estadisticas

check_autocorrelation(ajuste_train_1_lm)
Warning: Autocorrelated residuals detected (p < .001).
check_heteroscedasticity(ajuste_train_1_lm)
OK: Error variance appears to be homoscedastic (p = 0.092).
check_normality(ajuste_train_1_lm)
Warning: Non-normality of residuals detected (p = 0.029).

autocorrelacion que hay dependencia en los residuos se busca que los residuos sean de variaza constante

Modelo regresión lineal multiple TALLER

Particionamiento 2

set.seed(1234)
part2 <- initial_split(datos, prop = 0.8)

datos_train2 <- training(conf_part)
datos_test2 <- testing(conf_part)
tabyl(datos_train2$Species)
 datos_train2$Species  n   percent
               setosa 37 0.3083333
           versicolor 42 0.3500000
            virginica 41 0.3416667
tabyl(datos_test2$Species)
 datos_test2$Species  n   percent
              setosa 13 0.4333333
          versicolor  8 0.2666667
           virginica  9 0.3000000
part1 <- initial_split(datos, prop=0.8,
                            strata = Species)

datos_train2 <- training(part1)
datos_test2 <- training(part1)
tabyl(datos_train2$Species)
 datos_train2$Species  n   percent
               setosa 40 0.3333333
           versicolor 40 0.3333333
            virginica 40 0.3333333

Feature Engineering - recipes 2

datos_train2_sel <- datos_train2 |> 
  select(1:4)

datos_test2_sel <- datos_test2 |> 
  select(1:4)

receta2 <- recipe(Petal.Length ~ ., data = datos_train2_sel) |> 
  step_normalize(all_predictors()) |> 
  prep()
receta
── Recipe ──────────────────────────────────────────────────────────────────────
── Inputs 
Number of variables by role
outcome:   1
predictor: 3
── Training information 
Training data contained 120 data points and no incomplete rows.
── Operations 
• Centering and scaling for: Sepal.Length and Sepal.Width, ... | Trained

Aplicacion de la receta 2

datos_train2_sel_transf <- receta |> 
  juice()

mean(datos_train2_sel_transf$Sepal.Length)
[1] -3.91608e-15
sd(datos_train2_sel_transf$Sepal.Length)
[1] 1
datos_test2_sel_transf <- receta |> 
  bake(datos_test2_sel)

Especificaciones estadisticas del modelo2

Revisar los modelos existentes en el marco de la libreria parsnip

modelo_lineal2 <- linear_reg() |> 
  set_engine("lm") |> 
  set_mode("regression")

Estamos ejecutando una tarea de regression.

Entrenamiento del modelo (fitting-ajuste) 2

ajuste_train_2 <- modelo_lineal2 |> 
  fit(Petal.Length ~ Petal.Width, data = datos_train1_sel_transf)

Vamos a hacer un parentesis del flujo ML dentro de tidymodels

ajuste_train_2_lm <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = datos_train2_sel_transf)

summary(ajuste_train_2_lm)

Call:
lm(formula = Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, 
    data = datos_train2_sel_transf)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.90328 -0.26336  0.01991  0.22737  0.98184 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -3.15850    0.28572 -11.054  < 2e-16 ***
Sepal.Width   0.33482    0.03836   8.728 2.23e-14 ***
Petal.Length  0.83448    0.07495  11.134  < 2e-16 ***
Petal.Width  -0.49795    0.13000  -3.830 0.000208 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.374 on 116 degrees of freedom
Multiple R-squared:  0.8636,    Adjusted R-squared:  0.8601 
F-statistic: 244.9 on 3 and 116 DF,  p-value: < 2.2e-16
ajuste_train_2 |> 
  pluck("fit") |> 
  summary()

Call:
stats::lm(formula = Petal.Length ~ Petal.Width, data = data)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.15612 -0.32195  0.00475  0.25925  1.38425 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  3.78500    0.04428   85.47   <2e-16 ***
Petal.Width  1.71989    0.04447   38.68   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.4851 on 118 degrees of freedom
Multiple R-squared:  0.9269,    Adjusted R-squared:  0.9263 
F-statistic:  1496 on 1 and 118 DF,  p-value: < 2.2e-16

Aplicacion del modelo a la particion testing 2

ml_predicc_test2 <- ajuste_train_2 |> predict(datos_test2_sel_transf) |> 
  bind_cols(datos_test2_sel_transf)

ml_predicc_test_augment <- augment(ajuste_train_2, datos_test2_sel_transf)

EVALUACION DEL MODELO (METRICAS DE AJUSTE) 2

ml_predicc_metricas2 <- ml_predicc_test2 |> 
  metrics(truth = Petal.Length, estimate = .pred)
  

ml_predicc_metricas
# A tibble: 3 × 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 rmse    standard       1.63 
2 rsq     standard       0.160
3 mae     standard       1.40 

Metodo grafico2

check_model(ajuste_train_2_lm)

Metodos pruebas estadisticas 2

check_autocorrelation(ajuste_train_2_lm)
OK: Residuals appear to be independent and not autocorrelated (p = 0.998).
check_heteroscedasticity(ajuste_train_2_lm)
Warning: Heteroscedasticity (non-constant error variance) detected (p = 0.041).
check_normality(ajuste_train_2_lm)
OK: residuals appear as normally distributed (p = 0.865).