presentación de powerpoint - umhumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... ·...

26
Sesión 014b. “Interpretando un Árbol de Decisión” 1480-Técnicas Estadísticas en Investigación de Mercados Grado en Estadística empresarial Curso 2012-13 – Segundo semestre Profesor: Xavier Barber i Vallés Departamento: Estadística, Matemáticas e Informática

Upload: others

Post on 27-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Sesión 014b. “Interpretando un Árbol de Decisión” 1480-Técnicas Estadísticas en Investigación de Mercados Grado en Estadística empresarial Curso 2012-13 – Segundo semestre Profesor: Xavier Barber i Vallés Departamento: Estadística, Matemáticas e Informática

Page 2: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

library(rpart) ÁRBOLES DE DECISIÓN

Page 3: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Ejemplo: “CCS” library(BCA) ########################################################################

#### http://cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf

###########################################################################

library(BCA) ### libraría donde están los datos

library(rpart) ### libraría para los Árboles de Decisión

# recuperamos los datos de la sesión anterior (paquete BCA --> CCS)

data(CCS, package="BCA")

###################################################################

Page 4: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Generando el Árbol tree1CCS<-rpart(MonthGive ~ Age20t29 + Age70pls + AveDonAmt + AveIncEA +DonPerYear + EngPrmLang + FinUnivP + LastDonAmt + Region + YearsGive,

data=CCS[CCS$Sample=="Estimation",], method="class") print(tree1CCS)

n= 544 node), split, n, loss, yval, (yprob) * denotes terminal node 1) root 544 264 No (0.5147059 0.4852941)

2) AveDonAmt< 11.45833 166 28 No (0.8313253 0.1686747)* (nodo terminal) 3) AveDonAmt>=11.45833 378 142 Yes (0.3756614 0.6243386) 6) DonPerYear< 0.5227273 206 93 Yes (0.4514563 0.5485437) 12) DonPerYear>=0.08712121 192 93 Yes (0.4843750 0.5156250) 24) Age20t29< 0.110101 98 41 No (0.5816327 0.4183673)

Árboles de Decisión “class Árboles de Regresión= “anova”

Nodo, corte, cuantos me quedan, los que pierdo, el Valor con prob. MÁS ALTA; probabilidades de Y

Page 5: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Generando el Árbol > print(tree1CCSb)

n= 544

node), split, n, loss, yval, (yprob) * denotes terminal node

1) root 544 264 No (0.5147059 0.4852941)

2) AveDonAmt< 11.45833 166 28 No (0.8313253 0.1686747) * 3) AveDonAmt>=11.45833 378 142 Yes (0.3756614 0.6243386)

6) DonPerYear< 0.5227273 206 93 Yes (0.4514563 0.5485437)

12) DonPerYear>=0.08712121 192 93 Yes (0.4843750 0.5156250)

24) Age20t29< 0.110101 98 41 No (0.5816327 0.4183673)

48) EngPrmLang< 0.9751829 59 18 No (0.6949153 0.3050847) * 49) EngPrmLang>=0.9751829 39 16 Yes (0.4102564 0.5897436) * 25) Age20t29>=0.110101 94 36 Yes (0.3829787 0.6170213)

50) LastDonAmt< 17.5 13 3 No (0.7692308 0.2307692) * 51) LastDonAmt>=17.5 81 26 Yes (0.3209877 0.6790123) 102) YearsGive< 2.5 15 5 No (0.6666667 0.3333333) * 103) YearsGive>=2.5 66 16 Yes (0.2424242 0.7575758) * 13) DonPerYear< 0.08712121 14 0 Yes (0.0000000 1.0000000) * 7) DonPerYear>=0.5227273 172 49 Yes (0.2848837 0.7151163) *

Page 6: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

El Árbol |AveDonAmt< 11.46

DonPerYear< 0.5227DonPerYear>=0.08712

Age20t29< 0.1101

EngPrmLang< 0.9752AveDonAmt< 62.92

LastDonAmt< 17.5YearsGive< 2.5

AveIncEA>AveIncEA< 6.955e

LastDonAmt< 27.5

No

No Yes Yes No No Yes

Yes

No YesYes

Yes

Page 7: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

¿es el mejor árbol posible? Para estudiar si necesitamos quedarnos con todo el árbol o sólo con una parte de él, recurriremos a la Cross-Validation como medida de “eficiencia” de numero de ramas. > printcp(tree1CCS) Root node error: 264/544 = 0.48529 CP nsplit rel error xerror xstd [CP=parámetro de Complejidad xerror=Cross-Validation] 1 0.356061 0 1.00000 1.00000 0.044155 2 0.020202 1 0.64394 0.64773 0.041016 3 0.018939 6 0.53030 0.65909 0.041207 4 0.017677 7 0.51136 0.63258 0.040750 5 0.011364 10 0.45833 0.63636 0.040817 6 0.010000 11 0.44697 0.68182 0.041570

Page 8: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

¿es el mejor árbol posible?

Page 9: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Podando el árbol ################################################### # CP nsplit rel error xerror xstd #1 0.356061 0 1.00000 1.00000 0.044155 #2 0.020202 1 0.64394 0.64773 0.041016 #3 0.018939 6 0.53030 0.65909 0.041207 #4 0.017677 7 0.51136 0.63258 0.040750 #5 0.011364 10 0.45833 0.63636 0.040817 #6 0.010000 11 0.44697 0.68182 0.041570 ################################################## tree1CCSb <-prune(tree1CCS, cp=0.017677 ) print(tree1CCSb) par(mar = rep(0.1, 4)) plot(tree1CCSb) text(tree1CCSb, cex=0.8)

Page 10: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Podando el árbol n= 544 node), split, n, loss, yval, (yprob) * denotes terminal node 1) root 544 264 No (0.5147059 0.4852941) 2) AveDonAmt< 11.45833 166 28 No (0.8313253 0.1686747) * 3) AveDonAmt>=11.45833 378 142 Yes (0.3756614 0.6243386) 6) DonPerYear< 0.5227273 206 93 Yes (0.4514563 0.5485437) 12) DonPerYear>=0.08712121 192 93 Yes (0.4843750 0.5156250) 24) Age20t29< 0.110101 98 41 No (0.5816327 0.4183673) 48) EngPrmLang< 0.9751829 59 18 No (0.6949153 0.3050847) * 49) EngPrmLang>=0.9751829 39 16 Yes (0.4102564 0.5897436) * 25) Age20t29>=0.110101 94 36 Yes (0.3829787 0.6170213) 50) LastDonAmt< 17.5 13 3 No (0.7692308 0.2307692) * 51) LastDonAmt>=17.5 81 26 Yes (0.3209877 0.6790123) 102) YearsGive< 2.5 15 5 No (0.6666667 0.3333333) * 103) YearsGive>=2.5 66 16 Yes (0.2424242 0.7575758) * 13) DonPerYear< 0.08712121 14 0 Yes (0.0000000 1.0000000) * 7) DonPerYear>=0.5227273 172 49 Yes (0.2848837 0.7151163) *

Page 11: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Podando el árbol |AveDonAmt< 11.46

DonPerYear< 0.52DonPerYear>=0.08712

Age20t29< 0.1101

EngPrmLang< 0.9752 LastDonAmt< 17.5YearsGive< 2.5

No

No Yes No No Yes

YesYes

Page 12: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

El Árbol ORIGINAL |AveDonAmt< 11.46

DonPerYear< 0.5227DonPerYear>=0.08712

Age20t29< 0.1101

EngPrmLang< 0.9752AveDonAmt< 62.92

LastDonAmt< 17.5YearsGive< 2.5

AveIncEA>AveIncEA< 6.955e

LastDonAmt< 27.5

No

No Yes Yes No No Yes

Yes

No YesYes

Yes

Page 13: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

El árbol PODADO |AveDonAmt< 11.46

DonPerYear< 0.52DonPerYear>=0.08712

Age20t29< 0.1101

EngPrmLang< 0.9752 LastDonAmt< 17.5YearsGive< 2.5

No

No Yes No No Yes

YesYes

Page 14: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

library(partykit) Una forma más gráfica …. Dentro de lo posible

Page 15: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

De rpart a partykit >library(partykit) >tree_party<-as.party(tree1CCS) >print(tree_party, header=FALSE) [1] root | [2] AveDonAmt < 11.45833: No (n = 166, err = 16.9%) | [3] AveDonAmt >= 11.45833 | | [4] DonPerYear < 0.52273 | | | [5] DonPerYear >= 0.08712 | | | | [6] Age20t29 < 0.1101 | | | | | [7] EngPrmLang < 0.97518 | | | | | | [8] AveDonAmt < 62.91667: No (n = 48, err = 22.9%) | | | | | | [9] AveDonAmt >= 62.91667: Yes (n = 11, err = 36.4%) | | | | | [10] EngPrmLang >= 0.97518: Yes (n = 39, err = 41.0%) | | | | [11] Age20t29 >= 0.1101 | | | | | [12] LastDonAmt < 17.5: No (n = 13, err = 23.1%) | | | | | [13] LastDonAmt >= 17.5 | | | | | | [14] YearsGive < 2.5: No (n = 15, err = 33.3%) | | | | | | [15] YearsGive >= 2.5: Yes (n = 66, err = 24.2%) | | | [16] DonPerYear < 0.08712: Yes (n = 14, err = 0.0%) | | [17] DonPerYear >= 0.52273 | | | [18] AveIncEA >= 54376.5 | | | | [19] AveIncEA < 69546 | | | | | [20] LastDonAmt < 27.5: No (n = 24, err = 20.8%) | | | | | [21] LastDonAmt >= 27.5: Yes (n = 18, err = 27.8%) | | | | [22] AveIncEA >= 69546: Yes (n = 30, err = 16.7%) | | | [23] AveIncEA < 54376.5: Yes (n = 100, err = 20.0%)

Number of inner nodes: 11 Number of terminal nodes: 12

Page 16: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

De rpart a partykit

Page 17: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Y para presentarlo a la audiencia: library(rpart.plot) #### librería para mejorar rpart

cols <- ifelse(tree1CCS$frame$yval == 2,

"lightsalmon", "lightgreen")

rpart.plot(tree1CCS,main="Donante mensual?",

ycompress=TRUE,extra=1,branch=1,

cex=0.7,varlen=0,digits=4,

round=0.5,shadow.col="gray",

xsep=" / ", box.col=cols,split.cex=1.1,

split.suffix=" ?",

split.box.col="lightgray",

split.border.col="darkgray",

split.round=.5,yesno.yshift=0.6,

boxes.include.gap=TRUE,eq=" ",lt=" < ", ge=" >= ")

Page 18: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Y para presentarlo a la audiencia:

Page 19: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

ÁRBOLES DE REGRESIÓN

Page 20: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Tipos de variable Dependiente ¿Qué ocurre cuando la variable “dependiente” no es dicotómica?

Var. Dependiente

Categórica

Binomial Árboles d e Decision

rpart, party, tree

Ordinal o Multinomial

spartScore

mpt

Contínua

univariante Árboles de Regresión

class=“anova” rpart

multivariante mvpart

Page 21: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

library(party) Conditional inference trees via party

Page 22: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Conditional inference tree with 5 terminal nodes #### primero llamamos al modelo "tree1"

tree1<-(MonthGive ~ Age20t29 + Age70pls + AveDonAmt + AveIncEA + DonPerYear + EngPrmLang + FinUnivP + LastDonAmt + Region + YearsGive) ### y ahora generamos el árbol, es por comodidad de reutilización de código

tree1CCS<-ctree(tree1, data=CCS[CCS$Sample=="Estimation",]) Response: MonthGive

Inputs: Age20t29, Age70pls, AveDonAmt, AveIncEA, DonPerYear, EngPrmLang, FinUnivP, LastDonAmt, Region, YearsGive

Number of observations: 544

1) AveDonAmt <= 11.25; criterion = 1, statistic = 40.318

2) AveDonAmt <= 5.25; criterion = 0.991, statistic = 11.024

3)* weights = 78

2) AveDonAmt > 5.25

4) YearsGive <= 1; criterion = 0.964, statistic = 8.446

5)* weights = 18

4) YearsGive > 1

6) DonPerYear <= 0.5714286; criterion = 0.989, statistic = 10.719

7)* weights = 59

6) DonPerYear > 0.5714286

8)* weights = 11

1) AveDonAmt > 11.25

9)* weights = 378

Page 23: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

El Árbol

Ser o no caritativo mensualmente

AveDonAmtp < 0.001

1

11.25 11.25AveDonAmtp = 0.009

2

5.25 5.25

Node 3 (n = 78)

Yes

No

00.40.8

YearsGivep = 0.036

4

1 1

Node 5 (n = 18)

Yes

No

00.40.8

DonPerYearp = 0.011

6

0.571 0.571

Node 7 (n = 59)

Yes

No

00.40.8

Node 8 (n = 11)Y

esN

o

00.40.8

Node 9 (n =

Yes

No

00.40.8

Page 24: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Los resultados “Yes- No” por nodo table(where(tree1CCS))

datos_ini=cbind(CCS[CCS$Sample=="Estimation",],nodos=(where(tree1CCS)))

summary(datos_ini)

table(datos_ini$nodos,datos_ini$MonthGive ) ## los casos por nodos

nodes(tree1CCS,c(2,6)) #obtenemos los nodos que nos interesan

nodes((tree1CCS,c(3,7)) #obtenemos los nodos que nos interesan

nodos No Yes

3 73 5

5 7 11

7 53 6

8 5 6

9 142 236

Ser o no caritativo mensualmente

AveDonAmtp < 0.001

1

11.25 11.25AveDonAmtp = 0.009

2

5.25 5.25

Node 3 (n = 78)

Yes

No

00.40.8

YearsGivep = 0.036

4

1 1

Node 5 (n = 18)

Yes

No

00.40.8

DonPerYearp = 0.011

6

0.571 0.571

Node 7 (n = 59)

Yes

No

00.40.8

Node 8 (n = 11)Y

esN

o

00.40.8

Node 9 (n = 37

Yes

No

00.40.8

Page 25: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

PREDICCIÓN ### CON PARTYKIT

#### NEW DATA

###CCS[CCS$Sample=="Validation",]

### ELIJO LA OBSERVACIÓN 4931

newData<- CCS[853,]

predict(tree1CCS, newData)

[1] Yes Levels: No Yes ## y en los datos dice que es….????

CCS[853,] MonthGive= Yes

Page 26: Presentación de PowerPoint - UMHumh1480.edu.umh.es/wp-content/uploads/sites/44/2013/02/... · 2013. 5. 5. · Age20t29< 0.1101 EngPrmLang< 0.9752 AveDonAmt< 62.92 LastDonAmt

Comparando dos Árboles desde las predicciones

################################################## ### COMPARADO DOS ARBOLES desde la predicción ################################################## sele<-CCS[CCS$Sample=="Validation",] new_data<-sele[1:50,] pred_1<-predict(tree_party1, new_data) pred_2<-predict(tree_party4, new_data) table(pred_1, pred_2) pred_2 pred_1 No Yes No 283 0 Yes 0 245