library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## āœ” ggplot2 3.3.5     āœ” purrr   0.3.4
## āœ” tibble  3.1.8     āœ” stringr 1.4.0
## āœ” tidyr   1.2.0     āœ” forcats 0.5.1
## āœ” readr   2.1.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## āœ– dplyr::filter() masks stats::filter()
## āœ– dplyr::lag()    masks stats::lag()
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(ggplot2)

getwd()

Importar Base de Datos

#file.choose()
Titanic <- read_csv("/Users/danieltrevino/Documents/Quinto Semestre TEC/Bootcamp de Programación/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.

Entender Base de Datos

resumen <- summary(Titanic)
resumen
##      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

Filtras base de datos

Filtro <- Titanic[,c ("pclass", "age", "sex", "survived")]

Filtro$survived <- as.factor(ifelse(Filtro$survived == 0, "Murio", "Sobrevivio"))
Filtro$pclass <-as.factor(Filtro$pclass)
Filtro$sex <-as.factor(Filtro$sex)
str(Filtro)
## 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 "Murio","Sobrevivio": 2 2 1 1 1 2 2 1 2 1 ...

Eliminar NA

sum(is.na(Filtro))
## [1] 267
Filtro <- na.omit(Filtro)

Crear arbol de decision

#install.packages("rpart")
library(rpart)
arbol <- rpart(formula = survived  ~., data = Filtro)
arbol
## n= 1046 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 1046 427 Murio (0.59177820 0.40822180)  
##    2) sex=male 658 135 Murio (0.79483283 0.20516717)  
##      4) age>=9.5 615 110 Murio (0.82113821 0.17886179) *
##      5) age< 9.5 43  18 Sobrevivio (0.41860465 0.58139535)  
##       10) pclass=3 29  11 Murio (0.62068966 0.37931034) *
##       11) pclass=1,2 14   0 Sobrevivio (0.00000000 1.00000000) *
##    3) sex=female 388  96 Sobrevivio (0.24742268 0.75257732)  
##      6) pclass=3 152  72 Murio (0.52631579 0.47368421)  
##       12) age>=1.5 145  66 Murio (0.54482759 0.45517241) *
##       13) age< 1.5 7   1 Sobrevivio (0.14285714 0.85714286) *
##      7) pclass=1,2 236  16 Sobrevivio (0.06779661 0.93220339) *
#install.packages("rpart.plot")
library(rpart.plot)
rpart.plot(arbol)

prp(arbol, extra = 7, prefix = "fracción/n")

Conclusiones

En esta actividad, observamos como crear una grafica de arbol a partir de una base de datos. Esta grafica nos permite entender la base de datos de una manera mas visual y facil de entender o explicar. En este caso, observamos la propabilidad que tuvieron los pasajeros del Titanic de acuerdo a su edad, genero y el tipo de clase en el que viajaban.