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

Regression Tree

Regression 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