Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- credit_default <- read.csv("credit.csv",header=TRUE, sep=",")
- credit_default <- credit_default[which(credit_default$EDUCATION %in% c(1, 2, 3, 4)), ]
- credit_default <- credit_default[which(credit_default$MARRIAGE != 0), ]
- credit_default <- credit_default[, -1]
- credit_default[, c(2:4, 6:11, 24)] <- lapply(credit_default[, c(2:4, 6:11, 24)], factor)
- str(credit_default)
- attach(credit_default)
- library(MASS)
- library(tree) ##to fit trees
- library(randomForest) ##for random forests (and bagging)
- library(gbm)
- #regression
- set.seed(1)
- sample.data<-sample.int(nrow(credit_default), floor(.50*nrow(credit_default)), replace = F)
- train<-credit_default[sample.data, ]
- test<-credit_default[-sample.data, ]
- tree.class.train<-tree(LIMIT_BAL~default.payment.next.month+SEX+EDUCATION+MARRIAGE+PAY_0+PAY_2+PAY_3+PAY_4+PAY_5+PAY_6+BILL_AMT1+BILL_AMT2+BILL_AMT3+BILL_AMT4+BILL_AMT5+BILL_AMT6+PAY_AMT1+PAY_AMT2+PAY_AMT3+PAY_AMT4+PAY_AMT5+PAY_AMT6, data=train)
- plot(tree.class.train)
- text(tree.class.train, cex=0.75, pretty=0)
- summary(tree.class.train)
- # pruning
- set.seed(11)
- cv.credit<-cv.tree(tree.class.train, K=10)
- cv.credit
- plot(cv.credit$size, cv.credit$dev, type='b', ylab="Deviance", xlab="Size")
- trees.num<-cv.credit$size[which.min(cv.credit$dev)]
- print(trees.num)
- prune.credit<- prune.tree(tree.class.train, best = trees.num)
- plot(prune.credit)
- text(prune.credit, cex = 0.75, pretty = 0)
- yhat<-predict(prune.credit, newdata=test)
- credit.test<-test[,"LIMIT_BAL"]
- mse.tree<-mean((credit.test-yhat)^2)
- print(mse.tree)
- #classification
- tree.class.train<-tree(default.payment.next.month~LIMIT_BAL+SEX+EDUCATION+MARRIAGE+PAY_0+PAY_2+PAY_3+PAY_4+PAY_5+PAY_6+BILL_AMT1+BILL_AMT2+BILL_AMT3+BILL_AMT4+BILL_AMT5+BILL_AMT6+PAY_AMT1+PAY_AMT2+PAY_AMT3+PAY_AMT4+PAY_AMT5+PAY_AMT6)
- plot(tree.class.train)
- text(tree.class.train, cex=0.75, pretty=0)
- summary(tree.class.train)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement