audrey-b / sims

An R package to simulate data from JAGS or R Code
https://poissonconsulting.github.io/sims/
Other
1 stars 2 forks source link

chk_not_any_na(parameters) should be removed from sims_simulate? #28

Closed audrey-b closed 4 years ago

audrey-b commented 4 years ago

State-space model

code = " for(t in 2:K){ N[t] ~ dbin(1-phi, N[t-1]) } " sims <- sims::sims_simulate(code=code, constants=nlist(K=4), parameters=nlist(phi=0.95, N=c(100, rep(NA, 4))) )

chk_not_any_na(parameters) should be removed so an error is not thrown

As a comparison, the following is valid:

jags.model(textConnection(" model{for(t in 2:K){ N[t] ~ dbin(1-phi, N[t-1]) }}"), data=list(phi=0.95, N=c(100, rep(NA, 3)), K=4))

joethorley commented 4 years ago

Will do when I get a moment unless you want to tackle?

joethorley commented 4 years ago

@audrey-b It's more complicated than simply removing this assertion. I don't have time to address for the foreseeable future. Feel free to explore in a branch!

joethorley commented 4 years ago

Removing chk_not_any_na(parameters) causes the error

The following variable nodes are defined in parameters: 'N'.

If we relax as well then we are allowing parameters to be monitored. I'm not sure if this is a good idea.

You can achieve using the following which I think is clearer about intent.

library(sims)
code = "
x[1] <- N
for(t in 2:K){
x[t] ~ dbin(1-phi, x[t-1])
}
"
sims <- sims::sims_simulate(code=code,
                            constants=nlist(K=4),
                            parameters=nlist(phi=0.95, N=100))

sims
#> $x
#> [1] 100   7   0   0
#> 
#> $K
#> [1] 4
#> 
#> an nlists object of an nlist object with 2 numeric elements

Created on 2020-07-08 by the reprex package (v0.3.0)