Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write custom classifier in R and predict function

I would like to implement my own custom classifier in R, e.g., myClassifier(trainingSet, ...) which returns the learnt model m from a specified training set. I would like to call it just like any other classifier in r:

m <- myClassifier(trainingSet)

and then I want to overload (I don't know if this is the correct word) the generic function predict()

result <- predict(m, myNewData)

I have just a basic knowledge in R. I don't know which resources I should read in order to accomplish the desired task. In order for this to work, do I need to create a package?. I am looking for some initial directions.

Does the model m contain information about the overriden predict method?, or how does R knows which predict.* method corresponds to model m?

like image 945
Enrique Avatar asked Mar 18 '15 02:03

Enrique


People also ask

What is predict function in R?

The predict() function in R is used to predict the values based on the input data. All the modeling aspects in the R program will make use of the predict() function in their own way, but note that the functionality of the predict() function remains the same irrespective of the case.

What is a custom classifier?

Custom Classifier 2.0 is a revolutionary way to classify any piece of text into custom categories. Typically, text classifiers like sentiment analysis or email classification can be classified into predefined set of categories on which they were trained.

What is the use of predict function?

We can predict the value by using function Predict() in Rstudio. Now we have predicted values of the distance variable. We have to incorporate confidence level also in these predictions, this will help us to see how sure we are about our predicted values. Output with predicted values.


1 Answers

Here is some code that shows how to write a method for your own class for a generic function.

# create a function that returns an object of class myClassifierClass
myClassifier = function(trainingData, ...) {
  model = structure(list(x = trainingData[, -1], y = trainingData[, 1]), 
                    class = "myClassifierClass") 
  return(model)
}

# create a method for function print for class myClassifierClass
predict.myClassifierClass = function(modelObject) {
  return(rlogis(length(modelObject$y)))
} 

# test
mA = matrix(rnorm(100*10), nrow = 100, ncol = 10)
modelA = myClassifier(mA)
predict(modelA)
like image 76
tchakravarty Avatar answered Nov 13 '22 21:11

tchakravarty