r R中的树
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 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
以上是关于r R中的树的主要内容,如果未能解决你的问题,请参考以下文章
如何绘制从使用 R 中的“caret”包创建的随机森林中选择的树
如何在使用train()和r中的交叉验证时绘制RMSE与装袋尝试的树数
Vijos1448校门外的树 题解
R中反向下三角矩阵的树列表
校门外的树
r 闪亮的决策树(具有状态和概率的树)功能