8.46 Build a regression tree

We can reuse class_tree_spec as a base for the regression decision tree specification.

reg_tree_spec <- class_tree_spec %>% 
     set_mode("regression")

We are using the Carseats dataset. Let’s do the validation split.

set.seed(1010)
carseats_split <- initial_split(Carseats)

carseats_train <- training(carseats_split)
carseats_test <- testing(carseats_split)

Fit the decision tree regression model

reg_tree_fit <- fit(reg_tree_spec, Sales ~ ., data = carseats_train)
reg_tree_fit
## parsnip model object
## 
## n= 300 
## 
## node), split, n, deviance, yval
##       * denotes terminal node
## 
##   1) root 300 2529.534000  7.469333  
##     2) ShelveLoc=Bad,Medium 235 1476.666000  6.695702  
##       4) Price>=124.5 86  393.091600  5.283721  
##         8) CompPrice< 147.5 72  303.087600  4.916528  
##          16) Price>=137.5 27   99.462070  3.735556 *
##          17) Price< 137.5 45  143.374700  5.625111  
##            34) Advertising< 14.5 38   99.190880  5.249211 *
##            35) Advertising>=14.5 7    9.665971  7.665714 *
##         9) CompPrice>=147.5 14   30.370240  7.172143 *
##       5) Price< 124.5 149  813.155300  7.510671  
##        10) Age>=50.5 91  411.524700  6.751758  
##          20) Price>=86.5 80  283.338300  6.353750  
##            40) CompPrice< 123.5 52  157.669400  5.689231  
##              80) Price>=102.5 24   52.750780  4.710833  
##               160) ShelveLoc=Bad 8   12.693000  3.120000 *
##               161) ShelveLoc=Medium 16    9.688775  5.506250 *
##              81) Price< 102.5 28   62.252070  6.527857 *
##            41) CompPrice>=123.5 28   60.061870  7.587857 *
##          21) Price< 86.5 11   23.347450  9.646364 *
##        11) Age< 50.5 58  266.987700  8.701379  
##          22) Income< 59.5 18   65.389180  7.101111 *
##          23) Income>=59.5 40  134.760100  9.421500 *
##     3) ShelveLoc=Good 65  403.719500 10.266310  
##       6) Price>=109.5 42  197.649800  9.155238  
##        12) Price>=142.5 12   36.647220  7.152500 *
##        13) Price< 142.5 30   93.618500  9.956333  
##          26) Age>=61.5 9   17.323960  8.537778 *
##          27) Age< 61.5 21   50.422110 10.564290  
##            54) CompPrice< 129.5 11   14.599670  9.515455 *
##            55) CompPrice>=129.5 10   10.411360 11.718000 *
##       7) Price< 109.5 23   59.542770 12.295220 *