sns.run | R Documentation |
This is a wrapper around sns
, allowing one to draw multiple samples from a distribution while collecting diagnostic information.
sns.run(init, fghEval, niter = 100, nnr = min(10, round(niter/4)) , mh.diag = FALSE, part = NULL, print.level = 0 , report.progress = ceiling(niter/10) , numderiv = 0, numderiv.method = c("Richardson", "simple") , numderiv.args = list() , ...)
init |
Initial value for the MCMC chain. |
fghEval |
Log-density to be sampled from. A valid log-density can have one of 3 forms: 1) return log-density, but no gradient or Hessian, 2) return a list of |
niter |
Number of iterations to perform (in ‘nr’ and ‘mcmc’ mode combined). |
nnr |
Number of initial iterations to spend in ‘nr’ mode. |
mh.diag |
Boolean flag, indicating whether detailed MH diagnostics such as components of acceptance test must be returned or not. |
part |
List describing partitioning of state space into subsets. Each element of the list must be an integer vector containing a set of indexes (between |
print.level |
If greater than 0, print sampling progress report. |
report.progress |
Number of sampling iterations to wait before printing progress reports. |
numderiv |
Integer with value from the set |
numderiv.method |
Method used for numeric differentiation. This is passed to the |
numderiv.args |
Arguments to the numeric differentiation method chosen in |
... |
Other parameters to be passed to |
sns.run
returns an object of class sns
with elements:
samplesMat |
A matrix object with |
acceptance |
Metropolis proposal percentage acceptance. |
burn.iters |
Number of burn-in ierations. |
sample.time |
Time in seconds spent in sampling. |
burnin.time |
Time in seconds spent in burn-in. |
1. sns.run
cannot be used if SNS is being run as part of a Gibbs cycle, such that the conditional distribution being sampled by SNS changes from one iteration to next. In such cases, sns
must be used instead, inside an explicit Gibbs-cycle for
loop.
2. See package vignette for more details on SNS theory, software, examples, and performance.
Alireza S. Mahani, Asad Hasan, Marshall Jiang, Mansour T.A. Sharabiani
Mahani A.S., Hasan A., Jiang M. & Sharabiani M.T.A. (2016). Stochastic Newton Sampler: The R Package sns. Journal of Statistical Software, Code Snippets, 74(2), 1-33. doi:10.18637/jss.v074.c02
sns
, summary.sns
, plot.sns
, predict.sns
## Not run: # using RegressionFactory for generating log-likelihood and its derivatives library(RegressionFactory) loglike.poisson <- function(beta, X, y) { regfac.expand.1par(beta, X = X, y = y, fbase1 = fbase1.poisson.log) } # simulating data K <- 5 N <- 1000 X <- matrix(runif(N * K, -0.5, +0.5), ncol = K) beta <- runif(K, -0.5, +0.5) y <- rpois(N, exp(X beta.init <- rep(0.0, K) # glm estimate (ML), for reference beta.glm <- glm(y ~ X - 1, family = "poisson", start = beta.init)$coefficients # sampling of likelihood beta.smp <- sns.run(init = beta.init , fghEval = loglike.poisson, niter = 1000 , nnr = 20, X = X, y = y) smp.summ <- summary(beta.smp) # compare mean of samples against ML estimate (from glm) cbind(beta.glm, smp.summ$smp$mean) # trying numerical differentiation loglike.poisson.fonly <- function(beta, X, y) { regfac.expand.1par(beta, X = X, y = y, fgh = 0, fbase1 = fbase1.poisson.log) } beta.smp <- sns.run(init = beta.init , fghEval = loglike.poisson.fonly, niter = 1000, nnr = 20 , X = X, y = y, numderiv = 2) smp.summ <- summary(beta.smp) cbind(beta.glm, smp.summ$smp$mean) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.