UseMethod(predict) : no applicable method for 'predict' applied to an object of class train
I think I found why this happened...The predict
is a generic function from: stats
package. I use the namespace ::
-notation for invoking the functions from the caret
package (that is the recommendation for creating a user packages) and the equivalent predict
function from caret
package is: predict.train
, that is an internal function, that cannot be invoked by an external application. The only way to invoke this function, is using the generic predict
function from stats
package, then based on the class of the first input argument: predicted <- predict(fit, testData[-$Readmit])
it identifies the particular predict
function will be invoked.
For this particular case the class of this function is train
, so it would call actually the function: train.predict
from caret
package. This function also handles the particular function requested for prediction based on the algorithm (method) used, for example: predict.gbm
or predict.glm
, etc. It is explained, in detail, in the caret documentation section: "5.7 Extracting Predictions and Class Probabilities".
Therefore the ::
-notation works well for other functions in the package, such as: caret.train
for example, but not for this particular one: predict
. In such cases it is necessary to explicitly load the library, so it internally can invoke predict.train
function.
In short, the solution is just adding the following line before invoking the predict
function:
library(caret)
Then error disappears.
LinearRegression Predict: No applicable method for 'predict' applied to an object of class data.Frame
That's because you used:
model_fit <- lm(diff ~ ., data = trainingData, method = "model.frame")
class(model_fit)
[1] "data.frame"
The above gives you the model matrix used to fit the data.
You can do instead:
model_fit <- lm(diff ~ ., data = trainingData,model=TRUE)
newdata <- data.frame(
f.mean_slope = c(1,2,3)
)
distPred <- predict(model_fit, newdata)
And the model matrix can be found in model_fit$model
No applicable method for 'predict' applied to an object of class c('ksvm', 'vm') for iml::Predictor in R
You don't need to select the model$finalModel (do you have a typo in that line? You have $finalMode - no l). You run a line such as:
pred <- predict(model, newdata, type = "prob")
and Caret will automatically employ the model with best score. The output will give you complementary probabilities for diabetes (column 1) or not (column 2) if you select type = "prob". If you want a specific model from the caret 'model' object, then I believe you can pick it out (from your previous folds question) - but I've never done it and am not sure how.
For your partial dependency plot, well, I use the pdp package, so something like this should work:
library(pdp)
varname = 'X1' # Change this to whatever your first variable is called, or subsequently variables you are interested in.
partial(model, pred.var = varname,
train = X, chull=T, prob = T, progress = "text")
where X is the data you trained your model on (X in your case I think?)
R function error, no applicable method for 'predict' applied to an object of class NULL
The issue is probably at for (i in 1:N-1)
R interprets 1:N-1
as sequence 1 to N, then subtract 1 from the whole vector.
Try 1:5-1
to see what I mean.
Try for (i in 1:(N-1))
Also in this line
ee[[i]] = ks.test(predict(dd[[i]]), predict(dd[[i+1]])) ## this line may also have problem.
dd[[i+1]]
will not be defined yet, as your loop has only filled dd
to i
.
Not sure this is what you are trying to do but maybe:
if(i>1L)ee[[i]] = ks.test(predict(dd[[i-1]]), predict(dd[[i]]))
That way after you have filled the first entry of dd
you start testing the current dd
vs. the previous.
Error in UseMethod(predict) : no applicable method for 'predict' applied to an object of class c('double', 'numeric')
I think I found it. This seems to work, but not sure it his what you expect. In plot.bias
I changed your use of calc.bias
, (i.e. calc.bias(f, polyfit, x)
instead of calc.bias(f, polydeg, x)
). The whole code I used:
library(PolynomF)
f <- function(x) sin(x-5)/(x-5)
# From which we can sample datasets:
N <- 20
x <- runif(N,0,15)
t <- f(x) + rnorm(N, sd=0.1)
calc.bias <- function (f, polyfit, point) {
predictions <- numeric(0)
print(class(point))
for (i in 1:100)
{
x <- runif(N, 0, 15)
t <- f(x) + rnorm(N, sd=0.2)
d <- data.frame(point)
add <- predict(polyfit, newdata = data.frame(point))
predictions <- c(predictions, add)
}
return((f(point)-mean(predictions))^2)
}
plot.bias <- function (f, polydeg) {
plot(data.frame(x, t))
curve(f, type="l", col="green", add=TRUE)
polyfit <- lm(t ~ poly(x, polydeg, raw=TRUE))
p <- polynom(coef(polyfit))
curve(p, col="red", add=TRUE)
points(x, calc.bias(f, polyfit, x), col="blue")
abline(h=0, col='blue')
}
plot.bias(f, 1)
Related Topics
Apply a Function to All Variables Starting with Specific Pattern in R
Outputing N Tables in Shiny, Where N Depends on the Data
How to Write Data from R to Postgresql Tables with an Autoincrementing Primary Key
Directlabels: Avoid Clipping (Like Xpd=True)
How to Create a Pie Chart with Percentage Labels Using Ggplot2
Why Is Date Is Being Returned as Type 'Double'
Space Between Gpplot2 Horizontal Legend Elements
Split Concatenated Column to Corresponding Column Positions
Efficiently Counting Non-Na Elements in Data.Table
R Dplyr Filter Based on Matching Search Term with First Words of Any Work in Select Columns
Changing the Appearance of Facet Labels Size
Let Each Plot in Facet_Grid Have Its Own Y-Axis Value
Separate Ordering in Ggplot Facets
How to Reference Column Names That Start with a Number, in Data.Table