Skip to content

Commit

Permalink
null queries
Browse files Browse the repository at this point in the history
  • Loading branch information
dswatson committed Jul 31, 2023
1 parent 3959823 commit 187968c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
33 changes: 24 additions & 9 deletions R/map.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#'
#' @param params Circuit parameters learned via \code{\link{forde}}.
#' @param query Character vector of variable names. MAP estimates will be
#' computed for each.
#' computed for each. If \code{NULL}, all variables other than those
#' in \code{evidence} will be estimated.
#' @param evidence Optional set of conditioning events. This can take one of
#' three forms: (1) a partial sample, i.e. a single row of data with some but
#' not all columns; (2) a data frame of conditioning events, which allows for
Expand Down Expand Up @@ -43,6 +44,9 @@
#' evi <- data.frame(Species = "setosa")
#' map(psi, query = "Sepal.Length", evidence = evi)
#'
#' # Compute MAP estimates for all features other than Species
#' map(psi, evidence = evi)
#'
#'
#' @seealso
#' \code{\link{adversarial_rf}}, \code{\link{forde}}, \code{\link{lik}}
Expand All @@ -55,21 +59,14 @@

map <- function(
params,
query,
query = NULL,
evidence = NULL,
n_eval = 100) {

# To avoid data.table check issues
variable <- family <- tree <- f_idx <- cvg <- wt <- V1 <- value <- val <-
mu <- sigma <- obs <- prob <- fold <- . <- NULL

# Check query
if (any(!query %in% params$meta$variable)) {
err <- setdiff(query, params$meta$variable)
stop('Unrecognized feature(s) in query: ', err)
}
factor_cols <- params$meta[variable %in% query, family == 'multinom']

# Prep evidence
conj <- FALSE
if (!is.null(evidence)) {
Expand All @@ -79,6 +76,24 @@ map <- function(
}
}

# Check query
if (is.null(query)) {
if (isTRUE(conj)) {
query <- setdiff(params$meta$variable, evidence$variable)
} else {
query <- params$meta$variable
if (!is.null(evidence)) {
warning('Computing MAP estimates for all variables. To avoid this ',
'for conditioning variables, consider passing evidence in the ',
'form of a partial sample or data frame of events.')
}
}
} else if (any(!query %in% params$meta$variable)) {
err <- setdiff(query, params$meta$variable)
stop('Unrecognized feature(s) in query: ', err)
}
factor_cols <- params$meta[variable %in% query, family == 'multinom']

# PMF over leaves
if (is.null(evidence)) {
num_trees <- params$forest[, max(tree)]
Expand Down
8 changes: 6 additions & 2 deletions man/map.Rd

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

0 comments on commit 187968c

Please sign in to comment.