Because you are using train$
during model fitting. Try:
results <- lmer(y ~ factor(x1) + (1|factor(x2)), data = train)
predicted <- predict(results, newdata=test)
Don't use any of the following:
results <- lmer(train$y ~ factor(train$x1) + (1|factor(train$x2)))
results <- lmer(train$y ~ factor(train$x1) + (1|factor(train$x2)), data = train)
This issue is not exclusive to lmer
. For lm
, glm
, the same would happen.