R version 2.9.2 (2009-08-24)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
>
>
> # digits data: only take a subsample for computation time
>
> test.set = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tes", sep=',')[sample(1797,100),]
> train.set = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tra", sep=',')[sample(3823,300),]
>
> # support vector machine is implemented in e1071
>
> library(e1071)
Loading required package: class
>
> train.model = tune.svm(train.set[,-65], as.factor(train.set[,65]), kernel='linear')
There were 11 warnings (use warnings() to see them)
> test.fit = predict(train.model$best.model, test.set[,-65])
> train.fit = predict(train.model$best.model)
>
> test.error = 1 - sum(test.fit==test.set[,65]) / nrow(test.set)
> train.error = 1 - sum(train.fit==train.set[,65]) / nrow(train.set)
>
> print(data.frame(test.error, train.error))
test.error train.error
1 0.08 0
>
> # Naive Bayes is also in e1071
>
> nbayes.model = naiveBayes(train.set[,-65], as.factor(train.set[,65]))
> test.fit = predict(nbayes.model, test.set[,-65])
> train.fit = predict(nbayes.model, train.set[,-65])
>
> test.error = 1 - sum(test.fit==test.set[,65]) / nrow(test.set)
> train.error = 1 - sum(train.fit==train.set[,65]) / nrow(train.set)
>
> print(data.frame(test.error, train.error))
test.error train.error
1 0.16 0.09
>
> # Random forests
>
> library(randomForest)
randomForest 4.5-33
Type rfNews() to see new features/changes/bug fixes.
>
> rf.model = randomForest(train.set[,-65], as.factor(train.set[,65]))
> test.fit = predict(rf.model, test.set[,-65])
> train.fit = predict(rf.model, train.set[,-65])
>
> test.error = 1 - sum(test.fit==test.set[,65]) / nrow(test.set)
> train.error = 1 - sum(train.fit==train.set[,65]) / nrow(train.set)
>
> print(data.frame(test.error, train.error))
test.error train.error
1 0.1 0
>
>
>
> proc.time()
user system elapsed
8.212 0.124 8.685
R script