forked from kaskr/RTMB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spa_gauss.R
36 lines (31 loc) · 813 Bytes
/
spa_gauss.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
## Simulate data
set.seed(123)
n <- 1000
mu <- 3
sigma <- 1.5
y <- rnorm(n, mu, sigma) ## data
## data and parameters
parameters <- list(s=numeric(n), mu=0, sigma=1)
f <- function(parameters) {
## Build inner problem
s <- parameters$s
mu <- parameters$mu
sigma <- parameters$sigma
## MGF
K <- sum(mu * s + .5 * s * s * sigma^2)
## SPA adjustment
K - sum(s * y)
}
library(RTMB)
F <- MakeTape(f, parameters)
show(F) ## f
F <- F$laplace(random=1:n, SPA=TRUE)
show(F) ## Laplace is on the tape (verify using F$print())
## Tape can be used directly:
nlminb(c(0, 1), F, F$jacobian)
## Or re-used to build new objective functions
G <- function(x) F(x)
## Feed G into normal TMB interface
obj <- MakeADFun(G, list(mu=0, sigma=1) )
opt <- nlminb(obj$par, obj$fn, obj$gr)
sdreport(obj)