forked from kaskr/RTMB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompois.R
39 lines (34 loc) · 992 Bytes
/
compois.R
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
library(RTMB)
set.seed(123)
nu <- .1
mode <- 10
domain <- 0:100
prob <- dpois(domain, lambda=mode)^nu; prob <- prob / sum(prob)
sum(prob * domain) ## mean
x <- sample(domain, size=1e4, replace=TRUE, prob = prob)
## TMB data
data <- list( x = x )
parameters <- list( logmu = 0, lognu = 0 )
## Parameterization through the mode
parameterization <- "mode"
func <- function(p) {
mu <- exp(p$logmu)
nu <- exp(p$lognu)
ADREPORT(mu)
ADREPORT(nu)
x <- OBS(x)
if (parameterization == "mode")
-sum(dcompois(x, mu, nu, TRUE))
else ## mean
-sum(dcompois2(x, mu, nu, TRUE))
}
obj <- MakeADFun(func, parameters)
system.time(fit.mode <- nlminb(obj$par, obj$fn, obj$gr, obj$he))
rep.mode <- sdreport(obj)
## Parameterization through the mean
parameterization <- "mean"
obj <- MakeADFun(func, parameters)
system.time(fit.mean <- nlminb(obj$par, obj$fn, obj$gr, obj$he))
rep.mean <- sdreport(obj)
summary(rep.mode, "report")
summary(rep.mean, "report")