Error:Please run kfold with 'save_fits = TRUE'

Hello,
I use kfold function first as
kf<-brms::kfold(model_test,K=5,save_fits=TRUE,folds=hh,compare=TRUE)
and then I use
kp<-brms::kfold_predict(kf,method=“predict”)
But it turns out Error: Slots ‘fits’ and ‘data’ are required. Please run kfold with ‘save_fits = TRUE’.

What’s wrong with my code? Thanks!

I would need to see a minimal reproducible example of your problem.

Hello, here is a example. Thank you Paul!

s<-data.frame(
f1=c(3.85,0.88,5.09,-2.60,4.00,2.96,5.51,-0.14,2.27,3.96),
f2=c(-0.25,0.24,-0.38,0.34,0.23,-0.14,-0.37,-6.70,0.16,0.27),
f3=c(-1.66,0.44,-1.79,0.01,-1.43,-0.06,-1.19,0.56,-0.46,-0.26),
f4=c(0.90,1.05,-0.37,0.80,0.45,-0.23,-1.24,-0.25,-0.33,-1.45),
f5=c(-1.83,1.45,-0.66,-1.37,0.21,0.16,-1.22,1.92,0.39,0.22),
f6=c(-1.00,1.30,0.24,0.92,0.05,0.21,-0.68,-1.30,0.07,-1.07),
y=c(0,1,1,0,1,0,1,1,0,1)
);
#data size
N<-10 #sample size
K<-6 #number of predictors
n_fold<-5 #number of folds

#create 5 folds of data
library(rstan)
library(loo)
hh<-kfold_split_stratified(K=n_fold,x=s$y)
#==============using rstanarm package
library(rstanarm)
model_test <- stan_glm(y~f1+f2+f3+f4+f5+f6,
data=s, family = binomial(link = “logit”),prior=(normal(0,10000)),iter=1000,chains=1)

kf<-brms::kfold(model_test,K=5,save_fits=TRUE,folds=hh,compare=TRUE)
kp<-brms::kfold_predict(kf,method=“predict”)

Hello! I change the model specification by brms package, then it works. It seems that kfold can only act on brmsfit object… Thanks…

model_test<-brms::brm(y~f1+f2+f3+f4+f5+f6,
data=s,family=bernoulli,warmup=1000,iter=5000,chains=1)

But I have another question…
If I select method as ‘predicted’, does the function kfold_predict will predict use testing data in each fold, and if I select ‘fitted’, it will predict using training data in each fold? Thanks!

Kfold is generic that is it will work differently for different objects. Try out the kfold method in rstanarm for rstanarm objects.