Title: | Automatically Write 'JAGS' Code for Hierarchical Bayesian Models on Trees |
---|---|
Description: | When relationships between sources of data can be represented by a tree, the generation of appropriate Markov Chain Monte Carlo modeling code to be used with 'JAGS' to run a Bayesian hierarchical model can be automatically generated by this package. Any admissible tree-structured data can be used, under the assumption that node counts are multinomial and branching probabilities are Dirichlet among sibling groups. The methodological basis used to create this package can be found in Flynn (2023) <http://hdl.handle.net/2429/86174>. |
Authors: | Mallory J Flynn [cre, aut] |
Maintainer: | Mallory J Flynn <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0.1 |
Built: | 2024-11-12 05:33:52 UTC |
Source: | https://github.com/malfly/jagstree |
Small, artificially generated toy data set to demonstrate package functionality
data(data1)
data(data1)
An object of class "data.frame"
A node label and started point of directed edge (parent node)
A node label and endpoint of directed edge (child node)
A numerical value assumed to be survey count belonging to 'to' node (integer)
A numerical value assumed to be survey sample size (integer)
A numerical value for marginal count if leaf node (integer)
A boolean value for if survey size is entire population (logical)
A string describing 'to' node (string)
This data set was artificially created for the JAGStree package.
data(data1) head(data1)
data(data1) head(data1)
Small, artificially generated toy data set to demonstrate package functionality with a simpler data frame
data(data2)
data(data2)
An object of class "data.frame"
A node label and started point of directed edge (parent node)
A node label and endpoint of directed edge (child node)
This data set was artificially created for the JAGStree package.
data(data2) head(data2)
data(data2) head(data2)
Small, artificially generated toy data set to demonstrate package functionality with a multinomial distribution among node branching
data(data3)
data(data3)
An object of class "data.frame"
A node label and started point of directed edge (parent node)
A node label and endpoint of directed edge (child node)
A numerical value assumed to be survey count belonging to 'to' node (integer)
A numerical value assumed to be survey sample size (integer)
A numerical value for marginal count if leaf node (integer)
A boolean value for if survey size is entire population (logical)
A string describing 'to' node (string)
This data set was artificially created for the JAGStree package.
data(data3) head(data3)
data(data3) head(data3)
Generates a .mod or .txt file with 'JAGS' code for Bayesian hierarchical model on tree structured data
makeJAGStree(data, prior = "lognormal", filename = "JAGSmodel.mod")
makeJAGStree(data, prior = "lognormal", filename = "JAGSmodel.mod")
data |
A dataframe object representing tree-structure |
prior |
A string representing the choice of prior for the root node population size; can be set to "lognormal" (default) or "uniform" |
filename |
A string containing the file name for resulting output 'JAGS' model file; must end in .mod or .txt |
A .mod or .txt file that contains code ready to run a Bayesian hierarchical model in 'JAGS' based on the input tree-structured data
# optional use of the AutoWMM package to show tree structure Sys.setenv("RGL_USE_NULL" = TRUE) tree <- makeTree(data1) drawTree(tree) makeJAGStree(data1, filename=file.path(tempdir(), "data1_JAGSscript.mod")) makeJAGStree(data1, filename=file.path(tempdir(), "data1_JAGSscript.txt")) # second example makeJAGStree(data2, filename=file.path(tempdir(), "data2_JAGSscript.mod")) makeJAGStree(data2, filename=file.path(tempdir(), "data2_JAGSscript.mod", prior = "uniform")) # third example, showing optional execution with MCMC in R makeJAGStree(data3, filename=file.path(tempdir(), "multiScript.mod")) makeJAGStree(data3, filename=file.path(tempdir(), "multiScript.txt")) mcmc.data <- list( "DE" = c(50, NA), "ABC" = c(NA, 500, NA), "pZ1" = 4, # dirichlet parameters for prior on u "pZ2" = 5, # last parameter to sample U "pZ3" = 1, "pA1" = 10, # beta parameters for prior on p "pA2" = 1, "mu" = log(1000), # lognormal mean Z "tau" = 1/(0.1^2)) # lognormal precision (1/variance) of Z ## define parameters whose posteriors we are interested in mod.params <- c("Z", "ABC", "DE", "pZ", "pA") ## modify initial values mod.inits.cont <- function(){ list("Z.cont" = runif(1, 700, 1500), "ABC" = c(round(runif(1, 100, 200)), NA, NA), "pA" = as.vector(rbeta(1,1,1)), "pZ" = as.vector(rdirichlet(1, c(1,1,1)))) } ## Generate list of initial values to match number of chains numchains <- 6 mod.initial.cont <- list() i <- 1 while(i <= numchains){ mod.initial.cont[[i]] <- mod.inits.cont() i = i+1 } ## now fit the model in JAGS mod.fit <- jags(data = mcmc.data, inits = mod.initial.cont, parameters.to.save = mod.params, n.chains = numchains, n.iter = 500, n.burnin = 200, model.file = "multiScript.mod") print(mod.fit) ## plots using mcmcplots library mod.fit.mcmc <- as.mcmc(mod.fit) denplot(mod.fit.mcmc, parms = c("Z", "ABC[1]", "ABC[3]")) denplot(mod.fit.mcmc, parms = c("pZ", "pA")) traplot(mod.fit.mcmc, parms = c("Z", "ABC[1]", "ABC[3]")) traplot(mod.fit.mcmc, parms = c("pZ", "pA"))
# optional use of the AutoWMM package to show tree structure Sys.setenv("RGL_USE_NULL" = TRUE) tree <- makeTree(data1) drawTree(tree) makeJAGStree(data1, filename=file.path(tempdir(), "data1_JAGSscript.mod")) makeJAGStree(data1, filename=file.path(tempdir(), "data1_JAGSscript.txt")) # second example makeJAGStree(data2, filename=file.path(tempdir(), "data2_JAGSscript.mod")) makeJAGStree(data2, filename=file.path(tempdir(), "data2_JAGSscript.mod", prior = "uniform")) # third example, showing optional execution with MCMC in R makeJAGStree(data3, filename=file.path(tempdir(), "multiScript.mod")) makeJAGStree(data3, filename=file.path(tempdir(), "multiScript.txt")) mcmc.data <- list( "DE" = c(50, NA), "ABC" = c(NA, 500, NA), "pZ1" = 4, # dirichlet parameters for prior on u "pZ2" = 5, # last parameter to sample U "pZ3" = 1, "pA1" = 10, # beta parameters for prior on p "pA2" = 1, "mu" = log(1000), # lognormal mean Z "tau" = 1/(0.1^2)) # lognormal precision (1/variance) of Z ## define parameters whose posteriors we are interested in mod.params <- c("Z", "ABC", "DE", "pZ", "pA") ## modify initial values mod.inits.cont <- function(){ list("Z.cont" = runif(1, 700, 1500), "ABC" = c(round(runif(1, 100, 200)), NA, NA), "pA" = as.vector(rbeta(1,1,1)), "pZ" = as.vector(rdirichlet(1, c(1,1,1)))) } ## Generate list of initial values to match number of chains numchains <- 6 mod.initial.cont <- list() i <- 1 while(i <= numchains){ mod.initial.cont[[i]] <- mod.inits.cont() i = i+1 } ## now fit the model in JAGS mod.fit <- jags(data = mcmc.data, inits = mod.initial.cont, parameters.to.save = mod.params, n.chains = numchains, n.iter = 500, n.burnin = 200, model.file = "multiScript.mod") print(mod.fit) ## plots using mcmcplots library mod.fit.mcmc <- as.mcmc(mod.fit) denplot(mod.fit.mcmc, parms = c("Z", "ABC[1]", "ABC[3]")) denplot(mod.fit.mcmc, parms = c("pZ", "pA")) traplot(mod.fit.mcmc, parms = c("Z", "ABC[1]", "ABC[3]")) traplot(mod.fit.mcmc, parms = c("pZ", "pA"))