lrn = makeLearner("regr.svm")
fit = train(lrn, bh.task)
pd = generatePartialDependenceData(fit, bh.task, "lstat")
plotPartialDependence(pd, data = getTaskData(bh.task))
lrn = makeLearner("classif.rpart", predict.type = "prob")
fit = train(lrn, iris.task)
pd = generatePartialDependenceData(fit, iris.task, "Petal.Width")
plotPartialDependence(pd, data = getTaskData(iris.task))
# simulated example with weights computed via the joint distribution
# in practice empirical weights could be constructed by estimating the joint
# density from the training data (the data arg to fun) and computing the probability
# of the prediction grid under this estimated density (the newdata arg) or
# by using something like data depth or outlier classification to weight the
# unusualness of points in arg newdata.
sigma = matrix(c(1, .5, .5, 1), 2, 2)
C = chol(sigma)
X = replicate(2, rnorm(100)) %*% C
alpha = runif(2, -1, 1)
y = X %*% alpha
df = data.frame(y, X)
tsk = makeRegrTask(data = df, target = "y")
fit = train("regr.svm", tsk)
w.fun = function(x, newdata) {
# compute multivariate normal density given sigma
sigma = matrix(c(1, .5, .5, 1), 2, 2)
dec = chol(sigma)
tmp = backsolve(dec, t(newdata), transpose = TRUE)
rss = colSums(tmp^2)
logretval = -sum(log(diag(dec))) - 0.5 * ncol(newdata) * log(2 * pi) - 0.5 * rss
w = exp(logretval)
# weight prediction grid given probability of grid points under the joint
# density
sum(w * x) / sum(w)
}
generatePartialDependenceData(fit, tsk, "X1", fun = w.fun)
Run the code above in your browser using DataLab