Skip to content

Instantly share code, notes, and snippets.

@psamim
Last active May 28, 2016 19:27

Revisions

  1. psamim revised this gist May 28, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions danial.R
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    library("openxlsx")
    library("rpart")
    library("rpart.plot")


    ## setwd("/home/samim/workspace/danial")

  2. psamim revised this gist May 28, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion danial.R
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ formula <- Influence.in.Buying.Behaviour.Newsletter ~ Age + Monthly.Income
    model <- rpart(formula, method = "anova", data = wb, cp = 10^(-6))

    # Prune the tree
    cp9 = which(model$cptable[, 2] == 4)
    cp9 = which(model$cptable[, 2] == 9)
    tree9 = prune(model, model$cptable[cp9, 1])

    # create attractive pdf plot of tree
  3. psamim created this gist May 28, 2016.
    31 changes: 31 additions & 0 deletions danial.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    library("openxlsx")
    library("rpart")

    ## setwd("/home/samim/workspace/danial")

    # Load the data
    wb <- read.xlsx("data.xlsx")

    # get average over range Monthly.Income
    ## temp <- wb$Monthly.Income
    ## temp[temp == "1200-180000-800"] <- NA
    ## temp[temp == "9"] <- 15000
    ## wb$Monthly.Income <- sapply(strsplit(temp , "-") , function(i) mean(as.numeric(i)))

    # get average over range Age
    ## temp <- wb$Age
    ## temp <- gsub( "over" , "" , temp)
    ## wb$Age <- sapply(strsplit(temp , "to") , function(i) mean(as.numeric(i)))

    # Model it using rpart
    formula <- Influence.in.Buying.Behaviour.Newsletter ~ Age + Monthly.Income
    model <- rpart(formula, method = "anova", data = wb, cp = 10^(-6))

    # Prune the tree
    cp9 = which(model$cptable[, 2] == 4)
    tree9 = prune(model, model$cptable[cp9, 1])

    # create attractive pdf plot of tree
    pdf("spactree9.pdf")
    prp(tree9, extra = 100)
    dev.off()