# Using Rpart to find which factor influence the outcome the most

I have 3 factors x1, x2, x3, and one outcome y (True, False).

x1 has 3 levels, x2 has 40 levels, x3 has 2 levels.

I would like to find which parameter (x1, x2, or x3) and associated levels influence the most the outcome y. Is this possible with rpart?

I tried:

fit <- rpart(y ~ x1, model="class")
Error in if (model) { : argument is not interpretable as logical

> fit <- rpart(y ~ x1)
> fit
n= 181365

node), split, n, loss, yval, (yprob)
* denotes terminal node

1) root 181365 16370 FALSE (0.90974003 0.09025997) *


I am not sure which model to take: "anova", "poisson", "class" or "exp"?

The example in the manual (Kyphosis) uses only numeric input, but I have factors.

Here is a reproduction of the problem in a small data set:

> a = data.frame(c("A", "A", "B"), c(FALSE, FALSE, TRUE))
> a
c..A....A....B.. c.FALSE..FALSE..TRUE.
1                A                 FALSE
2                A                 FALSE
3                B                  TRUE
> colnames(a)=c("x1", "y")
> a
x1     y
1  A FALSE
2  A FALSE
3  B  TRUE
> rpart(y ~ x1, data=a, model="class")
Error in if (model) { : argument is not interpretable as logical
> a$y = factor(a$y)
> rpart(y ~ x1, data=a, model="class")
Error in if (model) { : argument is not interpretable as logical
> a$y = as.factor(a$y)
> rpart(y ~ x1, data=a, model="class")
Error in if (model) { : argument is not interpretable as logical


Short answer: yes*. FYI thanks for adding a small reproducible example, but your small example isn't large enough to build a tree with! Using kyphosis data set:

library(rpart)
data(kyphosis)

#I'm going to use a capital "K" to keep everything consistent!
Kyphosis <- kyphosis
Kyphosis <- lapply(Kyphosis, as.factor)

#These are all factors now
sapply(Kyphosis, class)
#Kyphosis      Age   Number    Start
#"factor" "factor" "factor" "factor"

Kyphosis.rpart <- rpart(
Kyphosis ~ Age + Number + Start, data = Kyphosis, method = "class")

#rpart runs fine
Kyphosis.rpart
# n= 81
#
# node), split, n, loss, yval, (yprob)
# * denotes terminal node
#
# 1) root 81 17 absent (0.79012346 0.20987654)
# 2) Start=9,10,11,13,14,15,16,17,18 57  3 absent (0.94736842 0.05263158) *
#   3) Start=1,2,3,5,6,8,12 24 10 present (0.41666667 0.58333333)
# 6) Number=2,3,4,9 12  4 absent (0.66666667 0.33333333) *
#   7) Number=5,6,7,10 12  2 present (0.16666667 0.83333333) *

#Here's the variable importance: it's an object of the model that rpart built
round(Kyphosis.rpart\$variable.importance)
#Start    Age Number
#8        3      2


*Longer answer: There's no "right" answer and rpart is a good start. Other algorithms are available also, each with their own strengths and qualifications.