-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overhaul the unused Probabilities method. Make use of the helper function CheckPredictionVariables that checks the newdata argument
- Loading branch information
jrwishart
committed
Apr 26, 2023
1 parent
7fbc515
commit 02d7e5a
Showing
5 changed files
with
104 additions
and
31 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
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,17 +1,33 @@ | ||
#' Probabilities | ||
#' \code{Probabilities} A generic function used to extract one or more | ||
#' variables containing probabilities relating to cases (e.g., segment membership). | ||
#' @param object An object for which probabilities are desired. | ||
#' @param ... Additional argument | ||
#' | ||
#' Estimates the probability of group membership for the data passed into \code{newdata} or | ||
#' the data used to fit the model if \code{newdata} is not specified. Intended to be used | ||
#' for the classifiers in the packages \code{flipRegression} and \code{flipMultivariates}. | ||
#' | ||
#' @param object A \code{MachineLearning} or \code{Regression} object. | ||
#' @param newdata Optionally, a data frame including the variables used to fit the model. | ||
#' If not provided, the object$model is used instead. | ||
#' @param ... Optional arguments to pass to \code{predict} or other functions. | ||
#' @return A matrix of predicted probabilities for the observation to belong to each | ||
#' class label. | ||
#' @export | ||
Probabilities <- function(object, ...) { | ||
Probabilities <- function(object, newdata = NULL, ...) | ||
{ | ||
newdata <- validateNewData(object, newdata) | ||
UseMethod("Probabilities") | ||
} | ||
|
||
#' #' @inheritParams Probabilities | ||
#' #' @describeIn Probabilities Error occurs as no method has been specified. | ||
#' #' @export | ||
#' Probabilities.default = function(object, ...) | ||
#' { | ||
#' stop("No 'Probabilities' method exists for this class of objects.") | ||
#' } | ||
validateNewData <- function(object, newdata) | ||
{ | ||
# CheckPredictionVariables is still required without newdata because empty training levels are removed | ||
if (is.null(newdata)) | ||
return(suppressWarnings(CheckPredictionVariables(object, object$model))) | ||
stopifnot("newdata must be a data.frame" = is.data.frame(newdata), | ||
"Need at least one observation in the newdata argument" = NROW(newdata) > 0) | ||
CheckPredictionVariables(object, newdata) | ||
} | ||
|
||
Probabilities.default <- function(object, newdata = NULL, ...) | ||
{ | ||
stop("Probabilities is not implemented for this object type") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
context("Probabilities") | ||
|
||
test_that("Probabilities", { | ||
|
||
expect_error(Probabilities(z <- 1)) | ||
# See test-checks.R for tests about Probabilities(object) which uses the model | ||
# Test newdata argument | ||
bad.newdata <- list(1:10, list(), TRUE, 1 + 1i, "") | ||
dummy.object <- list() | ||
for (newdat in bad.newdata) | ||
expect_error(Probabilities(object = dummy.object, newdata = newdat), | ||
"newdata must be a data.frame") | ||
expect_error(Probabilities(object = dummy.object, newdata = data.frame()), | ||
"Need at least one observation in the newdata argument") | ||
}) |