Hello,

I am working with rpart function but geting some error in prediction.
the same code works fine with iris dataset.

but applying other dataset it doesn't work.
sample code is given for reference.

> acc_model<-rpart(V1~V2+V3+V4+V5+V6+V7+V8, data=accEx.train)
> plotcp(acc_model)
>
acc_find_cp<-(acc_model$cptable)[,"CP"][which.min(acc_model$cptable[,"xerror"])]
> acc_prune_model<-prune(acc_model,acc_find_cp)
> acc_predict_tree<-predict(acc_prune_model,newdata=accEx.test,
type="class")
Error in predict.rpart(acc_prune_model, newdata = accEx.test, type =
"class") :
  Invalid prediction for rpart object

#conf.mat
#function(true, new)
{
t <- table(true, new)
s <- sum(t)
acc <- round((sum(diag(t)))/s, 2)
res <- list(conf = t, accuracy = acc)
}

# I thought the Error is  in match.arg(type) :
# 'arg' should be one of “vector”, “prob”, “class”, “matrix”

# accEx.test # V1 is response V2 to V8 are predictors
         V1      V2    V3  V4   V5    V6 V7  V8
1   3.66356 7.74414  -4.4 4.2  0.0  18.0 19 116
2   3.04452 8.03398  -5.7 4.8 -0.3  69.1  9 506
3   3.71357 4.70048 -13.5 4.3  0.2  80.0  3  95
4   2.94444 7.52510   1.4 3.0  0.1 177.0 22 161
5   4.06044 7.76260   4.1 5.6  1.1 287.0  7  80
6   3.68888 7.88683   5.8 2.3 -0.1 200.0  9  33
7   3.33220 7.81521   2.7 1.9  0.4 228.0  7 129
8   3.36730 7.77779   7.1 8.9  0.2 220.0 15 155
9   2.07944 6.89163   4.1 2.0  0.1 183.0  9 132
10  1.94591 7.67740   1.1 5.2  0.1  43.1 10 480
......
......
......

> acc_predict_tree<-predict(acc_prune_model,newdata=accEx.test,
type="vector")
> acc_conf<-conf.mat(accEx.train,acc_predict_tree)
Error in sort.list(unique.default(x), na.last = TRUE) :
  'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

how to solve this problem?

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to