Learn R Programming

bkmrhat v1.1.2

Diagnostics and multi-chain tools with Bayesian kernel machine regression (bkmr)

Bayesian kernel machine regression (BKMR) is a semi-parametric approach to Bayesian GLMs. The bkmr package implements BKMR under identity and probit links, but does not implement standard Bayesian diagnostics or interface with R packages that can do these diagnostics, nor does it allow easy implementation of parallel chains, which is one of the easiest ways to speed up Markov chain Monte Carlo on modern computers. This package fixes those problems.

Note that this package functions best using the development verision of the bkmr package (instructions here)

Quick start

install

install.packages("devtools")
devtools::install_github("alexpkeil1/bkmrhat", build_vignettes = TRUE)

simulate data from a bkmr package function

set.seed(111)
library(coda)
library(bkmr)
library(bkmrhat)
dat <- bkmr::SimData(n = 50, M = 4)
y <- dat$y
Z <- dat$Z
X <- dat$X
set.seed(111)

fit BKMR model via four parallel MCMC chains (10,000 iterations total)

future::plan(strategy = future::multisession)
# run 4 parallel Markov chains
fitkm.list <- kmbayes_parallel(nchains=4, y = y, Z = Z, X = X, iter = 2500,
  verbose = FALSE, varsel = TRUE)

run diagnostics from rstan package

# rstan has a few excellent diagnostics (modern r-hat and effective sample size)
diagres = kmbayes_diag(fitkm.list)
# estimate of standard error (gives margin of error for reporting estimates)
diagres[,"se_mean"]

convert to coda package object for other diagnostics

mcmcobj = as.mcmc.list(fitkm.list)
# lots of functions in the coda package to use
# get info on rejection probabilities (from h() function in bkmr - won't be correct for discrete parameters like delta parameters)
  rejectionRate(mcmcobj)
# check trace plots for obvious issues between/within chains
  plot(mcmcobj)
# autocorrelation for efficiency issues
  acfplot(mcmcobj)
# posterior correlation for
  crosscorr(mcmcobj)
# Gelman/Rubin diagnostics for convergence (multivariate may fail when including delta functions from variable selection)
  try(gelman.diag(mcmcobj, multivariate = FALSE))  
# batch standard error: a different approach to estimating standard error
  batchSE(kmres)
# effective size, another way to assess whether enough samples have been made (I prefer the `rstan` implementations)
  effectiveSize(mcmcobj)
  try(geweke.diag(mcmcobj))
# posterior summary
  summary(mcmcobj)
# HPD intervals by chain
  HPDinterval(mcmcobj)
  

combine chains and use bkmr native posterior functions

combobj = comb_bkmrfits(fitkm.list)
combobj$iter
summary(combobj) # rejection rates will be slightly off for multi-chain objects
# mean difference function from `bkmr` package (default burnin of half of total number of iterations)
  mdiff = OverallRiskSummaries(fit = combobj,
                                  qs = seq(.05, 0.95, by = 0.1),
                                  q.fixed = 0.5, method = "exact")
mdiff

Copy Link

Version

Install

install.packages('bkmrhat')

Monthly Downloads

403

Version

1.1.3

License

GPL (>= 3)

Maintainer

Alexander Keil

Last Published

March 29th, 2022

Functions in bkmrhat (1.1.3)

kmbayes_combine

Combine multiple BKMR chains
kmbayes_combine_lowmem

Combine multiple BKMR chains in lower memory settings
as.mcmc.bkmrfit

Convert bkmrfit to mcmc object for coda MCMC diagnostics
OverallRiskSummaries_parallel

Overall summary by chain
PredictorResponseBivar_parallel

Bivariate predictor response by chain
SamplePred_parallel

Posterior samples of E(Y|h(Z),X,beta) by chain
PredictorResponseUnivar_parallel

Univariate predictor response summary by chain
as.mcmc.list.bkmrfit.list

Convert multi-chain bkmrfit to mcmc.list for coda MCMC diagnostics
ExtractPIPs_parallel

Posterior inclusion probabilities by chain
SingVarRiskSummaries_parallel

Single variable summary by chain
predict.bkmrfit

Posterior mean/sd predictions
kmbayes_continue

Continue sampling from existing bkmr fit
kmbayes_diagnose

MCMC diagnostics using rstan
kmbayes_parallel

Run multiple BKMR chains in parallel
kmbayes_parallel_continue

Continue sampling from existing bkmr_parallel fit