I'm trying to use the bootstrap filter in nimble to estimate the likelihood of a time series for different parameter values.
The only way it has worked so far is by changing the value in the model (with model$a <- 1), then recompiling the model, then building the bootstrap filter, then compiling the bootstrap filter and finally running it. I've tried a few things to avoid some of these steps but haven't succeeded yet. Is there any way to skip some of the intermediate steps?
Here's some example code of what I'm trying to do, using the model in the documentation:
timeModelCode <- nimbleCode({
x[1] ~ dnorm(mu_0, 1)
y[1] ~ dnorm(x[1], 1)
for(i in 2:t){
x[i] ~ dnorm(x[i-1] * a + b, 1)
y[i] ~ dnorm(x[i] * c, 1)
}
a ~ dunif(0, 1)
b ~ dnorm(0, 1)
c ~ dnorm(1,1)
mu_0 ~ dnorm(0, 1)
})
# simulate some data
t <- 25; mu_0 <- 1
x <- rnorm(1 ,mu_0, 1)
y <- rnorm(1, x, 1)
a <- 0.5; b <- 1; c <- 1
for(i in 2:t){
x[i] <- rnorm(1, x[i-1] * a + b, 1)
y[i] <- rnorm(1, x[i] * c, 1)
}
# build the model
rTimeModel <- nimbleModel(timeModelCode, constants = list(t = t),
data <- list(y = y), check = FALSE )
# Set parameter values and compile the model
rTimeModel$a <- 0.5
rTimeModel$c <- 1
rTimeModel$mu_0 <- 1
b_candidates <- seq(-4, 4, by = 1)
b_ll <- numeric(length(b_candidates))
parNum <- 5000
for(i in 1:length(b_candidates)){
print(i)
rTimeModel$b <- b_candidates[i]
cTimeModel <- compileNimble(rTimeModel)
rBootF <- buildBootstrapFilter(rTimeModel, "x",
control = list(thresh = 0.8, saveAll = TRUE,
smoothing = FALSE))
cBootF <- compileNimble(rBootF,project = rTimeModel)
b_ll[i] <- cBootF$run(parNum)
}
plot(b_candidates, b_ll)