1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
> # test.c50.R: c50 tests for plotmo and plotres
>
> source("test.prolog.R")
> library(C50)
> library(rpart.plot) # for ptitanic, want data with NAs for testing
Loading required package: rpart
> library(plotmo)
Loading required package: Formula
Loading required package: plotrix
> library(earth) # for etitanic
> data(etitanic)
> get.tit <- function() # abbreviated titanic data
+ {
+ tit <- etitanic
+ pclass <- as.character(tit$pclass)
+ # change the order of the factors so not alphabetical
+ pclass[pclass == "1st"] <- "first"
+ pclass[pclass == "2nd"] <- "class2"
+ pclass[pclass == "3rd"] <- "classthird"
+ tit$pclass <- factor(pclass, levels=c("class2", "classthird", "first"))
+ # log age is so we have a continuous predictor even when model is age~.
+ set.seed(2015)
+ tit$logage <- log(tit$age) + rnorm(nrow(tit))
+ tit$parch <- NULL
+ # by=12 gives us a small fast model with an additive and a interaction term
+ tit <- tit[seq(1, nrow(etitanic), by=12), ]
+ }
> tit <- get.tit()
>
> c50.tree.xy <- C5.0(x=tit[,-1], y=tit[,1]) # predict pclass
> plotmo(c50.tree.xy, type="prob", nresponse="first", pmethod="apartdep")
calculating apartdep for survived
calculating apartdep for age
calculating apartdep for logage
calculating apartdep for survived:age 0123456790
calculating apartdep for survived:logage 0123456790
calculating apartdep for age:logage 01234567890
> plotmo(c50.tree.xy, type="class")
plotmo grid: survived sex age sibsp logage
0 male 30 0 3.06991
> # TODO following gives error: type should be either 'class', 'confidence' or 'prob'
> # try(plotmo(c50.tree.xy, type="confidence"))
> plotres(c50.tree.xy, type="prob", nresponse="first")
>
> c50.tree.form <- C5.0(pclass~., data=tit) # predict pclass
> plotmo(c50.tree.form, type="prob", nresponse="first")
plotmo grid: survived sex age sibsp logage
0 male 30 0 3.06991
> plotmo(c50.tree.form, type="class")
plotmo grid: survived sex age sibsp logage
0 male 30 0 3.06991
> # TODO following gives error: type should be either 'class', 'confidence' or 'prob'
> # try(plotmo(c50.tree.form, type="confidence"))
> plotres(c50.tree.form, type="prob", nresponse="first")
>
> tit$survived <- factor(ifelse(tit$survived == 1, "yes", "no"),
+ levels = c("yes", "no"))
> c50.tree.survived <- C5.0(survived~., data=tit, trials=5) # predict survived
> plotmo(c50.tree.survived, type="prob", nresponse="yes")
plotmo grid: pclass sex age sibsp logage
classthird male 30 0 3.06991
> plotmo(c50.tree.survived, type="class")
plotmo grid: pclass sex age sibsp logage
classthird male 30 0 3.06991
> # TODO following gives error: type should be either 'class', 'confidence' or 'prob'
> # try(plotmo(c50.tree.survived, type="confidence"))
> plotres(c50.tree.survived, type="prob", nresponse="yes")
>
> source("test.epilog.R")
|