annotate accuracy.R @ 0:4547b5a5169d draft

Uploaded
author testtool
date Fri, 13 Oct 2017 10:09:29 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4547b5a5169d Uploaded
testtool
parents:
diff changeset
1 require(caret, quietly = TRUE)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
2
4547b5a5169d Uploaded
testtool
parents:
diff changeset
3 args <- commandArgs(trailingOnly = TRUE)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
4
4547b5a5169d Uploaded
testtool
parents:
diff changeset
5 input = args[1]
4547b5a5169d Uploaded
testtool
parents:
diff changeset
6 p = args[2]
4547b5a5169d Uploaded
testtool
parents:
diff changeset
7 output1 = args[3]
4547b5a5169d Uploaded
testtool
parents:
diff changeset
8 output2 = args[4]
4547b5a5169d Uploaded
testtool
parents:
diff changeset
9
4547b5a5169d Uploaded
testtool
parents:
diff changeset
10 dataset <- read.csv(input, header=TRUE)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
11
4547b5a5169d Uploaded
testtool
parents:
diff changeset
12 validation_index <- createDataPartition(dataset$Species, p=p, list=FALSE)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
13
4547b5a5169d Uploaded
testtool
parents:
diff changeset
14 validation <- dataset[-validation_index,]
4547b5a5169d Uploaded
testtool
parents:
diff changeset
15
4547b5a5169d Uploaded
testtool
parents:
diff changeset
16 validdataset <- dataset[validation_index,]
4547b5a5169d Uploaded
testtool
parents:
diff changeset
17
4547b5a5169d Uploaded
testtool
parents:
diff changeset
18 percentage <- prop.table(table(validdataset$Species)) * 100
4547b5a5169d Uploaded
testtool
parents:
diff changeset
19 cbind(freq=table(validdataset$Species), percentage=percentage)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
20
4547b5a5169d Uploaded
testtool
parents:
diff changeset
21 output_summary <- summary(validdataset)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
22 write.csv(output_summary,output1)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
23
4547b5a5169d Uploaded
testtool
parents:
diff changeset
24 control <- trainControl(method="cv", number=10)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
25 metric <- "Accuracy"
4547b5a5169d Uploaded
testtool
parents:
diff changeset
26
4547b5a5169d Uploaded
testtool
parents:
diff changeset
27 # a) linear algorithms
4547b5a5169d Uploaded
testtool
parents:
diff changeset
28 set.seed(7)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
29 fit.lda <- train(Species~., data=validdataset, method="lda", metric=metric, trControl=control)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
30 # b) nonlinear algorithms
4547b5a5169d Uploaded
testtool
parents:
diff changeset
31 # CART
4547b5a5169d Uploaded
testtool
parents:
diff changeset
32 set.seed(7)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
33 fit.cart <- train(Species~., data=validdataset, method="rpart", metric=metric, trControl=control)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
34 # kNN
4547b5a5169d Uploaded
testtool
parents:
diff changeset
35 set.seed(7)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
36 fit.knn <- train(Species~., data=validdataset, method="knn", metric=metric, trControl=control)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
37 # c) advanced algorithms
4547b5a5169d Uploaded
testtool
parents:
diff changeset
38 # SVM
4547b5a5169d Uploaded
testtool
parents:
diff changeset
39 set.seed(7)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
40 fit.svm <- train(Species~., data=validdataset, method="svmRadial", metric=metric, trControl=control)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
41 # Random Forest
4547b5a5169d Uploaded
testtool
parents:
diff changeset
42 set.seed(7)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
43 fit.rf <- train(Species~., data=validdataset, method="rf", metric=metric, trControl=control)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
44
4547b5a5169d Uploaded
testtool
parents:
diff changeset
45 results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf))
4547b5a5169d Uploaded
testtool
parents:
diff changeset
46 output_results <- summary(results)
4547b5a5169d Uploaded
testtool
parents:
diff changeset
47
4547b5a5169d Uploaded
testtool
parents:
diff changeset
48 write.csv(as.matrix(output_results),output2)