# RMOA: Massive online data stream classifications with R & MOA

(This article was first published on BNOSAC - Belgium Network of Open Source Analytical Consultants, and kindly contributed to R-bloggers)

For those of you who don't know MOA. MOA stands for Massive On-line Analysis and is an open-source framework that allows to build and run experiments of machine learning or data mining on evolving data streams. The website of MOA (http://moa.cms.waikato.ac.nz) indicates it contains machine learning algorithms for classification, regression, clustering, outlier detection and recommendation engines.

For R users who work with a lot of data or encounter RAM issues when building models on large datasets, MOA and in general data streams have some nice features. Namely:
1. It uses a limited amount of memory. So this means no RAM issues when building models.
2. Processes one example at a time, and will run over it only once
3. Works incrementally - so that a model is directly ready to be used for prediction purposes

Unfortunately it is written in Java and not easily accessible for R users to use. For users mostly interested in clustering, the stream package already facilites this (this blog item gave an example when using ff alongside the stream package). In our day-to-day use cases, classification is a more common request. The stream package only allows to do clustering. So hence the decision to make the classification algorithms of MOA easily available to R users as well. For this the RMOA package was created and is available on github (https://github.com/jwijffels/RMOA).

The current features of RMOA are:
1. Easy to set up data streams on data in RAM (data.frame/matrix), data in files (csv, delimited, flat table) as well as out-of memory data in an ffdf (ff package).
2. Easy to set up a MOA classification model
3. There are 26 classification models available which range from
2. Bayes Rule (NaiveBayes, NaiveBayesMultinomial)
3. Ensemble learning
• Bagging (LeveragingBag, OzaBag, OzaBagAdwin, OzaBagASHT)
• Stacking (LimAttClassifier)
• Other (AccuracyUpdatedEnsemble, AccuracyWeightedEnsemble, ADACC, DACC, OnlineAccuracyUpdatedEnsemble, TemporallyAugmentedClassifier, WeightedMajorityAlgorithm)
4. Active learning (ActiveClassifier)
4. Easy R-familiar interface to train the model on streaming data with a familiar formula interface as in trainMOA(model, formula, data, subset, na.action = na.exclude, ...)
5. Easy to predict new data alongside the model as in predict(object, newdata, type = "response", ...)

Feel free to use it and we welcome any feedback in your day-to-day RMOA usage experiences at https://github.com/jwijffels/RMOA in order to improve the package. For more documentation on MOA for R users: see http://jwijffels.github.io/RMOA/

An example of R code which constructs a HoeffdingTree and a boosted set of HoeffdingTrees is shown below.

##
## Installation from github
##
library(devtools)
install.packages("ff")
install.packages("rJava")
install_github("jwijffels/RMOA", subdir="RMOAjars/pkg")
install_github("jwijffels/RMOA", subdir="RMOA/pkg")

##
## HoeffdingTree example
##
require(RMOA)
hdt <- HoeffdingTree(numericEstimator = "GaussianNumericAttributeClassObserver")
hdt
## Define a stream - e.g. a stream based on a data.frame
data(iris)
iris <- factorise(iris)
irisdatastream <- datastream_dataframe(data=iris)

## Train the HoeffdingTree on the iris dataset
mymodel <- trainMOA(model = hdt,
formula = Species ~ Sepal.Length + Sepal.Width + Petal.Length,
data = irisdatastream)
## Predict using the HoeffdingTree on the iris dataset
scores <- predict(mymodel, newdata=iris, type="response")
table(scores, iris$Species) scores <- predict(mymodel, newdata=iris, type="votes") head(scores) ## ## Boosted set of HoeffdingTrees ## irisdatastream <- datastream_dataframe(data=iris) mymodel <- OzaBoost(baseLearner = "trees.HoeffdingTree", ensembleSize = 30) mymodel <- trainMOA(model = mymodel, formula = Species ~ Sepal.Length + Sepal.Width + Petal.Length, data = irisdatastream) ## Predict scores <- predict(mymodel, newdata=iris, type="response") table(scores, iris$Species)