@@ -141,253 +141,6 @@ fit.model_spec <-
141141
142142# ##################################################################
143143
144- # ' @importFrom stats as.formula
145- fit_formula <- function (object , formula , data , engine = engine , control , ... ) {
146- opts <- quos(... )
147- # Look up the model's interface (e.g. formula, recipes, etc)
148- # and delegate to the connector functions (`formula_to_recipe` etc)
149- if (object $ method $ interface %in% c(" formula" , " spark" )) {
150- fit_args <- object $ method $ fit_args
151- fit_args <- resolve_args(fit_args , env = current_env())
152-
153- if (object $ method $ interface == " formula" ) {
154- fit_args $ data <- data
155- } else {
156- if (object $ method $ interface == " spark" )
157- fit_args $ x <- data
158- }
159- fit_args $ formula <- formula
160-
161- fit_call <- make_call(
162- fun = object $ method $ fit_name [" fun" ],
163- ns = object $ method $ fit_name [" pkg" ],
164- fit_args
165- )
166-
167- res <-
168- eval_mod(
169- fit_call ,
170- capture = control $ verbosity == 0 ,
171- catch = control $ catch ,
172- env = current_env()
173- )
174- } else {
175- if (object $ method $ interface %in% c(" data.frame" , " matrix" , " spark" )) {
176- res <- formula_to_xy(object = object , formula = formula , data = data , control )
177- } else {
178- stop(" I don't know about the " ,
179- object $ method $ interface , " interface." ,
180- call. = FALSE )
181- }
182- }
183- res
184- }
185-
186- # TODO find a hook for whether to make dummies or not. Test cases
187- # are all numeric. Solve via the fit the method object via entry for
188- # `requires_dummies`
189-
190- # ' @importFrom stats model.frame model.response terms
191- formula_to_xy <- function (object , formula , data , control ) {
192- # Q: how do we fill in the other standard things here (subset, contrasts etc)?
193- # Q: add a "matrix" option here and invoke model.matrix
194- x <- stats :: model.frame(eval(formula ), eval(data ))
195- y <- model.response(x )
196-
197- # Remove outcome column(s) from `x`
198- outcome_cols <- attr(terms(x ), " response" )
199- if (! isTRUE(all.equal(outcome_cols , 0 ))) {
200- x <- x [,- outcome_cols , drop = FALSE ]
201- }
202-
203- object $ method $ fit_args [[" x" ]] <- quote(x )
204- object $ method $ fit_args [[" y" ]] <- quote(y )
205-
206- fit_call <- make_call(
207- fun = object $ method $ fit_name [" fun" ],
208- ns = object $ method $ fit_name [" pkg" ],
209- object $ method $ fit_args
210- )
211-
212- eval_mod(
213- fit_call ,
214- capture = control $ verbosity == 0 ,
215- catch = control $ catch ,
216- env = current_env()
217- )
218- }
219-
220- # ##################################################################
221-
222- fit_xy <- function (object , x , y , control , ... ) {
223- opts <- quos(... )
224-
225- if (inherits(x , " tbl_spark" ) | inherits(y , " tbl_spark" ))
226- stop(" spark objects must be fit with the formula interface to `fit`" ,
227- call. = FALSE )
228-
229- res <- switch (
230- object $ method $ interface ,
231- formula = xy_to_formula(object = object , x = x , y = y , control , ... ),
232- matrix = xy_to_matrix(object = object , x = x , y = y , control , ... ),
233- data.frame = xy_to_df(object = object , x = x , y = y , control , ... ),
234- stop(" Unknown interface" )
235- )
236- res
237- }
238-
239- xy_to_xy <- function (object , x , y , control , ... ) {
240- fit_args <- object $ method $ fit_args
241-
242- fit_args <- resolve_args(fit_args , env = current_env())
243-
244- fit_args [[" x" ]] <- quote(x )
245- fit_args [[" y" ]] <- quote(y )
246-
247- fit_call <- make_call(
248- fun = object $ method $ fit_name [" fun" ],
249- ns = object $ method $ fit_name [" pkg" ],
250- fit_args
251- )
252-
253- eval_mod(
254- fit_call ,
255- capture = control $ verbosity == 0 ,
256- catch = control $ catch ,
257- env = current_env()
258- )
259- }
260- xy_to_matrix <- function (object , x , y , control , ... ) {
261- if (object $ method $ interface == " matrix" && ! is.matrix(x ))
262- x <- as.matrix(x )
263- xy_to_xy(object , x , y , control , ... )
264- }
265- xy_to_df <- function (object , x , y , control , ... ) {
266- if (object $ method $ interface == " data.frame" && ! is.data.frame(x ))
267- x <- as.data.frame(x )
268- xy_to_xy(object , x , y , control , ... )
269- }
270-
271-
272- fit_recipe <- function (object , recipe , data , control , ... ) {
273- opts <- quos(... )
274-
275- # Look up the model's interface (e.g. formula, recipes, etc)
276- # and delegate to the connector functions (`recipe_to_formula` etc)
277- if (object $ method $ interface == " formula" ) {
278- res <- recipe_to_formula(object = object , recipe = recipe , data = data , control )
279- } else {
280- if (object $ method $ interface %in% c(" data.frame" , " matrix" )) {
281- res <- recipe_to_xy(object = object , recipe = recipe , data = data , control )
282- } else {
283- stop(" I don't know about the " ,
284- object $ method $ interface , " interface." ,
285- call. = FALSE )
286- }
287- }
288- res
289-
290- }
291-
292-
293- # placeholder
294- fit_spark <- function (object , remote , engine = engine , control , ... ) {
295- NULL
296- }
297-
298- # ## or.... let `data, `x` and `y` be remote spark data tables or specifications
299-
300- invoke2 <- function (.f , envir = parent.frame(), ... ) {
301-
302- dots <- dots_values(... )
303- do.call(.f , dots , envir = envir )
304- }
305-
306- # ##################################################################
307-
308- formula_to_recipe <- function (object , formula , data , control ) {
309- # execute the formula
310- # extract terms _and roles_
311- # put into recipe
312-
313- }
314-
315- # ##################################################################
316-
317- # ' @importFrom recipes prep juice all_predictors all_outcomes
318- recipe_to_formula <- function (object , recipe , data , control ) {
319- # TODO case weights
320- recipe <-
321- prep(recipe , training = data , retain = TRUE , verbose = control $ verbosity > 1 )
322- dat <- juice(recipe , all_predictors(), all_outcomes())
323- dat <- as.data.frame(dat )
324-
325- data_info <- summary(recipe )
326- y_names <- data_info $ variable [data_info $ role == " outcome" ]
327- if (length(y_names ) > 1 )
328- y_names <-
329- paste0(" cbind(" , paste0(y_names , collapse = " ," ), " )" )
330-
331- fit_args <- object $ method $ fit_args
332- fit_args $ formula <- as.formula(paste0(y_names , " ~." ))
333- fit_args $ data <- quote(dat )
334- eval_mod(
335- fit_args ,
336- capture = control $ verbosity == 0 ,
337- catch = control $ catch ,
338- env = current_env()
339- )
340- }
341-
342- recipe_to_xy <- function (object , recipe , data , control ) {
343- # TODO case weights
344- recipe <-
345- prep(recipe , training = data , retain = TRUE , verbose = control $ verbosity > 1 )
346-
347- x <- juice(recipe , all_predictors())
348- x <- as.data.frame(x )
349- y <- juice(recipe , all_outcomes())
350- if (ncol(y ) > 1 )
351- y <- as.data.frame(y )
352- else
353- y <- y [[1 ]]
354-
355- fit_args <- object $ method $ fit_args
356-
357- fit_args [[" x" ]] <- quote(x )
358- fit_args [[" y" ]] <- quote(y )
359-
360- eval_mod(
361- fit_args ,
362- capture = control $ verbosity == 0 ,
363- catch = control $ catch ,
364- env = current_env()
365- )
366- }
367-
368- # ##################################################################
369-
370- xy_to_formula <- function (object , x , y , control ) {
371- if (! is.data.frame(x ))
372- x <- as.data.frame(x )
373- x $ .y <- y
374- fit_args <- object $ method $ fit_args
375- fit_args $ formula <- as.formula(.y ~ . )
376- fit_args $ data <- quote(x )
377- fit_call <- make_call(
378- fun = object $ method $ fit_name [" fun" ],
379- ns = object $ method $ fit_name [" pkg" ],
380- fit_args
381- )
382- eval_tidy(fit_call , env = current_env())
383- }
384-
385- xy_to_recipe <- function (object , x , y , control ) {
386-
387- }
388-
389- # ##################################################################
390-
391144# ' @importFrom utils capture.output
392145eval_mod <- function (e , capture = FALSE , catch = FALSE , ... ) {
393146 if (capture ) {
0 commit comments