How To Fix R Error Message: Error in eval(predvars, data, env) : object not found

The “error in evalpredvars data env : object not found” error message occurs when you are using the predict function with multiple r arguments and a data argument that is not entered properly. Fixing this problem can include either correcting the format of the data argument or removing it altogether. Unfortunately, the message itself is not helpful in suggesting an answer.

Description of the R Error

The “error in evalpredvars data env : object not found” error message results from an argument problem when using the predict function. The prediction formula is an algorithm that processes the content of an r object to produce a linear model by which to produce and plot new data. This new data is used in the validation of the model. This is a base r function and not part of a package so it can be accessed easily. The problem occurs because the object is not referred to properly in the function it results in an envir problem. Fixing this problem is simple even though there are several options for how to do it. As a result, it is necessary to determine which method is best to use on a case-by-case basis.

Explanation of the error

Here we have some example code that produces the “error in evalpredvars data env : object not found” error message. It provides an excellent illustration of what causes this problem.

> x = c(1,2,3,4,5,6,7,8)
> y = c(2,3,4,5,6,7,8,9)
> df = data.frame(x,y)
> M = lm(y~x,df)
> predict(M,newdata=df$x,interval=”confidence”)
Error in eval(predvars, data, env) :
numeric ‘envir’ arg not of length one

In this example, we enter a data frame column into the “newdata” argument of the predict function. The problem occurs because the column is a vector and not the data frame the argument is looking for. This is what triggers our error message.

How to fix the R Error

Here are three examples of code that fixes the “error in evalpredvars data env : object not found” error message.

> x = c(1,2,3,4,5,6,7,8)
> y = c(2,3,4,5,6,7,8,9)
> df = data.frame(x,y)
> M = lm(y~x,df)
> predict(M,newdata=data.frame(df$x),interval=”confidence”)
fit lwr upr
1 2 2 2
2 3 3 3
3 4 4 4
4 5 5 5
5 6 6 6
6 7 7 7
7 8 8 8
8 9 9 9

In this first example, we convert the column into a data frame, before putting it into the predict function. As a result, the function gets what it is looking for in the “newdata” argument.

> x = c(1,2,3,4,5,6,7,8)
> y = c(2,3,4,5,6,7,8,9)
> df = data.frame(x,y)
> M = lm(y~x,df)
> predict(M)
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9

In this example, we omit all the extra arguments. It fixes the problem by removing the source of it. As simple as this solution is, it does not produce the same results as the first example. This situation could result in problems in further processing the data down the line.

> x = c(1,2,3,4,5,6,7,8)
> y = c(2,3,4,5,6,7,8,9)
> df = data.frame(x,y)
> M = lm(y~x,df)
> predict(M,interval=”confidence”)
fit lwr upr
1 2 2 2
2 3 3 3
3 4 4 4
4 5 5 5
5 6 6 6
6 7 7 7
7 8 8 8
8 9 9 9

In this example, while we omit the “newdata” argument, we also retain the “interval” argument. This example actually produces the same results as the first example. This suggests that the “newdata” argument may actually be redundant. If this is the case, then this is the best solution to this problem because you are solving the problem by omitting an unnecessary element.

The “error in evalpredvars data env : object not found” error message is a fairly simple one to cause. Unfortunately, the message itself is not helpful in understanding the problem. However, once you understand the problem it is easy to fix. You can convert the column that is causing the problem into a data frame, or you can simply omit the argument altogether. In fact, if you only omit the “newdata” argument you will get the same result. As a result, this is an easy problem to fix, you just have to decide which approach works best for your situation.

Scroll to top
Privacy Policy