2015-07-28 94 views
0

我通常在SPSS中做decissions樹以從DDBB獲得目標,我做了一些研究,發現有三個包:可用於R的樹,派對和rpart,但哪個更適合該任務?如何在R中做決策樹?

謝謝!

+0

我已經用過去的派對,它是相當不錯的。以下是我的一個項目中使用示例的鏈接:https://github.com/fiedukow/SejMOWaKlasyfikacja/blob/master/prediction_models.r – Yester

+2

我已標記此問題以關閉。我認爲這主要是基於意見的,因爲*更好*可以取決於幾個因素,甚至是關於編程風格的個人品味。 – SabDeM

回答

2

我以前用過rpart,這很方便。我通過分割訓練和測試集來用於預測建模。這是代碼。希望這會給你一些想法...

library(rpart) 
    library(rattle) 
    library(rpart.plot) 
    ### Build the training/validate/test... 

data(iris) 
nobs <- nrow(iris) 
train <- sample(nrow(iris), 0.7*nobs) 
test <- setdiff(seq_len(nrow(iris)), train) 
colnames(iris) 


### The following variable selections have been noted. 
input <- c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width") 
numeric <- c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width") 
categoric <- NULL 
target <-"Species" 
risk <- NULL 
ident <- NULL 
ignore <- NULL 
weights <- NULL 

#set.seed(500) 
# Build the Decision Tree model. 
rpart <- rpart(Species~., 
    data=iris[train, ], 
    method="class", 
    parms=list(split="information"), 
     control=rpart.control(minsplit=12, 
     usesurrogate=0, 
     maxsurrogate=0)) 

# Generate a textual view of the Decision Tree model. 
print(rpart) 
printcp(rpart) 

# Decision Tree Plot... 
prp(rpart) 
dev.new() 
fancyRpartPlot(rpart, main="Decision Tree Graph")