#LAB1 Intro to R _______________________________
mydata = read.csv('', sep=",")
meantemp = mean(mydata$temp)
plot(mydata$temp,mydata$ISI)
lmfire=line(mydata$ISI~mydata$temp)
abline(coef(lmfire))
#LAB2 Classification _______________________________
#read in csv files & preparing dataset
iris = read.csv('C:\\iris.csv', sep=",")
colnames(iris) <- c("sepal","petal","width", "length")
iris_real = read.csv('C:\\iris_real.csv', sep=",")
iris <- rbind(c(5.1, 3.5, 1.4, 0.2), iris)
iris_real <- rbind(c(1), iris_real)
colnames(iris_real) <- c("Class")
iris <- cbind(iris,iris_real)
iris_rand=iris[sample(150,150),]
irisclass = iris_rand[5]
irisvalues = iris_rand[-5]
#setting up a training set
library(rpart)
irisclassTrain = irisclass[1:100,]
irisvaluesTrain = irisvalues[1:100,]
#testset
irisclassTest = irisclass[100:150,]
irisvaluesTest = irisvalues[100:150,]
#decision trees
fit <- rpart(irisclassTrain~., method="class",data=irisvaluesTrain)
pfit<- prune(fit, cp=0.4)
plot(pfit, uniform=TRUE, main ="Decision Tree for iris data")
text(pfit, use.n=TRUE, all=TRUE, cex=1)
#decision trees different degrees
fit <- rpart(irisclassTrain~., method="class",data=irisvaluesTrain)
pfit<- prune(fit, cp=0.5)
plot(pfit, uniform=TRUE, main ="Decision Tree for iris data")
text(pfit, use.n=TRUE, all=TRUE, cex=1)
#test the classifer on the test set by calculating the predictions for each
testcase in our testset
treepred <- predict(pfit, irisvaluesTest, type = 'class')
n=length(irisclassTest) # the number of test cases
ncorrect = sum(treepred==irisclassTest) #number of correctly predicted
accuracy = ncorrect/n
print(accuracy)
#2 selected variables
plot(irisvaluesTest$width,irisvaluesTest$length, col = treepred)
# K-nearest neighbour
library(class)
#generate our predicted classes
knn3pred = knn(irisvaluesTrain, irisvaluesTest, irisclassTrain, k=3)