Description Usage Arguments Value Examples
Works by setting everyone in the data to a range of values in the variable of interest and scoring repeatedly. Very slow. Only compatible with data.tables.
1 2 | marginal_effect(data, var, model, predict_model = predict, range = -1,
sample_frac = 1)
|
data |
data.table object on which to score |
var |
string naming the variable of interest |
model |
target model object |
predict_model |
a function with signature (model, data) => predictions. Defaults to predict. |
range |
of values to test. Defaults to 10 evenly spaced points between the min and max of the target variable. |
sample_frac |
fraction of data to sample randomly |
data.table of the variable and its derived marginal effect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | library("xgboost")
library("ggplot2")
library("foreach")
library("doMC")
data(diamonds)
set.seed(1234)
predict_model <- function(model, data) {
score_mm <- model.matrix(price ~ ., data)
score_dm <- xgb.DMatrix(score_mm)
predict(model, newdata = score_dm)
}
train <- data.table(diamonds[order(runif(nrow(diamonds))), ][1:10000, ])
train_mm <- model.matrix(price ~ ., train)
train_y <- train$price
train_dm <- xgb.DMatrix(train_mm, label = train$price)
model <- xgb.train(params = list(max_depth = 2,
subsample = 0.5,
eta = 0.1,
colsample_bytree = 0.5,
objective = "reg:linear"),
nrounds = 10,
data = train_dm)
marginal_effect(train, "x", model, predict_model)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.