Skip to content

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 4 commits into from
Apr 7, 2022
Merged
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
2 changes: 1 addition & 1 deletion DESCRIPTION
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"),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ export(bag_tree)
export(bart)
export(bartMachine_interval_calc)
export(boost_tree)
export(cforest_train)
export(check_empty_ellipse)
export(check_final_param)
export(check_model_doesnt_exist)
export(check_model_exists)
export(contr_one_hot)
export(control_parsnip)
export(convert_stan_interval)
export(ctree_train)
export(cubist_rules)
export(dbart_predict_calc)
export(decision_tree)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# parsnip (development version)

* Added `ctree_train()` and `cforest_train()` wrappers for the functions in the partykit package. Engines for these will be added to other parsnip extension packages.

* Exported `xgb_predict()` which wraps xgboost's `predict()` method for use with parsnip extension packages (#688).


Expand Down
126 changes: 126 additions & 0 deletions R/partykit.R
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
)
)
}
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)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ reference:
- tidy.model_fit
- translate
- starts_with("update")
- matches("_train")

- title: Developer tools
contents:
Expand Down
75 changes: 75 additions & 0 deletions man/ctree_train.Rd

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