Vous êtes sur la page 1sur 3

Tugas Responsi Data Mining

Pertemuan Ke-3
Tanggal, 05 Maret 2015

Nurul Qamariyah
G651140301

MEMBANGUN DECISION TREES DENGAN MENGGUNAKAN


DATA ORCHARDSPRAYS

Decision Trees dengan Package Party


> str(OrchardSprays)
'data.frame':
64 obs. of 4 variables:
$ decrease : num 57 95 8 69 92 90 15 2 84 6 ...
$ rowpos : num 1 2 3 4 5 6 7 8 1 2 ...
$ colpos : num 1 1 1 1 1 1 1 1 2 2 ...
$ treatment: Factor w/ 8 levels "A","B","C","D",..: 4 5 2 8 7 6 3 1 3 2 ...

Perintah > str() digunakan untuk mengukur jumlah data dan variabel yang diperoleh dalam
data OrchardSprays.
Data diatas menyatakan bahwa data OrchardSprays dengan class data.frame terdiri dari 4
variabel dimana setiap variabel terdiri dari 64 data observasi. Empat variabel tersebut adalah
decrease, rowpos, colpos dan treatment.
Menentukan nilai secara acak
> set.seed(1234)

Perintah untuk menentukan bagaimana penghasil bilangan acak harus diinisialisasi (seeded)
sehingga mendapatkan hasil yang tetap walaupun program dijalankan terus menerus. Apabila
tidak terdapatnya fungsi set.seed didalam melakukan simulasi, maka hasil akan berubah terus
menerus setiap kali program dijalankan.
> trainData <- OrchardSprays[ind==1,]
> testData <- OrchardSprays[ind==2,]

> library(party)
Loading required
Loading required
Loading required
Loading required
Loading required
Loading required

package:
package:
package:
package:
package:
package:

grid
mvtnorm
modeltools
stats4
strucchange
zoo

Attaching package: zoo


The following objects are masked from package:base:
as.Date, as.Date.numeric
Loading required package: sandwich

Nurul Qamariyah
G651140301

>
>
>
>

Tugas Responsi Data Mining


Pertemuan Ke-3
Tanggal, 05 Maret 2015

myFormula <- treatment ~ decrease + rowpos + colpos


OrchardSprays_ctree <- ctree(myFormula, data=trainData)
# check the prediction
table(predict(OrchardSprays_ctree), trainData$Species)

> print(OrchardSprays_ctree)
Conditional inference tree with 3 terminal nodes
Response: treatment
Inputs: decrease, rowpos, colpos
Number of observations: 50
1) decrease <= 10; criterion = 1, statistic = 33.881
2)* weights = 10
1) decrease > 10
3) decrease <= 20; criterion = 0.994, statistic = 20.919
4)* weights = 7
3) decrease > 20
5)* weights = 33
> plot(OrchardSprays_ctree)

plot(OrchardSprays_ctree, type="simple")

Nurul Qamariyah
G651140301

Tugas Responsi Data Mining


Pertemuan Ke-3
Tanggal, 05 Maret 2015

> # predict on test data


> testPred <- predict(OrchardSprays_ctree, newdata = testData)
> table(testPred, testData$treatment)
testPred A B C D E F G H
A00000000
B40100000
C01000100
D00000000
E00000232
F00000000
G00000000
H00000000

Vous aimerez peut-être aussi