Skip to content

Commit

Permalink
Merge pull request #106 from nutterb/current-devel
Browse files Browse the repository at this point in the history
Current devel
  • Loading branch information
nutterb authored Jul 6, 2018
2 parents d63edbb + 19d7faf commit 8914dd3
Show file tree
Hide file tree
Showing 80 changed files with 491 additions and 529 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: HydeNet
Type: Package
Title: Hybrid Bayesian Networks Using R and JAGS
Version: 0.10.5
Version: 0.10.8-01
Author: Jarrod E. Dalton <[email protected]> and Benjamin Nutter
<[email protected]>
Maintainer: Benjamin Nutter <[email protected]>
Expand Down Expand Up @@ -37,6 +37,7 @@ Imports:
utils
Suggests:
knitr,
RCurl,
survival,
testthat
VignetteBuilder: knitr
Expand All @@ -45,4 +46,4 @@ LazyLoad: yes
LazyData: true
URL: https://github.com/nutterb/HydeNet,
BugReports: https://github.com/nutterb/HydeNet/issues
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ S3method(modelToNode,survreg)
S3method(modelToNode,xtabs)
S3method(plot,HydeNetwork)
S3method(print,HydeNetwork)
S3method(print,HydePosterior)
S3method(print,HydeSim)
S3method(print,cpt)
S3method(summary,HydeNetwork)
S3method(update,HydeNetwork)
Expand All @@ -30,7 +30,9 @@ export("%>%")
export(HydeNetwork)
export(HydePlotOptions)
export(HydePosterior)
export(HydeSim)
export(bindPosterior)
export(bindSim)
export(compileDecisionModel)
export(compileJagsModel)
export(cpt)
Expand All @@ -53,7 +55,6 @@ export(writeNetworkModel)
import(nnet)
importFrom(dplyr,bind_rows)
importFrom(dplyr,group_by_)
importFrom(dplyr,mutate)
importFrom(dplyr,summarise)
importFrom(gRbase,dag)
importFrom(gRbase,graphNEL2adjMAT)
Expand All @@ -65,7 +66,6 @@ importFrom(stats,as.formula)
importFrom(stats,coef)
importFrom(stats,terms)
importFrom(stats,update)
importFrom(stringr,perl)
importFrom(stringr,str_extract)
importFrom(stringr,str_extract_all)
importFrom(stringr,str_split_fixed)
Expand Down
2 changes: 1 addition & 1 deletion R/Hyde-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' probability distributions, where each node represents a random variable and
#' each edge represents conditionality. The full joint distribution is
#' therefore factorized as a product of conditional densities, where each node
#' is assumed to be independent of its non-desendents given information on its
#' is assumed to be independent of its non-descendants given information on its
#' parent nodes. Since exact, closed-form algorithms are computationally
#' burdensome for inference within hybrid networks that contain a combination
#' of continuous and discrete nodes, particle-based approximation techniques
Expand Down
30 changes: 15 additions & 15 deletions R/HydeNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#' to fitter functions.
#' \item \code{nodeParams} A named list. Each element is a vector of parameters
#' that will be expected by JAGS.
#' \item \code{fromData} A named list with the logical value of wheather parameters
#' \item \code{fromData} A named list with the logical value of whether parameters
#' should be estimated from the data.
#' \item \code{nodeData} A named list with the data for each node. If a node's
#' entry in \code{fromData} is \code{TRUE} and \code{nodeData} is \code{NULL},
Expand Down Expand Up @@ -200,7 +200,7 @@ HydeNetwork.formula <- function(nodes, data=NULL, ...)
FUN = is.factor,
FUN.VALUE = logical(1))]
factorLevels[factor_vars] <-
lapply(X = data[, factor_vars, drop = FALSE],
lapply(X = data[factor_vars],
FUN = levels)
}

Expand Down Expand Up @@ -354,7 +354,7 @@ HydeNetwork_nodeFormula <- function(x, parents, data, fromData)
{
if (is.null(parents[[x]]))
{
if (fromData[[names(parents)[x]]] & !is.numeric(data[, names(parents)[x]]))
if (fromData[[names(parents)[x]]] & !is.numeric(data[[names(parents)[x]]]))
{
f <- paste("~ ", names(parents)[x])
}
Expand All @@ -381,24 +381,24 @@ HydeNetwork_nodeFitter <- function(node_name, data, parents)
{
return(NULL)
}
else if (is.numeric(data[, node_name]))
else if (is.numeric(data[[node_name]]))
{
return("lm")
}
else if (is.factor(data[, node_name]) & is.null(parents[[node_name]]))
else if (is.factor(data[[node_name]]) & is.null(parents[[node_name]]))
{
return("xtabs")
}
else if (is.factor(data[, node_name]) &
all(vapply(parents[[node_name]], function(p) is.factor(data[, p]), logical(1))))
else if (is.factor(data[[node_name]]) &
all(vapply(parents[[node_name]], function(p) is.factor(data[[p]]), logical(1))))
{
return("cpt")
}
else if (is.factor(data[, node_name]) & nlevels(data[, node_name]) == 2)
else if (is.factor(data[[node_name]]) & nlevels(data[[node_name]]) == 2)
{
return("glm")
}
else if (is.factor(data[, node_name]) & nlevels(data[, node_name]) > 2)
else if (is.factor(data[[node_name]]) & nlevels(data[[node_name]]) > 2)
{
return("multinom")
}
Expand All @@ -417,10 +417,10 @@ HydeNetwork_nodeType <- function(node_name, data, parents, nodeFitter)
if (node_name %in% names(data))
{
if ((is.null(parents[[node_name]]) &&
!is.numeric(data[, node_name])) ||
!is.numeric(data[[node_name]])) ||
(!is.null(parents[[node_name]]) &&
!is.numeric(data[, node_name]) &&
nlevels(data[, node_name]) > 2))
!is.numeric(data[[node_name]]) &&
nlevels(data[[node_name]]) > 2))
{
return('dcat')
}
Expand All @@ -429,10 +429,10 @@ HydeNetwork_nodeType <- function(node_name, data, parents, nodeFitter)
return('dcat')
}
else if ((is.null(parents[[node_name]]) &&
!is.numeric(data[, node_name])) ||
!is.numeric(data[[node_name]])) ||
(!is.null(parents[[node_name]]) &&
!is.numeric(data[, node_name]) &&
nlevels(data[, node_name]) == 2))
!is.numeric(data[[node_name]]) &&
nlevels(data[[node_name]]) == 2))
{
return('dbern')
}
Expand Down
35 changes: 20 additions & 15 deletions R/HydePosterior.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' @name HydePosterior
#' @export HydePosterior
#' @name HydeSim
#' @export HydeSim
#'
#' @title Posterior Distributions of a Decision Network
#' @description The posterior distributions of the decision network can be
#' @title Simulated Distributions of a Decision Network
#' @description The simulated distributions of the decision network can be
#' evaluated to determine the probabilistic outcomes based on the decision
#' inputs in the model as well as subject specific factors.
#'
Expand All @@ -18,7 +18,7 @@
#' forced into \code{variable.names} if not already provided. This is
#' recommended, especially if you will be binding multiple JAGS runs
#' together.
#' @param bind Logical. If \code{TRUE}, posterior distributions will be bound into
#' @param bind Logical. If \code{TRUE}, simulated distributions will be bound into
#' a single data frame. If \code{FALSE}, the standard output from \code{rjags}
#' is returned.
#'
Expand All @@ -27,16 +27,12 @@
#' the rows of the policy/decision matrix given in the \code{data} argument
#' of \code{compileJagsNetwork}.
#'
#' @return A list of class \code{HydePosterior} with elements \code{codas}
#' @return A list of class \code{HydeSim} with elements \code{codas}
#' (the MCMC matrices from \code{coda.samples}), \code{observed} (the values
#' of the variables that were observed), \code{dag} (the dag object for
#' convenience in displaying the network), and \code{factorRef} (giving the
#' mappings of factor levels to factor variables).
#'
#' The only rationale for giving this object its own class was because it
#' produces an enormous amount of material to be printed. A distinct
#' \code{print} method has been written for this object.
#'
#' @author Jarrod Dalton and Benjamin Nutter
#'
#' @examples
Expand All @@ -53,20 +49,20 @@
#' compiledNet <- compileJagsModel(Net, n.chains=5)
#'
#' #* Generate the posterior distribution
#' Posterior <- HydePosterior(compiledNet,
#' Posterior <- HydeSim(compiledNet,
#' variable.names = c("d.dimer", "death"),
#' n.iter = 1000)
#'
#' #* Posterior Distributions for a Decision Model
#' Net <- setDecisionNodes(Net, angio, treat)
#' decisionNet <- compileDecisionModel(Net, n.chains=5)
#' decisionsPost <- HydePosterior(decisionNet,
#' decisionsPost <- HydeSim(decisionNet,
#' variable.names = c("d.dimer", "death"),
#' n.iter = 1000)
#'
#'

HydePosterior <- function(cHN, variable.names, n.iter, thin=1, ...,
HydeSim <- function(cHN, variable.names, n.iter, thin=1, ...,
monitor_observed=TRUE, bind=TRUE)
{
if (monitor_observed)
Expand Down Expand Up @@ -121,14 +117,23 @@ HydePosterior <- function(cHN, variable.names, n.iter, thin=1, ...,



class(HydePost) <- "HydePosterior"
class(HydePost) <- "HydeSim"
if (bind)
{
bindPosterior(HydePost)
bindSim(HydePost)
}
else
{
HydePost
}

}

#' @rdname HydeSim
#' @export

HydePosterior <- function(...)
{
message("`HydePoseterior` has been deprecated and replaced by `HydeSim`")
HydeSim(...)
}
8 changes: 2 additions & 6 deletions R/HydeUtilities.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#' @name HydeUtilities
#' @importFrom dplyr group_by_
#' @importFrom dplyr mutate
#' @importFrom magrittr %>%
#' @export %>%
#'
#' @title Hyde Network Utility Functions
#' @description The functions described below are unexported functions that
#' are used internally by \code{HydeNet} to prepare modify network objects
#' are used internally by \code{HydeNet} to prepare and modify network objects
#' and prepare JAGS code.
#'
#' @details
Expand All @@ -25,7 +23,7 @@
#'
#' \code{matchLevelNumber}: Assigns the correct numeric value of a level to
#' a factor variable in a model. This is called from within
#' \code{makeJagsRead}.
#' \code{makeJagsReady}.
#'
#' \code{matchVars}: Given a list of existing node names, the terms of a formula
#' are matched to the node names. This allows functions to be used in
Expand Down Expand Up @@ -89,8 +87,6 @@ termName <- function(term, reg)
}

#' @rdname HydeUtilities
#' @importFrom stringr perl
#' @importFrom stringr str_extract
#'
#' @param node Character string indicating a node in a network
#' @param network A Hyde Network Object
Expand Down
6 changes: 3 additions & 3 deletions R/PolicyMatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
#' enforced. Thus, it is possible to include numeric values in a decision
#' matrix.
#'
#' Policy matrices can be passed to \code{HydePosterior} to run posterior
#' Policy matrices can be passed to \code{HydeSim} to run posterior
#' distributions on each row of the policy matrix. There is nothing
#' particularly special about the policy matrices returned by
#' \code{policyMatrix}; they are simply data frame that require names drawn
#' from the nodes in the network. Any data frame can be passed to
#' \code{HydePosterior} and a check is done there to confirm all of the
#' \code{HydeSim} and a check is done there to confirm all of the
#' column names match a node in the network.
#'
#' Whenever a node is identified as a deterministic node, its policy values
#' are forced to \code{NULL}, regardless of what the user has specified.
#'
#' @return Returns a data frame built by \code{expand.grid} and intended to be
#' used with \code{HydePosterior}.
#' used with \code{HydeSim}.
#'
#' @author Jarrod Dalton and Benjamin Nutter
#'
Expand Down
Loading

0 comments on commit 8914dd3

Please sign in to comment.