tldr
With brms 2.8.0, I now get the following error message when using kfold()
Error: New factor levels are not allowed.
Levels allowed: '1', '2', '4', '5', '7', '9', '10', '11', '12', '13', '14', '15', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48'
In the past when I used kfold()
on this exact model, I didn’t get an error. What’s changed and how should I be using the function?
Here are some details
The model is the first in McElreath’s chapter 12. Here’s the code.
# load the packages and get the data
library(tidyverse)
library(rethinking)
data(reedfrogs)
d <- reedfrogs
rm(reedfrogs)
detach(package:rethinking, unload = T)
library(brms)
# adjust the data
d <-
d %>%
mutate(tank = 1:nrow(d))
# fit the model
b12.1 <-
brm(data = d, family = binomial,
surv | trials(density) ~ 0 + factor(tank),
prior(normal(0, 5), class = b),
iter = 2000, warmup = 500, chains = 4, cores = 4,
seed = 12)
If you use the loo()
function, you get a warning.
Found 45 observations with a pareto_k > 0.7 in model 'b12.1'. With this many problematic observations, it may be more appropriate to use 'kfold' with argument 'K = 10' to perform 10-fold cross-validation rather than LOO.
In prior versions of brms kfold(b12.1, K = 10)
worked fine. Now I get the error message from above. I thought maybe since I was using 0 + factor(tank)
to fit tank
-specific intercepts that a adding a group
argument would be the answer. However, when I try kfold(b12.1, group = "factor(tank)", cores = 4)
, I get this warning.
Error: New factor levels are not allowed.
Levels allowed: '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48'
Levels found: '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48'
What am I missing?