r - building classification tree having categorical variables using rpart -


i have data set 14 features , few of them below, sex , marital status categorical variables.

height,sex,maritalstatus,age,edu,hometype  sex          1. male          2. female  marital status          1. married          2. living together, not married          3. divorced or separated          4. widowed          5. single, never married 

now using rpart library r build classification tree using following

rfit = rpart(hometype ~., data = trainingdata, method = "class", cp = 0.0001) 

this gives me decision tree not consider sex , marital status factors.

i thinking of using as.factor :

sex = as.factor(trainingdata$sex) ms = as.factor(trainingdata$maritalstatus) 

but not sure how pass information rpart. since data argument in rpart() takes in "trainingdata" data frame. take values in data frame. little new r , appreciate someone's on this.

you make changes trainingdata data frame directly, run rpart().

trainingdata$sex = as.factor(trainingdata$sex) trainingdata$maritalstatus = as.factor(trainingdata$maritalstatus) rfit = rpart(hometype ~., data = trainingdata, method = "class", cp = 0.0001) 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -