Description Usage Arguments Examples
View source: R/dsdive.obs.sampleparams_shared.R
Sampler uses a Gaussian approximation to the full conditional posterior. Computations are carried out via shared-memory parallelization, and designed for use with the dive-specific covariate model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
s0 |
the stage for which updated parameters should be sampled |
sample.betas |
|
theta |
list containing current values of model parmaeters |
alpha.priors.list |
List of lists. Each of the sublists specifies the
prior distribution for the parameter vectors |
beta.priors.list |
List of lists. Each of the sublists specifies the
prior distribution for the parameter vectors |
cl |
Shared-memory cluster to be used to distribute some computations.
The cluster must be fully initialized before running this function.
The cluster requires random seeds and |
shared.env |
environment containing shared-memory variable pointers.
|
gapprox |
(Optional) |
output.gapprox |
|
rw.sampler |
If |
adaptive |
|
gapprox.approx_ld |
|
optim.maxit |
maximum number of steps to take during numerical optimization to compute Gaussian approximation to full conditional posteriors used to propose model parameters |
adaptation.frequency |
Random walk proposals will only be updated at intervals of this step count |
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | ##
## input for gibbs sampler
##
data('dive.sim')
attach(dive.sim)
attach(dive.sim$params)
library(parallel)
library(Rdsm)
cl = makeCluster(2, 'SOCK')
clusterEvalQ(cl, library(dsdive))
clusterEvalQ(cl, library(Rdsm))
mgrinit(cl)
t.stages = sim$times[c(FALSE,diff(sim$stages)==1)]
tstep = diff(sim.obs$times[1:2])
obstx.mat = lapply(1:3, function(s) {
dsdive.obstx.matrix(depth.bins = depth.bins, beta = beta,
lambda = lambda, s0 = s, tstep = tstep,
include.raw = TRUE, delta = 1e-10)
})
alpha.priors.list = list(
list(mu = 0, sd = 3),
list(mu = 0, sd = 3),
list(mu = 0, sd = 3)
)
beta.priors.list = list(
list(mu = 0, sd = 3),
list(mu = 0, sd = 3)
)
T1.prior.params = c(25, .04)
T2.prior.params = c(56, .06)
dsobs.list = list(sim.obs, sim.obs)
t.stages.list = list(t.stages, t.stages)
beta.init = list(
c(qlogis(.5), 0),
c(0, 1)
)
alpha.init = c(beta.init, list(c(-1,-1)))
covs = data.frame(x1 = c(.5, 1), x2 = c(0, .3))
pi.formula = ~x1
lambda.formula = ~x1:x2
##
## barebones setup for gibbs sampler internals
##
n = length(dsobs.list)
#
# expand covariate design matrices
#
if(!inherits(pi.formula, 'list')) {
pi.formula = list(pi.formula, pi.formula)
}
if(!inherits(lambda.formula, 'list')) {
lambda.formula = list(lambda.formula, lambda.formula, lambda.formula)
}
pi.designs = lapply(pi.formula, function(f) model.matrix(f, covs))
lambda.designs = lapply(lambda.formula, function(f) model.matrix(f, covs))
#
# initialize nodes and shared memory pass-throughs
#
# required variables in environment
beta1.prior = beta.priors.list[[1]]
beta2.prior = beta.priors.list[[2]]
alpha1.prior = alpha.priors.list[[1]]
alpha2.prior = alpha.priors.list[[2]]
alpha3.prior = alpha.priors.list[[3]]
depth.bins = depth.bins
max.width = 100
max.width.offset = 30
t0.prior.params = c(1,1)
tf.prior.params = c(1,1)
delta = 1e-10
offsets = 0
offsets.tf = 0
shared.env = gibbs_init_shared(cl = cl, envir = environment())
#
# initialize sampler output
#
theta = list(beta1 = shared.env$beta1[], beta2 = shared.env$beta2[],
alpha1 = shared.env$alpha1[], alpha2 = shared.env$alpha2[],
alpha3 = shared.env$alpha3[])
##
## demo shared parameter sampling
##
# update stage 1 parameters
theta.raw = dsdive.obs.sampleparams_shared(
s0 = 1, theta = theta, alpha.priors.list = alpha.priors.list,
beta.priors.list = beta.priors.list, cl = cl, shared.env = shared.env,
optim.maxit = 1, sample.betas = TRUE)
##
## demo distributed likelihood evaluation
##
dsdive.obsld_shared(theta = theta, shared.env = shared.env,
cl = cl, s0 = 1, sf = 3)
##
## clean up
##
stopCluster(cl)
detach(dive.sim$params)
detach(dive.sim)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.