Docsity

Decision Trees for Beginners: An Analysis using R, Exercises of Economics

Partial preview of the text

Download Decision Trees for Beginners: An Analysis using R and more Exercises Economics in PDF only on Docsity!

Decision Trees for the Beginner

Dan Murphy

CANW September 29, 2017

Decisions, decisions

Sleepless in Seattle

The whole purpose of places like Starbucks is for people with no decision

making ability whatsoever to make six decisions just to buy one cup of coffee.

Short, tall, light, dark, caf, decaf, low-fat, non-fat, etc.

So for only $2.95, people get not just a cup of coffee but a defining sense of

self.

- The name “CART” is copyrighted by Salford Systems (San Diego) and the

software implementing their algorithm is proprietary

- The R package “rpart” (“Recursive PARTitioning”) is open-source

Pros/Cons vs. other predictive modeling

techniques (e.g., GLMs)

◾ Advantages

_- Easier to interpret, visualize, explain to non-tehnicians

  • Results are more easily squeezed into tabulated implemetations
  • More easily deal with non-linear relationships
  • Applicable to both categorical and continuous target variables_

◾ Disadvantages

- Algorithm is “greedy” in the sense that at each node it finds the best local choice

without awareness of a global optimum

_- Easy to overfit

  • Sensitive to the scoring rule_

Third party claims

176 records holding the number of claims in various cities of New South Wales

over a 12-month time period in the 1990’s.

Here are the first six records:

lga sd claims accidents ki population pop_density

1 ASHFIELD 1 1103 2304 920 124850 0.

2 AUBURN 1 1939 2660 1465 143500 0.

3 BANKSTOWN 1 4339 7381 3864 470700 0.

4 BAULKHAMHILLS 1 1491 3217 1554 311300 0.

5 BLACKTOWN 1 3801 6655 4175 584900 0.

6 BOTANY 1 387 2013 854 106350 0.

Field Definition

lga local government area (~city)

sd

statistical division (1, …, 13) (~county) – “a large,

homogeneous region characterised by identifiable

social and economic links between the inhabitants and

between the economic units within the region, under

the unifying influence of one or more major towns or

cities. They do not cross State or Territory boundaries

and are the largest statistical building blocks of States

and Territories.”

claims number of third party claims

accidents number of accidents

ki number of people killed or injured

population population size

pop_density population density

de Jong’s goal: Estimate number of claims using accidents and sd as

predictors.

de Jong’s first GLM (Model A)

Negative binomial GLM using log(accidents) to predict the claim rate ( claims

offset by log(population))

dat$L_accidents = log(dat$accidents) dat$L_population = log(dat$population) library(MASS) modelA <- glm.nb(claims ~ L_accidents + offset(L_population), data = dat) print(summary(modelA))

Call:

glm.nb(formula = claims ~ L_accidents + offset(L_population),

data = dat, init.theta = 5.830937458, link = log)

Deviance Residuals:

Min 1Q Median 3Q Max

-3.5448 -0.8172 -0.1964 0.4260 3.

Coefficients:

Estimate Std. Error z value Pr(>|z|)

(Intercept) -6.95443 0.15837 -43.91 <2e-16 ***

L_accidents 0.25389 0.02472 10.27 <2e-16 ***

---

Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for Negative Binomial(5.8309) family taken to be 1)

Null deviance: 298.16 on 175 degrees of freedom

Residual deviance: 192.33 on 174 degrees of freedom

AIC: 2041.

Number of Fisher Scoring iterations: 1

Theta: 5.

Std. Err.: 0.

2 x log-likelihood: -2035.

◾ Both coefficients are significant

Note: AIC = 2041.

dat$claimrate = dat$claims / dat$population * 1000

de Jong’s second GLM (Model B)

de Jong adds statistical division to the fit.

modelB <- glm.nb(claims ~ L_accidents + sd + offset(L_population), data = dat) print(summary(modelB))

Call:

glm.nb(formula = claims ~ L_accidents + sd + offset(L_population),

data = dat, init.theta = 7.024286456, link = log)

Deviance Residuals:

Min 1Q Median 3Q Max

-3.5319 -0.8373 -0.1939 0.4892 4.

Coefficients:

Estimate Std. Error z value Pr(>|z|)

(Intercept) -6.81943 0.31321 -21.773 < 2e-16 ***

L_accidents 0.23690 0.03884 6.099 1.07e-09 ***

sd2 -0.13302 0.18088 -0.735 0.

sd3 0.01324 0.13646 0.097 0.

sd4 -0.11683 0.18247 -0.640 0.

sd5 -0.16536 0.17037 -0.971 0.

sd6 0.05805 0.14891 0.390 0.

sd7 -0.40087 0.14669 -2.733 0.00628 **

sd8 0.07403 0.29682 0.249 0.

sd9 -0.01230 0.13524 -0.091 0.

sd10 0.15907 0.14060 1.131 0.

sd11 0.04945 0.15764 0.314 0.

sd12 -0.19986 0.17083 -1.170 0.

sd13 0.58630 0.25986 2.256 0.02406 *

---

Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for Negative Binomial(7.0243) family taken to be 1)

Null deviance: 351.03 on 175 degrees of freedom

Residual deviance: 193.14 on 162 degrees of freedom

AIC: 2035.

Number of Fisher Scoring iterations: 1

Theta: 7.

Std. Err.: 0.

2 x log-likelihood: -2005.

◾ Statistical division 7 is slightly significant

Note: AIC = 2035.

de Jong’s Generalized Additive Model

(GAM) (Model J)

◾ Similar to Model B

◾ Additionally uses statistical division to fit the shape parameter (“variance”)

suppressPackageStartupMessages(library(gamlss)) modelJ <- gamlss(claims ~ L_accidents + sd + offset(L_population), sigma.formula = ~ sd, family = PIG(), data = dat)

GAMLSS-RS iteration 1: Global Deviance = 2210.

GAMLSS-RS iteration 2: Global Deviance = 2137.

GAMLSS-RS iteration 3: Global Deviance = 2026.

GAMLSS-RS iteration 4: Global Deviance = 1969.

GAMLSS-RS iteration 5: Global Deviance = 1965.

GAMLSS-RS iteration 6: Global Deviance = 1964.

GAMLSS-RS iteration 7: Global Deviance = 1964.

GAMLSS-RS iteration 8: Global Deviance = 1964.

GAMLSS-RS iteration 9: Global Deviance = 1964.

print(summary(modelJ))

Family: c("PIG", "Poisson.Inverse.Gaussian")

Call:

gamlss(formula = claims ~ L_accidents + sd + offset(L_population),

sigma.formula = ~sd, family = PIG(), data = dat)

Fitting method: RS()

------------------------------------------------------------------

Mu link function: log

Mu Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) -6.539557 0.305630 -21.397 < 2e-16 ***

L_accidents 0.201638 0.037532 5.372 2.93e-07 ***

sd2 -0.153369 0.091075 -1.684 0..

sd3 -0.051417 0.124182 -0.414 0.

sd4 -0.147775 0.143583 -1.029 0.

sd5 -0.229441 0.166532 -1.378 0.

sd6 -0.002481 0.116547 -0.021 0.

sd7 -0.484987 0.138410 -3.504 0.000606 ***

sd8 -0.006451 0.247563 -0.026 0.

sd9 -0.095214 0.123997 -0.768 0.

sd10 0.087702 0.163152 0.538 0.

sd11 -0.040328 0.165013 -0.244 0.

sd12 -0.303488 0.233947 -1.297 0.

sd13 0.471732 0.492723 0.957 0.

---

Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1

------------------------------------------------------------------

Sigma link function: log

Sigma Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) -1.77809 0.24092 -7.380 1.03e-11 ***

sd2 -2.34305 0.75817 -3.090 0.00239 **

sd3 -0.70749 0.52508 -1.347 0.

sd4 -0.83799 0.74122 -1.131 0.

Let’s analyze with a decision tree

rpart is one of the R packages that implements decision trees

Let’s fit the claim rate to the number of accidents and to statistical division.

library(rpart) set.seed(12345) dtree <- rpart( claimrate ~ accidents + sd, data = dat )

The printout is a bit messy!

print(dtree)

n= 176

node), split, n, deviance, yval

* denotes terminal node

1) root 176 1397.570000 4.

2) accidents< 404 87 313.718500 3.

4) accidents< 126.5 28 53.406220 2.418278 *

5) accidents>=126.5 59 216.698100 3.

10) sd=7,9,12 33 124.804100 3.476660 *

11) sd=3,5,6,8,10,11,13 26 76.243230 4.514067 *

3) accidents>=404 89 744.099400 6.

6) accidents< 5278 82 458.620800 5.

12) sd=2,3,4,5,6,7,9,12 38 59.740590 4.

24) sd=5,7,12 9 6.751884 3.732405 *

25) sd=2,3,4,6,9 29 34.453760 5.375139 *

13) sd=1,10,11,13 44 346.912800 6.

26) accidents>=589 37 287.022800 6.

52) accidents< 1130 8 17.743400 4.260990 *

53) accidents>=1130 29 231.169500 6.726324 *

27) accidents< 589 7 24.653790 8.639924 *

7) accidents>=5278 7 133.085200 10.703660 *

Default plot

plot(dtree) text(dtree) title("decision tree")

The length of the branch indicates the importance of that variable.

- maximum number of levels

Many leaves have relatively few observations. Perhaps we overfit. Let’s prune

the tree.

Let’s Prune!