examples/dsdive.impute.sample_n.R

data('dive.sim')
attach(dive.sim)
attach(params)

#
# extract details about data
#

# time between observations
tstep = diff(sim.obs$times[1:2])

# stage transition indices
t.inds = which(c(FALSE, diff(sim$stages)==1))

# stage transition times
t.stages = sim$times[t.inds]

# uniformized transition rate
rate.unif = max(outer(lambda, 2 * depth.bins[,2], '/'))

# probability transition matrix for observations
P.raw = 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)
})

# probability transition matrix for uniformized DTMC
P.tx = lapply(1:3, function(s) {
  dsdive.tx.matrix.uniformized(depth.bins = depth.bins, beta = beta, 
                               lambda = lambda, s0 = s, 
                               rate.uniformized = rate.unif)
})


#
# within-stage transition
#

ind = 1

d0 = sim.obs$depths[ind]
df = sim.obs$depths[ind+1]

s0 = sim.obs$stages[ind]
sf = sim.obs$stages[ind+1]

t0 = sim.obs$times[ind]
tf = sim.obs$times[ind+1]

dsdive.impute.sample_n(
  d0 = d0, df = df, s0 = s0, sf = sf, t0 = t0, tf = tf, 
  t.stages = t.stages, rate.unif = rate.unif, P.raw = P.raw, P.tx = P.tx, 
  n.bins = nrow(depth.bins), max.tx = 100)


#
# between-stage transition
#

ind = max(which(sim.obs$times < t.stages[1]))

d0 = sim.obs$depths[ind]
df = sim.obs$depths[ind+1]

s0 = sim.obs$stages[ind]
sf = sim.obs$stages[ind+1]

t0 = sim.obs$times[ind]
tf = sim.obs$times[ind+1]

# sample number of depth bin transitions before stage 1->2 transition
n = dsdive.impute.sample_n(
  d0 = d0, df = df, s0 = s0, sf = sf, t0 = t0, tf = tf, 
  t.stages = t.stages, rate.unif = rate.unif, P.raw = P.raw, P.tx = P.tx, 
  n.bins = nrow(depth.bins), max.tx = 100)


#
# between-stage transition
#

ind = max(which(sim.obs$times < t.stages[2]))

d0 = sim.obs$depths[1]
df = sim.obs$depths[ind+1]

s0 = sim.obs$stages[1]
sf = sim.obs$stages[ind+1]

t0 = sim.obs$times[1]
tf = sim.obs$times[ind+1]

# sample number of depth bin transitions before stage 1->2 transition
n = dsdive.impute.sample_n(
  d0 = d0, df = df, s0 = s0, sf = sf, t0 = t0, tf = tf, 
  t.stages = t.stages, rate.unif = rate.unif, P.raw = P.raw, P.tx = P.tx, 
  n.bins = nrow(depth.bins), max.tx = 100)

detach(params)
detach(dive.sim)
jmhewitt/dsdive documentation built on May 29, 2020, 5:18 p.m.