Train error with lm model: Unused arguments

I am trying to create a multiple linear regression model, because I need to predict the number of Travelers in a series. When applying the train function I get the following error::

"Error in train(Viajeros ~ ., data = frm, method = "lm", trControl = trainControl) : unused arguments (data = frm, method = "lm", trControl = trainControl)".

The code is:

# Regresión lineal múltiple
trainControl <- trainControl(
                             method="cv", 
                             number=3, 
                             classProbs=TRUE,
                             summaryFunction=twoClassSummary
                           )
frm <- as.formula(paste("Viajeros ~ ", paste(relevant_features, collapse="+")))

set.seed(7)
fit <- train( 
             Viajeros~., 
             data = frm, 
             method="lm", 
             trControl=trainControl
            )

and the data is distributed as follows: enter image description here

I don't know what's wrong, but it won't let me apply any kind of model. I always get the same error.

Upvotes: 1

Views: 1110

Answers (2)

scrameri
scrameri

Reputation: 707

You need to correctly specify the formula and data arguments (formula should not be a data.frame, but a formula, and vice versa).

library(caret)

# Regresión lineal múltiple
trainControl <- trainControl(
  method="cv", 
  number=3, 
  classProbs=TRUE,
  #summaryFunction=twoClassSummary # does not work with lm
)

frm <- as.formula(paste("Viajeros ~ ", paste(relevant_features, collapse="+")))

set.seed(7)
fit <- train( 
  form = frm, 
  data = data, 
  method="lm", 
  trControl=trainControl
)

Upvotes: 0

danlooo
danlooo

Reputation: 10627

You must use glm instead of lm in caret to get it worked for binary classifcation. Then you can use a formula and a data frame like this:

library(caret)
#> Loading required package: lattice
#> Loading required package: ggplot2
library(tidyverse)

data <- iris %>% mutate(is_setosa = ifelse(Species == "setosa", "setosa", "other") %>% factor())

trainControl <- trainControl(
  method="cv", 
  number=3, 
  classProbs=TRUE,
  summaryFunction=twoClassSummary
)

frm <- is_setosa ~ Sepal.Length + Petal.Length

set.seed(7)
fit <- train( 
  form = frm,
  data = data,
  method="glm", 
  trControl=trainControl
)
#> Warning in train.default(x, y, weights = w, ...): The metric "Accuracy" was not
#> in the result set. ROC will be used instead.
#> Warning: glm.fit: algorithm did not converge
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: algorithm did not converge
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: algorithm did not converge
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: algorithm did not converge
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred

Created on 2021-09-19 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions