if (FALSE) {
#Example using a utility surface interpolated and observing the performance of
# two models: i) a model obtained with a strategy designed for maximizing
# predictions utility and a model obtained through a standard random Forest.
data(Boston, package = "MASS")
tgt <- which(colnames(Boston) == "medv")
sp <- sample(1:nrow(Boston), as.integer(0.7*nrow(Boston)))
train <- Boston[sp,]
test <- Boston[-sp,]
control.parms <- phi.control(Boston[,tgt], method="extremes", extr.type="both")
# the boundaries of the domain considered
minds <- min(train[,tgt])
maxds <- max(train[,tgt])
# build m.pts to include at least (minds, maxds) and (maxds, minds) points
# m.pts must only contain points in [minds, maxds] range.
m.pts <- matrix(c(minds, maxds, -1, maxds, minds, -1),
byrow=TRUE, ncol=3)
pred.res <- UtilOptimRegress(medv~., train, test, type = "util", strat = "interpol",
strat.parms=list(method = "bilinear"),
control.parms = control.parms,
m.pts = m.pts, minds = minds, maxds = maxds)
# assess the performance
eval.util <- EvalRegressMetrics(test$medv, pred.res$optim, pred.res$utilRes,
thr = 0.8, control.parms = control.parms)
# now train a normal model
model <- randomForest(medv~.,train)
normal.preds <- predict(model, test)
#obtain the utility of this model predictions
NormalUtil <- UtilInterpol(test$medv, normal.preds, type = "util",
control.parms = control.parms,
minds, maxds, m.pts, method = "bilinear")
#check the performance
eval.normal <- EvalRegressMetrics(test$medv, normal.preds, NormalUtil,
thr=0.8, control.parms = control.parms)
# 3 check visually the utility surface and the predictions of both models
UtilInterpol(NULL,NULL, type = "util", control.parms = control.parms,
minds, maxds, m.pts, method = "bilinear",
visual=TRUE, full.output = TRUE)
points(test$medv, normal.preds) # standard model predition points
points(test$medv, pred.res$optim, col="blue") # model with optimized predictions
}
Run the code above in your browser using DataLab