04pallav
9/13/2017 - 9:26 PM

Tree in R

Tree in R

#Tree
library(tree)
set.seed(1)
train = sample(1:nrow(data), nrow(data)*0.8)
tree1=tree(noofreservations~technology+actual_price+recommended_price+num_images+
             street_parked+description,data=data,subset =train)
summary(tree1)
plot(tree1)
text(tree1 ,pretty =0)

cv.tree1=cv.tree(tree1)
plot(cv.tree1$size,cv.tree1$dev,type='b')
prune.tree1=prune.tree(tree1,best=7)
plot(prune.tree1)
text(prune.tree1,pretty =0)
yhat=predict(prune.tree1,newdata=data[-train,])
target=data[-train,"noofreservations"]

#################### In case of Regression 
plot(yhat,target)
abline(0,1)
mean((yhat-target)^2) #MSE
SSE=sum((target-yhat)^2)
SST=sum((target-mean(target))^2)
1-SSE/SST

#########################RF
#install.packages('randomForest')
library(randomForest)
#Bagging
set.seed (1)
bag.tree1=randomForest(noofreservations~.,data=data,subset=train,mtry=6,importance =TRUE)
bag.tree1
yhat.bag = predict(bag.tree1 ,newdata=data[-train ,])
plot(yhat.bag, target)
abline (0 ,1)
mean((yhat.bag-target)^2) #MSE
SSE=sum((target-yhat.bag)^2)
SST=sum((target-mean(target))^2)
1-SSE/SST

#Random Forest
rf1=randomForest(noofreservations~.,data=data,subset=train,mtry=3,ntree=1000)
rf1
yhat.rf1 = predict(rf1,newdata=data[-train ,])
plot(yhat.rf1, target)
abline (0 ,1)
mean((yhat.rf1-target)^2) #MSE
SSE=sum((target-yhat.rf1)^2)
SST=sum((target-mean(target))^2)
1-SSE/SST


#######Tree wont work with missing values rf will