You are on page 1of 8

arxeio<-read.

table("C:\\Documents and
Settings\\Giorgos\\Desktop\\2000.csv",header=TRUE,sep=";")
head(arxeio)

apply(arxeio,1,function(x)sum(is.na(x)))
dim(arxeio)

library(rpart)
library(tree)
attach(arxeio)
str(arxeio)

arxeio_rpart<rpart(event_f~r300012+r300013+r300014+r300015+r300017+r300049+r300054
+
r300604+r300605+r300611+r300613+r300614+r300618+r300619+
r300622+r300623+r300632+r300643+r300674+r300684+r300718+r300750+r30
0794)

library(partykit)

plot(arxeio_rpart)

plot(as.party(arxeio_rpart),tp_args=list(id=FALSE))
print(arxeio_rpart$cptable)

opt<-which.min(arxeio_rpart$cptable[,"xerror"])
cp<-arxeio_rpart$cptable[opt,"CP"]

arxeio_prune<-prune(arxeio_rpart,cp=0.01)
arxeio_prune
plot(as.party(arxeio_prune,tp_args=list(id=FALSE)))
months_pred<-predict(arxeio_prune,newdata=arxeio)
months_pred
summary(months_pred)
hist(months_pred)
xlim<-range(months)
xlim
summary(months)
hist(months)
plot(months_pred~months,data=arxeio,xlab="observed",ylab="predicted",ylim=xli
m,xlim=xlim)
abline(a=0,b=1)

library(ISLR)
library(tree)
head(arxeio)

attach(arxeio)

arxeio=data.frame(Carseats,months)
head(Carseats)

set.seed(2)
train=sample(1:nrow(arxeio),nrow(arxeio)/2)
test=-train
training_data=arxeio[train,]
testing_data=arxeio[test,]
testing_high=months[test]

tree_model=tree(months~.,training_data)
plot(tree_model)
text(tree_model,pretty=0) #pretty=0

tree_pred=predict(tree_model,testing_data,type="class")
mean(tree_pred!=testing_high)

set.seed(3)

cv_tree=cv.tree(tree_model,FUN=prune.misclass)
plot(cv_tree) # ( 9)
names(cv_tree)

plot(cv_tree$size,cv_tree$dev,type="b") #
( 9)

pruned_model=prune.misclass(tree_model,best=9)
plot(pruned_model)
text(pruned_model,pretty=0)

tree_pred=predict(pruned_model,testing_data,type="class")
mean(tree_pred!=testing_high)

#missclassification error

arxeio<-read.table("C:\\Documents and
Settings\\Giorgos\\Desktop\\a_examino_all.csv",header=TRUE,sep=";")
head(arxeio)

apply(arxeio,1,function(x)sum(is.na(x)))
dim(arxeio)

library(rpart)
library(tree)
attach(arxeio)
str(arxeio)

arxeio_rpart<rpart(out~d_300012+d_300013+d_300014+d_300604+r300794+d_300044+d_300
677)

library(partykit)

plot(arxeio_rpart)

plot(as.party(arxeio_rpart),tp_args=list(id=FALSE))
print(arxeio_rpart$cptable)

opt<-which.min(arxeio_rpart$cptable[,"xerror"])
cp<-arxeio_rpart$cptable[opt,"CP"]

arxeio_prune<-prune(arxeio_rpart,cp=0.01)
arxeio_prune
plot(as.party(arxeio_prune,tp_args=list(id=FALSE)))
months_pred<-predict(arxeio_prune,newdata=arxeio)

months_pred
summary(months_pred)
hist(months_pred)
xlim<-range(months)
xlim
summary(months)
hist(months)
plot(months_pred~months,data=arxeio,xlab="observed",ylab="predicted",ylim=xli
m,xlim=xlim)
abline(a=0,b=1)

library(ISLR)
library(tree)
head(arxeio)

attach(arxeio)

arxeio=data.frame(Carseats,months)
head(Carseats)

set.seed(2)
train=sample(1:nrow(arxeio),nrow(arxeio)/2)
test=-train
training_data=arxeio[train,]

testing_data=arxeio[test,]
testing_high=months[test]

tree_model=tree(months~.,training_data)
plot(tree_model)
text(tree_model,pretty=0) #pretty=0

tree_pred=predict(tree_model,testing_data,type="class")
mean(tree_pred!=testing_high)

set.seed(3)

cv_tree=cv.tree(tree_model,FUN=prune.misclass)
plot(cv_tree) # ( 9)
names(cv_tree)

plot(cv_tree$size,cv_tree$dev,type="b") #
( 9)

pruned_model=prune.misclass(tree_model,best=9)
plot(pruned_model)
text(pruned_model,pretty=0)

tree_pred=predict(pruned_model,testing_data,type="class")

mean(tree_pred!=testing_high)

#missclassification error

You might also like