Description Usage Arguments Details Value See Also Examples
This is a simple MCMC sampler class that can be used to generate samples from a model.
1 | sampler = new("MCMCsampler",model)
|
model |
Constructor takes the model object create by the lexer |
This sampler inherits from the base class sampler. The function takeSample can be used to sample from the model. Before the sampler starts, it creates a list with all nodes to sample from. This is done to speed up the sampling process. It then calls the sample function, with a list of likelihood objects and prior prior object. This this can be overwritten for convenience by other samplers. The takeSample function can take up to 3 parameters (nSamples,initialValues = list(),addtionalNodes=list()). The first one is how many samples are to be taken, the second one is list with names and initial values to set, the last parameter is a list of names with addtional nodes to track. This can be useful if model variables (helperNodes) are to be tracked as well.
Returns a valid sampler object.
Package Overview:
Slice-Package
Base sampler class:
Sampler
Slice sampler class:
SliceSampler
Maximum likelihood based sampler class:
LaplaceApproximation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | require('Slice')
#define a regression model
my_model_str= "
model regModel{
#this is a comment
mu = x*beta1 +beta0
y~ dnorm(mu,sigma)
sigma~dgamma(0.01,0.01)
beta0 ~ dnorm(0,s)
s ~ dunif(0,1)
beta1 ~ dnorm(0,1) #one more comment
}
"
#generate some data
set.seed(1234)
n <- 100
beta <- c(1.4,-0.8)
sigma2 <- 1
X = cbind(rnorm(n,2,1),1)
val = X
y = matrix( val,ncol = 1)
x = matrix(X[,1],ncol = 1)
data_list = list('y'=y,'x'=x)
lex = new('Lexer') ##create lexer obj
lex$setModelString(my_model_str) #set model
lex$setModelData(data_list) # set data
lex$lexModel() ##lexx the syntax
root_plate = lex$parseModel() ##create the model
mcmcSample = new('MCMCsampler',root_plate)
samplesFromProblem = mcmcSample$takeSample(500, # take 500 samples from the posterior
addtionalNodes=c('mu')) # also track mu
colMeans(samplesFromProblem)
plot(samplesFromProblem[,1])
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.