0

I am maintaining a package that uses BayesLogit for Bayesian Logistic regression using the Polya-Gamma latent variable technique and return samples from a Markov Chain-Monte Carlo (MCMC). BayesLogit is no longer on CRAN and I can install a previous version with

install_version("BayesLogit", version = "0.6")

But this hack will prevent the submission of my package to CRAN. The source code was last updated a year ago, so I don't think it will return to CRAN.

I found another package that does the same thing with a similar syntax. But this package is not on CRAN either and installs with

devtools::install_github("kasparmartens/PolyaGamma")

Does a CRAN package implement Bayesian logistic regression with a Polya-Gamma scheme and return the MCMC samples?

miguelmorin
  • 5,025
  • 4
  • 29
  • 64
  • I couldn't find any others looking through [CRAN's list of packages](https://cran.r-project.org/). The [`PolyaGamma` package](https://github.com/kasparmartens/PolyaGamma) you discuss on GitHub has been updated even less recently -- it looks like 2015. The problems `BayesLogit` face on CRAN are that it tries to call C functions that it does not provide in `src/`; for example, in `R/FFBS.R`, it tries to call `.C("ffbs", ...)`, but there is not `ffbs()` C function defined anywhere in the package. You may end up needing to roll your own here. – duckmayr Feb 13 '19 at 16:08
  • Did you find any solution? I too am looking for implementing Bayesian Logistic Regression. – Registered User Feb 22 '19 at 16:43
  • @RegisteredUser I could not find a package, no. If you don't need the Polya-Gamma sampling scheme, several packages do Bayesian Logistic Regression without it. I did not try them. – miguelmorin Feb 25 '19 at 16:52

2 Answers2

2

Myself and a colleague were in a similar situation (a package we had depended on BayesLogit, and ended up being archived), so we decided to package up our C++ Polya-Gamma sampler implementation we had developed for MATLAB into an R package. It's currently up on CRAN as the package pgdraw. From our testing it is actually even slightly faster than the original BayesLogit package.

  • I see in the package examples the function `sample_simple_logreg <- function(y, nsamples)` but it doesn't take features to return regression coefficients. My math days are way behind me: can you provide an example of how to use your package to perform Bayesian logistic regression? – miguelmorin May 23 '19 at 15:40
0

We decided to use RStan because it was already on CRAN and we had used it in another part of the package. We had a stronger preference for something on CRAN that implemented Bayesian Regression than the Polya-Gamma scheme.

The Stan file contains:

// Code for 0-1 loss Bayes Logistic Regression model
data {
  int<lower=0> n; // number of observations
  int<lower=0> p; // number of covariates
  matrix[n,p] x; // Matrix of covariates
  int<lower=0,upper=1> y[n]; // Responses
  real<lower=0> beta_sd; // Stdev of beta
}
parameters {
  vector[p] beta;
}
model {
  beta ~ normal(0,beta_sd);
  y ~ bernoulli_logit(x * beta); // Logistic regression
}

And we call it with:

bayes_log_reg <- rstan::stan(stan_file, data = data, seed = seed,
                      iter = n_bootstrap * 2, chains = 1)
stan_bayes_sample <- rstan::extract(bayes_log_reg)$beta

See the package vignette for complete details.

I realise that many other packages implement Bayesian logistic regression in the general case.

miguelmorin
  • 5,025
  • 4
  • 29
  • 64