richarddmorey / BayesFactor

BayesFactor R package for Bayesian data analysis with common statistical models.
https://richarddmorey.github.io/BayesFactor/
131 stars 49 forks source link

regressionBF : declare the max number of covariate? #114

Closed sarahdata closed 6 years ago

sarahdata commented 6 years ago

Hello,

I'd like to constrain the number of covariate used in : regressionBF (Y~., data, whichModels="all"), I only want 2 covariate maximum in each model. I looked arount the manual without success. Does the option exists ? I tried by doing for loop and lmBF but unsucessfully too because 1/ data have to come from the same dataframe to be comparable and 2/ the exact covariate name need to be written as the argument.

Thank you,

Sarah

richarddmorey commented 6 years ago

Hi Sarah,

Here's some code to try, using the attitude data set in R as an example.

library(BayesFactor)

data(attitude)
# list column names
cols = colnames(attitude)
# get columns that are not the DV
x.cols = cols[!(cols %in% "rating")]

# Get all combinations
cm = combn(x.cols, m = 2)

bfs = apply(cm, 2, function(v){
  # Construct right hand side
  ivs = paste(v, collapse = " + ")
  # construct formula
  fmla = as.formula(paste0("rating ~ ", ivs ))
  # compute BF
  lmBF(formula = fmla, data = attitude)
})

# Paste together in a BayesFactor object
bfs = do.call(c, bfs)
sarahdata commented 6 years ago

Thanks a lot ! I was in a hurry (deadline for a review) so I did something else (creating dataframes of Y, X1, X2 in a loop and applying lmBF to each dataframe, comparing the BF manually...) but your solution is exactly what I was looking for. I could use for more practice with R... Thanks again et thanks for the package. Sarah