Allows for efficient editing of model code produced by nimbleCode() function

customize_model(
  model,
  append_code = NULL,
  append_line = NULL,
  remove_line = NULL,
  write = FALSE
)

Arguments

model

The nimbleCode() used to define model in nimble package, possibly generated from get_classic function.

append_code

Either NULL or model code produced from nimbleCode() or get_classic function. Note that if remove_line = NULL, then code will be appended just after existing model code; otherwise specify the lines to insert new code into by setting append_line.

append_line

Either NULL or an integer value as a scalar or vector defining which positions to insert new lines of code in the model. Note that if multiple lines of new code are to be inserted on the same line, then just use rep(44,3) for example if the new code had 3 lines to insert (not counting "{" and "}"). The lines to append to should be based on the original model given to model.

remove_line

Either NULL or an integer value as a scalar or vector defining which lines of code to remove from the original model. Set to NULL when only appending to and not replacing code in previous model file (i.e., model).

write

Logical. If TRUE, then a text file is written to the working directory called "new_model.txt". Otherwise, model is written to temp file and then deleted. Default is FALSE.

Value

A model description that can be run using run_classic.

Author

Daniel Eacker

Examples

# get model
scr_model = get_classic(dim_y = 2, enc_dist = "binomial",sex_sigma = TRUE,
hab_mask=TRUE,trapsClustered = TRUE)

# create new nimbleCode to use for replacement in 'scr_model'
p0_prior = nimble::nimbleCode({
   p0[g] ~ dbeta(1,1)
})

# replace line 3 of old model code with 'p0_prior' 
new_model = customize_model(model = scr_model, append_code = p0_prior, 
                                   append_line = 3, remove_line = 3)
                                   
# inspect new model code
new_model
#> {
#>     for (g in 1:nSites) {
#>         p0[g] ~ dbeta(1, 1)
#>     }
#>     psi_sex ~ dunif(0, 1)
#>     sigma[1] ~ dunif(0, sigma_upper)
#>     sigma[2] ~ dunif(0, sigma_upper)
#>     sigma.pixel[1] <- sigma[1]/pixelWidth
#>     sigma.pixel[2] <- sigma[2]/pixelWidth
#>     psi ~ dunif(0, 1)
#>     for (i in 1:M) {
#>         sex[i] ~ dbern(psi_sex)
#>         sx[i] <- sex[i] + 1
#>         z[i] ~ dbern(psim[i])
#>         psim[i] <- (1 - (1 - psi)^prop.habitat[site[i]])
#>         s[i, 1] ~ dunif(x_lower[site[i]], x_upper[site[i]])
#>         s[i, 2] ~ dunif(y_lower[site[i]], y_upper[site[i]])
#>         pOK[i] <- hab_mask[(trunc(s[i, 2]) + 1), (trunc(s[i, 
#>             1]) + 1), site[i]]
#>         OK[i] ~ dbern(pOK[i])
#>         dist[i, 1:J] <- sqrt((s[i, 1] - X[1:J, 1, site[i]])^2 + 
#>             (s[i, 2] - X[1:J, 2, site[i]])^2)
#>         p[i, 1:J] <- p0[site[i]] * exp(-dist[i, 1:J]^2/(2 * sigma.pixel[sx[i]]^2))
#>     }
#>     for (i in 1:n0) {
#>         for (j in 1:J) {
#>             y[i, j] ~ dbin(p[i, j], K)
#>         }
#>     }
#>     for (i in (n0 + 1):M) {
#>         zeros[i] ~ dbern((1 - prod(1 - p[i, 1:J])^K) * z[i])
#>     }
#>     N <- sum(z[1:M])
#>     D <- N/A
#> }