R's `randomForest` predicts constant value (but correctly differentiates OOB) I created a simple data.frame called dataset with 200 rows and two columns predictor and outcome:

*

*The first 100 rows contain predictor="a" and outcome=1.

*The next 100 rows contain predictor="b" and outcome=2.

I then fit a regression randomForest on this data. The OOB predictions are correct (1 for the first 100 rows, 2 for the others). But when I call predict, then I get constant predictions of 1, whether I feed "a" or "b" in!
What am I doing wrong?
I am running R 4.1.2 and randomForest_4.6-14. R code below:
library(randomForest)
n_per_class <- 100

dataset <- data.frame(
    outcome=c(rep(1,n_per_class),rep(2,n_per_class)),
    predictor=c(rep("a",n_per_class),rep("b",n_per_class)))

model <- randomForest(outcome~predictor,data=dataset)

by(predict(model),dataset$predictor,mean)   # yields 1 and 2

predict(model,newdata=data.frame(predictor="a"))    # yields 1
predict(model,newdata=data.frame(predictor="b"))    # yields 1, too!

Incidentally, the same happens if I use 1,000 or 10,000 data points for each class, or if I replace the constant values 1 and 2 by uniformly distributed random variables in $[0,1]$ and $[1,2]$, respectively.

It seems it's related to passing only 1 row in newdata, as
predict(model,newdata=data.frame(predictor=c("b","a")))
works fine.
However, my guess is that the real issue is in your predictor variable, in that you didn't code it as a factor. If you do that then
predict(model,data.frame(predictor=factor("a",levels=c("a","b"))))
predict(model,data.frame(predictor=factor("b",levels=c("a","b"))))

predicts right.
