# install.packages("rpart")
library(rpart)
# install.packages("rpart.plot")
library(rpart.plot)
# Cargar la base de datos
library(readr)
titanic <- read_csv("~/Conexión de interfaces/Conexión de interfaces/titanic.csv")
## Rows: 1310 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (7): name, sex, ticket, cabin, embarked, boat, home.dest
## dbl (7): pclass, survived, age, sibsp, parch, fare, body
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
summary(titanic)
## pclass survived name sex
## Min. :1.000 Min. :0.000 Length:1310 Length:1310
## 1st Qu.:2.000 1st Qu.:0.000 Class :character Class :character
## Median :3.000 Median :0.000 Mode :character Mode :character
## Mean :2.295 Mean :0.382
## 3rd Qu.:3.000 3rd Qu.:1.000
## Max. :3.000 Max. :1.000
## NA's :1 NA's :1
## age sibsp parch ticket
## Min. : 0.1667 Min. :0.0000 Min. :0.000 Length:1310
## 1st Qu.:21.0000 1st Qu.:0.0000 1st Qu.:0.000 Class :character
## Median :28.0000 Median :0.0000 Median :0.000 Mode :character
## Mean :29.8811 Mean :0.4989 Mean :0.385
## 3rd Qu.:39.0000 3rd Qu.:1.0000 3rd Qu.:0.000
## Max. :80.0000 Max. :8.0000 Max. :9.000
## NA's :264 NA's :1 NA's :1
## fare cabin embarked boat
## Min. : 0.000 Length:1310 Length:1310 Length:1310
## 1st Qu.: 7.896 Class :character Class :character Class :character
## Median : 14.454 Mode :character Mode :character Mode :character
## Mean : 33.295
## 3rd Qu.: 31.275
## Max. :512.329
## NA's :2
## body home.dest
## Min. : 1.0 Length:1310
## 1st Qu.: 72.0 Class :character
## Median :155.0 Mode :character
## Mean :160.8
## 3rd Qu.:256.0
## Max. :328.0
## NA's :1189
str(titanic)
## spc_tbl_ [1,310 × 14] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ pclass : num [1:1310] 1 1 1 1 1 1 1 1 1 1 ...
## $ survived : num [1:1310] 1 1 0 0 0 1 1 0 1 0 ...
## $ name : chr [1:1310] "Allen, Miss. Elisabeth Walton" "Allison, Master. Hudson Trevor" "Allison, Miss. Helen Loraine" "Allison, Mr. Hudson Joshua Creighton" ...
## $ sex : chr [1:1310] "female" "male" "female" "male" ...
## $ age : num [1:1310] 29 0.917 2 30 25 ...
## $ sibsp : num [1:1310] 0 1 1 1 1 0 1 0 2 0 ...
## $ parch : num [1:1310] 0 2 2 2 2 0 0 0 0 0 ...
## $ ticket : chr [1:1310] "24160" "113781" "113781" "113781" ...
## $ fare : num [1:1310] 211 152 152 152 152 ...
## $ cabin : chr [1:1310] "B5" "C22 C26" "C22 C26" "C22 C26" ...
## $ embarked : chr [1:1310] "S" "S" "S" "S" ...
## $ boat : chr [1:1310] "2" "11" NA NA ...
## $ body : num [1:1310] NA NA NA 135 NA NA NA NA NA 22 ...
## $ home.dest: chr [1:1310] "St Louis, MO" "Montreal, PQ / Chesterville, ON" "Montreal, PQ / Chesterville, ON" "Montreal, PQ / Chesterville, ON" ...
## - attr(*, "spec")=
## .. cols(
## .. pclass = col_double(),
## .. survived = 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(),
## .. boat = col_character(),
## .. body = col_double(),
## .. home.dest = col_character()
## .. )
## - attr(*, "problems")=<externalptr>
head(titanic)
## # A tibble: 6 × 14
## pclass survived name sex age sibsp parch ticket fare cabin embarked
## <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <chr> <chr>
## 1 1 1 Allen, M… fema… 29 0 0 24160 211. B5 S
## 2 1 1 Allison,… male 0.917 1 2 113781 152. C22 … S
## 3 1 0 Allison,… fema… 2 1 2 113781 152. C22 … S
## 4 1 0 Allison,… male 30 1 2 113781 152. C22 … S
## 5 1 0 Allison,… fema… 25 1 2 113781 152. C22 … S
## 6 1 1 Anderson… male 48 0 0 19952 26.6 E12 S
## # ℹ 3 more variables: boat <chr>, body <dbl>, home.dest <chr>
titanic <- titanic[,c("pclass","age","sex","survived")]
titanic$survived <- as.factor(titanic$survived)
titanic$pclass <- as.factor(titanic$pclass)
titanic$sex <- as.factor(titanic$sex)
str(titanic)
## tibble [1,310 × 4] (S3: tbl_df/tbl/data.frame)
## $ pclass : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
## $ age : num [1:1310] 29 0.917 2 30 25 ...
## $ sex : Factor w/ 2 levels "female","male": 1 2 1 2 1 2 1 2 1 2 ...
## $ survived: Factor w/ 2 levels "0","1": 2 2 1 1 1 2 2 1 2 1 ...
arbol_titanic <- rpart(survived~., data=titanic)
rpart.plot(arbol_titanic)
prp(arbol_titanic, extra=7, prefix="fracción\n")
En conclusión, las más altas probabilidades de sobrevivir en el naufragio del Titanic son:
100%: Si eres niño varón menor de 9.5 años de 1° o 2° clase.
73%: Si eres mujer.
Y, por el contrario, las más bajas probabilidades de sobrevivir son:
17%: Si eres hombre mayor a 9.5 años.
38%: Si eres niño varón menor de 9.5 años de 3° clase.