Learn R Programming

RMOA (version 1.1.0)

predict.MOA_trainedmodel: Predict using a MOA classifier, MOA regressor or MOA recommender on a new dataset

Description

Predict using a MOA classifier, MOA regressor or MOA recommender on a new dataset. \ Make sure the new dataset has the same structure and the same levels as get_points returns on the datastream which was used in trainMOA

Usage

# S3 method for MOA_trainedmodel
predict(object, newdata, type = "response",
  transFUN = object$transFUN, na.action = na.fail, ...)

Value

A matrix of votes or a vector with the predicted class for MOA classifier or MOA regressor. A

Arguments

object

an object of class MOA_trainedmodel, as returned by trainMOA

newdata

a data.frame with the same structure and the same levels as used in trainMOA for MOA classifier, MOA regressor, a data.frame with at least the user/item columns which were used in trainMOA when training the MOA recommendation engine

type

a character string, either 'response' or 'votes'

transFUN

a function which is used on newdata before applying model.frame. Useful if you want to change the results get_points on the datastream (e.g. for making sure the factor levels are the same in each chunk of processing, some data cleaning, ...). Defaults to transFUN available in object.

na.action

passed on to model.frame when constructing the model.matrix from newdata. Defaults to na.fail.

...

other arguments, currently not used yet

See Also

trainMOA

Examples

Run this code
## Hoeffdingtree
hdt <- HoeffdingTree(numericEstimator = "GaussianNumericAttributeClassObserver")
data(iris)
## Make a training set
iris <- factorise(iris)
traintest <- list()
traintest$trainidx <- sample(nrow(iris), size=nrow(iris)/2)
traintest$trainingset <- iris[traintest$trainidx, ]
traintest$testset <- iris[-traintest$trainidx, ]
irisdatastream <- datastream_dataframe(data=traintest$trainingset)
## Train the model
hdtreetrained <- trainMOA(model = hdt, 
 Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 
 data = irisdatastream)

## Score the model on the holdoutset
scores <- predict(hdtreetrained, 
   newdata=traintest$testset[, c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")], 
   type="response")
str(scores)
table(scores, traintest$testset$Species)
scores <- predict(hdtreetrained, newdata=traintest$testset, type="votes")
head(scores)

## Prediction based on recommendation engine
require(recommenderlab)
data(MovieLense)
x <- getData.frame(MovieLense)
x$itemid <- as.integer(as.factor(x$item))
x$userid <- as.integer(as.factor(x$user))
x$rating <- as.numeric(x$rating)
x <- head(x, 2000)

movielensestream <- datastream_dataframe(data=x)
movielensestream$get_points(3)

ctrl <- MOAoptions(model = "BRISMFPredictor", features = 10)
brism <- BRISMFPredictor(control=ctrl)
mymodel <- trainMOA(model = brism, rating ~ userid + itemid, 
 data = movielensestream, chunksize = 1000, trace=TRUE)

overview <- summary(mymodel$model)
str(overview)
predict(mymodel, head(x, 10), type = "response")

x <- expand.grid(userid=overview$users[1:10], itemid=overview$items)
predict(mymodel, x, type = "response")

Run the code above in your browser using DataLab