
```{r }
dataDir <- "../../finalDataSets"
body = read.csv(file.path(dataDir, "bodyfat_short.csv"), header = T)
```

Regression Tree. The function we will use is called rpart.
rpart stands for recursive partitioning. It is available in the R package rpart

```{r }
library(rpart)
rt = rpart(BODYFAT ~ AGE + WEIGHT + HEIGHT + CHEST + ABDOMEN + HIP + THIGH, data = body)
rt
plot(rt)
text(rt)
(4698.255 + 4358.48)/17578.99
scorecard = read.csv(file.path(dataDir, "college_short.csv"))
```

Variable Names

```{r }
names(scorecard)
req = rpart(RET_FT4 ~ TUITIONFEE_OUT + as.factor(TYPE), data = scorecard)
plot(req)
text(req)
req
rt = rpart(BODYFAT ~ AGE + WEIGHT + HEIGHT + CHEST + ABDOMEN + HIP + THIGH, data = body)
#"R^2" for regression tree:
1 - (sum(residuals(rt)^2))/(sum((body$BODYFAT- mean(body$BODYFAT))^2))
printcp(rt)
rt = rpart(BODYFAT ~ AGE + WEIGHT + HEIGHT + CHEST + ABDOMEN + HIP + THIGH, data = body, cp = 0.0122)
plot(rt)
text(rt)
library(DAAG)
data(spam7)
spam = spam7
library(rpart)
sprt = rpart(yesno ~ crl.tot + dollar + bang + money + n000 + make, method = "class", data = spam)
plot(sprt)
text(sprt)
sprt
printcp(sprt)
sprt = rpart(yesno ~ crl.tot + dollar + bang + money + n000 + make, method = "class", cp = 0.001, data = spam)
printcp(sprt)
sprt = rpart(yesno ~ crl.tot + dollar + bang + money + n000 + make, method = "class", cp = 0.0028, data = spam)
plot(sprt)
text(sprt)
x0 = data.frame(crl.tot = 100, dollar = 3, bang = 0.33, money = 1.2, n000 = 0, make = .3)
predict(sprt, x0)
y.tr = predict(sprt, spam)[,2]
#We can form the confusion matrix with these predicted probabilities: 
confusion <- function (y, yhat, thres)
  {
    n <- length(thres)
    conf <- matrix(0,length(thres),ncol=4)
    colnames(conf) <- c("a","b","c","d")
    for ( i in 1:n)
      {
        a <- sum((!y) & (yhat<=thres[i]))
        b <- sum((!y) & (yhat>thres[i]))
        c <- sum((y) & (yhat<=thres[i])) 
        d <- sum((y) & (yhat>thres[i]))
        conf[i,] <- c(a,b,c,d)
      }
    return(conf)
  }
v = seq(0.05, 0.95, by = 0.05)
y = as.numeric(spam$yesno == "y")
tree.conf = confusion(y, y.tr, v)
plot(v,tree.conf[,2]+tree.conf[,3], xlab="threshold", ylab="b+c", type = "l")
#Choose cut-off = 0.5
precision = 1449/(271 + 1449) #d/(b+d)
recall = 1449/(364 + 1449) #d/(c+d)
c(precision, recall)
body = read.csv(file.path(dataDir, "bodyfat_short.csv"), header = T)
library(randomForest)
ft = randomForest(BODYFAT ~ AGE + WEIGHT + HEIGHT + CHEST + ABDOMEN + HIP + THIGH, data = body, importance = TRUE)
ft
importance(ft)
x0 = data.frame(AGE = 40, WEIGHT = 170, HEIGHT = 76, CHEST = 120, ABDOMEN = 100, HIP = 101, THIGH = 60)
predict(ft, x0)
library(DAAG)
data(spam7)
spam = spam7
sprf = randomForest(as.factor(yesno) ~ crl.tot + dollar + bang + money + n000 + make, data = spam)
sprf
#Prediction:
x0 = data.frame(crl.tot = 100, dollar = 3, bang = 0.33, money = 1.2, n000 = 0, make = 0.3)
predict(sprf, x0)
```

