我想拟合一个线性模型,并且想使用 tidyverse 添加所有数值变量的样条线进行评估,最好是进行惩罚。
虽然这听起来很聪明,但我不知道如何让它工作,因为无论我尝试什么,它都会产生无意义的错误消息。我能找到的任何地方都没有给出正确的示例,GPT4o 也不知道。
此示例有效,但如果尝试将
step_ns(all_predictors(), deg_free = 3)
添加到配方中,如文档和示例所示,则会失败:
library(tidyverse)
library(tidymodels)
#choose a suitable build in dataset with numerical variables
bfi = psych::bfi %>% na.omit()
bfi %>% nrow()
#> [1] 2236
#minimal subset
bfi = bfi %>% select(A1:O5, age)
#with roles
bfi_rec = recipe(
bfi,
vars = bfi %>% select(age, A1:O5) %>% names(),
roles = c("outcome", rep("predictor", 25)),
)
#with formula
# bfi_rec = recipe(
# age ~ .,
# data = bfi,
# )
#model
bfi_model = linear_reg() %>%
set_engine("lm") %>%
set_mode("regression")
#workflow
bfi_wf = workflow() %>%
add_recipe(bfi_rec) %>%
add_model(bfi_model)
#cross validation
set.seed(1)
bfi_folds = vfold_cv(bfi, v = 5)
#resampling
bfi_res = fit_resamples(
bfi_wf,
bfi_folds,
control = control_resamples(save_pred = TRUE)
)
#results
bfi_res %>%
collect_metrics() %>%
filter(.metric == "rsq")
#> # A tibble: 1 × 6
#> .metric .estimator mean n std_err .config
#> <chr> <chr> <dbl> <int> <dbl> <chr>
#> 1 rsq standard 0.0730 5 0.00538 Preprocessor1_Model1
如果尝试使用
step_ns()
,则会在隐式调用 prep()
时导致错误:
#...
#with roles
bfi_rec = recipe(
bfi,
vars = bfi %>% select(age, A1:O5) %>% names(),
roles = c("outcome", rep("predictor", 25)),
) %>%
step_ns(all_predictors(), deg_free = 3)
#...
#resampling
bfi_res = fit_resamples(
bfi_wf,
bfi_folds,
control = control_resamples(save_pred = TRUE)
)
#> → A | error: Error in `step_ns()`:
#> Caused by error in `qr.default()`:
#> ! NA/NaN/Inf in foreign function call (arg 1)
#> There were issues with some computations A: x1
#> There were issues with some computations A: x5
#>
#> Warning: All models failed. Run `show_notes(.Last.tune.result)` for more
#> information.
我也尝试过的事情:
age ~ .
代替角色,但我更喜欢角色,因为我有很多变量。step_ns()
的参数。
deg_free = 3
显然并不像文档中所说的那样,而是导致了误导性错误,听起来好像缺少数据(我们知道没有)。如果忽略它,代码就可以工作:
#results
bfi_res %>%
collect_metrics() %>%
filter(.metric == "rsq")
#> # A tibble: 1 × 6
#> .metric .estimator mean n std_err .config
#> <chr> <chr> <dbl> <int> <dbl> <chr>
#> 1 rsq standard 0.0879 5 0.0101 Preprocessor1_Model1