Skip to content
Related Articles

Related Articles

How to Fix in R: invalid model formula in ExtractVars

View Discussion
Improve Article
Save Article
  • Last Updated : 23 May, 2022

In this article, we are going to see how to fix- Invalid model formula in ExtractVars in R Programming Language.

The error “Invalid model formula in ExtractVars” occurs when we incorrectly specify the variables in the formula while fitting the decision tree. It includes adding quotations around the name of variable while specifying them in rpart method i.e., fitting decision tree model. In order to build classification trees or decision trees, R provides a powerful machine learning library called rpart which implements recursive partitioning. Let’s look at the sample code which throws the error- Invalid model formula in ExtractVars

Error Generating

R




# create a data frame
playersData <- data.frame(pointsEarned=c(160, 171, 165, 49, 95),
                          starsEarned=c(80, 94, 100, 20, 36),
                          level=c(8, 9, 10, 2, 3))
 
# importing required libraries
library(rpart)
 
# fitting decision tree model to players data
model <- rpart(pointsEarned ~ "starsEarned" + "level",
               data = playersData)


Output

Error in terms.formula(formula, data = data) : 

  invalid model formula in ExtractVars

Calls: rpart … <Anonymous> -> model.frame.default -> terms -> terms.formula

Execution halted

[Execution complete with exit code 1]

The reason for getting this error invalid model formula in ExtractVars is in the rpart method we used quotations around variable names starsEarned and level. So in order to fix this Invalid model formula in ExtractVars error, there are two ways which can be explained below separately.

Method 1: Using Eliminate quotations 

Eliminate quotations around the variable name at the formula in rpart method is acceptable and it won’t produce any error. This was the easiest way to fix the error. So let’s look into the error-fixed code.

R




#create a data frame
playersData <- data.frame(pointsEarned=c(160, 171, 165, 49, 95),
                          starsEarned=c(80, 94, 100, 20, 36),
                          level=c(8, 9, 10, 2, 3))
 
#importing required libraries
library(rpart)
 
#fitting decision tree model to players data
model <- rpart(pointsEarned ~ starsEarned + level, data = playersData)


Output

[Execution complete with exit code 0]

exit code 0 in the output indicates that code is executed with no errors.

Method 2: Using standard notation/ conventional

Another way to fix the error was using standard notation/ conventional format for accessing the columns in the specified data frame in rpart method. This approach is widely used as standard notations are followed by every one in the coding. Below is the code which get executed with no errors.

R




# create a data frame
playersData <- data.frame(pointsEarned=c(160, 171, 165, 49, 95),
                          starsEarned=c(80, 94, 100, 20, 36),
                          level=c(8, 9, 10, 2, 3))
 
# importing required libraries
library(rpart)
 
# fitting decision tree model to players data
model <- rpart(playersData$pointsEarned ~
               playersData$starsEarned +
               playersData$level, data = playersData)


Output

[Execution complete with exit code 0]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!