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

Error by `log_lik()` in case of observation weights #567

Open fweber144 opened 2 years ago

fweber144 commented 2 years ago

Summary:

The log_lik.stanreg() method fails when observation weights exist.

Description:

As shown in the reproducible steps below, calling log_lik() in a situation where observation weights exist throws an error. In the example below, I specified argument weights in the call to log_lik() even though it does not exist (and the log_lik.stanreg() documentation for ... says "Currently ignored"). However, omitting argument weights in the log_lik() call leads to the same error.

Reproducible Steps:

data(df_gaussian, package = "projpred")
dat <- data.frame(y = df_gaussian$y, df_gaussian$x)
library(rstanarm)
options(mc.cores = parallel::detectCores(logical = FALSE))
set.seed(7352)
wobs <- sample.int(2, size = nrow(dat), replace = TRUE)
rfit <- stan_glm(y ~ X1 + X2 + X3 + X4 + X5,
                 data = dat,
                 weights = wobs,
                 seed = 1140350788)
nobs_new <- 3
dat_new <- setNames(as.data.frame(matrix(rnorm(nobs_new * 5), nrow = nobs_new)),
                    paste0("X", 1:5))
dat_new$y <- rnorm(nobs_new)
wobs_new <- sample.int(2, size = nobs_new, replace = TRUE)
ll_new <- log_lik(rfit,
                  newdata = dat_new,
                  weights = wobs_new)
## Throws:
# Error in `$<-.data.frame`(`*tmp*`, "weights", value = c(2L, 1L, 2L, 2L,  :
#   replacement has 100 rows, data has 3
##

RStanARM Version:

2.21.3

R Version:

4.2.1

Operating System:

Ubuntu 22.04 LTS