R Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “c(‘double’, ‘numeric’)”

 

In this R tutorial you’ll learn how to handle the Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “c(‘double’, ‘numeric’)”.

The page contains this:

Let’s just jump right in.

 

Creating Example Data

First, let’s create some example data:

set.seed(538946)                          # Create train data
data_train <- data.frame(x = rnorm(10),
                         y = rnorm(10))
head(data_train)                          # Print head of train data

 

table 1 data frame r error usemethod no applicable method for predict

 

Table 1 visualizes the output of the RStudio console that got returned by the previous code and illustrates that our example data is composed of two numerical columns. This data frame will be used to train our model.

Next, we also have to create a test data frame:

data_test <- data.frame(x = rnorm(10))    # Create test data
head(data_test)                           # Print head of test data

 

table 2 data frame r error usemethod no applicable method for predict

 

As shown in Table 2, the previous R programming syntax has created another data frame that contains only the predictor column x.

 

Example 1: Reproduce the Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “c(‘double’, ‘numeric’)”

This section illustrates how to replicate the error message Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “c(‘double’, ‘numeric’)”.

Let’s assume that we want to make predictions based on our data using the predict() function. Then, we might try to use the predict function as shown below:

predict(data_test$x, data_test)           # Try to predict values for test data
# Error in UseMethod("predict") : 
#   no applicable method for 'predict' applied to an object of class "c('double', 'numeric')"

Unfortunately, the previous R code has returned the Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “c(‘double’, ‘numeric’)”.

This is because we have inserted a numeric column as first argument to the predict function instead of a model object.

Let’s solve this problem!

 

Example 2: Fix the Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “c(‘double’, ‘numeric’)”

Example 2 shows how to debug the Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “c(‘double’, ‘numeric’)”.

For this, we first have to estimate a linear regression model based on our train data:

mod <- lm(y ~ x, data_train)              # Estimate linear regression model
summary(mod)                              # Summary of linear regression model
# Call:
# lm(formula = y ~ x, data = data_train)
# 
# Residuals:
#      Min       1Q   Median       3Q      Max 
# -1.79523 -1.09487  0.05202  0.53017  2.10266 
# 
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)
# (Intercept) -0.05067    0.44588  -0.114    0.912
# x            0.11632    0.47348   0.246    0.812
# 
# Residual standard error: 1.384 on 8 degrees of freedom
# Multiple R-squared:  0.007488,	Adjusted R-squared:  -0.1166 
# F-statistic: 0.06035 on 1 and 8 DF,  p-value: 0.8121

Next, we can apply the predict function to our model output and to our test data:

predict(mod, data_test)                   # Properly to predict values for test data
#            1            2            3            4            5            6 
#  0.033310908 -0.071341113 -0.067580482 -0.048135709 -0.151354152 -0.159618208 
#            7            8            9           10 
#  0.019528408 -0.160007711  0.003183706  0.035468402

This time, the predict function worked perfectly.

Note that this error message may appear with small modifications. For instance, the error message Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “train” occurrs when applying the predict function to a train object created by the caret package (see here), and the error message Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “c(‘elnet’, ‘glmnet’)” is returned when applying the predict function to an elnet object (see here).

 

Video & Further Resources

Have a look at the following video which I have published on my YouTube channel. In the video, I’m explaining the R syntax of this tutorial.

 

The YouTube video will be added soon.

 

Furthermore, you might have a look at the other articles on my homepage. You can find a selection of tutorials below:

 

To summarize: At this point you should know how to reproduce and fix the Error in UseMethod(“predict”) : no applicable method for ‘predict’ applied to an object of class “c(‘double’, ‘numeric’)” in the R programming language. Please let me know in the comments section, in case you have any further questions and/or comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top