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

On this page you’ll learn 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 R.

Creation of Example Data

data(iris)                                      # Create train and test data frames
set.seed(785683)
iris_train <- iris[sample(1:nrow(iris), 50), ]
head(iris_train)
#     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
# 22           5.1         3.7          1.5         0.4     setosa
# 64           6.1         2.9          4.7         1.4 versicolor
# 116          6.4         3.2          5.3         2.3  virginica
# 142          6.9         3.1          5.1         2.3  virginica
# 73           6.3         2.5          4.9         1.5 versicolor
# 13           4.8         3.0          1.4         0.1     setosa
iris_test <- iris[sample(1:nrow(iris), 50), ]
head(iris_test)
#     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
# 101          6.3         3.3          6.0         2.5  virginica
# 29           5.2         3.4          1.4         0.2     setosa
# 20           5.1         3.8          1.5         0.3     setosa
# 84           6.0         2.7          5.1         1.6 versicolor
# 60           5.2         2.7          3.9         1.4 versicolor
# 10           4.9         3.1          1.5         0.1     setosa

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

predict(iris_test$Sepal.Length, iris_test)      # predict function does not work
# Error in UseMethod("predict") : 
#   no applicable method for 'predict' applied to an object of class "c('double', 'numeric')"

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

iris_mod <- lm(Sepal.Length ~ ., iris_train)    # Estimating a linear model using lm()
predict(iris_mod, iris_test)                    # Applying predict() function
#      101       29       20       84       60       10      145      113 
# 7.087006 4.952600 5.189978 6.584670 5.783520 4.890242 6.887789 6.619792 
#       97       92      108       26      140      111        1       22 
# 6.065013 6.374984 7.102409 4.916580 6.595594 6.436444 4.994808 5.149911 
#      133       18       68       37      144       73       83       13 
# 6.603922 4.996949 5.907770 4.928403 6.974111 6.365302 5.779239 4.781628 
#        3      143       82       72       93        7       65      142 
# 4.801778 6.223264 5.515523 5.889994 5.803437 4.954741 5.666579 6.400657 
#       40       45       99      137      141       87      115       30 
# 5.019006 5.457742 5.095031 6.861451 6.734826 6.485739 6.276174 5.000996 
#       94       89       71       57      131       50       54      122 
# 5.207692 6.040815 6.600773 6.572295 6.929530 4.910392 5.678953 6.134801 
#       27       78 
# 5.089692 6.647028

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.
You need to agree with the terms to proceed

Menu
Top