You are on page 1of 3

party-package.

R
Admin

2019-12-12
#party decision tree package
#party
mydata<-read.csv(file.choose())
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

mydata$admit<-as.factor(mydata$admit)
mydata$rank<-as.factor(mydata$rank)
# Decision tree using ctree function
partymodel<-ctree(admit ~ gre +gpa +rank,data=mydata)
#prediction
predictmodel<-predict(partymodel,mydata,type="response")
# confusion matrix
table1 <- table(Predicted = predictmodel, Actual =mydata$admit)
# in confusion matrix - Actual 1 & Predicted 1
tp = 24 # true positive
# in confusion matrix - Actual 0 & Predicted 0
tn = 265 # True Negative
fp = 8 # in confusion matrix - Actual 0 & Predicted 1
fn = 103 # in confusion matrix - Actual 1 & Predicted 0

# recall - mydata
recall = tp / (tp + fn)
recall
## [1] 0.1889764

# precision - mydata
precision = tp / (tp + fp)
precision

## [1] 0.75

# accuracy - mydata
accuracy = (tn + tp) / nrow(mydata)
accuracy

## [1] 0.7225

# Misclassification error - test data


1-accuracy # or

## [1] 0.2775

1 - sum(diag(table1))/sum(table1) #or

## [1] 0.2775

1 - ((tp+tn)/(tp+tn+fp+fn)) # or

## [1] 0.2775

mean(predictmodel!=mydata$admit) * 100

## [1] 27.75

# plot the decision tree


plot(partymodel)

You might also like