#install.packages("party")
library(party)
## Loading required package: grid
## Loading required package: mvtnorm
## Loading required package: modeltools
## Loading required package: stats4
## Loading required package: strucchange
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
data("iris")
names(iris)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
## [5] "Species"
fit2 <- ctree(Species ~ Sepal.Length + Petal.Length + Sepal.Width ,
              data=iris)
plot(fit2)

print(fit2)
## 
##   Conditional inference tree with 4 terminal nodes
## 
## Response:  Species 
## Inputs:  Sepal.Length, Petal.Length, Sepal.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.Length <= 4.7; criterion = 1, statistic = 61.228
##     4)*  weights = 45 
##   3) Petal.Length > 4.7
##     5) Petal.Length <= 5; criterion = 0.984, statistic = 7.701
##       6)*  weights = 13 
##     5) Petal.Length > 5
##       7)*  weights = 42
nodes(fit2,1)
## [[1]]
## 1) Petal.Length <= 1.9; criterion = 1, statistic = 140.264
##   2)*  weights = 50 
## 1) Petal.Length > 1.9
##   3) Petal.Length <= 4.7; criterion = 1, statistic = 61.228
##     4)*  weights = 45 
##   3) Petal.Length > 4.7
##     5) Petal.Length <= 5; criterion = 0.984, statistic = 7.701
##       6)*  weights = 13 
##     5) Petal.Length > 5
##       7)*  weights = 42
nodes(fit2,3)
## [[1]]
## 3) Petal.Length <= 4.7; criterion = 1, statistic = 61.228
##   4)*  weights = 45 
## 3) Petal.Length > 4.7
##   5) Petal.Length <= 5; criterion = 0.984, statistic = 7.701
##     6)*  weights = 13 
##   5) Petal.Length > 5
##     7)*  weights = 42
table(Predict(fit2), iris$Species)
##             
##              setosa versicolor virginica
##   setosa         50          0         0
##   versicolor      0         44         1
##   virginica       0          6        49
#install.packages("randomForest")
library(randomForest)
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
fit3 <- randomForest(Species ~ Sepal.Length + Petal.Length + Sepal.Width ,
                     data=iris)
print(fit3)
## 
## Call:
##  randomForest(formula = Species ~ Sepal.Length + Petal.Length +      Sepal.Width, data = iris) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 1
## 
##         OOB estimate of  error rate: 7.33%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         44         6        0.12
## virginica       0          5        45        0.10
importance(fit3)
##              MeanDecreaseGini
## Sepal.Length         26.45171
## Petal.Length         54.09109
## Sepal.Width          15.53006
# plot(fit3)
varImpPlot(fit3)

iris$predicted.response <- predict(fit3 ,iris)
library(e1071)
#install.packages("caret")
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:randomForest':
## 
##     margin
confusionMatrix(data=iris$predicted.response,
                reference=iris$Species,
                positive='yes')
## Confusion Matrix and Statistics
## 
##             Reference
## Prediction   setosa versicolor virginica
##   setosa         50          0         0
##   versicolor      0         50         0
##   virginica       0          0        50
## 
## Overall Statistics
##                                      
##                Accuracy : 1          
##                  95% CI : (0.9757, 1)
##     No Information Rate : 0.3333     
##     P-Value [Acc > NIR] : < 2.2e-16  
##                                      
##                   Kappa : 1          
##  Mcnemar's Test P-Value : NA         
## 
## Statistics by Class:
## 
##                      Class: setosa Class: versicolor Class: virginica
## Sensitivity                 1.0000            1.0000           1.0000
## Specificity                 1.0000            1.0000           1.0000
## Pos Pred Value              1.0000            1.0000           1.0000
## Neg Pred Value              1.0000            1.0000           1.0000
## Prevalence                  0.3333            0.3333           0.3333
## Detection Rate              0.3333            0.3333           0.3333
## Detection Prevalence        0.3333            0.3333           0.3333
## Balanced Accuracy           1.0000            1.0000           1.0000