diff --git a/DESCRIPTION b/DESCRIPTION index 22dc5b3..e0d3a19 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,7 +33,8 @@ Imports: rjags, stats, stringr, - utils + utils, + rlang Suggests: knitr, RCurl, @@ -46,4 +47,6 @@ LazyLoad: yes LazyData: true URL: https://github.com/nutterb/HydeNet, BugReports: https://github.com/nutterb/HydeNet/issues -RoxygenNote: 7.1.0 +RoxygenNote: 7.3.2 +Encoding: UTF-8 + diff --git a/NAMESPACE b/NAMESPACE index 02336cd..e12a5f6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -53,7 +53,7 @@ export(vectorProbs) export(writeJagsFormula) export(writeNetworkModel) import(nnet) -importFrom(dplyr,group_by_) +importFrom(dplyr,group_by) importFrom(dplyr,summarise) importFrom(magrittr,"%$%") importFrom(magrittr,"%>%") diff --git a/R/Hyde-package.R b/R/HydeNet-package.R similarity index 88% rename from R/Hyde-package.R rename to R/HydeNet-package.R index 00a4854..41f800d 100644 --- a/R/Hyde-package.R +++ b/R/HydeNet-package.R @@ -1,21 +1,26 @@ -#' @name Hyde-package -#' @aliases Hyde +#' @keywords internal +"_PACKAGE" + +## usethis namespace: start #' -#' @title Hydbrid Decision Networks +#' +#' @aliases Hyde #' @description Facilities for easy implementation of hybrid Bayesian networks -#' using R. Bayesian networks are directed acyclic graphs representing joint -#' probability distributions, where each node represents a random variable and +#' @importFrom dplyr group_by +#' @name HydeNet +#' @title Hydbrid Decision Networks +#' burdensome for inference within hybrid networks that contain a combination #' each edge represents conditionality. The full joint distribution is -#' therefore factorized as a product of conditional densities, where each node +#' Econometric analyses (maximum expected utility under competing policies, +#' interface to constructing these networks and running inference using rjags. #' 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 #' like Markov Chain Monte Carlo are popular. We provide a user-friendly -#' interface to constructing these networks and running inference using rjags. -#' Econometric analyses (maximum expected utility under competing policies, -#' value of information) involving decision and utility nodes are also +#' of continuous and discrete nodes, particle-based approximation techniques +#' parent nodes. Since exact, closed-form algorithms are computationally +#' probability distributions, where each node represents a random variable and #' supported. -#' - -NULL \ No newline at end of file +#' therefore factorized as a product of conditional densities, where each node +#' using R. Bayesian networks are directed acyclic graphs representing joint +#' value of information) involving decision and utility nodes are also +## usethis namespace: end +NULL diff --git a/R/HydePosterior.R b/R/HydePosterior.R index 040a4c2..20c1ce8 100644 --- a/R/HydePosterior.R +++ b/R/HydePosterior.R @@ -68,7 +68,7 @@ HydeSim <- function(cHN, variable.names, n.iter, thin=1, ..., if (monitor_observed) { variable.names <- - if (class(cHN[["jags"]]) == "jags") + if (inherits(cHN[["jags"]],"jags")) { unique(c(variable.names, names(cHN$observed))) } @@ -80,7 +80,7 @@ HydeSim <- function(cHN, variable.names, n.iter, thin=1, ..., - if (class(cHN$jags) == "jags") + if (inherits(cHN$jags,"jags")) { codas <- rjags::coda.samples(model = cHN[["jags"]], variable.names = variable.names, diff --git a/R/bindPosterior.R b/R/bindPosterior.R index e93edee..4a4828e 100644 --- a/R/bindPosterior.R +++ b/R/bindPosterior.R @@ -50,7 +50,7 @@ #' bindSim <- function(hydeSim, relabel_factor=TRUE) { - if (class(hydeSim$codas) == "mcmc.list") + if (inherits(hydeSim$codas,"mcmc.list")) { bound <- do.call("rbind", diff --git a/R/cpt.R b/R/cpt.R index c096c99..46402ba 100644 --- a/R/cpt.R +++ b/R/cpt.R @@ -87,6 +87,7 @@ #' reduce = FALSE) #' } #' +utils::globalVariables(c("sumWt")) cpt <- function(x, data, wt, ...) { UseMethod("cpt") @@ -237,19 +238,19 @@ cpt_workhorse <- function(variables, dependentVar, independentVars, joint <- data %>% - dplyr::group_by_(.dots = ..vars) %>% - dplyr::summarise_(wt = ~sum(wt)) + group_by(!!!rlang::syms(..vars))%>% + dplyr::summarise(wt = sum(wt), .groups = "drop") marginal <- joint %>% - dplyr::group_by_(.dots = ..independentVars) %>% - dplyr::summarise_(sumWt = ~sum(wt)) + dplyr::group_by(!!!rlang::syms(..independentVars))%>% + dplyr::summarise(sumWt = sum(wt), .groups = "drop") cpt <- dplyr::left_join(joint, marginal, by = independentVars) %>% - dplyr::mutate_(p = ~ wt / sumWt) %>% - dplyr::select_(~-c(wt, sumWt)) %>% + dplyr::mutate(p = wt / sumWt) %>% + dplyr::select(-c(wt, sumWt)) %>% plyr::daply(c(vars[-1], vars[1]), function(x) x$p) diff --git a/R/print.HydeNetwork.R b/R/print.HydeNetwork.R index 76e7b79..501ebb9 100644 --- a/R/print.HydeNetwork.R +++ b/R/print.HydeNetwork.R @@ -86,11 +86,12 @@ print.HydeNetwork <- function(x, ...) { nodeType <- paste0(nodeType, "(", nodeParam, ")") } - - Formula <- paste0(x[["nodeFitter"]][[node]], ": ", - deparse(x[["nodeFormula"]][[node]]) %>% - trimws() %>% - paste0(collapse = " ")) + Formula <- formatNode(x[["nodeFitter"]][[node]], x[["nodeFormula"]][[node]]) + # + # Formula <- paste0(x[["nodeFitter"]][[node]], ": ", + # deparse(x[["nodeFormula"]][[node]]) %>% + # trimws() %>% + # paste0(collapse = " ")) return(paste(nodeName, nodeType, Formula, sep="\n")) } @@ -111,3 +112,24 @@ print.HydeNetwork <- function(x, ...) })) cat(paste0("\n\n", nodeSummaries, "\n")) } + +#' @name formatNode +#' +#' @param fitter fitter use for formula. +#' @param formula formula use to define node in network. +#' @title Helper function for formatting nodeFitter and nodeFormula. +#' @description Helper function for formatting nodeFitter and nodeFormula to ensure consistent behaviour. + + +formatNode <- function(fitter, formula) { + fitter_str <- if (is.function(fitter)) { + deparse(substitute(fitter)) + } else { + as.character(fitter) + } + formula_str <- deparse(formula) %>% + trimws() %>% + paste0(collapse = " ") + paste0(fitter_str, ": ", formula_str) +} + diff --git a/R/print.HydePosterior.R b/R/print.HydePosterior.R index e57e039..9af70d9 100644 --- a/R/print.HydePosterior.R +++ b/R/print.HydePosterior.R @@ -44,7 +44,7 @@ print.HydeSim <- function(x, ...){ n_distributions <- - if (class(x$codas) == "mcmc.list") + if (inherits(x$codas,"mcmc.list")) { 1 } @@ -54,7 +54,7 @@ print.HydeSim <- function(x, ...){ } n_chains <- - if (class(x[["codas"]]) == "mcmc.list") + if (inherits(x[["codas"]],"mcmc.list")) { length(x[["codas"]]) } @@ -64,7 +64,7 @@ print.HydeSim <- function(x, ...){ } n_iterations <- - if (class(x[["codas"]]) == "mcmc.list") + if (inherits(x[["codas"]], "mcmc.list")) { nrow(x[["codas"]][[1]]) } diff --git a/R/rewriteHydeFormula.R b/R/rewriteHydeFormula.R index 294f3fe..70f2e44 100644 --- a/R/rewriteHydeFormula.R +++ b/R/rewriteHydeFormula.R @@ -1,5 +1,5 @@ #' @name rewriteHydeFormula -#' @importFrom dplyr group_by_ +#' @importFrom dplyr group_by #' @importFrom dplyr summarise #' @importFrom stats as.formula #' @importFrom stringr str_split_fixed diff --git a/man/HydeNet-package.Rd b/man/HydeNet-package.Rd new file mode 100644 index 0000000..5a6f555 --- /dev/null +++ b/man/HydeNet-package.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/HydeNet-package.R +\docType{package} +\name{HydeNet-package} +\alias{HydeNet-package} +\title{HydeNet: Hybrid Bayesian Networks Using R and JAGS} +\description{ +Facilities for easy implementation of hybrid Bayesian networks using R. Bayesian networks are directed acyclic graphs representing joint 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-descendents 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 like Markov Chain Monte Carlo are popular. We provide a user-friendly interface to constructing these networks and running inference using the 'rjags' package. Econometric analyses (maximum expected utility under competing policies, value of information) involving decision and utility nodes are also supported. +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://github.com/nutterb/HydeNet,} + \item Report bugs at \url{https://github.com/nutterb/HydeNet/issues} +} + +} +\keyword{internal} diff --git a/man/Hyde-package.Rd b/man/HydeNet.Rd similarity index 83% rename from man/Hyde-package.Rd rename to man/HydeNet.Rd index 9e344dd..20752f1 100644 --- a/man/Hyde-package.Rd +++ b/man/HydeNet.Rd @@ -1,22 +1,22 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/Hyde-package.R -\name{Hyde-package} -\alias{Hyde-package} +% Please edit documentation in R/HydeNet-package.R +\name{HydeNet} +\alias{HydeNet} \alias{Hyde} -\title{Hydbrid Decision Networks} -\description{ -Facilities for easy implementation of hybrid Bayesian networks -using R. Bayesian networks are directed acyclic graphs representing joint -probability distributions, where each node represents a random variable and +\title{Hydbrid Decision Networks +burdensome for inference within hybrid networks that contain a combination each edge represents conditionality. The full joint distribution is -therefore factorized as a product of conditional densities, where each node +Econometric analyses (maximum expected utility under competing policies, +interface to constructing these networks and running inference using rjags. 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 like Markov Chain Monte Carlo are popular. We provide a user-friendly -interface to constructing these networks and running inference using rjags. -Econometric analyses (maximum expected utility under competing policies, -value of information) involving decision and utility nodes are also +of continuous and discrete nodes, particle-based approximation techniques +parent nodes. Since exact, closed-form algorithms are computationally +probability distributions, where each node represents a random variable and supported. +therefore factorized as a product of conditional densities, where each node +using R. Bayesian networks are directed acyclic graphs representing joint +value of information) involving decision and utility nodes are also} +\description{ +Facilities for easy implementation of hybrid Bayesian networks } diff --git a/man/cpt.Rd b/man/cpt.Rd index 5eb6fda..a3b49ff 100644 --- a/man/cpt.Rd +++ b/man/cpt.Rd @@ -9,8 +9,6 @@ \alias{inputCPT.list} \title{Compute a conditional probability table for a factor given other factors} \usage{ -cpt(x, data, wt, ...) - \method{cpt}{formula}(formula, data, wt, ...) \method{cpt}{list}(x, data, wt, ...) @@ -22,8 +20,8 @@ inputCPT(x, factorLevels, reduce = TRUE, ...) \method{inputCPT}{list}(x, factorLevels, reduce = TRUE, ...) } \arguments{ -\item{x}{a list containing the names of the variables used to compute -the conditional probability table. See details.} +\item{formula}{a formula specifying the relationship between the dependent and +independent variables.} \item{data}{a data frame containing all the factors represented by the \code{formula} parameter.} @@ -32,8 +30,8 @@ parameter.} \item{...}{Additional arguments to be passed to other methods.} -\item{formula}{a formula specifying the relationship between the dependent and -independent variables.} +\item{x}{a list containing the names of the variables used to compute +the conditional probability table. See details.} \item{factorLevels}{(optional) a named list with the following structure: Variable names for the factors specified in \code{vars} comprise the names