Monday, May 21, 2018

Predict with a Classification Tree ("rpart") Model


report.model.rpart <- function(fmla, dat, main = "Classification Tree")
{ 
    ## Purpose: Report a Classification Tree ("rpart") model and make plots.
    ##          Assume the response is a categorical variable (factor).
    ##          Require package "rpart" and "rpart.plot". 
    ## Arguments:
    ##   fmla: formula for the predictive model
    ##   dat: data frame for the training data
    ##   main: Title for the plot
    ## Return: 
    ## Author: Feiming Chen, Date: 18 May 2018, 11:41
    ## ________________________________________________

    library(rpart)
    library(rpart.plot)

    ans <- rpart(fmla, dat)

    rpart.plot(ans, main = main)

    mf <- model.frame(fmla, dat)
    Response <- factor(model.response(mf))                  # extract response

    Predicted.Response <- factor(predict(ans, type = "class"))
    err.mis(Response, Predicted.Response, file="Tree-Model-CART")

    if (nlevels(Response) == 2) {
        H0.label <- levels(Response)[1]
        o1 <- sens.spec(Response, Predicted.Response, H0=H0.label)
        print(o1)

        o2 <- sens.spec(Response, Predicted.Response, H0=H0.label, get.ppv = TRUE)
        print(o2)
    }

    ans
}
if (F) {                                # Unit Test
    data(iris)
    report.model.rpart(Species ~ ., iris) # Predict 3 species
    report.model.rpart(Species ~ ., subset(iris, Species != "setosa")) # Predict 2 species
}

Unit Test 1: Predict 3 Species



Unit Test 2: Predict 2 Species




1 comment: