Cargar Librerias

library(tree)
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(caret)
## Loading required package: ggplot2
## Loading required package: lattice
medicina <- read.csv("/Users/nathaliacarcoba/Downloads/drug200.csv", stringsAsFactors = T)

# Selección muestra entrenamiento de un 70%
set.seed(123)
train=sample(seq(length(medicina$Drug)),length(medicina$Drug)*0.7,replace=FALSE)

# Renombrar columnas para mejor visualizacion
medicina <- medicina %>% rename(BloodPressure = BP,
                               SodiumPotassium = Na_to_K)
head(medicina)
##   Age Sex BloodPressure Cholesterol SodiumPotassium  Drug
## 1  23   F          HIGH        HIGH          25.355 drugY
## 2  47   M           LOW        HIGH          13.093 drugC
## 3  47   M           LOW        HIGH          10.114 drugC
## 4  28   F        NORMAL        HIGH           7.798 drugX
## 5  61   F           LOW        HIGH          18.043 drugY
## 6  22   F        NORMAL        HIGH           8.607 drugX
head(medicina)
##   Age Sex BloodPressure Cholesterol SodiumPotassium  Drug
## 1  23   F          HIGH        HIGH          25.355 drugY
## 2  47   M           LOW        HIGH          13.093 drugC
## 3  47   M           LOW        HIGH          10.114 drugC
## 4  28   F        NORMAL        HIGH           7.798 drugX
## 5  61   F           LOW        HIGH          18.043 drugY
## 6  22   F        NORMAL        HIGH           8.607 drugX
tail(medicina)
##     Age Sex BloodPressure Cholesterol SodiumPotassium  Drug
## 195  46   F          HIGH        HIGH          34.686 drugY
## 196  56   F           LOW        HIGH          11.567 drugC
## 197  16   M           LOW        HIGH          12.006 drugC
## 198  52   M        NORMAL        HIGH           9.894 drugX
## 199  23   M        NORMAL      NORMAL          14.020 drugX
## 200  40   F           LOW      NORMAL          11.349 drugX
unique(medicina$Drug)
## [1] drugY drugC drugX drugA drugB
## Levels: drugA drugB drugC drugX drugY
length(medicina$Drug)
## [1] 200

Creación del árbol

drug.tree = tree(Drug~.,data = medicina,subset=train)
summary(drug.tree)
## 
## Classification tree:
## tree(formula = Drug ~ ., data = medicina, subset = train)
## Variables actually used in tree construction:
## [1] "SodiumPotassium" "BloodPressure"   "Age"             "Cholesterol"    
## Number of terminal nodes:  6 
## Residual mean deviance:  0 = 0 / 134 
## Misclassification error rate: 0 = 0 / 140
plot(drug.tree)
text(drug.tree,pretty=0)

Ver los valores del árbol

drug.tree
## node), split, n, deviance, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 140 364.90 drugY ( 0.09286 0.09286 0.07143 0.22143 0.52143 )  
##    2) SodiumPotassium < 14.839 67 171.10 drugX ( 0.19403 0.19403 0.14925 0.46269 0.00000 )  
##      4) BloodPressure: HIGH 26  36.04 drugA ( 0.50000 0.50000 0.00000 0.00000 0.00000 )  
##        8) Age < 50.5 13   0.00 drugA ( 1.00000 0.00000 0.00000 0.00000 0.00000 ) *
##        9) Age > 50.5 13   0.00 drugB ( 0.00000 1.00000 0.00000 0.00000 0.00000 ) *
##      5) BloodPressure: LOW,NORMAL 41  45.55 drugX ( 0.00000 0.00000 0.24390 0.75610 0.00000 )  
##       10) BloodPressure: LOW 20  27.73 drugC ( 0.00000 0.00000 0.50000 0.50000 0.00000 )  
##         20) Cholesterol: HIGH 10   0.00 drugC ( 0.00000 0.00000 1.00000 0.00000 0.00000 ) *
##         21) Cholesterol: NORMAL 10   0.00 drugX ( 0.00000 0.00000 0.00000 1.00000 0.00000 ) *
##       11) BloodPressure: NORMAL 21   0.00 drugX ( 0.00000 0.00000 0.00000 1.00000 0.00000 ) *
##    3) SodiumPotassium > 14.839 73   0.00 drugY ( 0.00000 0.00000 0.00000 0.00000 1.00000 ) *

Predecir el otro 30% de los datos

test=setdiff(seq(nrow(medicina)),train)
tree.pred=predict(drug.tree,newdata = medicina[test, ],type="class")
summary(tree.pred)
## drugA drugB drugC drugX drugY 
##    10     3     6    23    18

Medir exactitud

results <- data.frame(
  Actual = medicina$Drug[test],
  Predicted = tree.pred
)

exactitud <- sum(results$Actual == results$Predicted) / nrow(results)
print(paste("Exactitud:", exactitud))
## [1] "Exactitud: 1"