R error in eval(predvars, data, env) : numeric ‘envir’ arg not of length one

If you do any programming you will come across error messages. Here we have an error message that comes from the modeling data set. It is a problem related to the formatting of data for the predict() function.

Circumstances of this error message.

The error occurs when you improperly enter data into the predict() function. You use this function for predicting the results from a model based on your data. The predict() function has three parameters. A model, data frame and an optional parameter of an interval. The model is, of course, the model that you are testing. The data frame is the data you are using to test the model. The interval calculates a range with a margin of ninety-five percent. Here is a section of code that illustrates the problem.

# source: error in eval(predvars, data, env) : numeric 'envir' arg not of length one 
> df = read.table(text = '
 +       TRip   Distance  Duration
 + 1     "#3"  2027.072  081.082
 + 2     "#4"  3476.802  139.072
 + 3     "#5"  4622.174  184.886
 + 4     "#6"  4214.416  168.576
 + 5     "#7"  6553.586  262.143
 + 6     "#8"  7123.162  284.926
 + 7     "#9"  7987.369  319.494', header=TRUE)
 > 
 > model = lm(df$Duration ~ df$Distance)
 > 
 > x = predict(model, df$Distance)
   Error in eval(predvars, data, env) : numeric 'envir' arg not of length one

Running this code produces the error message, and the problem can be traced down to using “df$Distance” directly in the predict() function when it is not a data frame.

Reason for this error message

The reason this error message occurs is that the predict() function is looking for a data frame in the second position, however, “df$Distance” is simply a vector produced by extracting the second column from the data frames “df” such that you are entering the wrong type of data, this is what causes the function to result in an error message. Put another way you are giving the function data that is not correctly formatted and it is responding by telling you that cannot do it. Unfortunately, like a lot of error messages, the exact wording is not always helpful.

How to fix this error message.

Fixing this era is simply a matter of converting vector “df$Distance” into a data frame so that it will be accepted by the predict() function. You can do this both inside and outside of the predict() function. Here is a redo of our example code, entered properly so that it does not produce the error.

# solution: error in eval(predvars, data, env) : numeric 'envir' arg not of length one  
> df = read.table(text = '
 +       TRip   Distance  Duration
 + 1     "#3"  2027.072  081.082
 + 2     "#4"  3476.802  139.072
 + 3     "#5"  4622.174  184.886
 + 4     "#6"  4214.416  168.576
 + 5     "#7"  6553.586  262.143
 + 6     "#8"  7123.162  284.926
 + 7     "#9"  7987.369  319.494', header=TRUE)
 > 
 > model = lm(df$Duration ~ df$Distance)
 >x = predict(model, data.frame(df$Distance))

What distinguishes this version from the one that produces the error message is putting “df$Distance” through the data.frame() function.

> predict(model, df$Distance)
> predict(model, data.frame(df$Distance))

As you can see here the key difference is the addition of the data.frame() function.

For: error in eval(predvars, data, env) : numeric ‘envir’ arg not of length one