# fitting an HMM to the trex data and running sdreportMC
## negative log-likelihood function
nll = function(par) {
getAll(par, dat) # makes everything contained available without $
Gamma = tpm(eta) # computes transition probability matrix from unconstrained eta
delta = stationary(Gamma) # computes stationary distribution
# exponentiating because all parameters strictly positive
mu = exp(logmu)
sigma = exp(logsigma)
kappa = exp(logkappa)
# reporting statements for sdreportMC
REPORT(mu)
REPORT(sigma)
REPORT(kappa)
# calculating all state-dependent densities
allprobs = matrix(1, nrow = length(step), ncol = N)
ind = which(!is.na(step) & !is.na(angle)) # only for non-NA obs.
for(j in 1:N){
allprobs[ind,j] = dgamma2(step[ind],mu[j],sigma[j])*dvm(angle[ind],0,kappa[j])
}
-forward(delta, Gamma, allprobs) # simple forward algorithm
}
## initial parameter list
par = list(
logmu = log(c(0.3, 1)), # initial means for step length (log-transformed)
logsigma = log(c(0.2, 0.7)), # initial sds for step length (log-transformed)
logkappa = log(c(0.2, 0.7)), # initial concentration for turning angle (log-transformed)
eta = rep(-2, 2) # initial t.p.m. parameters (on logit scale)
)
## data and hyperparameters
dat = list(
step = trex$step[1:500], # hourly step lengths
angle = trex$angle[1:500], # hourly turning angles
N = 2
)
## creating AD function
obj = MakeADFun(nll, par, silent = TRUE) # creating the objective function
## optimising
opt = nlminb(obj$par, obj$fn, obj$gr) # optimization
## running sdreportMC
# `mu` has report statement, `delta` is automatically reported by `forward()`
sdrMC = sdreportMC(obj,
what = c("mu", "delta"),
n = 50)
dim(sdrMC$delta)
# now a matrix with 50 samples (rows)
Run the code above in your browser using DataLab