Programming Assign Unit 5
Programming Assign Unit 5
setwd(“working directory”)
library(rpart)
library(mlbench)
#load Ionosphere
data(Ionosphere)
> setwd('C:\\Users\\Admin\\Downloads')
> library(rpart)
> library(mlbench)
> data(Ionosphere)
b. Use the rpart() method to create a regression tree for the data.
rpart(Class~.,Ionosphere)
> rpart.ionosphere=rpart(Class~.,Ionosphere)
> rpart.ionosphere
n= 351
c. Use the plot() and text() methods to plot the decision tree.
> plot(rpart.ionosphere)
> text(rpart.ionosphere,pretty=0)
a. Split the data a test and train subsets using the sample() method.
> set.seed=(42)
> train=sample(1:nrow(Ionosphere),200)
b. Use the rpart method to create a decision tree using the training data.
rpart(Class~.,Ionosphere,subset=train)
> rpart.ionosphere=rpart(Class~.,Ionosphere,subset=train)
> rpart.ionosphere
n= 200
c. Use the predict method to find the predicted class labels for the testing data.
> rpart.pred=predict(rpart.ionosphere,Ionosphere.test,type="class")
d. Use the table method to create a table of the predictions versus true labels and then
compute the accuracy. The accuracy is the number of correctly assigned good cases
(true positives) plus the number of correctly assigned bad cases (true negatives) divided
by the total number of testing cases.
> table(rpart.pred,Ionosphere$Class[-train])
> (37+95)/(37+3+16+95)
[1] 0.8741722