Skip to content

Commit 236a39b

Browse files
committed
happy path for sparse matrix passed to fit()
1 parent 0e37996 commit 236a39b

File tree

3 files changed

+3
-10
lines changed

3 files changed

+3
-10
lines changed

R/fit.R

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,7 @@ fit.model_spec <-
138138
}
139139

140140
if (is_sparse_matrix(data)) {
141-
outcome_names <- all.names(rlang::f_lhs(formula))
142-
outcome_ind <- match(outcome_names, colnames(data))
143-
144-
y <- data[, outcome_ind]
145-
x <- data[, -outcome_ind, drop = TRUE]
146-
147-
return(fit_xy(object, x, y, case_weights, control, ...))
141+
data <- sparsevctrs::coerce_to_sparse_tibble(data)
148142
}
149143

150144
dots <- quos(...)

tests/testthat/_snaps/sparsevctrs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
Code
1212
lm_fit <- fit(spec, avg_price_per_room ~ ., data = hotel_data[1:100, ])
1313
Condition
14-
Error in `fit_xy()`:
15-
! `x` is a sparse matrix, but `linear_reg()` with engine "lm" doesn't accept that.
14+
Warning:
15+
`data` is a sparse tibble, but `linear_reg()` with engine "lm" doesn't accept that. Converting to non-sparse.
1616

1717
# sparse tibble can be passed to `fit_xy()
1818

tests/testthat/test-sparsevctrs.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ test_that("sparse matrix can be passed to `fit()", {
3939
set_engine("lm")
4040

4141
expect_snapshot(
42-
error = TRUE,
4342
lm_fit <- fit(spec, avg_price_per_room ~ ., data = hotel_data[1:100, ])
4443
)
4544
})

0 commit comments

Comments
 (0)