You are on page 1of 1

############ To split the dataset ##############

set.seed(1234)
ind<-sample(2,nrow(iris),replace=TRUE,prob=c(0.7,0.3))
traindata<-iris[ind==1,]
testdata<-iris[ind==2,]

########### Decision Tree ##############

library(party)
myformula<-Species~.
iris_ctree<-ctree(myformula,data=traindata)
testPred<-predict(iris_ctree,newdata=testdata)
table(predict(iris_ctree),traindata$Species)
print(iris_ctree)
plot(iris_ctree,type="simple")
table(testPred,testdata$Species)

########### Random Forest ##############

library(randomForest)
rf<-randomForest(myformula,data=traindata,ntree=100,proximity=TRUE)
table(predict(rf),traindata$Species)
print(rf)
plot(rf)

########### Regression ################

Time series prediction for 2015 where we have data till 2014
use 'lm' to fit a model with predictor as y and year+quarter as x.
eg: fit <- lm(cpi~year+quarter,data=.....)
Create a new dataframe(eg. data2015 <- data.frame(year=2015, quarter=1:4))
cpi2015<-predict(fit,newdata=data2015)
style<-c(rep(1,12), rep(2,4))
plot(c(cpi,cpi2015),xlab=...,ylab=...,pch=style,col=style)

You might also like