我希望使用workstream_set来迭代模型创建中的不同变量.
在https://www.tmwr.org/workflow-sets中它指出
有三种可能的预处理器:
- 标准R公式
- 食谱对象(在估计/准备之前)
- Dplyrr风格的 Select 器,用于 Select 结果和预测因子
但它只提供了标准R公式的一个例子. 我更喜欢以Dupper风格工作,这样如果我创建自己的功能,我就可以在future 轻松规定结果和预测因子. 我环顾四周,找不到任何杜特尔风格的例子.
但我发现Inbox %%updateroal%%updateroal有效,但它很冗长.(示例2)
不幸的是,我的第三种方法add_Variable不起作用.
有人知道如何以比我发现的更好的方式为workdown_set编写dupper样式吗?
例1 R公式风格
set.seed(123)
data <- data.frame(
x1 = rnorm(100),
x2 = rnorm(100),
x3 = rnorm(100),
x4 = rnorm(100),
y = rnorm(100)
)
variables<-list(
first = y ~ x1,
second = y ~ x2,
third = y ~ x1+x2,
fourth = y ~ x3+x4
)
lm_model <-
linear_reg() %>%
set_engine("lm")
location_models <- workflow_set(preproc = variables, models = list(lm = lm_model))
location_models
location_models$fit[[4]]
extract_workflow(location_models, id = "third_lm")
location_models <-
location_models %>%
mutate(fit = map(info, \(x) fit(x$workflow[[1]], data)))
location_models$fit[[4]]
示例2更新角色
variables<-list(
fist = recipe(data)%>%update_role( y, new_role = "outcome")%>%update_role( x1, new_role = "predictor"),
second = recipe(data)%>%update_role( y, new_role = "outcome")%>%update_role( x2, new_role = "predictor"),
third = recipe(data)%>%update_role( y, new_role = "outcome")%>%update_role( c(x1,x2), new_role = "predictor"),
forth = recipe(data)%>%update_role( y, new_role = "outcome")%>%update_role( c(x3,x4), new_role = "predictor")
)
示例3添加变量(不起作用)
variables<-list(
fist = add_variables(outcomes = y, predictors = x1),
second = add_variables(outcomes = y, predictors = x2),
third = add_variables(outcomes = y, predictors = c(x1,x2)),
forth = add_variables(outcomes = y, predictors = c(x3,x4))
)