if (FALSE) {
#Example using a utility surface:
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)
eval.util <- EvalRegressMetrics(test$medv, pred.res$optim, pred.res$utilRes,
thr=0.8, control.parms = control.parms)
# train a normal model
model <- randomForest(medv~.,train)
normal.preds <- predict(model, test)
#obtain the utility of the new points (trues, preds)
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)
#check both results
eval.util
eval.normal
#check visually both predictions and the surface used
UtilInterpol(test$medv, normal.preds, type = "util", control.parms = control.parms,
minds, maxds, m.pts, method = "bilinear", visual=TRUE)
points(test$medv, normal.preds, col="green")
points(test$medv, pred.res$optim, col="blue")
# another example now using points interpolation with splines
if (requireNamespace("DMwR2", quietly = TRUE)){
data(algae, package ="DMwR2")
ds <- data.frame(algae[complete.cases(algae[,1:12]), 1:12])
tgt <- which(colnames(ds) == "a1")
sp <- sample(1:nrow(ds), as.integer(0.7*nrow(ds)))
train <- ds[sp,]
test <- ds[-sp,]
control.parms <- phi.control(ds[,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 <- matrix(c(minds, maxds, -1, maxds, minds, -1),
byrow=TRUE, ncol=3)
pred.res <- UtilOptimRegress(a1~., train, test, type = "util", strat = "interpol",
strat.parms=list(method = "splines"),
control.parms = control.parms,
m.pts = m.pts, minds = minds, maxds = maxds)
# check the predictions
plot(test$a1, pred.res$optim)
# assess the performance
eval.util <- EvalRegressMetrics(test$a1, pred.res$optim, pred.res$utilRes,
thr=0.8, control.parms = control.parms)
#
# train a normal model
model <- randomForest(a1~.,train)
normal.preds <- predict(model, test)
#obtain the utility of the new points (trues, preds)
NormalUtil <- UtilInterpol(test$medv, normal.preds, type = "util",
control.parms = control.parms,
minds, maxds, m.pts, method="splines")
#check the performance
eval.normal <- EvalRegressMetrics(test$medv, normal.preds, NormalUtil,
thr=0.8, control.parms = control.parms)
eval.util
eval.normal
# observe the utility surface with the normal preds
UtilInterpol(test$a1, normal.preds, type="util", control.parms = control.parms,
minds, maxds, m.pts, method="splines", visual=TRUE)
# add the optim preds
points(test$a1, pred.res$optim, col="green")
}
# Example using a cost surface:
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,]
# if using interpolation methods for COST surface, the control.parms can be set to NULL
# 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 <- matrix(c(minds, maxds, 5, maxds, minds, 20),
byrow=TRUE, ncol=3)
pred.res <- UtilOptimRegress(medv~., train, test, type = "cost", strat = "interpol",
strat.parms = list(method = "bilinear"),
control.parms = NULL,
m.pts = m.pts, minds = minds, maxds = maxds)
# check the predictions
plot(test$medv, pred.res$optim)
# assess the performance
eval.util <- EvalRegressMetrics(test$medv, pred.res$optim, pred.res$utilRes,
type="cost", maxC = 20)
#
# train a normal model
model <- randomForest(medv~.,train)
normal.preds <- predict(model, test)
#obtain the utility of the new points (trues, preds)
NormalUtil <- UtilInterpol(test$medv, normal.preds, type="cost", control.parms = NULL,
minds, maxds, m.pts, method="bilinear")
#check the performance
eval.normal <- EvalRegressMetrics(test$medv, normal.preds, NormalUtil,
type="cost", maxC = 20)
eval.normal
eval.util
# check visually the surface and the predictions
UtilInterpol(test$medv, normal.preds, type="cost", control.parms = NULL,
minds, maxds, m.pts, method="bilinear",
visual=TRUE)
points(test$medv, pred.res$optim, col="blue")
}
Run the code above in your browser using DataLab