Monte-Carlo Methods for Prediction Functions

Zachary M. Jones

2018-10-23

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-14
## 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.78137
##  2:         7 71.70414
##  3:        13 71.58246
##  4:        18 66.35423
##  5:        24 64.44989
##  6:        30 63.74659
##  7:        36 64.17306
##  8:        41 64.08700
##  9:        47 63.67368
## 10:        53 63.67368
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 84.39867 72.30329 79.53754 65.55443 77.37592
##  2:         7 84.82855 72.42483 80.06325 64.72909 76.11628
##  3:        13 83.15669 72.33520 79.30514 64.62711 75.84375
##  4:        18 76.95320 66.37660 73.95984 60.50285 70.78110
##  5:        24 75.31183 63.94703 72.60880 57.96495 69.30223
##  6:        30 74.93934 63.19215 72.23631 56.96939 68.86829
##  7:        36 75.35473 63.74924 72.65170 57.37440 69.22738
##  8:        41 75.27137 63.67244 72.56834 57.14117 69.14402
##  9:        47 75.04955 63.22432 72.34652 56.76922 68.94907
## 10:        53 75.04955 63.22432 72.34652 56.76922 68.94907
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.61712    0.28536   0.09752
##  2:   0.3666667 0.61712    0.28536   0.09752
##  3:   0.6333333 0.61088    0.29120   0.09792
##  4:   0.9000000 0.17128    0.65824   0.17048
##  5:   1.1666667 0.16944    0.65992   0.17064
##  6:   1.4333333 0.16944    0.64584   0.18472
##  7:   1.7000000 0.16496    0.48072   0.35432
##  8:   1.9666667 0.16088    0.25072   0.58840
##  9:   2.2333333 0.16088    0.24968   0.58944
## 10:   2.5000000 0.16088    0.24968   0.58944
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.96640    0.03288   0.00072
##   2:   0.1000000     1.655556 0.96640    0.03288   0.00072
##   3:   0.1000000     2.311111 0.96472    0.03456   0.00072
##   4:   0.1000000     2.966667 0.47296    0.50912   0.01792
##   5:   0.1000000     3.622222 0.46904    0.51304   0.01792
##   6:   0.1000000     4.277778 0.46904    0.50480   0.02616
##   7:   0.1000000     4.933333 0.45112    0.44168   0.10720
##   8:   0.1000000     5.588889 0.44800    0.17448   0.37752
##   9:   0.1000000     6.244444 0.44800    0.17448   0.37752
##  10:   0.1000000     6.900000 0.44800    0.17448   0.37752
##  11:   0.3666667     1.000000 0.96640    0.03288   0.00072
##  12:   0.3666667     1.655556 0.96640    0.03288   0.00072
##  13:   0.3666667     2.311111 0.96472    0.03456   0.00072
##  14:   0.3666667     2.966667 0.47296    0.50912   0.01792
##  15:   0.3666667     3.622222 0.46904    0.51304   0.01792
##  16:   0.3666667     4.277778 0.46904    0.50480   0.02616
##  17:   0.3666667     4.933333 0.45112    0.44168   0.10720
##  18:   0.3666667     5.588889 0.44800    0.17448   0.37752
##  19:   0.3666667     6.244444 0.44800    0.17448   0.37752
##  20:   0.3666667     6.900000 0.44800    0.17448   0.37752
##  21:   0.6333333     1.000000 0.96072    0.03856   0.00072
##  22:   0.6333333     1.655556 0.96072    0.03856   0.00072
##  23:   0.6333333     2.311111 0.95904    0.04024   0.00072
##  24:   0.6333333     2.966667 0.46728    0.51480   0.01792
##  25:   0.6333333     3.622222 0.46336    0.51872   0.01792
##  26:   0.6333333     4.277778 0.46336    0.51048   0.02616
##  27:   0.6333333     4.933333 0.44736    0.44544   0.10720
##  28:   0.6333333     5.588889 0.44608    0.17552   0.37840
##  29:   0.6333333     6.244444 0.44608    0.17552   0.37840
##  30:   0.6333333     6.900000 0.44608    0.17552   0.37840
##  31:   0.9000000     1.000000 0.50936    0.47184   0.01880
##  32:   0.9000000     1.655556 0.50936    0.47184   0.01880
##  33:   0.9000000     2.311111 0.50768    0.47352   0.01880
##  34:   0.9000000     2.966667 0.01592    0.94808   0.03600
##  35:   0.9000000     3.622222 0.01200    0.95200   0.03600
##  36:   0.9000000     4.277778 0.01200    0.93848   0.04952
##  37:   0.9000000     4.933333 0.01040    0.80976   0.17984
##  38:   0.9000000     5.588889 0.01008    0.31672   0.67320
##  39:   0.9000000     6.244444 0.01008    0.31672   0.67320
##  40:   0.9000000     6.900000 0.01008    0.31672   0.67320
##  41:   1.1666667     1.000000 0.50624    0.47496   0.01880
##  42:   1.1666667     1.655556 0.50624    0.47496   0.01880
##  43:   1.1666667     2.311111 0.50456    0.47664   0.01880
##  44:   1.1666667     2.966667 0.01280    0.95120   0.03600
##  45:   1.1666667     3.622222 0.00888    0.95512   0.03600
##  46:   1.1666667     4.277778 0.00888    0.94160   0.04952
##  47:   1.1666667     4.933333 0.00728    0.81288   0.17984
##  48:   1.1666667     5.588889 0.00696    0.31984   0.67320
##  49:   1.1666667     6.244444 0.00696    0.31984   0.67320
##  50:   1.1666667     6.900000 0.00696    0.31984   0.67320
##  51:   1.4333333     1.000000 0.50624    0.46760   0.02616
##  52:   1.4333333     1.655556 0.50624    0.46760   0.02616
##  53:   1.4333333     2.311111 0.50456    0.46928   0.02616
##  54:   1.4333333     2.966667 0.01280    0.93672   0.05048
##  55:   1.4333333     3.622222 0.00888    0.94064   0.05048
##  56:   1.4333333     4.277778 0.00888    0.92712   0.06400
##  57:   1.4333333     4.933333 0.00728    0.80216   0.19056
##  58:   1.4333333     5.588889 0.00696    0.29392   0.69912
##  59:   1.4333333     6.244444 0.00696    0.29392   0.69912
##  60:   1.4333333     6.900000 0.00696    0.29392   0.69912
##  61:   1.7000000     1.000000 0.49256    0.34288   0.16456
##  62:   1.7000000     1.655556 0.49256    0.34288   0.16456
##  63:   1.7000000     2.311111 0.49184    0.34360   0.16456
##  64:   1.7000000     2.966667 0.01208    0.66600   0.32192
##  65:   1.7000000     3.622222 0.00816    0.66992   0.32192
##  66:   1.7000000     4.277778 0.00816    0.65640   0.33544
##  67:   1.7000000     4.933333 0.00656    0.59776   0.39568
##  68:   1.7000000     5.588889 0.00624    0.30968   0.68408
##  69:   1.7000000     6.244444 0.00624    0.30968   0.68408
##  70:   1.7000000     6.900000 0.00624    0.30968   0.68408
##  71:   1.9666667     1.000000 0.48136    0.21352   0.30512
##  72:   1.9666667     1.655556 0.48136    0.21352   0.30512
##  73:   1.9666667     2.311111 0.48064    0.21424   0.30512
##  74:   1.9666667     2.966667 0.01008    0.42160   0.56832
##  75:   1.9666667     3.622222 0.00696    0.42472   0.56832
##  76:   1.9666667     4.277778 0.00696    0.41240   0.58064
##  77:   1.9666667     4.933333 0.00544    0.13872   0.85584
##  78:   1.9666667     5.588889 0.00528    0.06256   0.93216
##  79:   1.9666667     6.244444 0.00528    0.06256   0.93216
##  80:   1.9666667     6.900000 0.00528    0.06256   0.93216
##  81:   2.2333333     1.000000 0.48136    0.21272   0.30592
##  82:   2.2333333     1.655556 0.48136    0.21272   0.30592
##  83:   2.2333333     2.311111 0.48064    0.21344   0.30592
##  84:   2.2333333     2.966667 0.01008    0.41968   0.57024
##  85:   2.2333333     3.622222 0.00696    0.42280   0.57024
##  86:   2.2333333     4.277778 0.00696    0.41048   0.58256
##  87:   2.2333333     4.933333 0.00544    0.13672   0.85784
##  88:   2.2333333     5.588889 0.00528    0.05952   0.93520
##  89:   2.2333333     6.244444 0.00528    0.05952   0.93520
##  90:   2.2333333     6.900000 0.00528    0.05952   0.93520
##  91:   2.5000000     1.000000 0.48136    0.21272   0.30592
##  92:   2.5000000     1.655556 0.48136    0.21272   0.30592
##  93:   2.5000000     2.311111 0.48064    0.21344   0.30592
##  94:   2.5000000     2.966667 0.01008    0.41968   0.57024
##  95:   2.5000000     3.622222 0.00696    0.42280   0.57024
##  96:   2.5000000     4.277778 0.00696    0.41048   0.58256
##  97:   2.5000000     4.933333 0.00544    0.13672   0.85784
##  98:   2.5000000     5.588889 0.00528    0.05952   0.93520
##  99:   2.5000000     6.244444 0.00528    0.05952   0.93520
## 100:   2.5000000     6.900000 0.00528    0.05952   0.93520
##      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.0128

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.0246     0.0138