step <- trex$step[1:2000] # subsetting trex data
N <- 2 # 2 states
# custom likelihood
nll <- function(par) {
getAll(par)
Gamma <- tpm(eta)
delta <- stationary(Gamma)
mu <- exp(log_mu); REPORT(mu)
sigma <- exp(log_sigma); REPORT(sigma)
allprobs <- matrix(1, length(step), N)
for(j in 1:N) allprobs[,j] <- dgamma2(step, mu[j], sigma[j])
-forward(delta, Gamma, allprobs)
}
# initial parameters in named list
par0 <- list(eta = rep(-1.5,2),
log_mu = log(c(0.3, 2.5)),
log_sigma = log(c(0.3, 1.5)))
# constructing AD object
obj <- MakeADFun(nll, par0, silent = TRUE)
# optimising
opt <- nlminb(obj$par, obj$fn, obj$gr)
# sampling from distribution of the MLE
samples <- MCreport(obj, nSamples = 10, report = TRUE)
Run the code above in your browser using DataLab