Monte-Carlo Methods for Prediction Functions

Zachary M. Jones

2017-12-04

This packages allows you to to marginalize arbitrary prediction functions using Monte-Carlo integration. Since many prediction functions cannot be easily decomposed into a sum of low dimensional components marginalization can be helpful in making these functions interpretable.

marginalPrediction does this computation and then evaluates the marginalized function at a set grid points, which can be uniformly created, subsampled from the training data, or explicitly specified via the points argument.

The create of a uniform grid is handled by the uniformGrid method. If uniform = FALSE and the points argument isn’t used to specify what points to evaluate, a sample of size n[1] is taken from the data without replacement.

The function is integrated against a sample of size n[2] taken without replacement from the data. The argument int.points can be used to override this (in which case you can specify n[2] = NA). int.points is a vector of integerish indices which specify rows of the data to use instead.

library(mmpf)
library(randomForest)
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:randomForest':
## 
##     margin
library(reshape2)

data(swiss)

fit = randomForest(Fertility ~ ., swiss)
mp = marginalPrediction(swiss[, -1], "Education", c(10, nrow(swiss)), fit)
mp
##     Education    preds
##  1:         1 71.52507
##  2:         7 71.46880
##  3:        13 71.50956
##  4:        18 66.30175
##  5:        24 64.07160
##  6:        30 63.55537
##  7:        36 63.98453
##  8:        41 63.87322
##  9:        47 63.23212
## 10:        53 63.23212
ggplot(data.frame(mp), aes(Education, preds)) + geom_point() + geom_line()

The output of marginalPrediction is a data.table which contains the marginalized predictions and the grid points of the vars.

By default the Monte-Carlo expectation is computed, which is set by the aggregate.fun argument’s default value, the mean function. Substituting, say, the median, would give a different output.

By passing the identity function to aggregate.fun, which simply returns its input exactly, the integration points are returned directly so that the prediction element of the return is a matrix of dimension n. n, although it is an argument, can be larger or smaller depending on the interaction between the input arguments n and data. For example if a uniform grid of size 10 is requested (via n[1]) from a factor with only 5 levels, a uniform grid of size 5 is created. If vars is a vector of length greater than 1, then n[1] becomes the size of the Cartesian product of the grids created for each element of vars, which can be at most n[1]^length(vars).

mp = marginalPrediction(swiss[, -1], "Education", c(10, 5), fit, aggregate.fun = identity)
mp
##     Education   preds1   preds2   preds3   preds4   preds5
##  1:         1 83.86244 69.78269 65.44333 82.69205 69.62129
##  2:         7 84.25184 70.23652 65.41109 83.85826 69.01064
##  3:        13 82.97897 70.84903 65.85964 82.56306 69.93955
##  4:        18 76.78209 66.57438 60.31616 77.70699 65.10153
##  5:        24 74.42816 64.07167 59.04374 75.34151 62.08573
##  6:        30 74.27101 63.93759 58.43624 75.26260 61.66055
##  7:        36 74.57887 64.42397 58.96906 75.57046 62.05979
##  8:        41 74.53487 64.33357 58.83893 75.48006 62.01579
##  9:        47 74.03337 63.72519 57.99898 74.99397 61.48559
## 10:        53 74.03337 63.72519 57.99898 74.99397 61.48559
ggplot(melt(data.frame(mp), id.vars = "Education"), aes(Education, value, group = variable)) + geom_point() + geom_line()

predict.fun specifies a prediction function to apply to the model argument. This function must take two arguments, object (where model is inserted) and newdata, which is a data.frame to compute predictions on, which is generated internally and is controlled by the other arguments. This allows marginalPrediction to handle cases in which predictions for a single data point are vector-valued. That is, classification tasks where probabilities are output, and multivariate regression and/or classification. In these cases aggregate.fun is applied separately to each column of the prediction matrix. aggregate.fun must take one argument x, a vector output from predict.fun and return a vector of no greater dimension than that of x.

data(iris)

fit = randomForest(Species ~ ., iris)
mp = marginalPrediction(iris[, -ncol(iris)], "Petal.Width", c(10, 25), fit,
  predict.fun = function(object, newdata) predict(object, newdata = newdata, type = "prob"))
mp
##     Petal.Width  setosa versicolor virginica
##  1:   0.1000000 0.69880    0.20928   0.09192
##  2:   0.3666667 0.69880    0.20928   0.09192
##  3:   0.6333333 0.69472    0.21336   0.09192
##  4:   0.9000000 0.23056    0.60688   0.16256
##  5:   1.1666667 0.22208    0.61536   0.16256
##  6:   1.4333333 0.22208    0.60480   0.17312
##  7:   1.7000000 0.21832    0.43888   0.34280
##  8:   1.9666667 0.21280    0.22912   0.55808
##  9:   2.2333333 0.21280    0.22912   0.55808
## 10:   2.5000000 0.21280    0.22912   0.55808
plt = melt(data.frame(mp), id.vars = "Petal.Width", variable.name = "class",
  value.name = "prob")

ggplot(plt, aes(Petal.Width, prob, color = class)) + geom_line() + geom_point()

As mentioned before, vars can include multiple variables.

mp = marginalPrediction(iris[, -ncol(iris)], c("Petal.Width", "Petal.Length"), c(10, 25), fit,
  predict.fun = function(object, newdata) predict(object, newdata = newdata, type = "prob"))
mp
##      Petal.Width Petal.Length  setosa versicolor virginica
##   1:   0.1000000     1.000000 0.96552    0.03288   0.00160
##   2:   0.1000000     1.655556 0.96552    0.03288   0.00160
##   3:   0.1000000     2.311111 0.96376    0.03456   0.00168
##   4:   0.1000000     2.966667 0.49256    0.49408   0.01336
##   5:   0.1000000     3.622222 0.49104    0.49496   0.01400
##   6:   0.1000000     4.277778 0.49104    0.49272   0.01624
##   7:   0.1000000     4.933333 0.46616    0.41400   0.11984
##   8:   0.1000000     5.588889 0.45672    0.16592   0.37736
##   9:   0.1000000     6.244444 0.45672    0.16592   0.37736
##  10:   0.1000000     6.900000 0.45672    0.16592   0.37736
##  11:   0.3666667     1.000000 0.96552    0.03288   0.00160
##  12:   0.3666667     1.655556 0.96552    0.03288   0.00160
##  13:   0.3666667     2.311111 0.96376    0.03456   0.00168
##  14:   0.3666667     2.966667 0.49256    0.49408   0.01336
##  15:   0.3666667     3.622222 0.49104    0.49496   0.01400
##  16:   0.3666667     4.277778 0.49104    0.49272   0.01624
##  17:   0.3666667     4.933333 0.46616    0.41400   0.11984
##  18:   0.3666667     5.588889 0.45672    0.16592   0.37736
##  19:   0.3666667     6.244444 0.45672    0.16592   0.37736
##  20:   0.3666667     6.900000 0.45672    0.16592   0.37736
##  21:   0.6333333     1.000000 0.95904    0.03936   0.00160
##  22:   0.6333333     1.655556 0.95904    0.03936   0.00160
##  23:   0.6333333     2.311111 0.95728    0.04104   0.00168
##  24:   0.6333333     2.966667 0.48608    0.50056   0.01336
##  25:   0.6333333     3.622222 0.48456    0.50144   0.01400
##  26:   0.6333333     4.277778 0.48456    0.49920   0.01624
##  27:   0.6333333     4.933333 0.46360    0.41656   0.11984
##  28:   0.6333333     5.588889 0.45672    0.16592   0.37736
##  29:   0.6333333     6.244444 0.45672    0.16592   0.37736
##  30:   0.6333333     6.900000 0.45672    0.16592   0.37736
##  31:   0.9000000     1.000000 0.48840    0.49488   0.01672
##  32:   0.9000000     1.655556 0.48840    0.49488   0.01672
##  33:   0.9000000     2.311111 0.48664    0.49656   0.01680
##  34:   0.9000000     2.966667 0.01544    0.95608   0.02848
##  35:   0.9000000     3.622222 0.01392    0.95696   0.02912
##  36:   0.9000000     4.277778 0.01392    0.95264   0.03344
##  37:   0.9000000     4.933333 0.01016    0.79600   0.19384
##  38:   0.9000000     5.588889 0.00904    0.33056   0.66040
##  39:   0.9000000     6.244444 0.00904    0.33056   0.66040
##  40:   0.9000000     6.900000 0.00904    0.33056   0.66040
##  41:   1.1666667     1.000000 0.48192    0.50136   0.01672
##  42:   1.1666667     1.655556 0.48192    0.50136   0.01672
##  43:   1.1666667     2.311111 0.48016    0.50304   0.01680
##  44:   1.1666667     2.966667 0.00896    0.96256   0.02848
##  45:   1.1666667     3.622222 0.00744    0.96344   0.02912
##  46:   1.1666667     4.277778 0.00744    0.95912   0.03344
##  47:   1.1666667     4.933333 0.00544    0.80072   0.19384
##  48:   1.1666667     5.588889 0.00432    0.33528   0.66040
##  49:   1.1666667     6.244444 0.00432    0.33528   0.66040
##  50:   1.1666667     6.900000 0.00432    0.33528   0.66040
##  51:   1.4333333     1.000000 0.48192    0.49592   0.02216
##  52:   1.4333333     1.655556 0.48192    0.49592   0.02216
##  53:   1.4333333     2.311111 0.48016    0.49760   0.02224
##  54:   1.4333333     2.966667 0.00896    0.95216   0.03888
##  55:   1.4333333     3.622222 0.00744    0.95304   0.03952
##  56:   1.4333333     4.277778 0.00744    0.94872   0.04384
##  57:   1.4333333     4.933333 0.00544    0.78648   0.20808
##  58:   1.4333333     5.588889 0.00432    0.29192   0.70376
##  59:   1.4333333     6.244444 0.00432    0.29192   0.70376
##  60:   1.4333333     6.900000 0.00432    0.29192   0.70376
##  61:   1.7000000     1.000000 0.47376    0.34584   0.18040
##  62:   1.7000000     1.655556 0.47376    0.34584   0.18040
##  63:   1.7000000     2.311111 0.47200    0.34752   0.18048
##  64:   1.7000000     2.966667 0.00880    0.66232   0.32888
##  65:   1.7000000     3.622222 0.00728    0.66320   0.32952
##  66:   1.7000000     4.277778 0.00728    0.65744   0.33528
##  67:   1.7000000     4.933333 0.00528    0.59784   0.39688
##  68:   1.7000000     5.588889 0.00416    0.31416   0.68168
##  69:   1.7000000     6.244444 0.00416    0.31416   0.68168
##  70:   1.7000000     6.900000 0.00416    0.31416   0.68168
##  71:   1.9666667     1.000000 0.45624    0.19936   0.34440
##  72:   1.9666667     1.655556 0.45624    0.19936   0.34440
##  73:   1.9666667     2.311111 0.45448    0.20104   0.34448
##  74:   1.9666667     2.966667 0.00720    0.40088   0.59192
##  75:   1.9666667     3.622222 0.00568    0.40176   0.59256
##  76:   1.9666667     4.277778 0.00568    0.39640   0.59792
##  77:   1.9666667     4.933333 0.00368    0.13024   0.86608
##  78:   1.9666667     5.588889 0.00280    0.05664   0.94056
##  79:   1.9666667     6.244444 0.00280    0.05664   0.94056
##  80:   1.9666667     6.900000 0.00280    0.05664   0.94056
##  81:   2.2333333     1.000000 0.45624    0.19936   0.34440
##  82:   2.2333333     1.655556 0.45624    0.19936   0.34440
##  83:   2.2333333     2.311111 0.45448    0.20104   0.34448
##  84:   2.2333333     2.966667 0.00720    0.40088   0.59192
##  85:   2.2333333     3.622222 0.00568    0.40176   0.59256
##  86:   2.2333333     4.277778 0.00568    0.39640   0.59792
##  87:   2.2333333     4.933333 0.00368    0.12976   0.86656
##  88:   2.2333333     5.588889 0.00280    0.05664   0.94056
##  89:   2.2333333     6.244444 0.00280    0.05664   0.94056
##  90:   2.2333333     6.900000 0.00280    0.05664   0.94056
##  91:   2.5000000     1.000000 0.45624    0.19936   0.34440
##  92:   2.5000000     1.655556 0.45624    0.19936   0.34440
##  93:   2.5000000     2.311111 0.45448    0.20104   0.34448
##  94:   2.5000000     2.966667 0.00720    0.40088   0.59192
##  95:   2.5000000     3.622222 0.00568    0.40176   0.59256
##  96:   2.5000000     4.277778 0.00568    0.39640   0.59792
##  97:   2.5000000     4.933333 0.00368    0.12976   0.86656
##  98:   2.5000000     5.588889 0.00280    0.05664   0.94056
##  99:   2.5000000     6.244444 0.00280    0.05664   0.94056
## 100:   2.5000000     6.900000 0.00280    0.05664   0.94056
##      Petal.Width Petal.Length  setosa versicolor virginica
plt = melt(data.frame(mp), id.vars = c("Petal.Width", "Petal.Length"),
  variable.name = "class", value.name = "prob")

ggplot(plt, aes(Petal.Width, Petal.Length, fill = prob)) + geom_raster() + facet_wrap(~ class)

Permutation importance is a Monte-Carlo method which estimates the importance of variables in determining predictions by computing the change in prediction error from repeatedly permuting the values of those variables.

permutationImportance can compute this type of importance under arbitrary loss functions and contrast (between the loss with the unpermuted and permuted data).

permutationImportance(iris, "Sepal.Width", "Species", fit)
## [1] 0.01253333

For methods which generate predictions which are characters or unordered factors, the default loss function is the mean misclassification error. For all other types of predictions mean squared error is used.

It is, for example, possible to compute the expected change in the mean misclassification rate by class. The two arguments to loss.fun are the permuted predictions and the target variable. In this case they are both vectors of factors.

contrast.fun takes the output of loss.fun on both the permuted and unpermuted predictions (x corresponds to the permuted predictions and y the unpermuted predictions).

This can, for example, be used to compute the mean misclassification error change on a per-class basis.

permutationImportance(iris, "Sepal.Width", "Species", fit,
  loss.fun = function(x, y) {
    mat = table(x, y)
    n = colSums(mat)
    diag(mat) = 0
    rowSums(mat) / n
  },
  contrast.fun = function(x, y) x - y)
##     setosa versicolor  virginica 
##     0.0000     0.0270     0.0122