-->

How to apply weights in rpart?

2019-05-22 15:13发布

问题:

I have this data on houses from the Kaggle practice competition and I'm using rpart to train a simple first model to predict the sale price.

The model is not correctly identifying sales where the sale condition was abnormal or a down payment. Therefore, I'd like to increase the importance of this variable which is obviously overlooked in the model.

I'm assuming this is done by using the "weights" parameter but how is this parameter used? How can I pinpoint which variables I want to have a higher weight?

回答1:

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)


标签: r rpart