Skip to content

Commit cb84e9a

Browse files
committed
export convert_form_to_xy_*() functions
1 parent c058586 commit cb84e9a

File tree

3 files changed

+114
-23
lines changed

3 files changed

+114
-23
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ export(check_empty_ellipse)
124124
export(check_final_param)
125125
export(contr_one_hot)
126126
export(control_parsnip)
127+
export(convert_form_to_xy_fit)
128+
export(convert_form_to_xy_new)
127129
export(convert_stan_interval)
128130
export(decision_tree)
129131
export(eval_args)

R/convert_data.R

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
11
# ------------------------------------------------------------------------------
22

3-
# Functions to take a formula interface and get the resulting
4-
# objects (y, x, weights, etc) back. For the most part, this
5-
# emulates the internals of `lm` (and also see the notes at
6-
# https://developer.r-project.org/model-fitting-functions.html).
7-
8-
# `convert_form_to_xy_fit` is for when the data are created for modeling.
9-
# It saves both the data objects as well as the objects needed
10-
# when new data are predicted (e.g. `terms`, etc.).
11-
12-
# `convert_form_to_xy_new` is used when new samples are being predicted
13-
# and only requires the predictors to be available.
14-
3+
#' Helper functions to convert between formula and matrix interface
4+
#'
5+
#' Functions to take a formula interface and get the resulting
6+
#' objects (y, x, weights, etc) back. For the most part, this
7+
#' emulates the internals of `lm` (and also see the notes at
8+
#' https://developer.r-project.org/model-fitting-functions.html).
9+
#'
10+
#' `convert_form_to_xy_fit` is for when the data are created for modeling.
11+
#' It saves both the data objects as well as the objects needed when new data
12+
#' are predicted (e.g. `terms`, etc.).
13+
#'
14+
#' `convert_form_to_xy_new` is used when new samples are being predicted and
15+
#' only requires the predictors to be available.
16+
#'
17+
#' @param data A data frame containing all relevant variables (e.g. outcome(s),
18+
#' predictors, case weights, etc).
19+
#' @param ... Additional arguments passed to [stats::model.frame()] and
20+
#' specification of `offset` and `contrasts`.
21+
#' @param na.action A function which indicates what should happen when the data
22+
#' contain NAs.
23+
#' @param indicators A string describing whether and how to create
24+
#' indicator/dummy variables from factor predictors.
25+
#' @param composition A string describing whether the resulting `x` and `y`
26+
#' should be returned as a `"matrix"` or a `"data.frame"`.
27+
#' @param remove_intercept A logical indicating whether to remove the intercept
28+
#' column after model.matrix() is finished.
29+
#' @inheritParams fit.model_spec
30+
#' @rdname convert_helpers
31+
#' @keywords internal
32+
#' @export
33+
#'
1534
#' @importFrom stats .checkMFClasses .getXlevels delete.response
1635
#' @importFrom stats model.offset model.weights na.omit na.pass
36+
convert_form_to_xy_fit <- function(formula,
37+
data,
38+
...,
39+
na.action = na.omit,
40+
indicators = "traditional",
41+
composition = "data.frame",
42+
remove_intercept = TRUE) {
1743

18-
convert_form_to_xy_fit <- function(
19-
formula,
20-
data,
21-
...,
22-
na.action = na.omit,
23-
indicators = "traditional",
24-
composition = "data.frame",
25-
remove_intercept = TRUE
26-
) {
2744
if (!(composition %in% c("data.frame", "matrix")))
2845
rlang::abort("`composition` should be either 'data.frame' or 'matrix'.")
2946

@@ -136,8 +153,16 @@ convert_form_to_xy_fit <- function(
136153
res
137154
}
138155

139-
convert_form_to_xy_new <- function(object, new_data, na.action = na.pass,
140-
composition = "data.frame") {
156+
157+
#' @param object An object of class `model_fit`.
158+
#' @rdname convert_helpers
159+
#' @keywords internal
160+
#' @export
161+
convert_form_to_xy_new <- function(object,
162+
new_data,
163+
na.action = na.pass,
164+
composition = "data.frame") {
165+
141166
if (!(composition %in% c("data.frame", "matrix")))
142167
rlang::abort("`composition` should be either 'data.frame' or 'matrix'.")
143168

man/convert_helpers.Rd

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)