Skip to content

Commit 3fd6a35

Browse files
authored
Remove ability to pass offset through ... (#580)
1 parent 42a5707 commit 3fd6a35

File tree

3 files changed

+12
-85
lines changed

3 files changed

+12
-85
lines changed

R/convert_data.R

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
#'
2020
#' @param data A data frame containing all relevant variables (e.g. outcome(s),
2121
#' predictors, case weights, etc).
22-
#' @param ... Additional arguments passed to [stats::model.frame()] and
23-
#' specification of `offset`.
22+
#' @param ... Additional arguments passed to [stats::model.frame()].
2423
#' @param na.action A function which indicates what should happen when the data
2524
#' contain NAs.
2625
#' @param indicators A string describing whether and how to create
@@ -55,13 +54,6 @@
5554
mf_call[[names(dots)[i]]] <- get_expr(dots[[i]])
5655
}
5756

58-
# For new data, save the expression to create offsets (if any)
59-
if (any(names(dots) == "offset")) {
60-
offset_expr <- get_expr(dots[["offset"]])
61-
} else {
62-
offset_expr <- NULL
63-
}
64-
6557
mod_frame <- eval_tidy(mf_call)
6658
mod_terms <- attr(mod_frame, "terms")
6759

@@ -81,14 +73,9 @@
8173
rlang::abort("`weights` must be a numeric vector")
8274
}
8375

84-
offset <- as.vector(model.offset(mod_frame))
85-
if (!is.null(offset)) {
86-
if (length(offset) != nrow(mod_frame)) {
87-
rlang::abort(
88-
glue::glue("The offset data should have {nrow(mod_frame)} elements.")
89-
)
90-
}
91-
}
76+
# TODO: Do we actually use the offset when fitting?
77+
# Extract any inline offsets specified in the formula from the model frame
78+
offset <- model.offset(mod_frame)
9279

9380
if (indicators != "none") {
9481
if (indicators == "one_hot") {
@@ -127,7 +114,6 @@
127114
offset = offset,
128115
terms = mod_terms,
129116
xlevels = .getXlevels(mod_terms, mod_frame),
130-
offset_expr = offset_expr,
131117
options = options
132118
)
133119
} else {
@@ -144,7 +130,6 @@
144130
offset = offset,
145131
terms = mod_terms,
146132
xlevels = .getXlevels(mod_terms, mod_frame),
147-
offset_expr = offset_expr,
148133
options = options
149134
)
150135
}
@@ -168,33 +153,6 @@
168153
mod_terms <- object$terms
169154
mod_terms <- delete.response(mod_terms)
170155

171-
# Calculate offset(s). These can show up in-line in the formula
172-
# (in multiple places) and might also be as its own argument. If
173-
# there is more than one offset, we add them together.
174-
175-
offset_cols <- attr(mod_terms, "offset")
176-
177-
# If offset was done at least once in-line
178-
if (!is.null(offset_cols)) {
179-
offset <- rep(0, nrow(new_data))
180-
for (i in offset_cols) {
181-
offset <- offset +
182-
eval_tidy(
183-
attr(mod_terms, "variables")[[i + 1]],
184-
new_data
185-
) # use na.action here and below?
186-
}
187-
} else {
188-
offset <- NULL
189-
}
190-
191-
if (!is.null(object$offset_expr)) {
192-
if (is.null(offset)) {
193-
offset <- rep(0, nrow(new_data))
194-
}
195-
offset <- offset + eval_tidy(object$offset_expr, new_data)
196-
}
197-
198156
new_data <-
199157
model.frame(
200158
mod_terms,
@@ -208,6 +166,11 @@
208166
.checkMFClasses(cl, new_data)
209167
}
210168

169+
# TODO: Do we actually use the returned offsets anywhere for prediction?
170+
# Extract offset from model frame. Multiple offsets will be added together.
171+
# Offsets might have been supplied through the formula.
172+
offset <- model.offset(new_data)
173+
211174
if (object$options$indicators != "none") {
212175
if (object$options$indicators == "one_hot") {
213176
local_one_hot_contrasts()
@@ -322,7 +285,7 @@ local_one_hot_contrasts <- function(frame = rlang::caller_env()) {
322285
}
323286

324287
check_form_dots <- function(x) {
325-
good_args <- c("subset", "weights", "offset")
288+
good_args <- c("subset", "weights")
326289
good_names <- names(x) %in% good_args
327290
if (any(!good_names)) {
328291
rlang::abort(

R/fit_helpers.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,15 @@ form_xy <- function(object, control, env,
138138

139139
res <- xy_xy(
140140
object = object,
141-
env = env, #weights! offsets!
141+
env = env, #weights!
142142
control = control,
143143
target = target
144144
)
145145
data_obj$y_var <- all.vars(env$formula[[2]])
146146
data_obj$x <- NULL
147147
data_obj$y <- NULL
148148
data_obj$weights <- NULL
149+
# TODO: Should we be using the offset that we remove here?
149150
data_obj$offset <- NULL
150151
res$preproc <- data_obj
151152
res

tests/testthat/test_convert_data.R

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,6 @@ test_that("numeric x and y, weights", {
8686
expect_null(observed$offset)
8787
})
8888

89-
test_that("numeric x and y, offset", {
90-
expected <- lm(
91-
mpg ~ . - disp,
92-
data = mtcars,
93-
offset = disp,
94-
x = TRUE,
95-
y = TRUE
96-
)
97-
observed <-
98-
.convert_form_to_xy_fit(
99-
mpg ~ . - disp,
100-
data = mtcars,
101-
offset = log(disp),
102-
indicators = "traditional",
103-
remove_intercept = TRUE
104-
)
105-
expect_equal(format_x_for_test(expected$x), observed$x)
106-
expect_equivalent(mtcars$mpg, observed$y)
107-
expect_equal(expected$terms, observed$terms)
108-
expect_equal(expected$xlevels, observed$xlevels)
109-
expect_equal(log(mtcars$disp), observed$offset)
110-
expect_null(observed$weights)
111-
112-
new_obs <-
113-
.convert_form_to_xy_new(observed, new_data = mtcars[1:6, ])
114-
expect_equal(mtcars[1:6, -c(1, 3)], new_obs$x)
115-
expect_equal(log(mtcars$disp)[1:6], new_obs$offset)
116-
})
117-
11889
test_that("numeric x and y, offset in-line", {
11990
expected <- lm(mpg ~ cyl + hp + offset(log(disp)),
12091
data = mtcars,
@@ -402,14 +373,6 @@ test_that("bad args", {
402373
remove_intercept = TRUE
403374
)
404375
)
405-
expect_error(
406-
.convert_form_to_xy_fit(
407-
mpg ~ ., data = mtcars,
408-
offset = 1:10,
409-
indicators = "traditional",
410-
remove_intercept = TRUE
411-
)
412-
)
413376
})
414377

415378
test_that("numeric x and y, matrix composition", {

0 commit comments

Comments
 (0)