1

I have written this R code to reproduce. Here, I have a created a unique column "ID", and I am not sure how to add the predicted column back to test dataset mapping to their respective IDs. Please guide me on the right way to do this.

#Code
library(C50)
data(churn)
data=rbind(churnTest,churnTrain)
data$ID<-seq.int(nrow(data)) #adding unique id column

rm(churnTrain)
rm(churnTest)

set.seed(1223)
ind <- sample(2,nrow(data),replace = TRUE, prob = c(0.7,0.3))
train <- data[ind==1,1:21]
test <- data[ind==2, 1:21]
xtrain <- train[,-20]
ytrain <- train$churn
xtest <- test[,-20]
ytest<- test$churn
x <- cbind(xtrain,ytrain)

## C50 Model
c50Model <- C5.0(churn ~ 
  state +
  account_length +
  area_code +
  international_plan +
  voice_mail_plan +
  number_vmail_messages +
  total_day_minutes +
  total_day_calls + 
  total_day_charge +
  total_eve_minutes +
  total_eve_calls +
  total_eve_charge +
  total_night_minutes +
  total_night_calls +
  total_night_charge +
  total_intl_minutes + 
  total_intl_calls +
  total_intl_charge +
  number_customer_service_calls,data=train, trials=10)

# Evaluate Model
c50Result <- predict(c50Model, xtest)
table(c50Result, ytest) 

#adding prediction to test data
testnew = cbind(xtest,c50Result)

#OR predict directly
xtest$churn = predict(c50Model, xtest)
Di sha
  • 37
  • 1
  • 7

1 Answers1

0

I’d use match(dataID, predictedID) to match ID columns in data sets.

In reply to your comment: If you want to add predicted values to the original dataframe, both ways of merging data and prediction are correct and produce identical result. The only thing is, I would use

xtest$churn_hut <- predict(c50Model, xtest)

instead of

xtest$churn <- predict(c50Model, xtest)

because here you are replacing original churn ( as in data$churn) with whatever the model predicted, so you can’t compare the two.

Pav El
  • 351
  • 2
  • 10
  • Thanks, but here I want to know how is the predicted value of "Churn" mapped to "ID" variable. I am not using "ID" variable in modelling. – Di sha Feb 01 '16 at 09:52
  • Yes that makes sense. Thanks for the clarification :) – Di sha Feb 02 '16 at 05:18