Created
November 1, 2016 11:23
-
-
Save BioSciEconomist/4c295d4034983d908d311ae4744e42d4 to your computer and use it in GitHub Desktop.
Decision Tree Basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *------------------------------------------------------------------ | |
# | PROGRAM NAME: R_tree_basic | |
# | DATE:4/26/11 | |
# | CREATED BY: Matt Bogard | |
# | PROJECT FILE:P:\R Code References\Data Mining_R | |
# *---------------------------------------------------------------- | |
# | PURPOSE: demo of basic decision tree mechanics | |
# | To support post: http://econometricsense.blogspot.com/2012/01/decision-tree-basics-in-sas-and-r.html | |
# *------------------------------------------------------------------ | |
rm(list=ls()) # get rid of any existing data | |
ls() # view open data sets | |
setwd('/Users/wkuuser/Desktop/R Data Sets') # mac | |
setwd("P:\\R Code References\\R Data") # windows | |
library(rpart) # install rpart decision tree library | |
# *------------------------------------------------------------------ | |
# | get data | |
# *----------------------------------------------------------------- | |
dat1 <- read.csv("basicTree.csv", na.strings=c(".", "NA", "", "?"), encoding="UTF-8") | |
plot( dat1$x2, dat1$x1, col = dat1$class) # plot data space | |
# fit decision tree | |
(r <- rpart(class ~ x1 + x2, data = dat1)) | |
plot(r) | |
text(r) | |
library(rattle) # data mining package | |
drawTreeNodes(r) # for more detailed tree plot supported by rattle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment