About

This notebook tries out some of the Bayesian Model Averaging (BMA) packages for R. For a nice introduction, see this paper. https://journals.sagepub.com/doi/full/10.1177/2515245919898657

To find packages, I searched at MetaCRAN: https://www.r-pkg.org/search.html?q=bayesian+model+averaging

There are 100s of packages. I chose 3 that are made for OLS BMA, the simplest case.

Of the three here, BMA is probably the best. Like BMS, it fits instantly, and provides lots of useful output. BAS is very slow. BMA has the advantage over BMS that it supports factor variables natively, for BMS one has to recode them manually. This solution results in a lack of group-wise selection, i.e., all dummy variables are not included or excluded together.

Init

options(
  digits = 3
)

library(pacman)
p_load(
  kirkegaard,
  BMA,
  BMS,
  BAS,
  formula.tools #easy formula use
)

Data

We use the mpg dataset for this. It has a mix of numerical and categorical variables. We pick a numeric one, displ, as the outcome.

mpg
#split the transmission variable
mpg %<>% mutate(
  trans_auto = str_detect(trans, "auto"),
  trans_type = str_match(trans, "\\((\\w+)")[, 2]
)

#change chr to fct
for (v in names(mpg)) {
  if (is.character(mpg[[v]])) mpg[[v]] = mpg[[v]] %>% as.factor()
}

#summarize variables
map(mpg, function(x) {
  if (is.numeric(x)) {
    return(psych::describe(x))
  } else {
    return(table2(x))
  }
})
## $manufacturer
## # A tibble: 16 x 3
##    Group      Count Percent
##    <chr>      <dbl>   <dbl>
##  1 dodge         37   15.8 
##  2 toyota        34   14.5 
##  3 volkswagen    27   11.5 
##  4 ford          25   10.7 
##  5 chevrolet     19    8.12
##  6 audi          18    7.69
##  7 hyundai       14    5.98
##  8 subaru        14    5.98
##  9 nissan        13    5.56
## 10 honda          9    3.85
## 11 jeep           8    3.42
## 12 pontiac        5    2.14
## 13 land rover     4    1.71
## 14 mercury        4    1.71
## 15 lincoln        3    1.28
## 16 <NA>           0    0   
## 
## $model
## # A tibble: 39 x 3
##    Group               Count Percent
##    <chr>               <dbl>   <dbl>
##  1 caravan 2wd            11    4.70
##  2 ram 1500 pickup 4wd    10    4.27
##  3 civic                   9    3.85
##  4 dakota pickup 4wd       9    3.85
##  5 jetta                   9    3.85
##  6 mustang                 9    3.85
##  7 a4 quattro              8    3.42
##  8 grand cherokee 4wd      8    3.42
##  9 impreza awd             8    3.42
## 10 a4                      7    2.99
## # … with 29 more rows
## 
## $displ
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 234 3.47 1.29    3.3    3.39 1.33 1.6   7   5.4 0.44    -0.91 0.08
## 
## $year
##    vars   n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 234 2004 4.51   2004    2004 6.67 1999 2008     9    0    -2.01 0.29
## 
## $cyl
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 234 5.89 1.61      6    5.86 2.97   4   8     4 0.11    -1.46 0.11
## 
## $trans
## # A tibble: 11 x 3
##    Group      Count Percent
##    <chr>      <dbl>   <dbl>
##  1 auto(l4)      83  35.5  
##  2 manual(m5)    58  24.8  
##  3 auto(l5)      39  16.7  
##  4 manual(m6)    19   8.12 
##  5 auto(s6)      16   6.84 
##  6 auto(l6)       6   2.56 
##  7 auto(av)       5   2.14 
##  8 auto(s4)       3   1.28 
##  9 auto(s5)       3   1.28 
## 10 auto(l3)       2   0.855
## 11 <NA>           0   0    
## 
## $drv
## # A tibble: 4 x 3
##   Group Count Percent
##   <chr> <dbl>   <dbl>
## 1 f       106    45.3
## 2 4       103    44.0
## 3 r        25    10.7
## 4 <NA>      0     0  
## 
## $cty
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 234 16.9 4.26     17    16.6 4.45   9  35    26 0.79     1.43 0.28
## 
## $hwy
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 234 23.4 5.95     24    23.2 7.41  12  44    32 0.36     0.14 0.39
## 
## $fl
## # A tibble: 6 x 3
##   Group Count Percent
##   <chr> <dbl>   <dbl>
## 1 r       168  71.8  
## 2 p        52  22.2  
## 3 e         8   3.42 
## 4 d         5   2.14 
## 5 c         1   0.427
## 6 <NA>      0   0    
## 
## $class
## # A tibble: 8 x 3
##   Group      Count Percent
##   <chr>      <dbl>   <dbl>
## 1 suv           62   26.5 
## 2 compact       47   20.1 
## 3 midsize       41   17.5 
## 4 subcompact    35   15.0 
## 5 pickup        33   14.1 
## 6 minivan       11    4.70
## 7 2seater        5    2.14
## 8 <NA>           0    0   
## 
## $trans_auto
## # A tibble: 3 x 3
##   Group Count Percent
##   <chr> <dbl>   <dbl>
## 1 TRUE    157    67.1
## 2 FALSE    77    32.9
## 3 <NA>      0     0  
## 
## $trans_type
## # A tibble: 11 x 3
##    Group Count Percent
##    <chr> <dbl>   <dbl>
##  1 l4       83  35.5  
##  2 m5       58  24.8  
##  3 l5       39  16.7  
##  4 m6       19   8.12 
##  5 s6       16   6.84 
##  6 l6        6   2.56 
##  7 av        5   2.14 
##  8 s4        3   1.28 
##  9 s5        3   1.28 
## 10 l3        2   0.855
## 11 <NA>      0   0
#model formula
#got errors when trying all the variables...
#https://github.com/hanase/BMA/issues/4
# model_form = "displ ~ manufacturer + model + year + cyl + drv + cty + hwy + fl + class + trans_auto + trans_type" %>% as.formula()
#but this selection works
model_form = "displ ~ manufacturer + year + cyl + drv + cty + hwy + fl + class + trans" %>% as.formula()

Examples

BMA

#fit
BMA_fit = BMA::bic.glm(model_form, data = mpg, glm.family = "gaussian")

#results
BMA_fit
## 
## Call:
## bic.glm.formula(f = model_form, data = mpg, glm.family = "gaussian")
## 
## 
##  Posterior probabilities(%): 
## manufacturer         year          cyl          drv          cty          hwy 
##        100.0        100.0        100.0        100.0        100.0          6.2 
##           fl        class        trans 
##        100.0        100.0          0.0 
## 
##  Coefficient posterior expected values: 
##            (Intercept)   manufacturerchevrolet       manufacturerdodge  
##              -3.13e+01                1.10e+00                7.90e-01  
##       manufacturerford       manufacturerhonda     manufacturerhyundai  
##               7.59e-01                2.89e-01                3.55e-01  
##       manufacturerjeep  manufacturerland rover     manufacturerlincoln  
##               6.57e-01               -8.50e-02                8.95e-01  
##    manufacturermercury      manufacturernissan     manufacturerpontiac  
##               6.42e-01                6.67e-01                1.05e+00  
##     manufacturersubaru      manufacturertoyota  manufacturervolkswagen  
##               5.02e-01                5.91e-01                3.14e-01  
##                   year                     cyl                    drvf  
##               1.64e-02                4.79e-01               -1.57e-01  
##                   drvr                     cty                     hwy  
##               1.60e-01               -4.60e-02                2.25e-04  
##                    fld                     fle                     flp  
##               4.39e-01               -4.86e-01                1.18e-01  
##                    flr            classcompact            classmidsize  
##              -1.78e-02               -8.42e-01               -8.87e-01  
##           classminivan             classpickup         classsubcompact  
##              -8.79e-01               -6.78e-01               -8.66e-01  
##               classsuv           transauto(l3)           transauto(l4)  
##              -6.12e-01                0.00e+00                0.00e+00  
##          transauto(l5)           transauto(l6)           transauto(s4)  
##               0.00e+00                0.00e+00                0.00e+00  
##          transauto(s5)           transauto(s6)         transmanual(m5)  
##               0.00e+00                0.00e+00                0.00e+00  
##        transmanual(m6)  
##               0.00e+00
#summary
BMA_fit %>% summary()
## 
## Call:
## bic.glm.formula(f = model_form, data = mpg, glm.family = "gaussian")
## 
## 
##   2  models were selected
##  Best  2  models (cumulative posterior probability =  1 ): 
## 
##                          p!=0    EV        SD       model 1    model 2  
## Intercept                100    -3.13e+01  9.32242  -3.13e+01  -3.08e+01
## manufacturer             100.0                                          
##             .chevrolet           1.10e+00  0.15955   1.10e+00   1.11e+00
##             .dodge               7.90e-01  0.14821   7.89e-01   7.97e-01
##             .ford                7.59e-01  0.14317   7.59e-01   7.67e-01
##             .honda               2.89e-01  0.18521   2.89e-01   2.98e-01
##             .hyundai             3.55e-01  0.15858   3.54e-01   3.64e-01
##             .jeep                6.57e-01  0.16197   6.56e-01   6.66e-01
##             .land rover         -8.50e-02  0.18564  -8.54e-02  -8.02e-02
##             .lincoln             8.95e-01  0.23851   8.94e-01   9.03e-01
##             .mercury             6.42e-01  0.19266   6.42e-01   6.50e-01
##             .nissan              6.67e-01  0.13560   6.66e-01   6.77e-01
##             .pontiac             1.05e+00  0.18075   1.05e+00   1.06e+00
##             .subaru              5.02e-01  0.12956   5.01e-01   5.07e-01
##             .toyota              5.91e-01  0.12616   5.90e-01   6.00e-01
##             .volkswagen          3.14e-01  0.12018   3.13e-01   3.22e-01
## year                     100.0   1.64e-02  0.00471   1.64e-02   1.61e-02
## cyl                      100.0   4.79e-01  0.02684   4.79e-01   4.80e-01
## drv                      100.0                                          
##    .f                           -1.57e-01  0.11059  -1.57e-01  -1.64e-01
##    .r                            1.60e-01  0.11141   1.60e-01   1.54e-01
## cty                      100.0  -4.60e-02  0.01464  -4.58e-02  -4.92e-02
## hwy                        6.2   2.25e-04  0.00500      .       3.61e-03
## fl                       100.0                                          
##   .d                             4.39e-01  0.36101   4.38e-01   4.44e-01
##   .e                            -4.86e-01  0.32638  -4.87e-01  -4.68e-01
##   .p                             1.18e-01  0.30998   1.18e-01   1.33e-01
##   .r                            -1.78e-02  0.30733  -1.87e-02  -5.20e-03
## class                    100.0                                          
##      .compact                   -8.42e-01  0.20270  -8.43e-01  -8.31e-01
##      .midsize                   -8.87e-01  0.19355  -8.88e-01  -8.79e-01
##      .minivan                   -8.79e-01  0.24365  -8.80e-01  -8.62e-01
##      .pickup                    -6.78e-01  0.21012  -6.79e-01  -6.60e-01
##      .subcompact                -8.66e-01  0.19635  -8.66e-01  -8.55e-01
##      .suv                       -6.12e-01  0.18984  -6.13e-01  -5.96e-01
## trans                      0.0                                          
##      .auto(l3)                   0.00e+00  0.00000      .          .    
##      .auto(l4)                   0.00e+00  0.00000      .          .    
##      .auto(l5)                   0.00e+00  0.00000      .          .    
##      .auto(l6)                   0.00e+00  0.00000      .          .    
##      .auto(s4)                   0.00e+00  0.00000      .          .    
##      .auto(s5)                   0.00e+00  0.00000      .          .    
##      .auto(s6)                   0.00e+00  0.00000      .          .    
##      .manual(m5)                 0.00e+00  0.00000      .          .    
##      .manual(m6)                 0.00e+00  0.00000      .          .    
##                                                                         
## nVar                                                   7          8     
## BIC                                                 -8.79e+02  -8.74e+02
## post prob                                            0.938      0.062
#default plots
BMA_fit %>% plot()

#coefs, manually
tibble(
  term = names(BMA_fit$postmean),
  post_mean = BMA_fit$postmean,
  post_sd = BMA_fit$postsd,
  cond_post_mean = BMA_fit$condpostmean,
  cond_post_sd = BMA_fit$condpostsd
) %>% print(n = Inf)
## # A tibble: 40 x 5
##    term                    post_mean post_sd cond_post_mean cond_post_sd
##    <chr>                       <dbl>   <dbl>          <dbl>        <dbl>
##  1 (Intercept)            -31.3      9.32         -31.3          9.32   
##  2 manufacturerchevrolet    1.10     0.160          1.10         0.178  
##  3 manufacturerdodge        0.790    0.148          0.790        0.197  
##  4 manufacturerford         0.759    0.143          0.759        0.417  
##  5 manufacturerhonda        0.289    0.185          0.289        0.433  
##  6 manufacturerhyundai      0.355    0.159          0.355        0.330  
##  7 manufacturerjeep         0.657    0.162          0.657        0.419  
##  8 manufacturerland rover  -0.0850   0.186         -0.0850       0.688  
##  9 manufacturerlincoln      0.895    0.239          0.895        0.311  
## 10 manufacturermercury      0.642    0.193          0.642        0.241  
## 11 manufacturernissan       0.667    0.136          0.667        0.330  
## 12 manufacturerpontiac      1.05     0.181          1.05         1.12   
## 13 manufacturersubaru       0.502    0.130          0.502        0.192  
## 14 manufacturertoyota       0.591    0.126          0.591        0.466  
## 15 manufacturervolkswagen   0.314    0.120          0.314        0.295  
## 16 year                     0.0164   0.00471        0.0164       0.00471
## 17 cyl                      0.479    0.0268         0.479        0.0268 
## 18 drvf                    -0.157    0.111         -0.157        0.137  
## 19 drvr                     0.160    0.111          0.160        0.327  
## 20 cty                     -0.0460   0.0146        -0.0460       0.0146 
## 21 hwy                      0.000225 0.00500        0.00361      0.0197 
## 22 fld                      0.439    0.361          0.439        0.429  
## 23 fle                     -0.486    0.326         -0.486        0.681  
## 24 flp                      0.118    0.310          0.118        0.465  
## 25 flr                     -0.0178   0.307         -0.0178       0.335  
## 26 classcompact            -0.842    0.203         -0.842        0.203  
## 27 classmidsize            -0.887    0.194         -0.887        0.200  
## 28 classminivan            -0.879    0.244         -0.879        0.252  
## 29 classpickup             -0.678    0.210         -0.678        0.269  
## 30 classsubcompact         -0.866    0.196         -0.866        0.202  
## 31 classsuv                -0.612    0.190         -0.612        0.310  
## 32 transauto(l3)            0        0              0            0      
## 33 transauto(l4)            0        0              0            0      
## 34 transauto(l5)            0        0              0            0      
## 35 transauto(l6)            0        0              0            0      
## 36 transauto(s4)            0        0              0            0      
## 37 transauto(s5)            0        0              0            0      
## 38 transauto(s6)            0        0              0            0      
## 39 transmanual(m5)          0        0              0            0      
## 40 transmanual(m6)          0        0              0            0

BMS

#fit
BMS_fit = BMS::bms(X.data = model_form, data = mpg)
## Warning in mean.default(y): argument is not numeric or logical: returning NA
## Error in y - mean(y): non-numeric argument to binary operator
#oh dear, it appears the package does not support non-numerical predictors
#so we make a new formula with only numerical ones
mpg_num_vars = map_dbl(mpg, is.numeric)
model_form_numerical = str_glue("displ ~ {str_c(names(mpg_num_vars[mpg_num_vars==1 & names(mpg_num_vars) != 'displ']), collapse = ' + ')}") %>% as.formula()
model_form_numerical
## displ ~ year + cyl + cty + hwy
## <environment: 0x55e82c1a66b0>
#fit
BMS_fit = BMS::bms(model_form_numerical, data = mpg)
##        PIP Post Mean Post SD Cond.Pos.Sign Idx
## cyl  1.000   0.65365 0.03232             1   2
## hwy  0.758  -0.02272 0.01560             0   4
## year 0.436   0.00596 0.00816             1   1
## cty  0.345  -0.01078 0.02082             0   3
## 
## Mean no. regressors               Draws             Burnins                Time 
##            "2.5385"                "16"                 "0"      "0.00761 secs" 
##  No. models visited      Modelspace 2^K           % visited         % Topmodels 
##                "16"                "16"               "100"               "100" 
##            Corr PMP            No. Obs.         Model Prior             g-Prior 
##                "NA"               "234"        "random / 2"               "UIP" 
##     Shrinkage-Stats 
##         "Av=0.9957" 
## 
## Time difference of 0.00761 secs
#print
BMS_fit
##        PIP Post Mean Post SD Cond.Pos.Sign Idx
## cyl  1.000   0.65365 0.03232             1   2
## hwy  0.758  -0.02272 0.01560             0   4
## year 0.436   0.00596 0.00816             1   1
## cty  0.345  -0.01078 0.02082             0   3
## 
## Mean no. regressors               Draws             Burnins                Time 
##            "2.5385"                "16"                 "0"      "0.00761 secs" 
##  No. models visited      Modelspace 2^K           % visited         % Topmodels 
##                "16"                "16"               "100"               "100" 
##            Corr PMP            No. Obs.         Model Prior             g-Prior 
##                "NA"               "234"        "random / 2"               "UIP" 
##     Shrinkage-Stats 
##         "Av=0.9957"
#summary
BMS_fit %>% summary()
## Mean no. regressors               Draws             Burnins                Time 
##            "2.5385"                "16"                 "0"      "0.00761 secs" 
##  No. models visited      Modelspace 2^K           % visited         % Topmodels 
##                "16"                "16"               "100"               "100" 
##            Corr PMP            No. Obs.         Model Prior             g-Prior 
##                "NA"               "234"        "random / 2"               "UIP" 
##     Shrinkage-Stats 
##         "Av=0.9957"
#default plots
BMS_fit %>% plot()

#coefs
BMS_fit %>% coef()
##        PIP Post Mean Post SD Cond.Pos.Sign Idx
## cyl  1.000   0.65365 0.03232             1   2
## hwy  0.758  -0.02272 0.01560             0   4
## year 0.436   0.00596 0.00816             1   1
## cty  0.345  -0.01078 0.02082             0   3
#alternatively, we can recode all the factors to binary manually
BMS_data = model.matrix(model_form, data = mpg) %>% 
  as.data.frame() %>% 
  bind_cols(displ = mpg$displ)
BMS_form = ("displ ~ `" + str_c(names(BMS_data)[-c(1, 41)], collapse = "` + `") + "`") %>% as.formula()
BMS_form
## displ ~ manufacturerchevrolet + manufacturerdodge + manufacturerford + 
##     manufacturerhonda + manufacturerhyundai + manufacturerjeep + 
##     `manufacturerland rover` + manufacturerlincoln + manufacturermercury + 
##     manufacturernissan + manufacturerpontiac + manufacturersubaru + 
##     manufacturertoyota + manufacturervolkswagen + year + cyl + 
##     drvf + drvr + cty + hwy + fld + fle + flp + flr + classcompact + 
##     classmidsize + classminivan + classpickup + classsubcompact + 
##     classsuv + `transauto(l3)` + `transauto(l4)` + `transauto(l5)` + 
##     `transauto(l6)` + `transauto(s4)` + `transauto(s5)` + `transauto(s6)` + 
##     `transmanual(m5)` + `transmanual(m6)`
## <environment: 0x55e82d26f4f0>
#fit
BMS_fit2 = BMS::bms(BMS_form, data = BMS_data)
##                            PIP Post Mean Post SD Cond.Pos.Sign Idx
## manufacturerchevrolet  1.00000  0.483059 0.10120        1.0000   1
## cyl                    1.00000  0.558532 0.02499        1.0000  16
## drvr                   1.00000  0.439126 0.10019        1.0000  18
## classcompact           1.00000 -0.593211 0.10848        0.0000  25
## classmidsize           1.00000 -0.480104 0.12170        0.0000  26
## classsubcompact        1.00000 -0.603914 0.10511        0.0000  29
## manufacturerland rover 0.87867 -0.570335 0.27114        0.0000   7
## manufacturerpontiac    0.79633  0.452436 0.27684        1.0000  11
## drvf                   0.67967 -0.160791 0.12689        0.0000  17
## fle                    0.35700 -0.117626 0.17739        0.0000  22
## transauto(s4)          0.26100  0.134249 0.25414        1.0000  35
## manufacturerhonda      0.23367 -0.069989 0.14498        0.0000   4
## transauto(s5)          0.23067  0.106832 0.21835        1.0000  36
## manufacturersubaru     0.19633  0.037026 0.09493        1.0000  12
## classminivan           0.18367 -0.055012 0.12873        0.0000  27
## year                   0.15067  0.001852 0.00485        1.0000  15
## manufacturernissan     0.09333  0.019610 0.06883        1.0000  10
## transauto(l4)          0.09067 -0.007028 0.02803        0.0000  32
## manufacturerdodge      0.06233  0.004570 0.02755        1.0000   2
## cty                    0.05467 -0.000581 0.00401        0.0915  19
## flr                    0.05267 -0.000820 0.01444        0.3734  24
## manufacturertoyota     0.05000  0.002604 0.01995        1.0000  13
## transauto(l5)          0.04700 -0.001754 0.01709        0.1489  33
## classsuv               0.04633 -0.001889 0.01734        0.0144  30
## hwy                    0.03967 -0.000441 0.00332        0.0420  20
## manufacturervolkswagen 0.03867 -0.002978 0.02284        0.0000  14
## transauto(s6)          0.02500  0.001813 0.01947        1.0000  37
## manufacturermercury    0.02467  0.000897 0.02880        1.0000   9
## transmanual(m6)        0.02333  0.002655 0.02165        1.0000  39
## manufacturerford       0.01967  0.002586 0.02244        1.0000   3
## transauto(l3)          0.01800 -0.001685 0.03621        0.0000  31
## manufacturerhyundai    0.01600 -0.000732 0.01482        0.0000   5
## fld                    0.01267  0.000657 0.01890        1.0000  21
## manufacturerjeep       0.01233  0.001253 0.01878        1.0000   6
## flp                    0.01133  0.000213 0.00690        1.0000  23
## transmanual(m5)        0.00933 -0.000418 0.00728        0.0000  38
## classpickup            0.00700  0.000378 0.00817        1.0000  28
## manufacturerlincoln    0.00467  0.000280 0.01592        1.0000   8
## transauto(l6)          0.00000  0.000000 0.00000            NA  34
## 
## Mean no. regressors               Draws             Burnins                Time 
##           "10.7270"              "3000"              "1000"       "0.1537 secs" 
##  No. models visited      Modelspace 2^K           % visited         % Topmodels 
##               "625"           "5.5e+11"           "1.1e-07"               "100" 
##            Corr PMP            No. Obs.         Model Prior             g-Prior 
##            "0.8248"               "234"     "random / 19.5"               "UIP" 
##     Shrinkage-Stats 
##         "Av=0.9957" 
## 
## Time difference of 0.154 secs
#print
BMS_fit2
##                            PIP Post Mean Post SD Cond.Pos.Sign Idx
## manufacturerchevrolet  1.00000  0.483059 0.10120        1.0000   1
## cyl                    1.00000  0.558532 0.02499        1.0000  16
## drvr                   1.00000  0.439126 0.10019        1.0000  18
## classcompact           1.00000 -0.593211 0.10848        0.0000  25
## classmidsize           1.00000 -0.480104 0.12170        0.0000  26
## classsubcompact        1.00000 -0.603914 0.10511        0.0000  29
## manufacturerland rover 0.87867 -0.570335 0.27114        0.0000   7
## manufacturerpontiac    0.79633  0.452436 0.27684        1.0000  11
## drvf                   0.67967 -0.160791 0.12689        0.0000  17
## fle                    0.35700 -0.117626 0.17739        0.0000  22
## transauto(s4)          0.26100  0.134249 0.25414        1.0000  35
## manufacturerhonda      0.23367 -0.069989 0.14498        0.0000   4
## transauto(s5)          0.23067  0.106832 0.21835        1.0000  36
## manufacturersubaru     0.19633  0.037026 0.09493        1.0000  12
## classminivan           0.18367 -0.055012 0.12873        0.0000  27
## year                   0.15067  0.001852 0.00485        1.0000  15
## manufacturernissan     0.09333  0.019610 0.06883        1.0000  10
## transauto(l4)          0.09067 -0.007028 0.02803        0.0000  32
## manufacturerdodge      0.06233  0.004570 0.02755        1.0000   2
## cty                    0.05467 -0.000581 0.00401        0.0915  19
## flr                    0.05267 -0.000820 0.01444        0.3734  24
## manufacturertoyota     0.05000  0.002604 0.01995        1.0000  13
## transauto(l5)          0.04700 -0.001754 0.01709        0.1489  33
## classsuv               0.04633 -0.001889 0.01734        0.0144  30
## hwy                    0.03967 -0.000441 0.00332        0.0420  20
## manufacturervolkswagen 0.03867 -0.002978 0.02284        0.0000  14
## transauto(s6)          0.02500  0.001813 0.01947        1.0000  37
## manufacturermercury    0.02467  0.000897 0.02880        1.0000   9
## transmanual(m6)        0.02333  0.002655 0.02165        1.0000  39
## manufacturerford       0.01967  0.002586 0.02244        1.0000   3
## transauto(l3)          0.01800 -0.001685 0.03621        0.0000  31
## manufacturerhyundai    0.01600 -0.000732 0.01482        0.0000   5
## fld                    0.01267  0.000657 0.01890        1.0000  21
## manufacturerjeep       0.01233  0.001253 0.01878        1.0000   6
## flp                    0.01133  0.000213 0.00690        1.0000  23
## transmanual(m5)        0.00933 -0.000418 0.00728        0.0000  38
## classpickup            0.00700  0.000378 0.00817        1.0000  28
## manufacturerlincoln    0.00467  0.000280 0.01592        1.0000   8
## transauto(l6)          0.00000  0.000000 0.00000            NA  34
## 
## Mean no. regressors               Draws             Burnins                Time 
##           "10.7270"              "3000"              "1000"       "0.1537 secs" 
##  No. models visited      Modelspace 2^K           % visited         % Topmodels 
##               "625"           "5.5e+11"           "1.1e-07"               "100" 
##            Corr PMP            No. Obs.         Model Prior             g-Prior 
##            "0.8248"               "234"     "random / 19.5"               "UIP" 
##     Shrinkage-Stats 
##         "Av=0.9957"
#summary
BMS_fit2 %>% summary()
## Mean no. regressors               Draws             Burnins                Time 
##           "10.7270"              "3000"              "1000"       "0.1537 secs" 
##  No. models visited      Modelspace 2^K           % visited         % Topmodels 
##               "625"           "5.5e+11"           "1.1e-07"               "100" 
##            Corr PMP            No. Obs.         Model Prior             g-Prior 
##            "0.8248"               "234"     "random / 19.5"               "UIP" 
##     Shrinkage-Stats 
##         "Av=0.9957"
#default plots
BMS_fit2 %>% plot()

#coefs
BMS_fit2 %>% coef()
##                            PIP Post Mean Post SD Cond.Pos.Sign Idx
## manufacturerchevrolet  1.00000  0.483059 0.10120        1.0000   1
## cyl                    1.00000  0.558532 0.02499        1.0000  16
## drvr                   1.00000  0.439126 0.10019        1.0000  18
## classcompact           1.00000 -0.593211 0.10848        0.0000  25
## classmidsize           1.00000 -0.480104 0.12170        0.0000  26
## classsubcompact        1.00000 -0.603914 0.10511        0.0000  29
## manufacturerland rover 0.87867 -0.570335 0.27114        0.0000   7
## manufacturerpontiac    0.79633  0.452436 0.27684        1.0000  11
## drvf                   0.67967 -0.160791 0.12689        0.0000  17
## fle                    0.35700 -0.117626 0.17739        0.0000  22
## transauto(s4)          0.26100  0.134249 0.25414        1.0000  35
## manufacturerhonda      0.23367 -0.069989 0.14498        0.0000   4
## transauto(s5)          0.23067  0.106832 0.21835        1.0000  36
## manufacturersubaru     0.19633  0.037026 0.09493        1.0000  12
## classminivan           0.18367 -0.055012 0.12873        0.0000  27
## year                   0.15067  0.001852 0.00485        1.0000  15
## manufacturernissan     0.09333  0.019610 0.06883        1.0000  10
## transauto(l4)          0.09067 -0.007028 0.02803        0.0000  32
## manufacturerdodge      0.06233  0.004570 0.02755        1.0000   2
## cty                    0.05467 -0.000581 0.00401        0.0915  19
## flr                    0.05267 -0.000820 0.01444        0.3734  24
## manufacturertoyota     0.05000  0.002604 0.01995        1.0000  13
## transauto(l5)          0.04700 -0.001754 0.01709        0.1489  33
## classsuv               0.04633 -0.001889 0.01734        0.0144  30
## hwy                    0.03967 -0.000441 0.00332        0.0420  20
## manufacturervolkswagen 0.03867 -0.002978 0.02284        0.0000  14
## transauto(s6)          0.02500  0.001813 0.01947        1.0000  37
## manufacturermercury    0.02467  0.000897 0.02880        1.0000   9
## transmanual(m6)        0.02333  0.002655 0.02165        1.0000  39
## manufacturerford       0.01967  0.002586 0.02244        1.0000   3
## transauto(l3)          0.01800 -0.001685 0.03621        0.0000  31
## manufacturerhyundai    0.01600 -0.000732 0.01482        0.0000   5
## fld                    0.01267  0.000657 0.01890        1.0000  21
## manufacturerjeep       0.01233  0.001253 0.01878        1.0000   6
## flp                    0.01133  0.000213 0.00690        1.0000  23
## transmanual(m5)        0.00933 -0.000418 0.00728        0.0000  38
## classpickup            0.00700  0.000378 0.00817        1.0000  28
## manufacturerlincoln    0.00467  0.000280 0.01592        1.0000   8
## transauto(l6)          0.00000  0.000000 0.00000            NA  34

BAS

#fit
BAS_fit = BAS::bas.lm(model_form, data = mpg)

#print
BAS_fit
## 
## Call:
## BAS::bas.lm(formula = model_form, data = mpg)
## 
## 
##  Marginal Posterior Inclusion Probabilities: 
##              Intercept   manufacturerchevrolet       manufacturerdodge  
##                 1.0000                  0.9991                  0.0318  
##       manufacturerford       manufacturerhonda     manufacturerhyundai  
##                 0.0319                  0.1631                  0.0260  
##       manufacturerjeep  manufacturerland rover     manufacturerlincoln  
##                 0.0231                  0.9265                  0.0209  
##    manufacturermercury      manufacturernissan     manufacturerpontiac  
##                 0.0204                  0.1149                  0.7963  
##     manufacturersubaru      manufacturertoyota  manufacturervolkswagen  
##                 0.0507                  0.0238                  0.0308  
##                   year                     cyl                    drvf  
##                 0.2018                  1.0000                  0.7243  
##                   drvr                     cty                     hwy  
##                 0.9967                  0.0437                  0.0387  
##                    fld                     fle                     flp  
##                 0.0232                  0.3303                  0.0257  
##                    flr            classcompact            classmidsize  
##                 0.0245                  1.0000                  0.9983  
##           classminivan             classpickup         classsubcompact  
##                 0.1808                  0.0242                  1.0000  
##               classsuv           transauto(l3)           transauto(l4)  
##                 0.0283                  0.0222                  0.0491  
##          transauto(l5)           transauto(l6)           transauto(s4)  
##                 0.0243                  0.0214                  0.2517  
##          transauto(s5)           transauto(s6)         transmanual(m5)  
##                 0.2187                  0.0287                  0.0313  
##        transmanual(m6)  
##                 0.0399
#summary
BAS_fit %>% summary()
##                        P(B != 0 | Y)  model 1  model 2  model 3  model 4
## Intercept                     1.0000   1.0000   1.0000   1.0000   1.0000
## manufacturerchevrolet         0.9991   1.0000   1.0000   1.0000   1.0000
## manufacturerdodge             0.0318   0.0000   0.0000   0.0000   0.0000
## manufacturerford              0.0319   0.0000   0.0000   0.0000   0.0000
## manufacturerhonda             0.1631   0.0000   0.0000   0.0000   0.0000
## manufacturerhyundai           0.0260   0.0000   0.0000   0.0000   0.0000
## manufacturerjeep              0.0231   0.0000   0.0000   0.0000   0.0000
## manufacturerland rover        0.9265   1.0000   1.0000   1.0000   1.0000
## manufacturerlincoln           0.0209   0.0000   0.0000   0.0000   0.0000
## manufacturermercury           0.0204   0.0000   0.0000   0.0000   0.0000
## manufacturernissan            0.1149   0.0000   0.0000   0.0000   0.0000
## manufacturerpontiac           0.7963   1.0000   1.0000   1.0000   1.0000
## manufacturersubaru            0.0507   0.0000   0.0000   0.0000   0.0000
## manufacturertoyota            0.0238   0.0000   0.0000   0.0000   0.0000
## manufacturervolkswagen        0.0308   0.0000   0.0000   0.0000   0.0000
## year                          0.2018   0.0000   0.0000   0.0000   1.0000
## cyl                           1.0000   1.0000   1.0000   1.0000   1.0000
## drvf                          0.7243   1.0000   1.0000   1.0000   1.0000
## drvr                          0.9967   1.0000   1.0000   1.0000   1.0000
## cty                           0.0437   0.0000   0.0000   0.0000   0.0000
## hwy                           0.0387   0.0000   0.0000   0.0000   0.0000
## fld                           0.0232   0.0000   0.0000   0.0000   0.0000
## fle                           0.3303   0.0000   0.0000   1.0000   1.0000
## flp                           0.0257   0.0000   0.0000   0.0000   0.0000
## flr                           0.0245   0.0000   0.0000   0.0000   0.0000
## classcompact                  1.0000   1.0000   1.0000   1.0000   1.0000
## classmidsize                  0.9983   1.0000   1.0000   1.0000   1.0000
## classminivan                  0.1808   0.0000   0.0000   0.0000   0.0000
## classpickup                   0.0242   0.0000   0.0000   0.0000   0.0000
## classsubcompact               1.0000   1.0000   1.0000   1.0000   1.0000
## classsuv                      0.0283   0.0000   0.0000   0.0000   0.0000
## transauto(l3)                 0.0222   0.0000   0.0000   0.0000   0.0000
## transauto(l4)                 0.0491   0.0000   0.0000   0.0000   0.0000
## transauto(l5)                 0.0243   0.0000   0.0000   0.0000   0.0000
## transauto(l6)                 0.0214   0.0000   0.0000   0.0000   0.0000
## transauto(s4)                 0.2517   0.0000   0.0000   0.0000   0.0000
## transauto(s5)                 0.2187   0.0000   1.0000   0.0000   0.0000
## transauto(s6)                 0.0287   0.0000   0.0000   0.0000   0.0000
## transmanual(m5)               0.0313   0.0000   0.0000   0.0000   0.0000
## transmanual(m6)               0.0399   0.0000   0.0000   0.0000   0.0000
## BF                                NA   0.6936   0.6563   0.6395   1.0000
## PostProbs                         NA   0.0907   0.0286   0.0279   0.0165
## R2                                NA   0.9323   0.9340   0.9340   0.9359
## dim                               NA  10.0000  11.0000  11.0000  12.0000
## logmarg                           NA 281.2686 281.2134 281.1873 281.6344
##                         model 5
## Intercept                1.0000
## manufacturerchevrolet    1.0000
## manufacturerdodge        0.0000
## manufacturerford         0.0000
## manufacturerhonda        0.0000
## manufacturerhyundai      0.0000
## manufacturerjeep         0.0000
## manufacturerland rover   1.0000
## manufacturerlincoln      0.0000
## manufacturermercury      0.0000
## manufacturernissan       0.0000
## manufacturerpontiac      1.0000
## manufacturersubaru       0.0000
## manufacturertoyota       0.0000
## manufacturervolkswagen   0.0000
## year                     1.0000
## cyl                      1.0000
## drvf                     1.0000
## drvr                     1.0000
## cty                      0.0000
## hwy                      0.0000
## fld                      0.0000
## fle                      0.0000
## flp                      0.0000
## flr                      0.0000
## classcompact             1.0000
## classmidsize             1.0000
## classminivan             0.0000
## classpickup              0.0000
## classsubcompact          1.0000
## classsuv                 0.0000
## transauto(l3)            0.0000
## transauto(l4)            0.0000
## transauto(l5)            0.0000
## transauto(l6)            0.0000
## transauto(s4)            0.0000
## transauto(s5)            0.0000
## transauto(s6)            0.0000
## transmanual(m5)          0.0000
## transmanual(m6)          0.0000
## BF                       0.3528
## PostProbs                0.0154
## R2                       0.9336
## dim                     11.0000
## logmarg                280.5926
#default plots
BAS_fit %>% plot()

#coefs
BAS_fit %>% coef()
## 
##  Marginal Posterior Summaries of Coefficients: 
## 
##  Using  BMA 
## 
##  Based on the top  524288 models 
##                         post mean  post SD    post p(B != 0)
## Intercept                3.471795   0.022403   1.000000     
## manufacturerchevrolet    0.480615   0.100959   0.999117     
## manufacturerdodge        0.002324   0.020977   0.031846     
## manufacturerford         0.002740   0.022423   0.031863     
## manufacturerhonda       -0.048674   0.124861   0.163073     
## manufacturerhyundai     -0.001701   0.022057   0.026037     
## manufacturerjeep         0.001505   0.022050   0.023064     
## manufacturerland rover  -0.601233   0.241350   0.926543     
## manufacturerlincoln      0.000669   0.033285   0.020947     
## manufacturermercury      0.000268   0.025317   0.020372     
## manufacturernissan       0.022457   0.071530   0.114919     
## manufacturerpontiac      0.455230   0.277301   0.796308     
## manufacturersubaru       0.008221   0.046696   0.050725     
## manufacturertoyota       0.000834   0.012177   0.023765     
## manufacturervolkswagen  -0.002075   0.019748   0.030761     
## year                     0.002386   0.005333   0.201832     
## cyl                      0.556705   0.023859   1.000000     
## drvf                    -0.174740   0.124634   0.724319     
## drvr                     0.436080   0.103187   0.996738     
## cty                     -0.000662   0.004280   0.043691     
## hwy                     -0.000408   0.003236   0.038680     
## fld                      0.001761   0.029961   0.023196     
## fle                     -0.110006   0.175106   0.330327     
## flp                      0.001042   0.014124   0.025651     
## flr                      0.000393   0.012094   0.024467     
## classcompact            -0.588832   0.109251   0.999993     
## classmidsize            -0.475386   0.120403   0.998326     
## classminivan            -0.054624   0.130719   0.180804     
## classpickup              0.000600   0.018764   0.024160     
## classsubcompact         -0.604796   0.107325   0.999983     
## classsuv                -0.001766   0.019723   0.028289     
## transauto(l3)           -0.002265   0.040481   0.022186     
## transauto(l4)           -0.003506   0.019951   0.049053     
## transauto(l5)           -0.000811   0.012120   0.024271     
## transauto(l6)           -0.000596   0.023202   0.021391     
## transauto(s4)            0.133088   0.257093   0.251730     
## transauto(s5)            0.099927   0.211357   0.218744     
## transauto(s6)            0.002164   0.021304   0.028654     
## transmanual(m5)         -0.001708   0.014458   0.031325     
## transmanual(m6)          0.003844   0.025464   0.039860