-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/current-devel' into current-devel
Conflicts: DESCRIPTION NEWS
- Loading branch information
Showing
9 changed files
with
188 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Package: HydeNet | ||
Type: Package | ||
Title: Hybrid Bayesian Networks Using R and JAGS | ||
Version: 0.10.0 | ||
Date: 2015-10-11 | ||
Version: 0.10.1 | ||
Date: 2015-10-30 | ||
Author: Jarrod E. Dalton <[email protected]> and Benjamin Nutter <[email protected]> | ||
Maintainer: Benjamin Nutter <[email protected]> | ||
Description: Facilities for easy implementation of hybrid Bayesian networks | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#' @name factorFormula | ||
#' @export factorFormula | ||
#' @importFrom stringr str_extract_all | ||
#' @importFrom stringr str_split_fixed | ||
#' | ||
#' @title Convert Factor Levels in Formula to Numeric Values | ||
#' @description When working in R, it is often more convenient to work in | ||
#' terms of the factor labels rather than the underlying numeric values. | ||
#' JAGS, however, requires that the numeric values be used. | ||
#' \code{factorFormula} permits the user to define formulae to be passed | ||
#' to JAGS using R style coding, and having factor levels translated | ||
#' to the underlying values as determined by the network structure. | ||
#' | ||
#' @param form A formula object. | ||
#' @param network A \code{HydeNetwork} object. | ||
#' | ||
#' @details It is assumed that factor variables will be used in logical | ||
#' comparisons of the format \code{[variable_name] == '[factor_level]'} and | ||
#' only this pattern is recognized in the text search. Single or | ||
#' double quotes may be used around the level, and the spaces aroudn the | ||
#' \code{==} are optional. | ||
#' | ||
#' While there are certainly limitations to this function that we have | ||
#' not yet found, we believe it covers the majority of cases in which | ||
#' it is useful. More complex cases that can't be handled by | ||
#' \code{factorFormula} may require writing native JAGS code. | ||
#' | ||
#' @author Jarrod Dalton and Benjamin Nutter | ||
#' | ||
#' @examples | ||
#' Net <- HydeNetwork(~ wells + | ||
#' pe | wells + | ||
#' d.dimer | pregnant*pe + | ||
#' angio | pe + | ||
#' treat | d.dimer*angio + | ||
#' death | pe*treat, | ||
#' data = PE) | ||
#' factorFormula(form = payoff ~ (death == 'No') + (pe == 'Yes'), | ||
#' network = Net) | ||
#' | ||
factorFormula <- function(form, network){ | ||
form <- deparse(form) | ||
|
||
relabel <- extractFactors(form) | ||
|
||
relabel_mat <- isolateVariableFromLabel(relabel) | ||
|
||
new_label <- | ||
mapply(getNumericLevel, | ||
varname = relabel_mat[, 1], | ||
label = relabel_mat[, 2], | ||
nodeType = relabel_mat[, 3], | ||
MoreArgs = list(network = network)) | ||
|
||
form <- rewriteFormula(relabel, new_label, form) | ||
|
||
as.formula(form) | ||
} | ||
|
||
extractFactors <- function(form){ | ||
#* This pattern looks for any combination of numbers, letters, | ||
#* periods or underscores, | ||
#* followed by a space (or not), | ||
#* followed by == | ||
#* followed by a space (or not) | ||
#* followed by a quote (single or double) | ||
#* followed by any character string | ||
#* followed by a quote (single or double) | ||
#* It is intened to catch a [variable_name] == [factor_level] | ||
relabel <- | ||
stringr::str_extract_all(string = form, | ||
pattern = "[[:alpha:],[0-9],[.],[_]]+( |)[=][=]( |)('|\").*?('|\")") | ||
unlist(relabel) | ||
} | ||
|
||
isolateVariableFromLabel <- function(relabel){ | ||
relabel_mat <- stringr::str_split_fixed(relabel, "[=][=]", 2) | ||
relabel_mat <- trimws(relabel_mat) | ||
relabel_mat <- gsub("('|\")", "", relabel_mat) | ||
cbind(relabel_mat, | ||
unlist(network$nodeType[relabel_mat[, 1]])) | ||
} | ||
|
||
getNumericLevel <- function(varname, label, nodeType, network){ | ||
value <- which(network$factorLevels[[varname]] == label) | ||
if (nodeType == "dbern") value <- as.numeric(value) - 1 | ||
as.character(value) | ||
} | ||
|
||
rewriteFormula <- function(relabel, new_label, form){ | ||
for (i in seq_along(relabel)){ | ||
new_label[i] <- sub("(?<=('|\")).*?(?=('|\"))", | ||
new_label[i], | ||
relabel[i], | ||
perl = TRUE) | ||
new_label[i] <- gsub("('|\")", "", new_label[i]) | ||
form <- sub(relabel[i], new_label[i], form) | ||
} | ||
form | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
% Generated by roxygen2 (4.1.1): do not edit by hand | ||
% Please edit documentation in R/factorFormula.R | ||
\name{factorFormula} | ||
\alias{factorFormula} | ||
\title{Convert Factor Levels in Formula to Numeric Values} | ||
\usage{ | ||
factorFormula(form, network) | ||
} | ||
\arguments{ | ||
\item{form}{A formula object.} | ||
|
||
\item{network}{A \code{HydeNetwork} object.} | ||
} | ||
\description{ | ||
When working in R, it is often more convenient to work in | ||
terms of the factor labels rather than the underlying numeric values. | ||
JAGS, however, requires that the numeric values be used. | ||
\code{factorFormula} permits the user to define formulae to be passed | ||
to JAGS using R style coding, and having factor levels translated | ||
to the underlying values as determined by the network structure. | ||
} | ||
\details{ | ||
It is assumed that factor variables will be used in logical | ||
comparisons of the format \code{[variable_name] == '[factor_level]'} and | ||
only this pattern is recognized in the text search. Single or | ||
double quotes may be used around the level, and the spaces aroudn the | ||
\code{==} are optional. | ||
|
||
While there are certainly limitations to this function that we have | ||
not yet found, we believe it covers the majority of cases in which | ||
it is useful. More complex cases that can't be handled by | ||
\code{factorFormula} may require writing native JAGS code. | ||
} | ||
\examples{ | ||
Net <- HydeNetwork(~ wells + | ||
pe | wells + | ||
d.dimer | pregnant*pe + | ||
angio | pe + | ||
treat | d.dimer*angio + | ||
death | pe*treat, | ||
data = PE) | ||
factorFormula(form = payoff ~ (death == 'No') + (pe == 'Yes'), | ||
network = Net) | ||
} | ||
\author{ | ||
Jarrod Dalton and Benjamin Nutter | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
context("factorFormula") | ||
|
||
test_that("factorFormula", | ||
{ | ||
Net <- HydeNetwork(~ wells + | ||
pe | wells + | ||
d.dimer | pregnant*pe + | ||
angio | pe + | ||
treat | d.dimer*angio + | ||
death | pe*treat, | ||
data = PE) | ||
expect_equal(factorFormula(death ~ ilogit((treat == "No") + (angio == "Positive")), | ||
Net), | ||
death ~ ilogit((treat == 0) + (angio == 2))) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters