After looking at this question: Numeric comparison difficulty in R
I'm still stuck, because I'm depending on an equality comparison that is deep down in some function that I can't edit (or can I?)
I test in a local environment whether three numbers sum to one (sum(p)==1 --> TRUE
), but when i pass this vector of three numbers to another function, a similar equality test is failing - which makes me think that the numbers are being changed as they pass from one function to the next - is this possible?
More detail: I'm trying to 'optimize' the prior probabilities that feed into a CART model, using an optimizer (dfoptim package, nmkb
) to choose combinations of priors, sending them to the rpart package for model fitting, then the verification (rps
function) package for scoring - but somewhere in the rpart
package, my prior probabilities are throwing an error because rpart
thinks that they don't sum to 1.
Here's a reproducible example:
require('rpart')
require('verification')
require('dfoptim')
data(iris)
set.seed(1)
tmp1 <- paste0(names(iris),collapse="+")
tmp2 <- gsub("\\+Species","",tmp1)
fmlatext <- paste0("Species~",tmp2)
tree <- rpart(as.formula(fmlatext),data=iris,method="class")
objfun <- function(priors,fmlatext,data){
p <- priors/sum(priors) # turn arbitrary threesome into numbers that sum to 1
p[1] <- 1-(sum(p)-p[1]) # ensure that numbers sum to 1
print(c(p,sum(p)),digits=16)
tree <- rpart(as.formula(fmlatext),data=data,parms=list(prior=p),
method="class")
rpst <- rps(data$Species,predict(tree,data=data))
return(rpst$rpss)
}
nlev <- nlevels(iris$Species)
guess <- seq(nlev)*10
lb <- rep(1,nlev)
ub <- rep(100,nlev)
bestpriors <- nmkb(par=guess,fn=objfun,lower=lb,upper=ub,
control=list(maximize=TRUE),fmlatext=fmlatext,data=iris)
Running this code gives me this output:
[1] 0.1666666666666667 0.3333333333333333 0.5000000000000000 1.0000000000000000
[1] 0.4353687449261023 0.2354416940871099 0.3291895609867877 1.0000000000000000
[1] 0.1224920651311070 0.5548713793562775 0.3226365555126156 1.0000000000000000
[1] 0.1268712138061573 0.2390044736120877 0.6341243125817551 1.0000000000000000
[1] 0.35141687748184969 0.57028058689316308 0.07830253562498726 1.00000000000000000
[1] 0.2997590406445614 0.5077659444797995 0.1924750148756391 1.0000000000000000
[1] 0.3598141573675122 0.4350423262345758 0.2051435163979119 0.9999999999999999
Error in get(paste("rpart", method, sep = "."), envir = environment())(Y, :
Priors must sum to 1
In my real code, this happens inconsistently, depending on the data and guess value, but it does happen, and is a real pain.
How can I get past this error? Cheers, R