Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Low level fixes #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Imports:
rjags,
stats,
stringr,
utils
utils,
rlang
Suggests:
knitr,
RCurl,
Expand All @@ -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

2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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,"%>%")
Expand Down
35 changes: 20 additions & 15 deletions R/Hyde-package.R → R/HydeNet-package.R
Original file line number Diff line number Diff line change
@@ -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
#' 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
4 changes: 2 additions & 2 deletions R/HydePosterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion R/bindPosterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 7 additions & 6 deletions R/cpt.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#' reduce = FALSE)
#' }
#'
utils::globalVariables(c("sumWt"))
cpt <- function(x, data, wt, ...)
{
UseMethod("cpt")
Expand Down Expand Up @@ -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)

Expand Down
32 changes: 27 additions & 5 deletions R/print.HydeNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand All @@ -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)
}

6 changes: 3 additions & 3 deletions R/print.HydePosterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

print.HydeSim <- function(x, ...){
n_distributions <-
if (class(x$codas) == "mcmc.list")
if (inherits(x$codas,"mcmc.list"))
{
1
}
Expand All @@ -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"]])
}
Expand All @@ -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]])
}
Expand Down
2 changes: 1 addition & 1 deletion R/rewriteHydeFormula.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 18 additions & 0 deletions man/HydeNet-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions man/Hyde-package.Rd → man/HydeNet.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions man/cpt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.