dsdive.obs.sampleparams_shared: Sample pi and lambda parameters for a single dive stage

Description Usage Arguments Examples

View source: R/dsdive.obs.sampleparams_shared.R

Description

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
dsdive.obs.sampleparams_shared(
  s0,
  sample.betas,
  theta,
  alpha.priors.list,
  beta.priors.list,
  cl,
  shared.env,
  gapprox = NULL,
  output.gapprox = FALSE,
  rw.sampler = NULL,
  adaptive = FALSE,
  gapprox.approx_ld = FALSE,
  optim.maxit = 1000,
  adaptation.frequency = 10
)

Arguments

s0

the stage for which updated parameters should be sampled

sample.betas

TRUE to sample the directional preference coefficients, or FALSE to sample the speed coefficients.

theta

list containing current values of model parmaeters beta1, beta2, alpha1, alpha2, and alpha3.

alpha.priors.list

List of lists. Each of the sublists specifies the prior distribution for the parameter vectors alpha1, alpha2, and alpha3. See dsdive.gibbs.obs.cov for more details.

beta.priors.list

List of lists. Each of the sublists specifies the prior distribution for the parameter vectors beta1 and beta2. See dsdive.gibbs.obs.cov for more details.

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 Rdsm-initialization.

shared.env

environment containing shared-memory variable pointers. shared.env is expected to be the output from the initialization function gibbs_init_shared.

gapprox

(Optional) gaussapprox object containing the Gaussian approximation used to propose model parameters. If NULL, then a Gaussian approximation will be computed.

output.gapprox

TRUE to return the Gaussian approximation used to propose model parameters.

rw.sampler

If NULL, then a new sampler will be created, otherwise the sampler will be called.

adaptive

TRUE to use adaptive Random walk Metropolis-Hastings proposals instead of Gaussian approximations

gapprox.approx_ld

TRUE to use an approximate likelihood when building Gaussian approximations to full conditional posteriors.

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

Examples

  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)

jmhewitt/dsdive documentation built on May 29, 2020, 5:18 p.m.