r - 食谱包无法在 step_interact 中创建交互项
问题描述
我正在使用医疗保险数据集来磨练我的建模技能,如下所示:
> insur_dt
age sex bmi children smoker region charges
1: 19 female 27.900 0 yes southwest 16884.924
2: 18 male 33.770 1 no southeast 1725.552
3: 28 male 33.000 3 no southeast 4449.462
4: 33 male 22.705 0 no northwest 21984.471
5: 32 male 28.880 0 no northwest 3866.855
---
1334: 50 male 30.970 3 no northwest 10600.548
1335: 18 female 31.920 0 no northeast 2205.981
1336: 18 female 36.850 0 no southeast 1629.833
1337: 21 female 25.800 0 no southwest 2007.945
1338: 61 female 29.070 0 yes northwest 29141.360
我使用recipes
作为tidymodels
元数据包的一部分来准备我的数据以在模型中使用,并且我已经确定了bmi
、age
和 ,并smoker
形成了一个交互项。
insur_split <- initial_split(insur_dt)
insur_train <- training(insur_split)
insur_test <- testing(insur_split)
# we are going to do data processing and feature engineering with recipes
# below, we are going to predict charges using everything else(".")
insur_rec <- recipe(charges ~ age + bmi + smoker, data = insur_train) %>%
step_dummy(all_nominal()) %>%
step_zv(all_numeric()) %>%
step_normalize(all_numeric()) %>%
step_interact(~ bmi:smoker:age) %>%
prep()
根据tidymodels 指南/文档,我必须将交互指定为 as 中的一个recipe
步骤step_interact
。但是,当我尝试这样做时出现错误:
> insur_rec <- recipe(charges ~ age + bmi + smoker, data = insur_train) %>%
+ step_dummy(all_nominal()) %>%
+ step_zv(all_numeric()) %>%
+ step_normalize(all_numeric()) %>%
+ step_interact(~ bmi:smoker:age) %>%
+ prep()
Interaction specification failed for: ~bmi:smoker:age. No interactions will be created.partial match of 'object' to 'objects'
我是建模新手,不太确定为什么会出现此错误。我只是试图charges
说明所有其他预测变量都可以解释这一点,并且smoker
(是/否因素)、age
(数字)和bmi
(双)都相互作用以告知结果。我究竟做错了什么?
解决方案
从文档中:
step_interact
可以创建变量之间的交互。它主要用于数字数据;step_dummy()
在用于交互之前,分类变量可能应该使用转换为虚拟变量。
step_dummy(all_nominal())
把变量smoker
变成smoker_yes
. 下面,您会看到我只是将smoker
交互术语中的名称更改为smoker_yes
。
insur_rec <- recipe(charges ~ bmi + age + smoker, data = insur_train) %>%
step_dummy(all_nominal()) %>%
step_normalize(all_numeric(), -all_outcomes()) %>%
step_interact(terms = ~ bmi:age:smoker_yes) %>%
prep(verbose = TRUE, log_changes = TRUE)
推荐阅读
- python - python中的日期列具有更多的NAT值
- sql - SQL如何选择浏览次数最多的产品
- postgresql - 如果在连接语句中不使用主表/键有任何缺点
- node.js - 使用 nodeJS 向 mongoose 模型 mongoDB 添加密钥
- google-api - 每天通过 youtube api 发送的视频限制
- python-3.x - Simpy 请求特定资源
- html-table - 背景颜色 mailchimp
- node.js - 如何在没有 OAuth 的情况下仅使用 API 密钥存储联系人?
- python - odoo 更改图形的默认类型
- java - 将 swing 组件添加到 javafx 选项卡中