library(car) #> Loading required package: carData library(readr)
library(readr)
partners <- read_delim("https://themys.sid.uncu.edu.ar/rpalma/R-cran/SCM_Perform/scm_perform.csv",
delim = ";", escape_double = FALSE, col_types = cols(Performance = col_factor(levels = c("good",
"fair", "poor"))), locale = locale(decimal_mark = ",",
grouping_mark = "."), trim_ws = TRUE)
summary(partners)
## Inventory.Turnover Lead.Time Perfect.Order Trnasport.Km/Ton
## 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
## Performance
## good:50
## fair:50
## poor:50
##
##
##
library(scatterPlotMatrix)
scatterPlotMatrix(partners)
Esta técnica utiliza un set de datos representativos de una situaci’on y utilizando recursivamente el teoréma de Bayes puede armar un pronosticador o clasificador de datos. Es una t’ecnica parecida a la de clustering, pero m’as refinada, pues no se basa en reglas sino en parendizaje del set de datos usado como entrenamento. En el paquete party existen dos funciones ctree que se utiliza para entrenar y predict que se usa para pronosticar o generar la regla de decici’on que debemos usar.
library(party)
## Loading required package: grid
## Loading required package: mvtnorm
## Loading required package: modeltools
## Loading required package: stats4
## Loading required package: strucchange
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
attach(partners)
str(partners)
## spc_tbl_ [150 × 5] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ Inventory.Turnover: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Lead.Time : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Perfect.Order : num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Trnasport.Km/Ton : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Performance : Factor w/ 3 levels "good","fair",..: 1 1 1 1 1 1 1 1 1 1 ...
## - attr(*, "spec")=
## .. cols(
## .. Inventory.Turnover = col_double(),
## .. Lead.Time = col_double(),
## .. Perfect.Order = col_double(),
## .. `Trnasport.Km/Ton` = col_double(),
## .. Performance = col_factor(levels = c("good", "fair", "poor"), ordered = FALSE, include_na = FALSE)
## .. )
## - attr(*, "problems")=<externalptr>
ind <- sample(2, nrow(partners), replace=TRUE, prob=c(0.7, 0.3))
# toma una muestra
ind
## [1] 2 1 1 2 1 2 2 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
## [38] 1 1 1 1 1 1 2 1 1 2 1 2 1 1 2 1 1 2 2 1 1 2 2 1 2 2 1 2 1 2 2 1 2 1 2 1 1
## [75] 1 1 2 2 2 2 2 1 1 2 1 1 1 2 1 2 1 2 2 2 2 2 1 2 1 1 1 1 1 1 2 1 2 2 1 1 1
## [112] 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 2 1 1 1 1 1
## [149] 1 1
# nos imprime la muestra tomada.
trainData <- partners [ind==1,]
# genero un set de entrenamiento
testData <- partners [ind==2,]
# genero un set de datos de prueba
myFormula <- Performance ~ Inventory.Turnover + Lead.Time + `Trnasport.Km/Ton` + Perfect.Order
transit_ctree <- ctree(myFormula, data=trainData)
# creo el motor de entrenamiento
# Verificar las prediciones
table(predict(transit_ctree), trainData$Performance)
##
## good fair poor
## good 40 0 0
## fair 0 23 3
## poor 0 1 39
print(transit_ctree)
##
## Conditional inference tree with 4 terminal nodes
##
## Response: Performance
## Inputs: Inventory.Turnover, Lead.Time, Trnasport.Km/Ton, Perfect.Order
## Number of observations: 106
##
## 1) Perfect.Order <= 1.9; criterion = 1, statistic = 99.592
## 2)* weights = 40
## 1) Perfect.Order > 1.9
## 3) Trnasport.Km/Ton <= 1.6; criterion = 1, statistic = 41.909
## 4) Perfect.Order <= 4.5; criterion = 0.993, statistic = 9.723
## 5)* weights = 16
## 4) Perfect.Order > 4.5
## 6)* weights = 10
## 3) Trnasport.Km/Ton > 1.6
## 7)* weights = 40
Muestra el arbol entrenado vs el resto de la muestra
Prediccion con el dataset de prueba
table(predict(transit_ctree), trainData$Performance)
##
## good fair poor
## good 40 0 0
## fair 0 23 3
## poor 0 1 39
Impresión del arbol de decision
plot(transit_ctree, las=2)