I'm using the CHAID package in R to create a tree for a binary dependent variable.

I think that assigning a loss matrix to my model would help improve my result, but it seems that only rpart library is able to affect that kind of parameter.

How does one implement this for the CHAID package?


I've found something. Basically, you just have to weight your probability differently cause a loss matrix doesn't change the tree rules. It just changes the final node classification from 0 to 1 or the contrary. So once you have your tree you can just apply the loss matrix to your prediction result to reclassified them.

1) So, first of all, you have to create some probability columns in your test data set :

testDataSet$Prob = predict(chaidTree.model,testDataSet,type='prob')

you should have something like this example in your testDataSet$Prob

              0          1
1     0.3346154 0.66538462
2     0.3430981 0.65690191
3     0.6248424 0.37515763
4     0.7759181 0.22408194

2) You have to multiply the probability by factors of your loss matrix

let's say this is your loss matrix

#Loss Matrix
                  #0   #1 
#observation  #0   0   X0
              #1   X1  0

So, what you want to do is weight your probability of being classified as 1 by X1 and your probability of being classified as 0 by X0

This can be done by creating two new column like this

testDataSet$Prob_Of_1_Weighted= testDataSet[[Put here the position that your testDataSet$Prob table have in your testDataSet dataframe]][,2]*X1

testDataSet$Prob_Of_0_Weighted= testDataSet[[Put here the position that your testDataSet$Prob table have in your testDataSet dataframe]][,1]*X0

3) Create a new prediction classification column based on the comparison between testDataSet$Prob_Of_1_Weighted and testDataSet$Prob_Of_0_Weighted:

testDataSet$Weighted_Prediction= ifelse(testDataSet$Prob_Of_1_Weighted>testDataSet$Prob_Of_0_Weighted,1,0)

4) Use the caret library confusion matrix function to observe the effect of your loss matrix on your prediction

confusionMatrix(testDataSet$Weighted_Prediction,testDataSet$Your_Dependant_variable_Column,positive = '1')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.