# Error message from projpred with binomial regression model fitted in rstanarm

**URL:** <https://discourse.mc-stan.org/t/error-message-from-projpred-with-binomial-regression-model-fitted-in-rstanarm/14856>\
**Category:** Modeling\
**Tags:** rstanarm, projpred\
**Created:** [May 5, 2020, 6:53am UTC](https://discourse.mc-stan.org/t/error-message-from-projpred-with-binomial-regression-model-fitted-in-rstanarm/14856 "2020-05-05T06:53:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vianeylb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/vianeylb/32/6195_2.png) [@vianeylb](https://discourse.mc-stan.org/u/vianeylb)\
**Post date:** [May 5, 2020, 6:53am UTC](https://discourse.mc-stan.org/t/error-message-from-projpred-with-binomial-regression-model-fitted-in-rstanarm/14856/1 "2020-05-05T06:53:49Z")

</div>

I’m running into an error message that I don’t understand and can’t find an answer for online when running a binomial regression model using `rstanarm` and then trying to use some functions from the R package `projpred`. I can’t share the data, but my best guess is that there’s a minor issue with the data I’m using that’s causing some issues. I’m following the approach detailed here: [https://mc-stan.org/projpred/articles/quickstart.html](https://mc-stan.org/projpred/articles/quickstart.html)

The model runs fine:

* * *

```stan
n <- 227 
D <- 27
p0 <- 5 # prior guess for the number of relevant variables
tau0 <- p0/(D-p0) * 1/sqrt(n) 
prior_coeff <- hs(global_scale = tau0, slab_scale = 1) # regularized horseshoe prior
binregcov <- stan_glm(formula = cbind(PresenceTotal, NumberOfSamples - PresenceTotal)~., family = binomial(link="logit"), prior = prior_coeff, 
                      data = NTMgrouped%>%dplyr::select(-landcover3))

```

* * *

But then once I try:

```stan
cvs <- cv_varsel(binregcov, method='forward', cv_method='kfold', K=5)

```

I get the following error:

```stan
STATS is longer than the extent of 'dim(x)[MARGIN]'Error in dimnames(x) <- dn : 
  length of 'dimnames' [2] not equal to array extent

```

There are only 227 observations and 27 covariate values, some categorical variables, etc. I can run the examples provided here [https://mc-stan.org/projpred/articles/quickstart.html](https://mc-stan.org/projpred/articles/quickstart.html), but it’s not quite working out with my data. Perhaps it’s not the best data set for this, but I wanted to try it out anyway.

Has anyone come across this error message before?

---

<div class="post-metadata">

**Author:** ![vianeylb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/vianeylb/32/6195_2.png) [@vianeylb](https://discourse.mc-stan.org/u/vianeylb)\
**Post date:** [May 5, 2020, 7:36am UTC](https://discourse.mc-stan.org/t/error-message-from-projpred-with-binomial-regression-model-fitted-in-rstanarm/14856/2 "2020-05-05T07:36:16Z")

</div>

I’ll add that I’ve run the model using `brms` and used the `kfold` function in `loo`, and it seems to work fine. Hopefully what I’ve been doing so far has been correct. I haven’t used the Stan affiliated R packages too often.

```stan
binregcov <- brm(data = NTMgrouped%>%dplyr::select(-landcover3), formula = PresenceTotal| trials(NumberOfSamples)~ ., family = "binomial", prior = prior(normal(0, 1), class = b), control = list(max_treedepth = 15))
kfold(binregcov, K=5)

```

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 5, 2020, 3:00pm UTC](https://discourse.mc-stan.org/t/error-message-from-projpred-with-binomial-regression-model-fitted-in-rstanarm/14856/3 "2020-05-05T15:00:58Z")

</div>

It sounds like a bug in projpred.

---

<div class="post-metadata">

**Author:** ![vianeylb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/vianeylb/32/6195_2.png) [@vianeylb](https://discourse.mc-stan.org/u/vianeylb)\
**Post date:** [May 5, 2020, 8:27pm UTC](https://discourse.mc-stan.org/t/error-message-from-projpred-with-binomial-regression-model-fitted-in-rstanarm/14856/4 "2020-05-05T20:27:26Z")

</div>

Ah, okay!

---

<div class="post-metadata">

**Author:** ![vianeylb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/vianeylb/32/6195_2.png) [@vianeylb](https://discourse.mc-stan.org/u/vianeylb)\
**Post date:** [May 5, 2020, 8:48pm UTC](https://discourse.mc-stan.org/t/error-message-from-projpred-with-binomial-regression-model-fitted-in-rstanarm/14856/5 "2020-05-05T20:48:49Z")

</div>

Figured it out (hopefully). In my data argument in `stan_glm()`, I specify

```stan
data = NTMgrouped%>%dplyr::select(-landcover3)

```

but that data frame has `NA`s for some of the covariate values, which `rstanarm` removes (so a few rows of the data set are removed automatically). However, it seems to cause the issue when trying to use `projpred`. At least it’s working now that I’ve removed the rows with `NA`s.
