How to apply weights in rpart?

北战南征 提交于 2019-12-06 12:53:18

From the documentation:

weights

optional case weights.

cost

a vector of non-negative costs, one for each variable in the model. Defaults to one for all variables. These are scalings to be applied when considering splits, so the improvement on splitting on a variable is divided by its cost in deciding which split to choose.

The weights is for rows (e.g. give higher weight to smaller class), the cost is for columns.

Example usage for applying the weights parameter (not necessarily the best way to define the weights):

positiveWeight = 1.0 / (nrow(subset(training, Y == TRUE)) / nrow(training))
negativeWeight = 1.0 / (nrow(subset(training, Y != TRUE)) / nrow(training))

modelWeights <- ifelse(training$Y== TRUE, positiveWeight, negativeWeight)

dtreeModel <- rpart(predFormula, training, weights = modelWeights)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!