merliseclyde / BAS

BAS R package https://merliseclyde.github.io/BAS/
https://merliseclyde.github.io/BAS/
GNU General Public License v3.0
41 stars 16 forks source link

non-conformable arrays error when confint is used on a BMA output of predict.bas #6

Closed muraiki closed 7 years ago

muraiki commented 7 years ago
library(BAS)
set.seed(42)

d <- data.frame(
  likes.cats = c(rep("yes", 60), rep("no", 40)),
  likes.dogs = c(rep("yes", 30), rep("no", 70)),
  fish.eaten.per.year = c(rnorm(100, mean=20, sd=5)),
  donated.to.cats = c(rnorm(50, mean=50, sd=10), rep(0, 50))
)

m <- bas.lm(donated.to.cats ~ likes.cats + likes.dogs + fish.eaten.per.year,
            data=d, prior="BIC", modelprior=uniform())

nd <- data.frame("yes", "no", 100, 50)
colnames(nd) <- colnames(d)

dnd <- rbind(d, nd)

fit <- predict(m, dnd[101,], se.fit=TRUE)
confint(fit)

Output is:

Error in pred * sd : non-conformable arrays

This only happens when predict is used with estimator = "BMA", which is the default.

merliseclyde commented 7 years ago

This should now be fixed in the current version here on github (problem I believe with predict using a new dataframe with one row)

library(devtools)
install_github("merliseclyde/BAS")
library(BAS)
set.seed(42)

d <- data.frame(
  likes.cats = c(rep("yes", 60), rep("no", 40)),
  likes.dogs = c(rep("yes", 30), rep("no", 70)),
  fish.eaten.per.year = c(rnorm(100, mean=20, sd=5)),
  donated.to.cats = c(rnorm(50, mean=50, sd=10), rep(0, 50))
)

m <- bas.lm(donated.to.cats ~ likes.cats + likes.dogs + fish.eaten.per.year,
            data=d, prior="BIC", modelprior=uniform())

nd <- data.frame("yes", "no", 100, 50)
colnames(nd) <- colnames(d)

dnd <- rbind(d, nd)

fit <- predict(m, dnd[101,], se.fit=TRUE)
confint(fit)

produces output

          2.5  %  97.5  %     pred
[1,] 0.005899873 66.47635 33.35064
attr(,"Probability")
[1] 0.95
attr(,"class")
[1] "confint.bas"