Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write tree rules as SQL case when statements #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

carlyw
Copy link

@carlyw carlyw commented Apr 22, 2019

asSQL prints the decision tree rules in the format of SQL case when statements. Modified labels.rpart and path.rpart to accomodate asSQL needs

asSQL prints the decision tree rules in the format of SQL case when statements. Modified labels and path to accomodate asSQL needs
@carlyw
Copy link
Author

carlyw commented Apr 23, 2019

devtools::load_all(".")

(form <- as.formula(paste("Price ~", 
                    paste(setdiff(colnames(car90), "Price"), collapse = "+"))))

(dtree <- rpart(formula = form, data = car90))

asSQL(dtree)
devtools::load_all(".")
## Loading rpart
(form <- as.formula(paste("Price ~",
                         paste(setdiff(colnames(car90), "Price"),
                               collapse = "+"))))
## Price ~ Country + Disp + Disp2 + Eng.Rev + Front.Hd + Frt.Leg.Room + 
##     Frt.Shld + Gear.Ratio + Gear2 + HP + HP.revs + Height + Length + 
##     Luggage + Mileage + Model2 + Rear.Hd + Rear.Seating + RearShld + 
##     Reliability + Rim + Sratio.m + Sratio.p + Steering + Tank + 
##     Tires + Trans1 + Trans2 + Turning + Type + Weight + Wheel.base + 
##     Width
(dtree <- rpart(formula = form, data = car90))
## n=105 (6 observations deleted due to missingness)
## 
## node), split, n, deviance, yval
##       * denotes terminal node
## 
##  1) root 105 7118267000 15805.220  
##    2) Rim=R12,R13,R14 68 1101501000 11487.500  
##      4) Tank< 15.65 36  189145300  8988.278  
##        8) Tires=145,145/80,155,155/65,155/80,165/65,165/80,175/70,185/80 20   31853590  7419.050 *
##        9) Tires=185/60,185/70,195/60,195/70,195/75,205/75 16   46480320 10949.810 *
##      5) Tank>=15.65 32  434528300 14299.120  
##       10) HP< 147.5 25  119553300 13014.720 *
##       11) HP>=147.5 7  126438300 18886.290 *
##    3) Rim=R15,R16,R17 37 2419229000 23740.490  
##      6) HP< 162.5 23  863713000 20147.610  
##       12) Height< 49.75 9   87042290 14755.000 *
##       13) Height>=49.75 14  346698700 23614.290 *
##      7) HP>=162.5 14  770847400 29643.070 *
asSQL(dtree)
## CASE WHEN 
##    Rim in ('R12', 'R13', 'R14')
##     Tank>=15.65
##     HP< 147.5
##    THEN 13014.72
## WHEN 
##    Rim in ('R12', 'R13', 'R14')
##     Tank< 15.65
##     Tires in ('145', '145/80', '155', '155/65', '155/80', '165/65', '165/80', '175/70', '185/80')
##    THEN 7419.05
## WHEN 
##    Rim in ('R12', 'R13', 'R14')
##     Tank< 15.65
##     Tires in ('185/60', '185/70', '195/60', '195/70', '195/75', '205/75')
##    THEN 10949.8125
## WHEN 
##    Rim in ('R15', 'R16', 'R17')
##     HP< 162.5
##     Height>=49.75
##    THEN 23614.2857142857
## WHEN 
##    Rim in ('R15', 'R16', 'R17')
##     HP>=162.5
##    THEN 29643.0714285714
## WHEN 
##    Rim in ('R15', 'R16', 'R17')
##     HP< 162.5
##     Height< 49.75
##    THEN 14755
## WHEN 
##    Rim in ('R12', 'R13', 'R14')
##     Tank>=15.65
##     HP>=147.5
##    THEN 18886.2857142857

Created on 2019-04-23 by the reprex package (v0.2.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant