我有这个数据集:
"Density","bodyfat","Age","state"
1.0708,12.3,23,normal
1.0853,6.1,22,slim
1.0414,25.3,22,fat
1.0751,10.4,26,normal
我写了这段代码:
library(rpart)
set.seed(1234)
ind <- sample(2,nrow(mydata),replace=TRUE, prob= c(0.7,0.3))
trainData <- mydata[ind==1,]
testData <- mydata[ind==2,]
myFormula <- state ~ bodyfat
albero <- rpart(state ~ bodyfat)
newdata <- data.frame(Density=1.0515,bodyfat=11.1,Age=24)
newdata
predict(albero,newdata,type="class")
print(albero)
此代码不起作用,我得到了这个2错误:
albero <- rpart(state~bodyfat)
eval(expr,envir,enclos)出错:对象“state”找不到
和
predict(albero,newdata,type="class")
match.arg(type)中的错误:'arg'应该是“responde”,“node”,“prob”之一