Saturday, September 21, 2013

Decision Tree with R

For decision tree analysis we will use iris data set.

We need ctree() function to perform the analysis. This function is available in package "party".

Fire up R and check if "party" is available on your system.  type:
> library("party")

If you don't get the following error, you are good to go:
Error in library("party") : there is no package called ‘party’
Else, you have to install party package by typing:
> install.packages("party")

This may take few minutes. Once done type the following at R prompt:
> library("party")

Then
> iris_ctree <- ctree(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data=iris)
> iris_ctree
Yow will get the following output:
 inference tree with 4 terminal nodes

Response:  Species
Inputs:  Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
Number of observations:  150

1) Petal.Length <= 1.9; criterion = 1, statistic = 140.264
  2)*  weights = 50
1) Petal.Length > 1.9
  3) Petal.Width <= 1.7; criterion = 1, statistic = 67.894
    4) Petal.Length <= 4.8; criterion = 0.999, statistic = 13.865
      5)*  weights = 46
    4) Petal.Length > 4.8
      6)*  weights = 8
  3) Petal.Width > 1.7
    7)*  weights = 46

Type:
> plot(iris_ctree)

You get the following output:

Again type:
plot(iris_ctree, type="simple")

You get the following output:



No comments:

Post a Comment