stan-dev / rstanarm

rstanarm R package for Bayesian applied regression modeling
https://mc-stan.org/rstanarm
GNU General Public License v3.0
385 stars 132 forks source link

`kfold()` does not work for `stan_polr()` fit #564

Open fweber144 opened 2 years ago

fweber144 commented 2 years ago

Summary:

There seems to be a bug in kfold.stanreg() when applied to a stan_polr() fit.

Description:

When calling kfold.stanreg() on a stan_polr() fit, the error

Error in x$z[omitted_k, , drop = FALSE] : incorrect number of dimensions

is thrown. This is probably due to the fact that in lines https://github.com/stan-dev/rstanarm/blob/3dab09cabb4c889ae9fab2f98351757bfb267a84/R/loo-kfold.R#L196, https://github.com/stan-dev/rstanarm/blob/3dab09cabb4c889ae9fab2f98351757bfb267a84/R/loo-kfold.R#L219, and https://github.com/stan-dev/rstanarm/blob/3dab09cabb4c889ae9fab2f98351757bfb267a84/R/loo-kfold.R#L241, x$z is expanded to x$zeta.

Reproducible Steps:

options(mc.cores = parallel::detectCores(logical = FALSE))
data(inhaler, package = "brms")
inhaler$rating <- paste0("rtg", inhaler$rating)
library(rstanarm)
rfit <- stan_polr(rating ~ period + carry + treat,
                  data = inhaler,
                  prior = R2(location = 0.5, what = "median"),
                  seed = 1140350788)
rkfold <- kfold(rfit, K = 2, cores = 1)

RStanARM Version:

2.21.3

R Version:

4.1.3

Operating System:

Ubuntu 20.04.4 LTS