9.1 C
London
Friday, December 20, 2024
HomeRFix Common Errors in RHow to Fix: Error in eval(predvars, data, env) : object ‘x’ not...

How to Fix: Error in eval(predvars, data, env) : object ‘x’ not found

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

One error you may encounter in R is:

Error in eval(predvars, data, env) : object 'x' not found 

This error occurs when you attempt to use a regression model in R to predict the response values of a new data frame, but the column names in the new data frame do not match the column names of the data frame that you used to fit the model.

This tutorial shares exactly how to fix this error.

How to Reproduce the Error

Suppose we fit a simple linear regression model in R:

#create data frame
data frame(x=c(1, 2, 2, 3, 5, 6, 8, 9),
                   y=c(7, 8, 8, 6, 9, 8, 12, 14))

#fit linear regression model to data
model #view summary of model
summary(model)

Call:
lm(formula = y ~ x, data = data)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.1613 -0.7500  0.5000  0.9355  1.5161 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept)   5.5161     0.9830   5.611  0.00137 **
x             0.7742     0.1858   4.167  0.00590 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.463 on 6 degrees of freedom
Multiple R-squared:  0.7432,	Adjusted R-squared:  0.7004 
F-statistic: 17.37 on 1 and 6 DF,  p-value: 0.005896

Now suppose we attempt to use the predict() function to predict the response values for a new data frame:

#define new data frame
new_data frame(x1=c(4, 5, 7, 8, 9))

#attempt to predict y values for new data frame
predict(model, newdata=new_data)

Error in eval(predvars, data, env) : object 'x' not found

We receive an error because the data frame that we used when fitting the model had a predictor variable named x, but in the new data frame we named the predictor variable x1.

Since these names don’t match, we receive an error.

How to Fix the Error

The way to fix this error is to simply make sure that the predictor variable in the new data frame has the same name.

So, we’ll be sure to name the predictor variable x in the new data frame:

#define new data frame
new_data frame(x=c(4, 5, 7, 8, 9)) 

Now we can use the predict() function to predict the response values for the new data frame:

#predict y values for new data frame
predict(model, newdata=new_data)

        1         2         3         4         5 
 8.612903  9.387097 10.935484 11.709677 12.483871 

We’re able to successfully predict the y values for the new data frame without any errors since the column names matched.

Additional Resources

The following tutorials explain how to troubleshoot other common errors in R:

How to Fix in R: names do not match previous names
How to Fix in R: longer object length is not a multiple of shorter object length
How to Fix in R: contrasts can be applied only to factors with 2 or more levels

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories