1
$\begingroup$

I was wondering if it is possible to use the caret package with non numerical data. I know, for example, if I want to use a simple linear regression lm I could have a factor variable for classification. However, caret blows up if I attempt this. I'm also following the step outlined here The caret Package: A Unifed Interface for Predictive Models

for illustration I'm attempting to run stepDurationlm <- train (x= trainDescr, y=trainClass, method="lm")

on

str(trainDescr)
'data.frame':   589235 obs. of  2 variables:
 $ Anon.Student.Id    : Factor w/ 574 levels "02i5jCrfQK","02ZjVTxC34",..: 7 7 7 7 7 7 7 7 7 7 ...
 $ Step.Duration..sec.: num  5 5 5 4 5 5 5 5 4 4 ...

I get

Error in train.default(x = trainDescr, y = trainClass, method = "lm") : 
All predictors must be numeric for this model. Use the formula interface: train(formula, data)

alternatively, could anyone explain how to have a test set for model performance in R? That's what's motivating me to get caret working.

$\endgroup$
2
$\begingroup$

Please post a reproducible example. You should also be more specific: in this case by "non-numeric data" you mean "factor data."

If you wish to use lm with categorical variables, you have 2 options: create the dummy matrix yourself, or use caret's formula interface. Here's an example of option 2:

train(y~., data=data.frame(x=trainDescr,y=trainClass), method = "lm")

Where . covers all variables in x

$\endgroup$
1
$\begingroup$

As laid out here:

The train function has the following arguments: x: a matrix or data frame of predictors. Currently, the function only accepts numeric values (i.e., no factors or character variables). In some cases, the model.matrix function may be needed to generate a data frame or matrix of purely numeric data

Although maybe this has changed by now.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.