我正在使用学习器 regr.gbm
来预测计数。在 mlr
之外,直接使用 gbm
包,我使用 distribution = "poisson"
和 predict.gbm
,使用 type = "response"
,返回原始比例的预测,但是我注意到,当我使用 mlr
执行此操作时,预测似乎采用对数比例:
truth response
913 4 0.67348708
914 1 0.28413256
915 3 0.41871237
916 1 0.13027792
2101 1 -0.02092168
2102 2 0.23394970
但是,“真相”并不在对数尺度上,因此我担心 mlr
中的超参数调整例程将不起作用。为了进行比较,这是我使用 distribution = "gaussian"
得到的输出。
truth response
913 4 2.028177
914 1 1.334658
915 3 1.552846
916 1 1.153072
2101 1 1.006362
2102 2 1.281811
处理这个问题的最佳方法是什么?
最佳答案
发生这种情况是因为 gbm
默认情况下会对链接函数尺度(对于 distribution = "poisson"
来说是 log
)进行预测。这是由 gbm::predict.gbm
的 type
参数控制的(请参阅该函数的帮助页面)。不幸的是,默认情况下,mlr
不提供更改此参数的功能(mlr bugtracker 中的 it was reported)。目前的解决方法是手动添加此参数:
lrn <- makeLearner("regr.gbm", distribution = "poisson")
lrn$par.set <- c(lrn$par.set,
makeParamSet(
makeDiscreteLearnerParam("type", c("link", "response"),
default = "link", when = "predict", tunable = FALSE)))
lrn <- setHyperPars(lrn, type = "response")
# show that it works:
counttask <- makeRegrTask("counttask", getTaskData(pid.task),
target = "pregnant")
pred <- predict(train(lrn, counttask), counttask)
pred
请注意,在调整计数数据的参数时,默认回归度量(均方误差)可能会过分强调对计数值较大的数据点的拟合。预测“10”而不是“1”的平方误差与预测“1010”而不是“1001”的误差相同,但根据您的目标,您可能希望更加重视本示例中的第一个错误。
一个可能的解决方案是使用(归一化)平均泊松对数似然作为度量:
poisllmeasure = makeMeasure(
id = "poissonllnorm",
minimize = FALSE,
best = 0,
worst = -Inf,
properties = "regr",
name = "Mean Poisson Log Likelihood",
note = "For count data. Normalized to 0 for perfect fit.",
fun = function(task, model, pred, feats, extra.args) {
mean(dpois(pred$data$truth, pred$data$response, log = TRUE) -
dpois(pred$data$truth, pred$data$truth, log = TRUE))
})
# example
performance(pred, poisllmeasure)
通过将此测量值赋予 tuneParams()
中的 measures
参数,可用于调整。 (请注意,您必须将其放在列表中:tuneParams(...measures = list(poisllmeasure) ...)
)
关于r - 使用 mlr 预测计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53086410/