Description Usage Arguments Details Value See Also Examples
This is a simple slice sampler that uses stepping out for interval estimation.
1 | sampler = new("SliceSampler",model)
|
model |
Constructor takes the model object create by the lexer |
This sampler inherits from MCMCsampler, so that it can benefit from the there implement functionality. It overwrites the sample function that gets called with the likelihood and prior. It samples than a random value regarding the heights and uses the stepping out procedure to estimate the with that height.
Returns a valid sampler object.
Package Overview:
Slice-Package
Base sampler class:
Sampler
Slice sampler class:
MCMCsampler
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | require('Slice')
#### ordered logit model
#Generate data:
set.seed(1234)
n<-200
x<-cbind(rnorm(n), rnorm(n) )
true.beta<-c(0.2,1.1)
true.alpha = c(0.5,1.8)
mu = x
y = c()
for( i in 1:n){
probs=c()
p = 0
q = 0
for(k in 1:(length(true.alpha)) ){
tmp = 1/(1+exp(-(true.alpha[k]-mu[i]) ) )
p = tmp -q
q = tmp;
probs = c(probs,p)
}
p = 1-q
probs = c(probs,p)
y=c(y,sample(c(1:3),probs,size = 1,replace=FALSE))
}
y = matrix(y,ncol=1)
## data is generated
data_list = list(y=matrix(y),x1=matrix(x[,1]),x2=matrix(x[,2]) )
###define the model
sigmoid = function(x){ matrix(1/(1+exp(-x) )) }
model_str = '
model{
mu = x1*beta[1] + x2*beta[2]
alpha = sort(alpha0)
Q[1] = sigmoid( alpha[1]-mu)
p[1] = Q[1]
for(j in 2:2){
Q[j] = sigmoid(alpha[j]-mu)
p[j] = Q[j] - Q[j-1]
}
p[3] = 1 - Q[2]
y ~ dcat(p[1],p[2],p[3])
## priors over thresholds
for(r in 1:2){
alpha0[r] ~ dnorm(0,1)
}
for(j in 1:2){
beta[j] ~ dnorm(0,1)
}
}
'
##create model
lex = new('Lexer')
lex$setModelString(model_str)
lex$setModelData(data_list)
lex$lexModel()
root_plate = lex$parseModel()
sliceSampler = new('SliceSampler',root_plate)
##### ok lets use it to set initial values and sample
samplesFromProblem = sliceSampler$takeSample(100,initialValues =list('alpha0[1]'=matrix(0),'alpha0[2]'=matrix(1) ))
colMeans(samplesFromProblem)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.