-
Notifications
You must be signed in to change notification settings - Fork 92
add partykit wrappers #697
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
Package: parsnip | ||
Title: A Common API to Modeling and Analysis Functions | ||
Version: 0.2.1.9000 | ||
Version: 0.2.1.9001 | ||
Authors@R: c( | ||
person("Max", "Kuhn", , "[email protected]", role = c("aut", "cre")), | ||
person("Davis", "Vaughan", , "[email protected]", role = "aut"), | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,126 @@ | ||
#' A wrapper function for conditional inference tree models | ||
#' | ||
#' These functions are slightly different APIs for [partykit::ctree()] and | ||
#' [partykit::cforest()] that have several important arguments as top-level | ||
#' arguments (as opposed to being specified in [partykit::ctree_control()]). | ||
#' @param formula A symbolic description of the model to be fit. | ||
#' @param data A data frame containing the variables in the model. | ||
#' @param teststat A character specifying the type of the test statistic to be | ||
#' applied. | ||
#' @param testtype A character specifying how to compute the distribution of | ||
#' the test statistic. | ||
#' @param mincriterion The value of the test statistic (for \code{testtype == | ||
#' "Teststatistic"}), or 1 - p-value (for other values of \code{testtype}) that | ||
#' must be exceeded in order to implement a split. | ||
#' @param minsplit The minimum sum of weights in a node in order to be | ||
#' considered for splitting. | ||
#' @param maxdepth maximum depth of the tree. The default \code{maxdepth = Inf} | ||
#' means that no restrictions are applied to tree sizes. | ||
#' @param mtry Number of input variables randomly sampled as candidates at each | ||
#' node for random forest like algorithms. The default \code{mtry = Inf} means | ||
#' that no random selection takes place. | ||
#' @param ntree Number of trees to grow in a forest. | ||
#' @param ... Other options to pass to [partykit::ctree()] or [partykit::cforest()]. | ||
#' @return An object of class `party` (for `ctree`) or `cforest`. | ||
#' @examples | ||
#' if (rlang::is_installed(c("modeldata", "partykit"))) { | ||
#' data(bivariate, package = "modeldata") | ||
#' ctree_train(Class ~ ., data = bivariate_train) | ||
#' ctree_train(Class ~ ., data = bivariate_train, maxdepth = 1) | ||
#' } | ||
#' @export | ||
ctree_train <- | ||
function(formula, | ||
data, | ||
minsplit = 20L, | ||
maxdepth = Inf, | ||
teststat = "quadratic", | ||
testtype = "Bonferroni", | ||
mincriterion = 0.95, | ||
...) { | ||
rlang::check_installed("partykit") | ||
opts <- rlang::list2(...) | ||
|
||
if (any(names(opts) == "control")) { | ||
opts$control$minsplit <- minsplit | ||
opts$control$maxdepth <- maxdepth | ||
opts$control$teststat <- teststat | ||
opts$control$testtype <- testtype | ||
opts$control$mincriterion <- mincriterion | ||
} else { | ||
opts$control <- | ||
rlang::call2( | ||
"ctree_control", | ||
.ns = "partykit", | ||
!!!list( | ||
minsplit = minsplit, | ||
maxdepth = maxdepth, | ||
teststat = teststat, | ||
testtype = testtype, | ||
mincriterion = mincriterion | ||
) | ||
) | ||
} | ||
|
||
tree_call <- | ||
rlang::call2( | ||
"ctree", | ||
.ns = "partykit", | ||
formula = rlang::expr(formula), | ||
data = rlang::expr(data), | ||
!!!opts | ||
) | ||
rlang::eval_tidy(tree_call) | ||
} | ||
|
||
#' @rdname ctree_train | ||
#' @export | ||
cforest_train <- | ||
function(formula, | ||
data, | ||
minsplit = 20L, | ||
maxdepth = Inf, | ||
teststat = "quadratic", | ||
testtype = "Univariate", | ||
mincriterion = 0, | ||
mtry = ceiling(sqrt(ncol(data) - 1)), | ||
ntree = 500L, | ||
...) { | ||
rlang::check_installed("partykit") | ||
force(mtry) | ||
opts <- rlang::list2(...) | ||
|
||
if (any(names(opts) == "control")) { | ||
opts$control$minsplit <- minsplit | ||
opts$control$maxdepth <- maxdepth | ||
opts$control$teststat <- teststat | ||
opts$control$testtype <- testtype | ||
opts$control$logmincriterion <- log(mincriterion) | ||
opts$control$mincriterion <- mincriterion | ||
} else { | ||
opts$control <- | ||
rlang::call2( | ||
"ctree_control", | ||
.ns = "partykit", | ||
!!!list( | ||
minsplit = minsplit, | ||
maxdepth = maxdepth, | ||
teststat = teststat, | ||
testtype = testtype, | ||
mincriterion = mincriterion, | ||
saveinfo = FALSE | ||
) | ||
topepo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
} | ||
opts$mtry <- mtry | ||
opts$ntree <- ntree | ||
forest_call <- | ||
rlang::call2( | ||
"cforest", | ||
.ns = "partykit", | ||
formula = rlang::expr(formula), | ||
data = rlang::expr(data), | ||
!!!opts | ||
) | ||
rlang::eval_tidy(forest_call) | ||
} |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.