1.- Entendimiento del proyecto: tarea de prediccion en el conjunto de datos titanic.
1.1.-El objetivo principal se basa en predecir que numero de pasajeros pueden sobrevivir al undimiento (naufragio) que sufrio el titanic.
1.2.-La importanacia de esta tarea se basa en sacar el maximo provecho que se generaron durante el naufragio; asi mismo, poder generar establecer un patron predictivo entre, las personas que se salvaron y las que no se salvaron.
1.3.-Problema: ¿Que tipo de personas tenian mas probabilidades de sobrevivir? considerando las variables de dataset titanic.
2.- Conocimiento de los datos 2.1.-Recoleccion de Los datos: fueron suministrados por la red social kaggle | https://www.kaggle.com/c/titanic/data en un ejercicio de competencia estos datos suministrados son parte de una competencia de ML; por esta razon, los datos ya se encuentran subdivididos en train.csv y test.csv
#EXPLORACION DE LOS DATOS
library(readr)
train <- read_csv("train.csv")
## Parsed with column specification:
## cols(
## PassengerId = col_double(),
## Survived = col_double(),
## Pclass = col_double(),
## Name = col_character(),
## Sex = col_character(),
## Age = col_double(),
## SibSp = col_double(),
## Parch = col_double(),
## Ticket = col_character(),
## Fare = col_double(),
## Cabin = col_character(),
## Embarked = col_character()
## )
test = read_csv("test.csv")
## Parsed with column specification:
## cols(
## PassengerId = col_double(),
## Pclass = col_double(),
## Name = col_character(),
## Sex = col_character(),
## Age = col_double(),
## SibSp = col_double(),
## Parch = col_double(),
## Ticket = col_character(),
## Fare = col_double(),
## Cabin = col_character(),
## Embarked = col_character()
## )
2.2.-comprension de los datos
head(train)
## # A tibble: 6 x 12
## PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin
## <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <chr>
## 1 1 0 3 Brau… male 22 1 0 A/5 2… 7.25 <NA>
## 2 2 1 1 Cumi… fema… 38 1 0 PC 17… 71.3 C85
## 3 3 1 3 Heik… fema… 26 0 0 STON/… 7.92 <NA>
## 4 4 1 1 Futr… fema… 35 1 0 113803 53.1 C123
## 5 5 0 3 Alle… male 35 0 0 373450 8.05 <NA>
## 6 6 0 3 Mora… male NA 0 0 330877 8.46 <NA>
## # … with 1 more variable: Embarked <chr>
head(test)
## # A tibble: 6 x 11
## PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
## <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <chr> <chr>
## 1 892 3 Kelly,… male 34.5 0 0 330911 7.83 <NA> Q
## 2 893 3 Wilkes… fema… 47 1 0 363272 7 <NA> S
## 3 894 2 Myles,… male 62 0 0 240276 9.69 <NA> Q
## 4 895 3 Wirz, … male 27 0 0 315154 8.66 <NA> S
## 5 896 3 Hirvon… fema… 22 1 1 31012… 12.3 <NA> S
## 6 897 3 Svenss… male 14 0 0 7538 9.22 <NA> S
df <- train
df_test = test
glimpse(df)
## Rows: 891
## Columns: 12
## $ PassengerId <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17…
## $ Survived <dbl> 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, …
## $ Pclass <dbl> 3, 1, 3, 1, 3, 3, 1, 3, 3, 2, 3, 1, 3, 3, 3, 2, 3, 2, 3, …
## $ Name <chr> "Braund, Mr. Owen Harris", "Cumings, Mrs. John Bradley (F…
## $ Sex <chr> "male", "female", "female", "female", "male", "male", "ma…
## $ Age <dbl> 22, 38, 26, 35, 35, NA, 54, 2, 27, 14, 4, 58, 20, 39, 14,…
## $ SibSp <dbl> 1, 1, 0, 1, 0, 0, 0, 3, 0, 1, 1, 0, 0, 1, 0, 0, 4, 0, 1, …
## $ Parch <dbl> 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 0, 0, 5, 0, 0, 1, 0, 0, …
## $ Ticket <chr> "A/5 21171", "PC 17599", "STON/O2. 3101282", "113803", "3…
## $ Fare <dbl> 7.2500, 71.2833, 7.9250, 53.1000, 8.0500, 8.4583, 51.8625…
## $ Cabin <chr> NA, "C85", NA, "C123", NA, NA, "E46", NA, NA, NA, "G6", "…
## $ Embarked <chr> "S", "C", "S", "S", "S", "Q", "S", "S", "S", "C", "S", "S…
glimpse(df_test)
## Rows: 418
## Columns: 11
## $ PassengerId <dbl> 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 902, 90…
## $ Pclass <dbl> 3, 3, 2, 3, 3, 3, 3, 2, 3, 3, 3, 1, 1, 2, 1, 2, 2, 3, 3, …
## $ Name <chr> "Kelly, Mr. James", "Wilkes, Mrs. James (Ellen Needs)", "…
## $ Sex <chr> "male", "female", "male", "male", "female", "male", "fema…
## $ Age <dbl> 34.5, 47.0, 62.0, 27.0, 22.0, 14.0, 30.0, 26.0, 18.0, 21.…
## $ SibSp <dbl> 0, 1, 0, 0, 1, 0, 0, 1, 0, 2, 0, 0, 1, 1, 1, 1, 0, 0, 1, …
## $ Parch <dbl> 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ Ticket <chr> "330911", "363272", "240276", "315154", "3101298", "7538"…
## $ Fare <dbl> 7.8292, 7.0000, 9.6875, 8.6625, 12.2875, 9.2250, 7.6292, …
## $ Cabin <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "B45", NA…
## $ Embarked <chr> "Q", "S", "Q", "S", "S", "S", "Q", "S", "C", "S", "S", "S…
los datos se distribuyen entre la variables: Pclass : Un indicador de estatus socioeconómico (SES) primero = Alta segunda = Medio tercio = Baja
Age : La edad es fraccionada si es inferior a 1. Si se estima la edad, es en la forma de XX.5
sibsp : El conjunto de datos define las relaciones familiares de esta manera … Hermano = numero de hermano, hermana, hermanastro, hermanastra Cónyuge = numero de esposo, esposa (se ignoraron amantes y novios)
parch : El conjunto de datos define las relaciones familiares de esta manera … Padre = madre, padre Niño = hija, hijo, hijastra, hijastro Algunos niños viajaron solo con una niñera, por lo tanto, parch = 0 para ellos.
Conclusiones: existen tipos de variables no correspondientes con su valor; °debe hacerse una transformacion de ellos. (dbl %>% int) && la variable tarjet (dbl %>% fct). asi mismo, debe considerarse que los datos de la variable “Age” se sitetizaron en formato dbl, debe considerarse su transformacion a int con el fin de minimizar errores. °es posible que no exista una distribucion normal en las variables de entrada. °exiten valores NA, por esta razon deben omitirse.
2.3 analisis de los datos ## Including Plots
skim(df)
| Name | df |
| Number of rows | 891 |
| Number of columns | 12 |
| _______________________ | |
| Column type frequency: | |
| character | 5 |
| numeric | 7 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| Name | 0 | 1.00 | 12 | 82 | 0 | 891 | 0 |
| Sex | 0 | 1.00 | 4 | 6 | 0 | 2 | 0 |
| Ticket | 0 | 1.00 | 3 | 18 | 0 | 681 | 0 |
| Cabin | 687 | 0.23 | 1 | 15 | 0 | 147 | 0 |
| Embarked | 2 | 1.00 | 1 | 1 | 0 | 3 | 0 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| PassengerId | 0 | 1.0 | 446.00 | 257.35 | 1.00 | 223.50 | 446.00 | 668.5 | 891.00 | ▇▇▇▇▇ |
| Survived | 0 | 1.0 | 0.38 | 0.49 | 0.00 | 0.00 | 0.00 | 1.0 | 1.00 | ▇▁▁▁▅ |
| Pclass | 0 | 1.0 | 2.31 | 0.84 | 1.00 | 2.00 | 3.00 | 3.0 | 3.00 | ▃▁▃▁▇ |
| Age | 177 | 0.8 | 29.70 | 14.53 | 0.42 | 20.12 | 28.00 | 38.0 | 80.00 | ▂▇▅▂▁ |
| SibSp | 0 | 1.0 | 0.52 | 1.10 | 0.00 | 0.00 | 0.00 | 1.0 | 8.00 | ▇▁▁▁▁ |
| Parch | 0 | 1.0 | 0.38 | 0.81 | 0.00 | 0.00 | 0.00 | 0.0 | 6.00 | ▇▁▁▁▁ |
| Fare | 0 | 1.0 | 32.20 | 49.69 | 0.00 | 7.91 | 14.45 | 31.0 | 512.33 | ▇▁▁▁▁ |
skim(df_test)
| Name | df_test |
| Number of rows | 418 |
| Number of columns | 11 |
| _______________________ | |
| Column type frequency: | |
| character | 5 |
| numeric | 6 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| Name | 0 | 1.00 | 13 | 63 | 0 | 418 | 0 |
| Sex | 0 | 1.00 | 4 | 6 | 0 | 2 | 0 |
| Ticket | 0 | 1.00 | 3 | 18 | 0 | 363 | 0 |
| Cabin | 327 | 0.22 | 1 | 15 | 0 | 76 | 0 |
| Embarked | 0 | 1.00 | 1 | 1 | 0 | 3 | 0 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| PassengerId | 0 | 1.00 | 1100.50 | 120.81 | 892.00 | 996.25 | 1100.50 | 1204.75 | 1309.00 | ▇▇▇▇▇ |
| Pclass | 0 | 1.00 | 2.27 | 0.84 | 1.00 | 1.00 | 3.00 | 3.00 | 3.00 | ▃▁▃▁▇ |
| Age | 86 | 0.79 | 30.27 | 14.18 | 0.17 | 21.00 | 27.00 | 39.00 | 76.00 | ▂▇▃▂▁ |
| SibSp | 0 | 1.00 | 0.45 | 0.90 | 0.00 | 0.00 | 0.00 | 1.00 | 8.00 | ▇▁▁▁▁ |
| Parch | 0 | 1.00 | 0.39 | 0.98 | 0.00 | 0.00 | 0.00 | 0.00 | 9.00 | ▇▁▁▁▁ |
| Fare | 1 | 1.00 | 35.63 | 55.91 | 0.00 | 7.90 | 14.45 | 31.50 | 512.33 | ▇▁▁▁▁ |
#funcion para graficar, parametros de entrada dataframe y la variables a graficar
graf = function(db, Y) {
db %>%
ggplot(aes(x = 1, y = Y, fill = 1)) +
geom_boxplot() +
scale_fill_viridis(discrete = F, alpha = 1) +
geom_jitter(color = 'black', size = 0.5, alpha = 1) +
theme_ipsum() +
theme(
legend.position="none",
plot.title = element_text(size=11)
) +
ggtitle("Gráfico de caja en al variable Age") +
xlab("")
}
print(graf(df, df$Age))
## Warning: Removed 177 rows containing non-finite values (stat_boxplot).
## Warning: Removed 177 rows containing missing values (geom_point).
print(graf(df_test, df_test$Age))
## Warning: Removed 86 rows containing non-finite values (stat_boxplot).
## Warning: Removed 86 rows containing missing values (geom_point).
#numero de outliers
outAge <- boxplot.stats(df$Age)
outAgetest = boxplot.stats(df_test$Age)
Conclusiones: podemos observar la existencia de datos faltantes en la variable “Age” asi como tambien 6 valores atipicos. Apesar de la existencia de estos valores faltantes, podemos obviar su trato pues no son lo suficientemente grandes como para ocasionar errores en el modelado. por otro lado, existen 177 filas con valores faltantes (NA), esto si pueden ser transformados (eliminados) de manera que no puedan afectar de manera negativo.
3.-Transformacion de los datos: se deben tranformar las variables tipo, filtrar y eliminar los valores nulos en la variables “Age”. 3.1.- transformacion de los datos
df <- df %>%
mutate(PassengerId = as.integer(PassengerId),
Survived = as.factor(Survived),
Pclass = as.factor(Pclass),
Sex = as.factor(Sex),
Age = as.integer(Age),
SibSp = as.factor(SibSp),
Parch = as.factor(Parch),
Embarked = as.factor(Embarked)) %>%
filter(Age < 66)
df_test <- df_test %>%
mutate(PassengerId = as.integer(PassengerId),
Pclass = as.factor(Pclass),
Sex = as.factor(Sex),
Age = as.integer(Age),
SibSp = as.factor(SibSp),
Parch = as.factor(Parch),
Embarked = as.factor(Embarked)) %>%
filter(Age < 66)
#nueva vista a los datos
print(glimpse(df))
## Rows: 706
## Columns: 12
## $ PassengerId <int> 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1…
## $ Survived <fct> 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, …
## $ Pclass <fct> 3, 1, 3, 1, 3, 1, 3, 3, 2, 3, 1, 3, 3, 3, 2, 3, 3, 2, 2, …
## $ Name <chr> "Braund, Mr. Owen Harris", "Cumings, Mrs. John Bradley (F…
## $ Sex <fct> male, female, female, female, male, male, male, female, f…
## $ Age <int> 22, 38, 26, 35, 35, 54, 2, 27, 14, 4, 58, 20, 39, 14, 55,…
## $ SibSp <fct> 1, 1, 0, 1, 0, 0, 3, 0, 1, 1, 0, 0, 1, 0, 0, 4, 1, 0, 0, …
## $ Parch <fct> 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 0, 0, 5, 0, 0, 1, 0, 0, 0, …
## $ Ticket <chr> "A/5 21171", "PC 17599", "STON/O2. 3101282", "113803", "3…
## $ Fare <dbl> 7.2500, 71.2833, 7.9250, 53.1000, 8.0500, 51.8625, 21.075…
## $ Cabin <chr> NA, "C85", NA, "C123", NA, "E46", NA, NA, NA, "G6", "C103…
## $ Embarked <fct> S, C, S, S, S, S, S, S, C, S, S, S, S, S, S, Q, S, S, S, …
## # A tibble: 706 x 12
## PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin
## <int> <fct> <fct> <chr> <fct> <int> <fct> <fct> <chr> <dbl> <chr>
## 1 1 0 3 Brau… male 22 1 0 A/5 2… 7.25 <NA>
## 2 2 1 1 Cumi… fema… 38 1 0 PC 17… 71.3 C85
## 3 3 1 3 Heik… fema… 26 0 0 STON/… 7.92 <NA>
## 4 4 1 1 Futr… fema… 35 1 0 113803 53.1 C123
## 5 5 0 3 Alle… male 35 0 0 373450 8.05 <NA>
## 6 7 0 1 McCa… male 54 0 0 17463 51.9 E46
## 7 8 0 3 Pals… male 2 3 1 349909 21.1 <NA>
## 8 9 1 3 John… fema… 27 0 2 347742 11.1 <NA>
## 9 10 1 2 Nass… fema… 14 1 0 237736 30.1 <NA>
## 10 11 1 3 Sand… fema… 4 1 1 PP 95… 16.7 G6
## # … with 696 more rows, and 1 more variable: Embarked <fct>
print(glimpse(df_test))
## Rows: 330
## Columns: 11
## $ PassengerId <int> 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 903, 90…
## $ Pclass <fct> 3, 3, 2, 3, 3, 3, 3, 2, 3, 3, 1, 1, 2, 1, 2, 2, 3, 3, 3, …
## $ Name <chr> "Kelly, Mr. James", "Wilkes, Mrs. James (Ellen Needs)", "…
## $ Sex <fct> male, female, male, male, female, male, female, male, fem…
## $ Age <int> 34, 47, 62, 27, 22, 14, 30, 26, 18, 21, 46, 23, 63, 47, 2…
## $ SibSp <fct> 0, 1, 0, 0, 1, 0, 0, 1, 0, 2, 0, 1, 1, 1, 1, 0, 0, 1, 0, …
## $ Parch <fct> 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ Ticket <chr> "330911", "363272", "240276", "315154", "3101298", "7538"…
## $ Fare <dbl> 7.8292, 7.0000, 9.6875, 8.6625, 12.2875, 9.2250, 7.6292, …
## $ Cabin <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "B45", NA, "E…
## $ Embarked <fct> Q, S, Q, S, S, S, Q, S, C, S, S, S, S, S, C, Q, C, S, C, …
## # A tibble: 330 x 11
## PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
## <int> <fct> <chr> <fct> <int> <fct> <fct> <chr> <dbl> <chr> <fct>
## 1 892 3 Kelly… male 34 0 0 330911 7.83 <NA> Q
## 2 893 3 Wilke… fema… 47 1 0 363272 7 <NA> S
## 3 894 2 Myles… male 62 0 0 240276 9.69 <NA> Q
## 4 895 3 Wirz,… male 27 0 0 315154 8.66 <NA> S
## 5 896 3 Hirvo… fema… 22 1 1 31012… 12.3 <NA> S
## 6 897 3 Svens… male 14 0 0 7538 9.22 <NA> S
## 7 898 3 Conno… fema… 30 0 0 330972 7.63 <NA> Q
## 8 899 2 Caldw… male 26 1 1 248738 29 <NA> S
## 9 900 3 Abrah… fema… 18 0 0 2657 7.23 <NA> C
## 10 901 3 Davie… male 21 2 0 A/4 4… 24.2 <NA> S
## # … with 320 more rows
print(skim(df))
## ── Data Summary ────────────────────────
## Values
## Name df
## Number of rows 706
## Number of columns 12
## _______________________
## Column type frequency:
## character 3
## factor 6
## numeric 3
## ________________________
## Group variables None
##
## ── Variable type: character ────────────────────────────────────────────────────
## skim_variable n_missing complete_rate min max empty n_unique whitespace
## 1 Name 0 1 13 82 0 706 0
## 2 Ticket 0 1 3 18 0 535 0
## 3 Cabin 524 0.258 1 15 0 132 0
##
## ── Variable type: factor ───────────────────────────────────────────────────────
## skim_variable n_missing complete_rate ordered n_unique
## 1 Survived 0 1 FALSE 2
## 2 Pclass 0 1 FALSE 3
## 3 Sex 0 1 FALSE 2
## 4 SibSp 0 1 FALSE 6
## 5 Parch 0 1 FALSE 7
## 6 Embarked 2 0.997 FALSE 3
## top_counts
## 1 0: 417, 1: 289
## 2 3: 353, 1: 182, 2: 171
## 3 mal: 445, fem: 261
## 4 0: 464, 1: 182, 2: 25, 4: 18
## 5 0: 514, 1: 109, 2: 68, 3: 5
## 6 S: 549, C: 128, Q: 27
##
## ── Variable type: numeric ──────────────────────────────────────────────────────
## skim_variable n_missing complete_rate mean sd p0 p25 p50 p75
## 1 PassengerId 0 1 449. 259. 1 224. 444. 678.
## 2 Age 0 1 29.2 13.9 0 20 28 38
## 3 Fare 0 1 34.8 53.2 0 8.05 15.7 33
## p100 hist
## 1 891 ▇▇▇▇▇
## 2 65 ▂▇▇▃▂
## 3 512. ▇▁▁▁▁
print(skim(df_test))
## ── Data Summary ────────────────────────
## Values
## Name df_test
## Number of rows 330
## Number of columns 11
## _______________________
## Column type frequency:
## character 3
## factor 5
## numeric 3
## ________________________
## Group variables None
##
## ── Variable type: character ────────────────────────────────────────────────────
## skim_variable n_missing complete_rate min max empty n_unique whitespace
## 1 Name 0 1 13 63 0 330 0
## 2 Ticket 0 1 3 18 0 284 0
## 3 Cabin 245 0.258 1 15 0 71 0
##
## ── Variable type: factor ───────────────────────────────────────────────────────
## skim_variable n_missing complete_rate ordered n_unique
## 1 Pclass 0 1 FALSE 3
## 2 Sex 0 1 FALSE 2
## 3 SibSp 0 1 FALSE 7
## 4 Parch 0 1 FALSE 7
## 5 Embarked 0 1 FALSE 3
## top_counts
## 1 3: 146, 1: 96, 2: 88
## 2 mal: 204, fem: 126
## 3 0: 214, 1: 95, 2: 11, 3: 4
## 4 0: 245, 1: 50, 2: 29, 3: 3
## 5 S: 226, C: 82, Q: 22
##
## ── Variable type: numeric ──────────────────────────────────────────────────────
## skim_variable n_missing complete_rate mean sd p0 p25 p50 p75
## 1 PassengerId 0 1 1101. 123. 892 995. 1100. 1211.
## 2 Age 0 1 30.0 13.9 0 21 27 39
## 3 Fare 1 0.997 40.3 60.6 0 8.05 15.9 39.4
## p100 hist
## 1 1307 ▇▇▇▇▇
## 2 64 ▂▇▇▃▂
## 3 512. ▇▁▁▁▁
#eliminacion de los datos en "Age"
df[["Age"]][is.na(df[["Age"]])] <- 0
df_test[["Age"]][is.na(df_test[["Age"]])] = 0
print(graf(df, df$Age))
print(graf(df_test, df_test$Age))
conclusiones: se aprecian un total de 891 filas antes del filtrado y eliminacion de las variables. luego de cumplirse dichos objetivos se observan 706 filas en el dataset.
3.2 analisis exploratorio de variables (EDA)
eda = function(db, tipe) {
db %>%
select_if(tipe) %>%
gather() %>%
ggplot(aes(value)) +
geom_density() +
facet_wrap(~key, scales = 'free') +
theme(axis.text = element_text(size = 6))
}
print(eda(df, is.integer))
print(eda(df_test, is.integer))
#analisis de correlacion
df %>%
select_if(is.integer) %>%
cor() %>%
round(digits = 2)
## PassengerId
## PassengerId 1
df_test %>%
select_if(is.integer) %>%
cor() %>%
round (digits = 2)
## PassengerId
## PassengerId 1
table(df$Survived)
##
## 0 1
## 417 289
Conclusiones: Como se espera, luego de la transformacion de los datos, la varibale Age contiene una distribucion normal; en ambos conjunto de datos, por lo que podemos continuar con el modelado de Aprendizaje automatico
4.- modelizacion Tecnica a usar: “regresion logistica”
DTfit <- glm(df$Survived ~ df$Age +
df$Pclass + df$Sex +
df$Parch + df$SibSp,
family = binomial(link = "logit"))
summary(DTfit)
##
## Call:
## glm(formula = df$Survived ~ df$Age + df$Pclass + df$Sex + df$Parch +
## df$SibSp, family = binomial(link = "logit"))
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.9019 -0.6465 -0.3866 0.6223 2.4508
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 4.003e+00 4.883e-01 8.200 2.41e-16 ***
## df$Age -4.167e-02 9.027e-03 -4.617 3.90e-06 ***
## df$Pclass2 -1.395e+00 2.902e-01 -4.807 1.53e-06 ***
## df$Pclass3 -2.473e+00 2.970e-01 -8.327 < 2e-16 ***
## df$Sexmale -2.607e+00 2.212e-01 -11.787 < 2e-16 ***
## df$Parch1 3.933e-01 2.968e-01 1.325 0.18510
## df$Parch2 3.068e-01 4.063e-01 0.755 0.45019
## df$Parch3 5.466e-01 1.037e+00 0.527 0.59812
## df$Parch4 -1.578e+01 1.047e+03 -0.015 0.98797
## df$Parch5 -1.009e+00 1.173e+00 -0.860 0.38968
## df$Parch6 -1.627e+01 2.400e+03 -0.007 0.99459
## df$SibSp1 -3.122e-02 2.417e-01 -0.129 0.89720
## df$SibSp2 -6.995e-01 5.738e-01 -1.219 0.22284
## df$SibSp3 -2.061e+00 7.705e-01 -2.675 0.00747 **
## df$SibSp4 -1.925e+00 7.879e-01 -2.444 0.01454 *
## df$SibSp5 -1.625e+01 9.636e+02 -0.017 0.98654
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 955.39 on 705 degrees of freedom
## Residual deviance: 617.69 on 690 degrees of freedom
## AIC: 649.69
##
## Number of Fisher Scoring iterations: 15
conclusiones: no hay valor de significancia con respecto a la variable Parch por lo tanto, se eliminara del modelo.
4.1. aplicacion del modelo a los nuevos datos
DTfit = glm(df$Survived ~ df$Age + df$Pclass +
df$Sex, family = binomial(link = "logit"))
summary(DTfit)
##
## Call:
## glm(formula = df$Survived ~ df$Age + df$Pclass + df$Sex, family = binomial(link = "logit"))
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.7338 -0.6792 -0.3944 0.6533 2.4698
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 3.787587 0.406591 9.315 < 2e-16 ***
## df$Age -0.037459 0.007885 -4.751 2.03e-06 ***
## df$Pclass2 -1.302686 0.279293 -4.664 3.10e-06 ***
## df$Pclass3 -2.580385 0.282561 -9.132 < 2e-16 ***
## df$Sexmale -2.523074 0.207843 -12.139 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 955.39 on 705 degrees of freedom
## Residual deviance: 641.73 on 701 degrees of freedom
## AIC: 651.73
##
## Number of Fisher Scoring iterations: 5
4.1. aplicacion del modelo a los nuevos datos
newtest = data.frame(df_test$Pclass, df_test$Sex, df_test$Age)
df$Scoring = predict(DTfit, newtest, type = "response")
## Warning: 'newdata' had 330 rows but variables found have 706 rows
df$prediction = ifelse(df$Scoring > 0.65, 1, 0)
5.- evaluacion del modelo
evaluacion = table(df$prediction, df$Survived) #FIXME
print(confusionMatrix(evaluacion))
## Confusion Matrix and Statistics
##
##
## 0 1
## 0 395 124
## 1 22 165
##
## Accuracy : 0.7932
## 95% CI : (0.7614, 0.8225)
## No Information Rate : 0.5907
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.5479
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Sensitivity : 0.9472
## Specificity : 0.5709
## Pos Pred Value : 0.7611
## Neg Pred Value : 0.8824
## Prevalence : 0.5907
## Detection Rate : 0.5595
## Detection Prevalence : 0.7351
## Balanced Accuracy : 0.7591
##
## 'Positive' Class : 0
##
6.- Despliegue de visualizacion
mod = data.frame(df$prediction, df$Survived)
mod%>%
ggplot(aes(x=df.prediction, fill=df.Survived)) +
geom_histogram( color="#e9ecef", alpha=0.6, position = 'identity') +
scale_fill_manual(values=c("#69b3a2", "#404080")) +
theme_ipsum() +
labs(fill="") + stat_bin(bins = 100) +
ggtitle("histograma de evaluacion predictiva")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Conclusiones: la visualizacion muestra, la cantidad de personas que sobrevivieron segun su Age (edad), Sex (sexo), Pclass (clase social) durante el undimiento del titanic el modelo predice un total de 395 personas que no sobreviviran a la catastrofe (con respecto al datatest) y 165 personas que si sobreviviran. segun sus atributos sobre las variables evaluadas.
Conclusion final: Al tomar los valores de las variables Pclass (clase social), Age (edad), Sex (sexo), el modelo encuentra una linealidad con la variables Survived (supervivencia); debido a esto, usamos entonces estas variables sobre el conjunto de datos train y test. el P-value correspondientes a estas variables independiente son significativas para el modelo con respecto a la variables target (dependiente. asi mismo, la evaluacion toma estas mismas caracteristicas para adaptar el modelo con la misma cantidad de variables. La probabilidad P del umbral que mejor proporciona la repdiccion es sobre un 0.65 para que la misma de valor 1 si es mayor a esta y 0 si es menor. Por otro lado, la matriz de confusion da como resultado un “ac: 79.3%” de acierto, en contraste el modelo conserva un error del 20.7% sobre el modelo. Lo que se estima como un buen “modelo de clasificacion por regresion”.