How can I fix the error “object ‘x’ not found” in eval(predvars, data, env)?

How can I fix the error “object ‘x’ not found” in eval(predvars, data, env)?

The error “object ‘x’ not found” in the function eval(predvars, data, env) indicates that the object named ‘x’ is not present in the specified environment. To fix this error, the user must ensure that the object ‘x’ is defined and available in the specified environment before using the eval() function. This can be done by explicitly creating and assigning a value to ‘x’ or by importing the object from another environment. Additionally, the user should check for any spelling errors or typos in the object name.

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


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 <- 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 <- lm(y ~ x, data=data)

#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 <- 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 <- 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 

Additional Resources

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

How to Fix in R: longer object length is not a multiple of shorter object length

Cite this article

stats writer (2024). How can I fix the error “object ‘x’ not found” in eval(predvars, data, env)?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-object-x-not-found-in-evalpredvars-data-env/

stats writer. "How can I fix the error “object ‘x’ not found” in eval(predvars, data, env)?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-object-x-not-found-in-evalpredvars-data-env/.

stats writer. "How can I fix the error “object ‘x’ not found” in eval(predvars, data, env)?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-object-x-not-found-in-evalpredvars-data-env/.

stats writer (2024) 'How can I fix the error “object ‘x’ not found” in eval(predvars, data, env)?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-object-x-not-found-in-evalpredvars-data-env/.

[1] stats writer, "How can I fix the error “object ‘x’ not found” in eval(predvars, data, env)?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I fix the error “object ‘x’ not found” in eval(predvars, data, env)?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top